No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forth.c 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "PILE/pile.h"
  2. #include "LEXER/lexer.h"
  3. typedef struct Etat {
  4. struct pile * une_pile;
  5. Programme * programme;
  6. }Etat;
  7. void executer(Etat * etat) {
  8. int k;
  9. int a, b, result;
  10. int sortie = 1;
  11. for (k=0; k<etat->programme->taille; k++) {
  12. if (!strcmp((etat->programme->tokens)[k],"+")) {
  13. b = pop(etat->une_pile);
  14. a = pop(etat->une_pile);
  15. push(a+b, etat->une_pile);
  16. } else if (!strcmp((etat->programme->tokens)[k],"-")) {
  17. b = pop(etat->une_pile);
  18. a = pop(etat->une_pile);
  19. push(a-b, etat->une_pile);
  20. } else if (!strcmp((etat->programme->tokens)[k],"*")) {
  21. b = pop(etat->une_pile);
  22. a = pop(etat->une_pile);
  23. push(a*b, etat->une_pile);
  24. } else if (!strcmp((etat->programme->tokens)[k],"/")) {
  25. b = pop(etat->une_pile);
  26. a = pop(etat->une_pile);
  27. push(a/b, etat->une_pile);
  28. } else {
  29. push(atoi((etat->programme->tokens)[k]), etat->une_pile);
  30. }
  31. }
  32. if (sortie == 0) {
  33. printf("SORTIE : ERROR\n");
  34. } else {
  35. result = pop(etat->une_pile);
  36. printf("SORTIE : %d\n", result);
  37. }
  38. }
  39. int main (int argc, char * argv[]) {
  40. struct Etat etat;
  41. init(&(etat.une_pile));
  42. if (argc == 2) {
  43. etat.programme = lexer(argv[1]);
  44. executer(&etat);
  45. } else {
  46. printf("Le jour ou on mettra les cons en orbite t'as pas fini de tourner vieux !\n");
  47. }
  48. }