statement.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #ifndef VIRTUALMATH_STATEMENT_H
  2. #define VIRTUALMATH_STATEMENT_H
  3. #include "__macro.h"
  4. #define makeConnectStatementList(base, condition, var, code, type) connectStatementList(base, makeStatementList(condition, var, code, type))
  5. struct Statement{
  6. enum ValueAuthority aut;
  7. enum StatementType{
  8. start = 1,
  9. base_value,
  10. base_list,
  11. base_dict,
  12. base_var,
  13. base_svar,
  14. base_lambda,
  15. operation,
  16. set_function,
  17. set_class,
  18. call_function,
  19. if_branch,
  20. while_branch,
  21. for_branch,
  22. try_branch,
  23. with_branch,
  24. break_cycle,
  25. continue_cycle,
  26. rego_if,
  27. restart,
  28. return_code,
  29. yield_code,
  30. raise_code,
  31. include_file,
  32. import_file,
  33. from_import_file,
  34. default_var,
  35. assert,
  36. label_,
  37. goto_,
  38. } type;
  39. union StatementU{
  40. struct base_value{
  41. enum BaseValueType{
  42. link_value = 0,
  43. string_str = 1,
  44. number_str = 2,
  45. bool_true = 3,
  46. bool_false = 4,
  47. pass_value = 5,
  48. null_value = 6,
  49. } type;
  50. struct LinkValue *value;
  51. char *str;
  52. } base_value;
  53. struct base_var{
  54. char *name;
  55. struct Statement *times;
  56. } base_var;
  57. struct base_svar{
  58. struct Statement *name;
  59. struct Statement *times;
  60. } base_svar;
  61. struct {
  62. enum ListType type;
  63. struct Parameter *list;
  64. } base_list;
  65. struct {
  66. struct Parameter *dict;
  67. } base_dict;
  68. struct {
  69. struct Parameter *parameter;
  70. struct Statement *function;
  71. } base_lambda;
  72. struct operation{
  73. enum OperationType{
  74. OPT_ADD = 1,
  75. OPT_SUB = 2,
  76. OPT_MUL = 3,
  77. OPT_DIV = 4,
  78. OPT_ASS = 5,
  79. OPT_POINT = 6,
  80. OPT_BLOCK = 7,
  81. } OperationType;
  82. struct Statement *left;
  83. struct Statement *right;
  84. } operation;
  85. struct {
  86. struct Statement *name;
  87. struct Statement *function;
  88. struct Parameter *parameter;
  89. struct DecorationStatement *decoration;
  90. } set_function;
  91. struct {
  92. struct Statement *name;
  93. struct Statement *st;
  94. struct Parameter *father;
  95. struct DecorationStatement *decoration;
  96. } set_class;
  97. struct {
  98. struct Statement *function;
  99. struct Parameter *parameter;
  100. } call_function;
  101. struct {
  102. struct StatementList *if_list; // if elif
  103. struct Statement *else_list; // else分支(无condition)
  104. struct Statement *finally;
  105. } if_branch;
  106. struct {
  107. enum {
  108. while_,
  109. do_while_,
  110. } type;
  111. struct Statement *first; // first do
  112. struct StatementList *while_list; // while循环体
  113. struct Statement *after; // after do
  114. struct Statement *else_list; // else分支(无condition)
  115. struct Statement *finally;
  116. } while_branch;
  117. struct {
  118. struct Statement *var; // first do
  119. struct Statement *iter; // after do
  120. struct StatementList *for_list; // for循环体
  121. struct Statement *else_list; // else分支(无condition)
  122. struct Statement *finally;
  123. } for_branch;
  124. struct {
  125. struct Statement *try; // first do
  126. struct StatementList *except_list; // for循环体
  127. struct Statement *else_list; // else分支(无condition)
  128. struct Statement *finally;
  129. } try_branch;
  130. struct {
  131. struct StatementList *with_list; // for循环体
  132. struct Statement *else_list; // else分支(无condition)
  133. struct Statement *finally;
  134. } with_branch;
  135. struct {
  136. struct Statement *times;
  137. } break_cycle;
  138. struct {
  139. struct Statement *times;
  140. } continue_cycle;
  141. struct {
  142. struct Statement *times;
  143. } rego_if;
  144. struct {
  145. struct Statement *times;
  146. } restart;
  147. struct {
  148. struct Statement *value;
  149. } return_code;
  150. struct {
  151. struct Statement *value;
  152. } yield_code;
  153. struct {
  154. struct Statement *value;
  155. } raise_code;
  156. struct {
  157. struct Statement *file;
  158. } include_file;
  159. struct {
  160. struct Statement *file;
  161. struct Statement *as;
  162. } import_file;
  163. struct {
  164. struct Statement *file;
  165. struct Parameter *pt;
  166. struct Parameter *as;
  167. } from_import_file;
  168. struct {
  169. struct Parameter *var;
  170. enum DefaultType{
  171. default_,
  172. global_,
  173. nonlocal_,
  174. } default_type;
  175. } default_var;
  176. struct {
  177. struct Statement *conditions;
  178. } assert;
  179. struct {
  180. struct Statement *command;
  181. struct Statement *as;
  182. char *label;
  183. } label_;
  184. struct {
  185. struct Statement *times;
  186. struct Statement *return_;
  187. struct Statement *label;
  188. } goto_;
  189. }u;
  190. struct { // 运行info信息
  191. bool have_info;
  192. struct VarList *var_list;
  193. struct Statement *node;
  194. struct {
  195. struct StatementList *sl_node;
  196. enum StatementInfoStatus{
  197. info_vl_branch,
  198. info_else_branch,
  199. info_finally_branch,
  200. info_first_do,
  201. info_after_do,
  202. } status;
  203. struct{
  204. LinkValue *value;
  205. LinkValue *_exit_;
  206. LinkValue *_enter_;
  207. } with_;
  208. } branch;
  209. } info;
  210. long int line;
  211. char *code_file;
  212. struct Statement *next;
  213. };
  214. struct StatementList{
  215. enum {
  216. if_b,
  217. do_b,
  218. while_b,
  219. except_b,
  220. } type;
  221. struct Statement *condition;
  222. struct Statement *var;
  223. struct Statement *code;
  224. struct StatementList *next;
  225. };
  226. struct DecorationStatement {
  227. struct Statement *decoration;
  228. struct DecorationStatement *next;
  229. };
  230. typedef struct Token Token;
  231. typedef struct Statement Statement;
  232. typedef struct StatementList StatementList;
  233. typedef struct DecorationStatement DecorationStatement;
  234. Statement *makeStatement(long int line, char *file);
  235. void setRunInfo(Statement *st);
  236. void freeRunInfo(Statement *st);
  237. void freeStatement(Statement *st);
  238. Statement *copyStatement(Statement *st);
  239. Statement *copyStatementCore(Statement *st);
  240. void connectStatement(Statement *base, Statement *new);
  241. Statement *makeOperationBaseStatement(enum OperationType type, long int line, char *file);
  242. Statement *makeOperationStatement(enum OperationType type, Statement *left, Statement *right);
  243. Statement *makeBaseLinkValueStatement(LinkValue *value, long int line, char *file);
  244. Statement *makeBaseStrValueStatement(char *value, enum BaseValueType type, long int line, char *file);
  245. Statement *makeBaseValueStatement(enum BaseValueType type, long int line, char *file);
  246. Statement *makeBaseVarStatement(char *name, Statement *times, long int line, char *file);
  247. Statement *makeBaseSVarStatement(Statement *name, Statement *times);
  248. Statement *makeBaseDictStatement(Parameter *pt, long int line, char *file);
  249. Statement *makeTupleStatement(Parameter *pt, enum ListType type, long int line, char *file);
  250. Statement *makeClassStatement(Statement *name, Statement *function, Parameter *pt);
  251. Statement *makeFunctionStatement(Statement *name, Statement *function, struct Parameter *pt);
  252. Statement *makeLambdaStatement(Statement *function, Parameter *pt);
  253. Statement *makeCallStatement(Statement *function, struct Parameter *pt);
  254. Statement *makeIfStatement(long int line, char *file);
  255. Statement *makeWhileStatement(long int line, char *file);
  256. Statement *makeTryStatement(long int line, char *file);
  257. Statement *makeBreakStatement(Statement *times, long int line, char *file);
  258. Statement *makeWithStatement(long int line, char *file);
  259. Statement *makeContinueStatement(Statement *times, long int line, char *file);
  260. Statement *makeRegoStatement(Statement *times, long int line, char *file);
  261. Statement *makeRestartStatement(Statement *times, long int line, char *file);
  262. Statement *makeReturnStatement(Statement *value, long int line, char *file);
  263. Statement *makeYieldStatement(Statement *value, long int line, char *file);
  264. Statement *makeRaiseStatement(Statement *value, long int line, char *file);
  265. Statement *makeAssertStatement(Statement *conditions, long int line, char *file);
  266. Statement *makeIncludeStatement(Statement *file, long int line, char *file_dir);
  267. Statement *makeImportStatement(Statement *file, Statement *as);
  268. Statement *makeFromImportStatement(Statement *file, Parameter *as, Parameter *pt);
  269. Statement *makeDefaultVarStatement(Parameter *var, long int line, char *file_dir, enum DefaultType type);
  270. Statement *makeLabelStatement(Statement *var, Statement *command, char *label, long int line, char *file_dir);
  271. Statement *makeGotoStatement(Statement *return_, Statement *times, Statement *label, long int line, char *file_dir);
  272. struct Token *setOperationFromToken(Statement **st_ad, struct Token *left, struct Token *right, enum OperationType type, bool is_right);
  273. StatementList *makeStatementList(Statement *condition, Statement *var, Statement *code, int type);
  274. StatementList *connectStatementList(StatementList *base, StatementList *new);
  275. void freeStatementList(StatementList *base);
  276. StatementList *copyStatementList(StatementList *sl);
  277. DecorationStatement *makeDecorationStatement();
  278. DecorationStatement *connectDecorationStatement(Statement *decoration, DecorationStatement *base);
  279. void freeDecorationStatement(DecorationStatement *base);
  280. DecorationStatement *copyDecorationStatement(DecorationStatement *ds);
  281. DecorationStatement *copyDecorationStatementCore(DecorationStatement *base);
  282. #endif //VIRTUALMATH_STATEMENT_H