53 lines
1.3 KiB
C
53 lines
1.3 KiB
C
#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");
|
|
}
|
|
}
|