statement.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. rego_if,
  20. restart,
  21. return_code,
  22. raise_code,
  23. } type;
  24. union StatementU{
  25. struct base_value{
  26. struct VirtualMathValue *value;
  27. } base_value;
  28. struct base_var{
  29. char *name;
  30. struct Statement *times;
  31. } base_var;
  32. struct operation{
  33. enum OperationType{
  34. ADD = 1,
  35. SUB,
  36. MUL,
  37. DIV,
  38. ASS,
  39. } OperationType;
  40. struct Statement *left;
  41. struct Statement *right;
  42. } operation;
  43. struct {
  44. struct Statement *name;
  45. struct Statement *function;
  46. } set_function;
  47. struct {
  48. struct Statement *function;
  49. } call_function;
  50. struct {
  51. struct StatementList *if_list; // if elif
  52. struct Statement *else_list; // else分支(无condition)
  53. struct Statement *finally;
  54. } if_branch;
  55. struct {
  56. enum {
  57. while_,
  58. do_while_,
  59. } type;
  60. struct Statement *first; // first do
  61. struct StatementList *while_list; // while循环体
  62. struct Statement *after; // after do
  63. struct Statement *else_list; // else分支(无condition)
  64. struct Statement *finally;
  65. } while_branch;
  66. struct {
  67. struct Statement *var; // first do
  68. struct Statement *iter; // after do
  69. struct StatementList *for_list; // for循环体
  70. struct Statement *else_list; // else分支(无condition)
  71. struct Statement *finally;
  72. } for_branch;
  73. struct {
  74. struct Statement *try; // first do
  75. struct StatementList *except_list; // for循环体
  76. struct Statement *else_list; // else分支(无condition)
  77. struct Statement *finally;
  78. } try_branch;
  79. struct {
  80. struct StatementList *with_list; // for循环体
  81. struct Statement *else_list; // else分支(无condition)
  82. struct Statement *finally;
  83. } with_branch;
  84. struct {
  85. struct Statement *times;
  86. } break_cycle;
  87. struct {
  88. struct Statement *times;
  89. } continue_cycle;
  90. struct {
  91. struct Statement *times;
  92. } rego_if;
  93. struct {
  94. struct Statement *times;
  95. } restart;
  96. struct {
  97. struct Statement *value;
  98. } return_code;
  99. struct {
  100. struct Statement *value;
  101. } raise_code;
  102. }u;
  103. struct Statement *next;
  104. } Statement;
  105. typedef struct StatementList{
  106. enum {
  107. if_b,
  108. do_b,
  109. while_b,
  110. except_b,
  111. } type;
  112. struct Statement *condition;
  113. struct Statement *var;
  114. struct Statement *code;
  115. struct StatementList *next;
  116. } StatementList;
  117. Statement *makeStatement();
  118. Statement *makeOperationStatement(int type);
  119. struct Token *setOperationFromToken(Statement *st, struct Token *left, struct Token *right, int type);
  120. Statement *makeFunctionStatement(Statement *name, Statement *function);
  121. Statement *makeCallStatement(Statement *function);
  122. Statement *makeIfStatement();
  123. Statement *makeWhileStatement();
  124. Statement *makeTryStatement();
  125. Statement *makeBreakStatement(Statement *times);
  126. Statement *makeContinueStatement(Statement *times);
  127. Statement *makeRegoStatement(Statement *times);
  128. Statement *makeRestartStatement(Statement *times);
  129. Statement *makeReturnStatement(Statement *value);
  130. Statement *makeRaiseStatement(Statement *value);
  131. void connectStatement(Statement *base, Statement *new);
  132. void freeStatement(Statement *st);
  133. StatementList *connectStatementList(StatementList *base, StatementList *new);
  134. StatementList *makeStatementList(Statement *condition, Statement *var, Statement *code, int type);
  135. void freeStatementList(StatementList *base);
  136. #define makeConnectStatementList(base, condition, var, code, type) connectStatementList(base, makeStatementList(condition, var, code, type))
  137. #endif //VIRTUALMATH_STATEMENT_H