statement.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef VIRTUALMATH_STATEMENT_H
  2. #define VIRTUALMATH_STATEMENT_H
  3. #include "__macro.h"
  4. typedef struct Statement{
  5. enum StatementType{
  6. start = 1,
  7. base_value,
  8. base_var,
  9. operation,
  10. set_function,
  11. call_function,
  12. if_branch,
  13. while_branch,
  14. for_branch,
  15. try_branch,
  16. with_branch,
  17. break_cycle,
  18. continue_cycle,
  19. } type;
  20. union StatementU{
  21. struct base_value{
  22. struct VirtualMathValue *value;
  23. } base_value;
  24. struct base_var{
  25. char *name;
  26. struct Statement *times;
  27. } base_var;
  28. struct operation{
  29. enum OperationType{
  30. ADD = 1,
  31. SUB,
  32. MUL,
  33. DIV,
  34. ASS,
  35. } OperationType;
  36. struct Statement *left;
  37. struct Statement *right;
  38. } operation;
  39. struct {
  40. struct Statement *name;
  41. struct Statement *function;
  42. } set_function;
  43. struct {
  44. struct Statement *function;
  45. } call_function;
  46. struct {
  47. struct StatementList *if_list; // if elif
  48. struct Statement *else_list; // else分支(无condition)
  49. struct Statement *finally;
  50. } if_branch;
  51. struct {
  52. enum {
  53. while_,
  54. do_while_,
  55. } type;
  56. struct Statement *first; // first do
  57. struct StatementList *while_list; // while循环体
  58. struct Statement *after; // after do
  59. struct Statement *else_list; // else分支(无condition)
  60. struct Statement *finally;
  61. } while_branch;
  62. struct {
  63. struct Statement *var; // first do
  64. struct Statement *iter; // after do
  65. struct StatementList *for_list; // for循环体
  66. struct Statement *else_list; // else分支(无condition)
  67. struct Statement *finally;
  68. } for_branch;
  69. struct {
  70. struct Statement *try; // first do
  71. struct StatementList *except_list; // for循环体
  72. struct Statement *else_list; // else分支(无condition)
  73. struct Statement *finally;
  74. } try_branch;
  75. struct {
  76. struct StatementList *with_list; // for循环体
  77. struct Statement *else_list; // else分支(无condition)
  78. struct Statement *finally;
  79. } with_branch;
  80. struct {
  81. struct Statement *times;
  82. } break_cycle;
  83. struct {
  84. struct Statement *times;
  85. } continue_cycle;
  86. }u;
  87. struct Statement *next;
  88. } Statement;
  89. typedef struct StatementList{
  90. enum {
  91. if_b,
  92. do_b,
  93. } type;
  94. struct Statement *condition;
  95. struct Statement *var; // TODO-szh if等分支计算结果允许赋值
  96. struct Statement *code;
  97. struct StatementList *next;
  98. } StatementList;
  99. Statement *makeStatement();
  100. Statement *makeOperationStatement(int type);
  101. struct Token *setOperationFromToken(Statement *st, struct Token *left, struct Token *right, int type);
  102. Statement *makeFunctionStatement(Statement *name, Statement *function);
  103. Statement *makeCallStatement(Statement *function);
  104. Statement *makeIfStatement();
  105. Statement *makeWhileStatement();
  106. Statement *makeBreakStatement(Statement *times);
  107. Statement *makeContinueStatement(Statement *times);
  108. void connectStatement(Statement *base, Statement *new);
  109. void freeStatement(Statement *st);
  110. StatementList *connectStatementList(StatementList *base, StatementList *new);
  111. StatementList *makeStatementList(Statement *condition, Statement *var, Statement *code, int type);
  112. void freeStatementList(StatementList *base);
  113. #define makeConnectStatementList(base, condition, var, code, type) connectStatementList(base, makeStatementList(condition, var, code, type))
  114. #endif //VIRTUALMATH_STATEMENT_H