statement.h 4.5 KB

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