Version fonctionnelle : Lexer, execute, main implémentés
This commit is contained in:
parent
2f82b4098a
commit
4805fe44c5
2 changed files with 55 additions and 2 deletions
|
|
@ -9,3 +9,5 @@ typedef struct Programme {
|
||||||
char **tokens;
|
char **tokens;
|
||||||
int taille;
|
int taille;
|
||||||
}Programme;
|
}Programme;
|
||||||
|
|
||||||
|
Programme *lexer(char *chaine);
|
||||||
|
|
|
||||||
55
forth.c
55
forth.c
|
|
@ -1,2 +1,53 @@
|
||||||
// Ligne et commentaire a supprimer (utile juste pour que le projet initial comp)
|
#include "PILE/pile.h"
|
||||||
int main() {}
|
#include "LEXER/lexer.h"
|
||||||
|
|
||||||
|
typedef struct Etat {
|
||||||
|
struct pile * une_pile;
|
||||||
|
Programme * programme;
|
||||||
|
}Etat;
|
||||||
|
|
||||||
|
void executer(Etat * etat) {
|
||||||
|
int k;
|
||||||
|
int a, b, result;
|
||||||
|
int sortie = 1;
|
||||||
|
for (k=0; k<etat->programme->taille; k++) {
|
||||||
|
if (!strcmp((etat->programme->tokens)[k],"+")) {
|
||||||
|
b = pop(etat->une_pile);
|
||||||
|
a = pop(etat->une_pile);
|
||||||
|
push(a+b, etat->une_pile);
|
||||||
|
} else if (!strcmp((etat->programme->tokens)[k],"-")) {
|
||||||
|
b = pop(etat->une_pile);
|
||||||
|
a = pop(etat->une_pile);
|
||||||
|
push(a-b, etat->une_pile);
|
||||||
|
} else if (!strcmp((etat->programme->tokens)[k],"*")) {
|
||||||
|
b = pop(etat->une_pile);
|
||||||
|
a = pop(etat->une_pile);
|
||||||
|
push(a*b, etat->une_pile);
|
||||||
|
} else if (!strcmp((etat->programme->tokens)[k],"/")) {
|
||||||
|
b = pop(etat->une_pile);
|
||||||
|
a = pop(etat->une_pile);
|
||||||
|
push(a/b, etat->une_pile);
|
||||||
|
} else {
|
||||||
|
push(atoi((etat->programme->tokens)[k]), etat->une_pile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sortie == 0) {
|
||||||
|
printf("SORTIE : ERROR\n");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
result = pop(etat->une_pile);
|
||||||
|
printf("SORTIE : %d\n", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc, char * argv[]) {
|
||||||
|
struct Etat etat;
|
||||||
|
init(&(etat.une_pile));
|
||||||
|
if (argc == 2) {
|
||||||
|
etat.programme = lexer(argv[1]);
|
||||||
|
executer(&etat);
|
||||||
|
} else {
|
||||||
|
printf("Le jour ou on mettra les cons en orbite t'as pas fini de tourner vieux !\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue