Résultat du TP en C sur Forth
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.

logic.c 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "logic.h"
  2. #include "pileInt.h"
  3. static struct PileInt loops;
  4. void initLogic() {
  5. initPileI(&loops);
  6. }
  7. void ifCmd(struct State* state) {
  8. if (state->mode == EXECUTE) {
  9. struct NumContainer cond;
  10. cond = top(&state->pile);
  11. pop(&state->pile);
  12. if (cond.t == INT && cond.n.vali == 0) {
  13. state->mode = IGNORE;
  14. } else if (cond.t == FLOAT && cond.n.valf == 0.0 ) {
  15. state->mode = IGNORE;
  16. }
  17. }
  18. }
  19. void elseCmd(struct State* state) {
  20. if (state->mode == EXECUTE) {
  21. state->mode = WAIT_FOR_ENDIF;
  22. } else if (state->mode == IGNORE) {
  23. state->mode = EXECUTE;
  24. }
  25. }
  26. void thenCmd(struct State* state) {
  27. if (state->mode == IGNORE || state->mode == WAIT_FOR_ENDIF) {
  28. state->mode = EXECUTE;
  29. }
  30. }
  31. void beginCmd(struct State* state) {
  32. if (state->mode == EXECUTE) {
  33. if (evalasBool(top(&state->pile))) {
  34. pushPileI(&loops, state->instructionPointer);
  35. } else {
  36. state->mode = WAIT_FOR_UNTIL;
  37. }
  38. }
  39. }
  40. void untilCmd(struct State* state) {
  41. if (state->mode == WAIT_FOR_UNTIL) {
  42. state->mode = EXECUTE;
  43. } else if (state->mode == EXECUTE) {
  44. if (evalasBool(top(&state->pile))) {
  45. state->instructionPointer = topPileI(&loops);
  46. } else {
  47. popPileI(&loops);
  48. }
  49. }
  50. }