Version fonctionnelle : Lexer, execute, main implémentés

This commit is contained in:
Paul Faure 2020-10-01 17:01:04 +02:00
parent 2f82b4098a
commit 4805fe44c5
2 changed files with 55 additions and 2 deletions

View file

@ -9,3 +9,5 @@ typedef struct Programme {
char **tokens;
int taille;
}Programme;
Programme *lexer(char *chaine);

55
forth.c
View file

@ -1,2 +1,53 @@
// Ligne et commentaire a supprimer (utile juste pour que le projet initial comp)
int main() {}
#include "PILE/pile.h"
#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");
}
}