statement.c 16 KB

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