statement.h 4.1 KB

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