statement.h 11 KB

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