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.

command.c 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "command.h"
  2. #include "functions.h"
  3. #include "calcop.h"
  4. #include "cmpop.h"
  5. #include "stackops.h"
  6. #include "show.h"
  7. #include "logic.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. void initCmds( struct CmdList** cmds ) {
  12. initLogic();
  13. initFunctions();
  14. prependCmd(cmds, "DROP", dropCmd);
  15. prependCmd(cmds, "DUP", dupCmd);
  16. prependCmd(cmds, "SWAP", swapCmd);
  17. prependCmd(cmds, "ROT", rotCmd);
  18. prependCmd(cmds, "CR", crCmd);
  19. prependCmd(cmds, "IF", ifCmd);
  20. prependCmd(cmds, "ELSE", elseCmd);
  21. prependCmd(cmds, "THEN", thenCmd);
  22. prependCmd(cmds, "BEGIN", beginCmd);
  23. prependCmd(cmds, "UNTIL", untilCmd);
  24. prependCmd(cmds, ":", colonCmd);
  25. prependCmd(cmds, ";", semicolonCmd);
  26. prependCmd(cmds, "+", add);
  27. prependCmd(cmds, "-", sub);
  28. prependCmd(cmds, "*", mult);
  29. prependCmd(cmds, "/", divide);
  30. prependCmd(cmds, "=", equal);
  31. prependCmd(cmds, "<", less);
  32. prependCmd(cmds, ">", greater);
  33. prependCmd(cmds, ".", point);
  34. prependCmd(cmds, ".S", pointS);
  35. prependCmd(cmds, ".\"", pointQuote);
  36. }
  37. void prependCmd( struct CmdList** cmds, char* cmdName, void (*func)(struct State*) ) {
  38. struct CmdList* aux = *cmds;
  39. *cmds = (struct CmdList*)malloc(sizeof(struct CmdList));
  40. (*cmds)->next = aux;
  41. (*cmds)->func = func;
  42. strncpy((*cmds)->cmdName, cmdName, MAX_CMD_NAME_LENGTH);
  43. (*cmds)->cmdName[MAX_CMD_NAME_LENGTH-1] = '\0';
  44. }
  45. void endCmds( struct CmdList** pcmds ) {
  46. struct CmdList* cmds = *pcmds;
  47. struct CmdList* aux;
  48. while (cmds != NULL) {
  49. aux = cmds;
  50. cmds = cmds->next;
  51. free(aux);
  52. }
  53. *pcmds = NULL;
  54. }
  55. int tryCallCmds( struct CmdList* cmds, char* s, struct State* state ) {
  56. while (cmds != NULL) {
  57. if ( !strcmp(cmds->cmdName, s) ) {
  58. cmds->func(state);
  59. return 1;
  60. }
  61. cmds = cmds->next;
  62. }
  63. return 0;
  64. }