statement.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include "__virtualmath.h"
  2. Statement *makeStatement(long int line, char *file) {
  3. Statement *tmp = memCalloc(1, sizeof(Statement));
  4. tmp->type = start;
  5. tmp->next = NULL;
  6. tmp->line = line;
  7. tmp->code_file = memStrcpy(file, 0, false, false);
  8. return tmp;
  9. }
  10. Token *setOperationFromToken(Statement **st_ad, struct Token *left, struct Token *right, int type, bool is_right) {
  11. Token *new_token = NULL;
  12. Statement *st = *st_ad, *left_st = left->data.st;
  13. if (is_right && left->data.st->type == operation &&
  14. left_st->u.operation.OperationType == st->u.operation.OperationType){
  15. st->u.operation.left = left_st->u.operation.right;
  16. left_st->u.operation.right = st;
  17. st->u.operation.right = right->data.st;
  18. st = left_st; // left_st是主中心
  19. }
  20. else{
  21. st->u.operation.left = left_st;
  22. st->u.operation.right = right->data.st;
  23. }
  24. new_token = makeToken(0);
  25. new_token->token_type = type;
  26. new_token->data.st = st;
  27. st->line = left->line;
  28. freeToken(left, true, false);
  29. freeToken(right, true, false);
  30. *st_ad = st;
  31. return new_token;
  32. }
  33. Statement *makeBaseLinkValueStatement(LinkValue *value, long int line, char *file) {
  34. Statement *tmp = makeStatement(line, file);
  35. tmp->type = base_value;
  36. tmp->u.base_value.type = link_value;
  37. tmp->u.base_value.value = value;
  38. tmp->u.base_value.str = NULL;
  39. gcAddStatementLink(&value->gc_status);
  40. return tmp;
  41. }
  42. Statement *makeBaseStrValueStatement(char *value, enum BaseValueType type, long int line, char *file) {
  43. Statement *tmp = makeStatement(line, file);
  44. tmp->type = base_value;
  45. tmp->u.base_value.type = type;
  46. tmp->u.base_value.value = NULL;
  47. tmp->u.base_value.str = memStrcpy(value, 0, false, false);
  48. return tmp;
  49. }
  50. Statement *makeBaseVarStatement(char *name, Statement *times, long int line, char *file) {
  51. Statement *tmp = makeStatement(line, file);
  52. tmp->type = base_var;
  53. tmp->u.base_var.name = memStrcpy(name, 0, false, false);
  54. tmp->u.base_var.times = times;
  55. return tmp;
  56. }
  57. Statement *makeBaseSVarStatement(Statement *name, Statement *times){
  58. Statement *tmp = makeStatement(name->line, name->code_file);
  59. tmp->type = base_svar;
  60. tmp->u.base_svar.name = name;
  61. tmp->u.base_svar.times = times;
  62. return tmp;
  63. }
  64. Statement *makeBaseDictStatement(Parameter *pt, long int line, char *file) {
  65. Statement *tmp = makeStatement(line, file);
  66. tmp->type = base_dict;
  67. tmp->u.base_dict.dict = pt;
  68. return tmp;
  69. }
  70. Statement *makeOperationStatement(int type, long int line, char *file) {
  71. Statement *tmp = makeStatement(line, file);
  72. tmp->type = operation;
  73. tmp->u.operation.OperationType = type;
  74. tmp->u.operation.left = NULL;
  75. tmp->u.operation.right = NULL;
  76. return tmp;
  77. }
  78. Statement *makeTupleStatement(Parameter *pt, enum ListType type, long int line, char *file) {
  79. Statement *tmp = makeStatement(line, file);
  80. tmp->type = base_list;
  81. tmp->u.base_list.type = type;
  82. tmp->u.base_list.list = pt;
  83. return tmp;
  84. }
  85. Statement *makeFunctionStatement(Statement *name, Statement *function, Parameter *pt) {
  86. Statement *tmp = makeStatement(name->line, name->code_file);
  87. tmp->type = set_function;
  88. tmp->u.set_function.name = name;
  89. tmp->u.set_function.function = function;
  90. tmp->u.set_function.parameter = pt;
  91. return tmp;
  92. }
  93. Statement *makeCallStatement(Statement *function, Parameter *pt) {
  94. Statement *tmp = makeStatement(function->line, function->code_file);
  95. tmp->type = call_function;
  96. tmp->u.call_function.function = function;
  97. tmp->u.call_function.parameter = pt;
  98. return tmp;
  99. }
  100. Statement *makeIfStatement(long int line, char *file) {
  101. Statement *tmp = makeStatement(line, file);
  102. tmp->type = if_branch;
  103. tmp->u.if_branch.if_list = NULL;
  104. tmp->u.if_branch.else_list = NULL;
  105. tmp->u.if_branch.finally = NULL;
  106. return tmp;
  107. }
  108. Statement *makeWhileStatement(long int line, char *file) {
  109. Statement *tmp = makeStatement(line, file);
  110. tmp->type = while_branch;
  111. tmp->u.while_branch.type = while_;
  112. tmp->u.while_branch.while_list = NULL;
  113. tmp->u.while_branch.else_list = NULL;
  114. tmp->u.while_branch.finally = NULL;
  115. tmp->u.while_branch.first = NULL;
  116. tmp->u.while_branch.after = NULL;
  117. return tmp;
  118. }
  119. Statement *makeTryStatement(long int line, char *file) {
  120. Statement *tmp = makeStatement(line, file);
  121. tmp->type = try_branch;
  122. tmp->u.try_branch.except_list = NULL;
  123. tmp->u.try_branch.else_list = NULL;
  124. tmp->u.try_branch.finally = NULL;
  125. tmp->u.try_branch.try = NULL;
  126. return tmp;
  127. }
  128. Statement *makeBreakStatement(Statement *times, long int line, char *file){
  129. Statement *tmp = makeStatement(line, file);
  130. tmp->type = break_cycle;
  131. tmp->u.break_cycle.times = times;
  132. return tmp;
  133. }
  134. Statement *makeContinueStatement(Statement *times, long int line, char *file){
  135. Statement *tmp = makeStatement(line, file);
  136. tmp->type = continue_cycle;
  137. tmp->u.continue_cycle.times = times;
  138. return tmp;
  139. }
  140. Statement *makeRegoStatement(Statement *times, long int line, char *file){
  141. Statement *tmp = makeStatement(line, file);
  142. tmp->type = rego_if;
  143. tmp->u.rego_if.times = times;
  144. return tmp;
  145. }
  146. Statement *makeRestartStatement(Statement *times, long int line, char *file){
  147. Statement *tmp = makeStatement(line, file);
  148. tmp->type = restart;
  149. tmp->u.restart.times = times;
  150. return tmp;
  151. }
  152. Statement *makeReturnStatement(Statement *value, long int line, char *file){
  153. Statement *tmp = makeStatement(line, file);
  154. tmp->type = return_code;
  155. tmp->u.return_code.value = value;
  156. return tmp;
  157. }
  158. Statement *makeRaiseStatement(Statement *value, long int line, char *file){
  159. Statement *tmp = makeStatement(line, file);
  160. tmp->type = raise_code;
  161. tmp->u.raise_code.value = value;
  162. return tmp;
  163. }
  164. Statement *makeIncludeStatement(Statement *file, long int line, char *file_dir){
  165. Statement *tmp = makeStatement(line, file_dir);
  166. tmp->type = include_file;
  167. tmp->u.include_file.file = file;
  168. return tmp;
  169. }
  170. void connectStatement(Statement *base, Statement *new){
  171. while (base->next != NULL){
  172. base = base->next;
  173. }
  174. base->next = new;
  175. }
  176. void freeStatement(Statement *st){
  177. freeBase(st, return_);
  178. Statement *next_tmp;
  179. while (st != NULL){
  180. switch (st->type) {
  181. case operation:
  182. freeStatement(st->u.operation.right);
  183. freeStatement(st->u.operation.left);
  184. break;
  185. case base_value:
  186. if (st->u.base_value.type == link_value)
  187. gcFreeStatementLink(&st->u.base_value.value->gc_status);
  188. else
  189. memFree(st->u.base_value.str);
  190. break;
  191. case base_var:
  192. memFree(st->u.base_var.name);
  193. freeStatement(st->u.base_var.times);
  194. break;
  195. case base_svar:
  196. freeStatement(st->u.base_svar.name);
  197. freeStatement(st->u.base_svar.times);
  198. break;
  199. case set_function:
  200. freeStatement(st->u.set_function.name);
  201. freeStatement(st->u.set_function.function);
  202. freeParameter(st->u.set_function.parameter, true);
  203. break;
  204. case call_function:
  205. freeStatement(st->u.call_function.function);
  206. freeParameter(st->u.call_function.parameter, true);
  207. break;
  208. case base_list:
  209. freeParameter(st->u.base_list.list, true);
  210. break;
  211. case base_dict:
  212. freeParameter(st->u.base_dict.dict, true);
  213. break;
  214. case if_branch:
  215. freeStatementList(st->u.if_branch.if_list);
  216. freeStatement(st->u.if_branch.finally);
  217. freeStatement(st->u.if_branch.else_list);
  218. break;
  219. case while_branch:
  220. freeStatementList(st->u.while_branch.while_list);
  221. freeStatement(st->u.while_branch.first);
  222. freeStatement(st->u.while_branch.after);
  223. freeStatement(st->u.while_branch.else_list);
  224. freeStatement(st->u.while_branch.finally);
  225. break;
  226. case for_branch:
  227. freeStatementList(st->u.for_branch.for_list);
  228. freeStatement(st->u.for_branch.var);
  229. freeStatement(st->u.for_branch.iter);
  230. freeStatement(st->u.for_branch.else_list);
  231. freeStatement(st->u.for_branch.finally);
  232. break;
  233. case try_branch:
  234. freeStatementList(st->u.try_branch.except_list);
  235. freeStatement(st->u.try_branch.try);
  236. freeStatement(st->u.try_branch.else_list);
  237. freeStatement(st->u.try_branch.finally);
  238. break;
  239. case with_branch:
  240. freeStatementList(st->u.with_branch.with_list);
  241. freeStatement(st->u.with_branch.else_list);
  242. freeStatement(st->u.with_branch.finally);
  243. break;
  244. case break_cycle:
  245. freeStatement(st->u.break_cycle.times);
  246. break;
  247. case continue_cycle:
  248. freeStatement(st->u.continue_cycle.times);
  249. break;
  250. case rego_if:
  251. freeStatement(st->u.rego_if.times);
  252. break;
  253. case restart:
  254. freeStatement(st->u.restart.times);
  255. break;
  256. case return_code:
  257. freeStatement(st->u.return_code.value);
  258. break;
  259. case raise_code:
  260. freeStatement(st->u.raise_code.value);
  261. break;
  262. case include_file:
  263. freeStatement(st->u.include_file.file);
  264. break;
  265. default:
  266. break;
  267. }
  268. memFree(st->code_file);
  269. next_tmp = st->next;
  270. memFree(st);
  271. st = next_tmp;
  272. }
  273. return_:
  274. return;
  275. }
  276. Statement *copyStatement(Statement *st){
  277. if (st == NULL)
  278. return NULL;
  279. Statement *tmp = copyStatementCore(st);
  280. Statement *base_tmp = tmp;
  281. while (st->next != NULL){
  282. tmp->next = copyStatementCore(st->next);
  283. tmp = tmp->next;
  284. st = st->next;
  285. }
  286. return base_tmp;
  287. }
  288. Statement *copyStatementCore(Statement *st){
  289. Statement *new = makeStatement(st->line, st->code_file);
  290. new->type = st->type;
  291. new->next = NULL;
  292. switch (st->type) {
  293. case base_value:
  294. new->u.base_value.type = st->u.base_value.type;
  295. if (new->u.base_value.type == link_value) {
  296. new->u.base_value.value = st->u.base_value.value;
  297. gcAddStatementLink(&new->u.base_value.value->gc_status);
  298. }
  299. else
  300. new->u.base_value.str = memStrcpy(st->u.base_value.str, 0, false, false);
  301. break;
  302. case operation:
  303. new->u.operation.OperationType = st->u.operation.OperationType;
  304. new->u.operation.right = copyStatement(st->u.operation.right);
  305. new->u.operation.left = copyStatement(st->u.operation.left);
  306. break;
  307. case base_var:
  308. new->u.base_var.name = memStrcpy(st->u.base_var.name, 0, false, false);
  309. new->u.base_var.times = copyStatement(st->u.base_var.times);
  310. break;
  311. case base_svar:
  312. new->u.base_svar.name = copyStatement(st->u.base_svar.name);
  313. new->u.base_svar.times = copyStatement(st->u.base_svar.times);
  314. break;
  315. case set_function:
  316. new->u.set_function.name = copyStatement(st->u.set_function.name);
  317. new->u.set_function.function = copyStatement(st->u.set_function.function);
  318. new->u.set_function.parameter = copyParameter(st->u.set_function.parameter);
  319. break;
  320. case call_function:
  321. new->u.call_function.function = copyStatement(st->u.call_function.function);
  322. new->u.call_function.parameter = copyParameter(st->u.call_function.parameter);
  323. break;
  324. case base_list:
  325. new->u.base_list.type = st->u.base_list.type;
  326. new->u.base_list.list = copyParameter(st->u.base_list.list);
  327. break;
  328. case base_dict:
  329. new->u.base_dict.dict = copyParameter(st->u.base_dict.dict);
  330. break;
  331. case if_branch:
  332. new->u.if_branch.if_list = copyStatementList(st->u.if_branch.if_list);
  333. new->u.if_branch.finally = copyStatement(st->u.if_branch.finally);
  334. new->u.if_branch.else_list = copyStatement(st->u.if_branch.else_list);
  335. break;
  336. case while_branch:
  337. new->u.while_branch.type = st->u.while_branch.type;
  338. new->u.while_branch.while_list = copyStatementList(st->u.while_branch.while_list);
  339. new->u.while_branch.first = copyStatement(st->u.while_branch.first);
  340. new->u.while_branch.after = copyStatement(st->u.while_branch.after);
  341. new->u.while_branch.else_list = copyStatement(st->u.while_branch.else_list);
  342. new->u.while_branch.finally = copyStatement(st->u.while_branch.finally);
  343. break;
  344. case for_branch:
  345. new->u.for_branch.for_list = copyStatementList(st->u.for_branch.for_list);
  346. new->u.for_branch.var = copyStatement(st->u.for_branch.var);
  347. new->u.for_branch.iter = copyStatement(st->u.for_branch.iter);
  348. new->u.for_branch.else_list = copyStatement(st->u.for_branch.else_list);
  349. new->u.for_branch.finally = copyStatement(st->u.for_branch.finally);
  350. break;
  351. case try_branch:
  352. new->u.try_branch.except_list = copyStatementList(st->u.try_branch.except_list);
  353. new->u.try_branch.try = copyStatement(st->u.try_branch.try);
  354. new->u.try_branch.else_list = copyStatement(st->u.try_branch.else_list);
  355. new->u.try_branch.finally = copyStatement(st->u.try_branch.finally);
  356. break;
  357. case with_branch:
  358. new->u.with_branch.with_list = copyStatementList(st->u.with_branch.with_list);
  359. new->u.with_branch.else_list = copyStatement(st->u.with_branch.else_list);
  360. new->u.with_branch.finally = copyStatement(st->u.with_branch.finally);
  361. break;
  362. case break_cycle:
  363. new->u.break_cycle.times = copyStatement(st->u.break_cycle.times);
  364. break;
  365. case continue_cycle:
  366. new->u.continue_cycle.times = copyStatement(st->u.continue_cycle.times);
  367. break;
  368. case rego_if:
  369. new->u.rego_if.times = copyStatement(st->u.rego_if.times);
  370. break;
  371. case restart:
  372. new->u.restart.times = copyStatement(st->u.restart.times);
  373. break;
  374. case return_code:
  375. new->u.return_code.value = copyStatement(st->u.return_code.value);
  376. break;
  377. case raise_code:
  378. new->u.raise_code.value = copyStatement(st->u.raise_code.value);
  379. break;
  380. case include_file:
  381. new->u.include_file.file = copyStatement(st->u.include_file.file);
  382. break;
  383. default:
  384. break;
  385. }
  386. return new;
  387. }
  388. StatementList *makeStatementList(Statement *condition, Statement *var, Statement *code, int type) {
  389. StatementList *tmp = memCalloc(1, sizeof(StatementList));
  390. tmp->condition = condition;
  391. tmp->var = var;
  392. tmp->code = code;
  393. tmp->type = type;
  394. tmp->next = NULL;
  395. return tmp;
  396. }
  397. StatementList *connectStatementList(StatementList *base, StatementList *new){
  398. StatementList *tmp = base;
  399. if (base == NULL)
  400. return new;
  401. while (tmp->next != NULL){
  402. tmp = tmp->next;
  403. }
  404. tmp->next = new;
  405. return base;
  406. }
  407. void freeStatementList(StatementList *base){
  408. while (base != NULL){
  409. freeStatement(base->condition);
  410. freeStatement(base->code);
  411. freeStatement(base->var);
  412. StatementList *tmp = base;
  413. base = base->next;
  414. memFree(tmp);
  415. }
  416. }
  417. StatementList *copyStatementList(StatementList *sl){
  418. if (sl == NULL)
  419. return NULL;
  420. StatementList *tmp = makeStatementList(copyStatement(sl->condition), copyStatement(sl->var),
  421. copyStatement(sl->code), sl->type);
  422. StatementList *base_tmp = tmp;
  423. while (sl->next != NULL){
  424. tmp->next = makeStatementList(copyStatement(sl->condition), copyStatement(sl->var),
  425. copyStatement(sl->code), sl->type);
  426. tmp = tmp->next;
  427. sl = sl->next;
  428. }
  429. return base_tmp;
  430. }