runbranch.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. #include "__run.h"
  2. static bool checkNumber(FUNC){
  3. if (!isType(result->value->value, V_int)) {
  4. setResultErrorSt(E_TypeException, L"Don't get a int of layers", true, st, CNEXT_NT);
  5. return false;
  6. }
  7. return true;
  8. }
  9. static void newBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, VarList *new_var, enum StatementInfoStatus status, Inter *inter){
  10. if (new_var != NULL)
  11. new_var->next = NULL;
  12. branch_st->info.var_list = new_var;
  13. branch_st->info.node = node->type == yield_code ? node->next : node;
  14. branch_st->info.branch.sl_node = sl_node;
  15. branch_st->info.branch.status = status;
  16. branch_st->info.have_info = true;
  17. }
  18. static void newWithBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, VarList *new_var, enum StatementInfoStatus status,
  19. Inter *inter, LinkValue *value, LinkValue *_exit_, LinkValue *_enter_, LinkValue *with){
  20. newBranchYield(branch_st, node, sl_node, new_var, status, inter);
  21. branch_st->info.branch.with_.value = value;
  22. branch_st->info.branch.with_._exit_ = _exit_;
  23. branch_st->info.branch.with_._enter_ = _enter_;
  24. branch_st->info.branch.with_.with_belong = with;
  25. }
  26. static void newForBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, VarList *new_var, enum StatementInfoStatus status,
  27. Inter *inter, LinkValue *iter){
  28. newBranchYield(branch_st, node, sl_node, new_var, status, inter);
  29. branch_st->info.branch.for_.iter = iter;
  30. gc_addTmpLink(&iter->gc_status);
  31. }
  32. static void updateBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, enum StatementInfoStatus status){
  33. branch_st->info.node = node->type == yield_code ? node->next : node;
  34. branch_st->info.branch.sl_node = sl_node;
  35. branch_st->info.branch.status = status;
  36. branch_st->info.have_info = true;
  37. }
  38. static void setBranchResult(bool yield_run, StatementList *sl, Statement *st, Result *result, enum StatementInfoStatus status, FUNC_CORE) {
  39. if (yield_run) {
  40. if (result->type == R_yield)
  41. updateBranchYield(st, result->node, sl, status);
  42. else
  43. freeRunInfo(st);
  44. } else {
  45. if (result->type == R_yield)
  46. newBranchYield(st, result->node, sl, var_list, status, inter);
  47. else
  48. var_list = popVarList(var_list);
  49. }
  50. }
  51. static bool runBranchHeard(Statement *condition, Statement *var, LinkValue **condition_value, FUNC_NT) {
  52. setResultCore(result);
  53. if (operationSafeInterStatement(CFUNC(condition, var_list, result, belong)))
  54. return false;
  55. *condition_value = result->value;
  56. if (var != NULL) {
  57. result->value = NULL;
  58. freeResult(result);
  59. assCore(var, *condition_value, false, false, CNEXT_NT);
  60. gc_freeTmpLink(&(*condition_value)->gc_status);
  61. if (!CHECK_RESULT(result))
  62. return false;
  63. }
  64. return true;
  65. }
  66. static int checkCondition(bool is_rego, LinkValue *condition_value, fline line, char *file, FUNC_NT) {
  67. bool return_;
  68. if (is_rego)
  69. return 1;
  70. return_ = checkBool(condition_value, line, file, CNEXT_NT);
  71. if (!CHECK_RESULT(result))
  72. return -1;
  73. return return_;
  74. }
  75. static bool runFinally(bool set_result, bool (*run)(FUNC), FUNC) {
  76. Result finally_tmp;
  77. setResultCore(&finally_tmp);
  78. if (run(CFUNC(st, var_list, &finally_tmp, belong))) {
  79. if (!set_result)
  80. freeResult(result);
  81. *result = finally_tmp;
  82. return false;
  83. }
  84. freeResult(&finally_tmp);
  85. return true;
  86. }
  87. static int runIfList(StatementList *if_list, Statement *info_vl, fline line, char *file, FUNC_NT) {
  88. bool is_rego = false;
  89. setResultCore(result);
  90. for (PASS; if_list != NULL; if_list = if_list->next){
  91. bool condition;
  92. if (info_vl != NULL){
  93. if (ifBranchSafeInterStatement(CFUNC(info_vl, var_list, result, belong)))
  94. return -1;
  95. if (result->type == R_rego)
  96. is_rego = true;
  97. info_vl = NULL;
  98. } else if (if_list->type == if_b){
  99. LinkValue *condition_value = NULL;
  100. if (!runBranchHeard(if_list->condition, if_list->var, &condition_value, CNEXT_NT))
  101. return -1;
  102. freeResult(result);
  103. condition = checkCondition(is_rego, condition_value, line, file, CNEXT_NT);
  104. if (condition == -1)
  105. return -1;
  106. else if (condition == 1){
  107. is_rego = false;
  108. freeResult(result);
  109. if (ifBranchSafeInterStatement(CFUNC(if_list->code, var_list, result, belong)))
  110. return -1;
  111. if (result->type == R_rego)
  112. is_rego = true;
  113. else {
  114. freeResult(result);
  115. return 0;
  116. }
  117. }
  118. } else{
  119. if (ifBranchSafeInterStatement(CFUNC(if_list->code, var_list, result, belong)))
  120. return -1;
  121. if (result->type == R_rego)
  122. is_rego = true;
  123. }
  124. freeResult(result);
  125. }
  126. return 1;
  127. }
  128. ResultType ifBranch(FUNC) {
  129. StatementList *if_list = NULL;
  130. Statement *else_st = NULL;
  131. Statement *finally = NULL;
  132. Statement *info_vl = NULL;
  133. bool set_result = true;
  134. bool yield_run = false;
  135. enum StatementInfoStatus result_from = info_vl_branch;
  136. setResultCore(result);
  137. yield_run = popYieldVarList(st, &var_list, var_list, inter);
  138. if (yield_run) {
  139. if (st->info.branch.status == info_vl_branch){
  140. if_list = st->info.branch.sl_node;
  141. info_vl = st->info.node;
  142. else_st = st->u.if_branch.else_list;
  143. finally = st->u.if_branch.finally;
  144. if (info_vl == NULL)
  145. if_list = NULL; // 证明if分支的yield已经到头了
  146. } else if (st->info.branch.status == info_else_branch) {
  147. else_st = st->info.node;
  148. finally = st->u.if_branch.finally;
  149. } else if (st->info.branch.status == info_finally_branch)
  150. finally = st->info.node;
  151. else {
  152. var_list = popVarList(var_list);
  153. setResultError(E_SystemException, L"yield info error", st->line, st->code_file, true, CNEXT_NT);
  154. return R_error;
  155. }
  156. } else {
  157. if_list = st->u.if_branch.if_list;
  158. else_st = st->u.if_branch.else_list;
  159. finally = st->u.if_branch.finally;
  160. }
  161. if (if_list != NULL) {
  162. int status = runIfList(if_list, info_vl, st->line, st->code_file, CNEXT_NT);
  163. if (status == -1 || status == 0)
  164. else_st = NULL;
  165. if (status == -1)
  166. set_result = false;
  167. }
  168. if (else_st != NULL) {
  169. if (ifBranchSafeInterStatement(CFUNC(else_st, var_list, result, belong))) {
  170. set_result = false;
  171. result_from = info_else_branch;
  172. } else
  173. freeResult(result);
  174. }
  175. if (finally != NULL && !runFinally(set_result, ifBranchSafeInterStatement, finally, CNEXT_NT)) {
  176. set_result = false;
  177. result_from = info_finally_branch;
  178. }
  179. setBranchResult(yield_run, if_list, st, result, result_from, CFUNC_CORE(var_list));
  180. if (set_result)
  181. setResult(result, inter);
  182. return result->type;
  183. }
  184. static int runWhileList(StatementList *while_list, Statement *after, Statement *info_vl, Statement *after_vl, bool do_while, fline line, char *file, enum StatementInfoStatus *from, FUNC_NT) {
  185. bool is_break = false;
  186. setResultCore(result);
  187. while (!is_break){
  188. LinkValue *condition_value = NULL;
  189. Statement *after_st = after;
  190. Statement *while_st = while_list->code;
  191. bool condition;
  192. *from = info_vl_branch;
  193. if (info_vl != NULL) {
  194. condition = 1;
  195. while_st = info_vl;
  196. info_vl = NULL;
  197. } else if (after_vl != NULL) {
  198. condition = -2;
  199. after_st = after_vl;
  200. after_vl = NULL;
  201. } else{
  202. if (!runBranchHeard(while_list->condition, while_list->var, &condition_value, CNEXT_NT))
  203. return -1;
  204. freeResult(result);
  205. condition = checkCondition(do_while, condition_value, line, file, CNEXT_NT);
  206. }
  207. do_while = false;
  208. if (condition == -1)
  209. return -1;
  210. else if (condition == 1){
  211. freeResult(result);
  212. if (cycleBranchSafeInterStatement(CFUNC(while_st, var_list, result, belong)))
  213. return -1;
  214. else if (result->type == R_break)
  215. is_break = true;
  216. } else if (condition != -2){
  217. freeResult(result);
  218. break;
  219. }
  220. if (after_st != NULL) {
  221. *from = info_after_do;
  222. freeResult(result);
  223. if (cycleBranchSafeInterStatement(CFUNC(after_st, var_list, result, belong)))
  224. return -1;
  225. else if (result->type == R_break) {
  226. freeResult(result);
  227. return 0;
  228. }
  229. }
  230. freeResult(result);
  231. }
  232. return is_break ? 0 : 1;
  233. }
  234. ResultType whileBranch(FUNC) {
  235. StatementList *while_list = st->u.while_branch.while_list;
  236. Statement *first = st->u.while_branch.first;
  237. Statement *after = st->u.while_branch.after;
  238. Statement *else_st = st->u.while_branch.else_list;
  239. Statement *finally = st->u.while_branch.finally;
  240. Statement *info_vl = NULL;
  241. Statement *after_vl = NULL;
  242. bool set_result = true;
  243. bool yield_run = false;
  244. bool do_while = st->u.while_branch.type == do_while_;
  245. enum StatementInfoStatus result_from = info_first_do;
  246. setResultCore(result);
  247. yield_run = popYieldVarList(st, &var_list, var_list, inter);
  248. if (yield_run) {
  249. if (st->info.branch.status == info_first_do)
  250. first = st->info.node;
  251. else if (st->info.branch.status == info_vl_branch){
  252. first = NULL;
  253. info_vl = st->info.node;
  254. }
  255. else if (st->info.branch.status == info_after_do){
  256. first = NULL;
  257. after_vl = st->info.node;
  258. }
  259. else if (st->info.branch.status == info_else_branch){
  260. first = NULL;
  261. while_list = NULL;
  262. else_st = st->info.node;
  263. }
  264. else if (st->info.branch.status == info_finally_branch){
  265. first = NULL;
  266. while_list = NULL;
  267. else_st = NULL;
  268. finally = st->info.node;
  269. } else {
  270. var_list = popVarList(var_list);
  271. setResultError(E_SystemException, L"yield info error", st->line, st->code_file, true, CNEXT_NT);
  272. return R_error;
  273. }
  274. }
  275. if (first != NULL) {
  276. if (cycleBranchSafeInterStatement(CFUNC(first, var_list, result, belong))) {
  277. set_result = false;
  278. while_list = NULL;
  279. else_st = NULL;
  280. } else
  281. freeResult(result);
  282. }
  283. if (while_list != NULL){
  284. int status = runWhileList(while_list, after, info_vl, after_vl, do_while, st->line, st->code_file, &result_from, CNEXT_NT);
  285. if (status == -1 || status == 0)
  286. else_st = NULL;
  287. if (status == -1)
  288. set_result = false;
  289. }
  290. if (else_st != NULL) {
  291. if (cycleBranchSafeInterStatement(CFUNC(else_st, var_list, result, belong))) {
  292. result_from = info_else_branch;
  293. set_result = false;
  294. } else
  295. freeResult(result);
  296. }
  297. if (finally != NULL && !runFinally(set_result, cycleBranchSafeInterStatement, finally, CNEXT_NT)) {
  298. set_result = false;
  299. result_from = info_finally_branch;
  300. }
  301. setBranchResult(yield_run, while_list, st, result, result_from, CFUNC_CORE(var_list));
  302. if (set_result)
  303. setResult(result, inter);
  304. return result->type;
  305. }
  306. static int getForHeard(LinkValue **iter, LinkValue **first_yield, StatementList *for_list, fline line, char *file, FUNC_NT) {
  307. LinkValue *tmp = NULL;
  308. setResultCore(result);
  309. if (operationSafeInterStatement(CFUNC(for_list->condition, var_list, result, belong)))
  310. return -1;
  311. if (result->is_yield){
  312. iter = NULL;
  313. GET_RESULTONLY(*first_yield, result);
  314. return 1;
  315. }
  316. GET_RESULT(tmp, result);
  317. getIter(tmp, 1, line, file, CNEXT_NT);
  318. gc_freeTmpLink(&tmp->gc_status);
  319. if (!CHECK_RESULT(result))
  320. return -1;
  321. GET_RESULTONLY(*iter, result);
  322. return 0;
  323. }
  324. static int runForHeard(LinkValue *iter, LinkValue *first_yield, StatementList *for_list, fline line, char *file, FUNC_NT) {
  325. LinkValue *element = NULL;
  326. if (iter != NULL) {
  327. getIter(iter, 0, line, file, CNEXT_NT);
  328. if (!CHECK_RESULT(result)) {
  329. if (is_iterStop(result->value, inter)) {
  330. freeResult(result);
  331. return 1;
  332. } else
  333. return -1;
  334. }
  335. GET_RESULT(element, result);
  336. } else if (first_yield != NULL)
  337. element = first_yield;
  338. else {
  339. if (operationSafeInterStatement(CFUNC(for_list->condition, var_list, result, belong)))
  340. return -1;
  341. GET_RESULT(element, result);
  342. if (!result->is_yield)
  343. return 1;
  344. }
  345. assCore(for_list->var, element, false, false, CNEXT_NT);
  346. gc_freeTmpLink(&element->gc_status);
  347. if (!CHECK_RESULT(result))
  348. return -1;
  349. freeResult(result);
  350. return 0;
  351. }
  352. static int runForList(StatementList *for_list, Statement *after, Statement *info_vl, Statement *after_vl, LinkValue *iter, LinkValue *first_yield, fline line, char *file, enum StatementInfoStatus *from, FUNC_NT) {
  353. bool is_break = false;
  354. while (!is_break){ // 循环执行的本体
  355. Statement *for_st = for_list->code;
  356. Statement *after_st = after;
  357. *from = info_vl_branch;
  358. if (info_vl != NULL){
  359. for_st = info_vl;
  360. info_vl = NULL;
  361. }
  362. else if (after_vl != NULL){
  363. after_st = after_vl;
  364. after_vl = NULL;
  365. for_st = NULL;
  366. } else {
  367. int status = runForHeard(iter, first_yield, for_list, line, file, CNEXT_NT);
  368. first_yield = NULL;
  369. if (status == -1)
  370. return -1;
  371. else if (status == 1)
  372. break;
  373. }
  374. if (for_st != NULL) {
  375. if (cycleBranchSafeInterStatement(CFUNC(for_st, var_list, result, belong)))
  376. return -1;
  377. else if (result->type == R_break)
  378. is_break = true;
  379. }
  380. *from = info_after_do;
  381. if (after_st != NULL) {
  382. freeResult(result);
  383. if (cycleBranchSafeInterStatement(CFUNC(after_st, var_list, result, belong)))
  384. return -1;
  385. else if (result->type == R_break)
  386. is_break = true;
  387. }
  388. freeResult(result);
  389. }
  390. return is_break ? 0 : 1;
  391. }
  392. static void setForResult(bool yield_run, StatementList *sl, Statement *st, Result *result, LinkValue *iter, enum StatementInfoStatus status, FUNC_CORE) {
  393. if (yield_run) {
  394. if (result->type == R_yield)
  395. updateBranchYield(st, result->node, sl, status);
  396. else
  397. freeRunInfo(st);
  398. } else {
  399. if (result->type == R_yield)
  400. newForBranchYield(st, result->node, sl, var_list, status, inter, iter);
  401. else
  402. popVarList(var_list);
  403. }
  404. }
  405. ResultType forBranch(FUNC) {
  406. StatementList *for_list = st->u.for_branch.for_list;
  407. Statement *first = st->u.for_branch.first_do;
  408. Statement *after = st->u.for_branch.after_do;
  409. Statement *else_st = st->u.for_branch.else_list;
  410. Statement *finally = st->u.for_branch.finally;
  411. Statement *info_vl = NULL;
  412. Statement *after_vl = NULL;
  413. bool set_result = true;
  414. bool yield_run = false;
  415. bool heard = true;
  416. LinkValue *first_yield = NULL;
  417. LinkValue *iter = NULL;
  418. enum StatementInfoStatus result_from = info_first_do;
  419. setResultCore(result);
  420. yield_run = popYieldVarList(st, &var_list, var_list, inter);
  421. if (yield_run) {
  422. if (st->info.branch.status == info_first_do)
  423. first = st->info.node;
  424. else if (st->info.branch.status == info_vl_branch) {
  425. first = NULL;
  426. info_vl = st->info.node;
  427. iter = st->info.branch.for_.iter;
  428. heard = false;
  429. } else if (st->info.branch.status == info_after_do) {
  430. first = NULL;
  431. after_vl = st->info.node;
  432. iter = st->info.branch.for_.iter;
  433. heard = false;
  434. } else if (st->info.branch.status == info_else_branch) {
  435. first = NULL;
  436. heard = false;
  437. for_list = false;
  438. else_st = st->info.node;
  439. } else if (st->info.branch.status == info_finally_branch) {
  440. first = NULL;
  441. heard = false;
  442. for_list = false;
  443. else_st = NULL;
  444. finally = st->info.node;
  445. } else {
  446. var_list = popVarList(var_list);
  447. setResultError(E_SystemException, L"yield info error", st->line, st->code_file, true, CNEXT_NT);
  448. return R_error;
  449. }
  450. }
  451. if (first != NULL) {
  452. if (cycleBranchSafeInterStatement(CFUNC(first, var_list, result, belong))) {
  453. set_result = false;
  454. heard = false;
  455. for_list = NULL;
  456. else_st = NULL;
  457. } else
  458. freeResult(result);
  459. }
  460. if (heard){
  461. int status = getForHeard(&iter, &first_yield, for_list, st->line, st->code_file, CNEXT_NT);
  462. if (status == -1) {
  463. set_result = false;
  464. for_list = NULL;
  465. else_st = NULL;
  466. } else
  467. freeResult(result);
  468. }
  469. if (for_list != NULL){
  470. int status = runForList(for_list, after, info_vl, after_vl, iter, first_yield, st->line, st->code_file, &result_from, CNEXT_NT);
  471. if (status == -1 || status == 0)
  472. else_st = NULL;
  473. if (status == -1)
  474. set_result = false;
  475. }
  476. if (else_st != NULL) {
  477. if (cycleBranchSafeInterStatement(CFUNC(else_st, var_list, result, belong))) {
  478. result_from = info_else_branch;
  479. set_result = false;
  480. }
  481. else
  482. freeResult(result);
  483. }
  484. if (finally != NULL && !runFinally(set_result, cycleBranchSafeInterStatement, finally, CNEXT_NT)) {
  485. set_result = false;
  486. result_from = info_finally_branch;
  487. }
  488. setForResult(yield_run, for_list, st, result, iter, result_from, CFUNC_CORE(var_list));
  489. if (!yield_run && iter != NULL)
  490. gc_freeTmpLink(&iter->gc_status);
  491. if (set_result)
  492. setResult(result, inter);
  493. return result->type;
  494. }
  495. static bool getEnterExit(LinkValue *value, LinkValue **_enter_, LinkValue **_exit_, fline line, char *file, FUNC_NT) {
  496. setResultCore(result);
  497. *_enter_ = findAttributes(inter->data.mag_func[M_ENTER], false, LINEFILE, true, CFUNC_NT(var_list, result, value));
  498. if (!CHECK_RESULT(result))
  499. return false;
  500. freeResult(result);
  501. *_exit_ = findAttributes(inter->data.mag_func[M_EXIT], false, LINEFILE, true, CFUNC_NT(var_list, result, value));
  502. if (!CHECK_RESULT(result))
  503. return false;
  504. freeResult(result);
  505. if (*_enter_ == NULL || *_exit_ == NULL) {
  506. *_enter_ = NULL;
  507. *_exit_ = NULL;
  508. setResultError(E_TypeException, OBJ_NOTSUPPORT(__enter__/__exit__), line, file, true, CNEXT_NT);
  509. return false;
  510. }
  511. gc_addTmpLink(&(*_enter_)->gc_status);
  512. gc_addTmpLink(&(*_exit_)->gc_status);
  513. return true;
  514. }
  515. static int runWithList(StatementList *with_list, LinkValue **with_belong, LinkValue **value, VarList **new, LinkValue **_enter_, LinkValue **_exit_, fline line, char *file, FUNC_NT) {
  516. if (operationSafeInterStatement(CFUNC(with_list->condition, var_list, result, belong)))
  517. return -1;
  518. GET_RESULT(*value, result);
  519. if (with_list->var == NULL) {
  520. *with_belong = *value;
  521. gc_addTmpLink(&(*with_belong)->gc_status);
  522. *new = copyVarListCore((*value)->value->object.var, inter);
  523. (*new)->next = var_list;
  524. *_enter_ = NULL;
  525. *_exit_ = NULL;
  526. } else {
  527. LinkValue *enter_value = NULL;
  528. *with_belong = belong;
  529. gc_addTmpLink(&(*with_belong)->gc_status);
  530. *_enter_ = findAttributes(inter->data.mag_func[M_ENTER], false, LINEFILE, true, CFUNC_NT(var_list, result, *value));
  531. if (!CHECK_RESULT(result))
  532. goto error_;
  533. freeResult(result);
  534. *_exit_ = findAttributes(inter->data.mag_func[M_EXIT], false, LINEFILE, true, CFUNC_NT(var_list, result, *value));
  535. if (!CHECK_RESULT(result))
  536. goto error_;
  537. freeResult(result);
  538. if (!getEnterExit(*value, _enter_, _exit_, line, file, CNEXT_NT)) {
  539. error_:
  540. gc_freeTmpLink(&(*value)->gc_status);
  541. gc_freeTmpLink(&(*with_belong)->gc_status);
  542. *value = NULL;
  543. return -1;
  544. }
  545. callBackCore(*_enter_, NULL, line, file, 0, CNEXT_NT);
  546. if (!CHECK_RESULT(result))
  547. return 0;
  548. *new = pushVarList(var_list, inter);
  549. enter_value = result->value;
  550. freeResult(result);
  551. assCore(with_list->var, enter_value, false, false, CFUNC_NT(*new, result, belong));
  552. if (!CHECK_RESULT(result)) {
  553. *new = popVarList(*new);
  554. return 0;
  555. }
  556. freeResult(result);
  557. }
  558. return 1;
  559. }
  560. static void setWithResult(bool yield_run, StatementList *sl, Statement *st, Result *result, LinkValue *value, LinkValue *_enter_, LinkValue *_exit_, LinkValue *with, enum StatementInfoStatus status, FUNC_CORE) {
  561. if (yield_run) {
  562. if (result->type == R_yield)
  563. if (status == info_finally_branch) {
  564. freeRunInfo(st);
  565. newBranchYield(st, result->node, sl, NULL, status, inter);
  566. } else
  567. updateBranchYield(st, result->node, sl, status);
  568. else
  569. freeRunInfo(st);
  570. } else {
  571. if (result->type == R_yield)
  572. if (status == info_finally_branch) {
  573. newBranchYield(st, result->node, sl, NULL, status, inter);
  574. popVarList(var_list);
  575. }
  576. else
  577. newWithBranchYield(st, result->node, sl, var_list, status, inter, value, _exit_, _enter_, with);
  578. else {
  579. if (var_list != NULL)
  580. popVarList(var_list);
  581. }
  582. }
  583. }
  584. ResultType withBranch(FUNC) {
  585. StatementList *with_list = st->u.with_branch.with_list;
  586. Statement *else_st = st->u.with_branch.else_list;
  587. Statement *finally = st->u.with_branch.finally;
  588. Statement *vl_info = NULL;
  589. VarList *new = NULL;
  590. LinkValue *_enter_ = NULL;
  591. LinkValue *_exit_ = NULL;
  592. LinkValue *value = NULL;
  593. LinkValue *with_belong = NULL; // with在运行的时候调整belong, 就类似于定义class的时候调整belong
  594. bool set_result = true;
  595. bool run_block = true;
  596. bool run_exit = true;
  597. bool yield_run;
  598. enum StatementInfoStatus result_from = info_vl_branch;
  599. setResultCore(result);
  600. if ((yield_run = st->info.have_info)){
  601. value = st->info.branch.with_.value;
  602. _enter_ = st->info.branch.with_._enter_;
  603. _exit_ = st->info.branch.with_._exit_;
  604. with_belong = st->info.branch.with_.with_belong;
  605. if (st->info.var_list != NULL) {
  606. new = st->info.var_list;
  607. new->next = var_list;
  608. }
  609. if (st->info.branch.status == info_vl_branch) {
  610. vl_info = st->info.node;
  611. if (vl_info == NULL)
  612. run_block = false;
  613. }
  614. else if (st->info.branch.status == info_else_branch) {
  615. run_block = false;
  616. else_st = st->info.node;
  617. }
  618. else if (st->info.branch.status == info_finally_branch){
  619. run_block = false;
  620. else_st = NULL;
  621. finally = st->info.node;
  622. }
  623. } else {
  624. int status = runWithList(with_list, &with_belong, &value, &new, &_enter_, &_exit_, st->line, st->code_file, CNEXT_NT);
  625. if (status == -1) {
  626. set_result = false;
  627. run_block = false;
  628. else_st = NULL;
  629. } else if (status == 0) {
  630. set_result = false;
  631. gc_freeTmpLink(&value->gc_status);
  632. gc_freeTmpLink(&_enter_->gc_status);
  633. gc_freeTmpLink(&_exit_->gc_status);
  634. gc_freeTmpLink(&with_belong->gc_status);
  635. run_block = false;
  636. else_st = NULL;
  637. }
  638. }
  639. if (run_block) {
  640. if (vl_info == NULL)
  641. vl_info = with_list->code;
  642. if (withBranchSafeInterStatement(CFUNC(vl_info, new, result, with_belong))) {
  643. set_result = false;
  644. if (result->type == R_yield) {
  645. run_exit = false;
  646. else_st = NULL;
  647. }
  648. } else
  649. freeResult(result);
  650. }
  651. if (else_st != NULL){
  652. Result else_tmp;
  653. setResultCore(&else_tmp);
  654. if (withBranchSafeInterStatement(CFUNC(else_st, new, &else_tmp, with_belong))) {
  655. if (!set_result)
  656. freeResult(result);
  657. set_result = false;
  658. *result = else_tmp;
  659. result_from = info_else_branch;
  660. if (result->type == R_yield)
  661. run_exit = false;
  662. } else
  663. freeResult(&else_tmp);
  664. }
  665. if (run_exit){ // 若运行中的某处返回了R_yield则不执行exit
  666. if (_exit_ != NULL){
  667. Result exit_tmp;
  668. setResultCore(&exit_tmp);
  669. callBackCore(_exit_, NULL, st->line, st->code_file, 0, CFUNC_NT(var_list, &exit_tmp, belong));
  670. if (!RUN_TYPE(exit_tmp.type)) {
  671. if (!set_result)
  672. freeResult(result);
  673. set_result = false;
  674. *result = exit_tmp;
  675. } else
  676. freeResult(&exit_tmp);
  677. if (!yield_run) {
  678. gc_freeTmpLink(&_enter_->gc_status);
  679. gc_freeTmpLink(&_exit_->gc_status);
  680. _enter_ = NULL;
  681. _exit_ = NULL;
  682. }
  683. }
  684. if (!yield_run) {
  685. if (with_belong != NULL){
  686. gc_freeTmpLink(&with_belong->gc_status);
  687. with_belong = NULL;
  688. }
  689. if (value != NULL){
  690. gc_freeTmpLink(&value->gc_status);
  691. value = NULL;
  692. }
  693. }
  694. }
  695. if (finally != NULL && !runFinally(set_result, withBranchSafeInterStatement, finally, CNEXT_NT)) {
  696. set_result = false;
  697. result_from = info_finally_branch;
  698. }
  699. setWithResult(yield_run, with_list, st, result, value, _enter_, _exit_, with_belong, result_from, CFUNC_CORE(new));
  700. if (set_result)
  701. setResult(result, inter);
  702. return result->type;
  703. }
  704. static int checkError(StatementList **except_list, LinkValue *error_value, FUNC_NT) {
  705. setResultCore(result);
  706. if (*except_list == NULL)
  707. return -1;
  708. for (PASS; *except_list != NULL; except_list = &(*except_list)->next) {
  709. freeResult(result);
  710. if ((*except_list)->condition == NULL)
  711. break;
  712. if (operationSafeInterStatement(CFUNC((*except_list)->condition, var_list, result, belong)))
  713. return 0;
  714. if (result->value->value == error_value->value || checkAttribution(error_value->value, result->value->value))
  715. break;
  716. }
  717. return *except_list == NULL ? -1 : 1;
  718. }
  719. ResultType tryBranch(FUNC) {
  720. StatementList *except_list = NULL;
  721. Statement *try = st->u.try_branch.try;
  722. Statement *else_st = st->u.try_branch.else_list;
  723. Statement *finally = st->u.try_branch.finally;
  724. Statement *info_vl = NULL;
  725. LinkValue *error_value = NULL;
  726. bool set_result = true;
  727. bool yield_run;
  728. enum StatementInfoStatus result_from = info_first_do;
  729. setResultCore(result);
  730. yield_run = popYieldVarList(st, &var_list, var_list, inter);
  731. if (yield_run && st->info.branch.status == info_first_do)
  732. try = st->info.node;
  733. else if (yield_run && st->info.branch.status == info_vl_branch){
  734. try = NULL;
  735. info_vl = st->info.node;
  736. }
  737. else if (yield_run && st->info.branch.status == info_else_branch){
  738. try = NULL;
  739. else_st = st->info.node;
  740. }
  741. else if (yield_run && st->info.branch.status == info_finally_branch){
  742. try = NULL;
  743. else_st = NULL;
  744. finally = st->info.node;
  745. }
  746. if (try != NULL){
  747. if (tryBranchSafeInterStatement(CFUNC(try, var_list, result, belong))) {
  748. if (result->type == R_yield) {
  749. result_from = info_first_do;
  750. set_result = false;
  751. else_st = NULL;
  752. } else {
  753. int status;
  754. GET_RESULT(error_value, result);
  755. except_list = st->u.try_branch.except_list;
  756. status = checkError(&except_list, error_value, CNEXT_NT);
  757. if (status == -1 || status == 0) {
  758. set_result = false;
  759. result_from = info_vl_branch;
  760. except_list = NULL;
  761. else_st = NULL;
  762. } else
  763. freeResult(result);
  764. gc_freeTmpLink(&error_value->gc_status);
  765. }
  766. } else
  767. freeResult(result);
  768. }
  769. if (except_list != NULL) {
  770. if (except_list->var != NULL) {
  771. gc_addTmpLink(&error_value->gc_status);
  772. assCore(except_list->var, error_value, false, false, CNEXT_NT);
  773. gc_freeTmpLink(&error_value->gc_status);
  774. if (!CHECK_RESULT(result)) {
  775. set_result = false;
  776. else_st = NULL;
  777. } else
  778. freeResult(result);
  779. }
  780. info_vl = except_list->code;
  781. }
  782. if (info_vl != NULL) {
  783. freeResult(result);
  784. if (tryBranchSafeInterStatement(CFUNC(info_vl, var_list, result, belong))) {
  785. result_from = info_vl_branch;
  786. set_result = false;
  787. } else
  788. freeResult(result);
  789. }
  790. if (else_st != NULL) {
  791. if (tryBranchSafeInterStatement(CFUNC(else_st, var_list, result, belong))) {
  792. set_result = false;
  793. result_from = info_else_branch;
  794. }
  795. else
  796. freeResult(result);
  797. }
  798. if (finally != NULL && !runFinally(set_result, ifBranchSafeInterStatement, finally, CNEXT_NT)) {
  799. set_result = false;
  800. result_from = info_finally_branch;
  801. }
  802. setBranchResult(yield_run, except_list, st, result, result_from, CFUNC_CORE(var_list));
  803. if (set_result)
  804. setResult(result, inter);
  805. return result->type;
  806. }
  807. ResultType breakCycle(FUNC){
  808. int times_int = 0;
  809. setResultCore(result);
  810. if (st->u.break_cycle.times == NULL)
  811. goto not_times;
  812. if (operationSafeInterStatement(CFUNC(st->u.break_cycle.times, var_list, result, belong)))
  813. return result->type;
  814. if (!checkNumber(CNEXT))
  815. return result->type;
  816. times_int = (int)result->value->value->data.int_.num;
  817. freeResult(result);
  818. not_times:
  819. setResult(result, inter);
  820. if (times_int >= 0) {
  821. result->type = R_break;
  822. result->times = times_int;
  823. }
  824. return result->type;
  825. }
  826. ResultType continueCycle(FUNC){
  827. int times_int = 0;
  828. setResultCore(result);
  829. if (st->u.continue_cycle.times == NULL)
  830. goto not_times;
  831. if (operationSafeInterStatement(CFUNC(st->u.continue_cycle.times, var_list, result, belong)))
  832. return result->type;
  833. if (!checkNumber(CNEXT))
  834. return result->type;
  835. times_int = (int)result->value->value->data.int_.num;
  836. freeResult(result);
  837. not_times:
  838. setResult(result, inter);
  839. if (times_int >= 0) {
  840. result->type = R_continue;
  841. result->times = times_int;
  842. }
  843. return result->type;
  844. }
  845. ResultType regoIf(FUNC){
  846. int times_int = 0;
  847. setResultCore(result);
  848. if (st->u.rego_if.times == NULL)
  849. goto not_times;
  850. if (operationSafeInterStatement(CFUNC(st->u.rego_if.times, var_list, result, belong)))
  851. return result->type;
  852. if (!checkNumber(CNEXT))
  853. return result->type;
  854. times_int = (int)result->value->value->data.int_.num;
  855. freeResult(result);
  856. not_times:
  857. setResult(result, inter);
  858. if (times_int >= 0) {
  859. result->type = R_rego;
  860. result->times = times_int;
  861. }
  862. return result->type;
  863. }
  864. ResultType restartCode(FUNC){
  865. int times_int = 0;
  866. setResultCore(result);
  867. if (st->u.restart.times == NULL)
  868. goto not_times;
  869. if (operationSafeInterStatement(CFUNC(st->u.restart.times, var_list, result, belong)))
  870. return result->type;
  871. if (!checkNumber(CNEXT))
  872. return result->type;
  873. times_int = (int)result->value->value->data.int_.num;
  874. freeResult(result);
  875. not_times:
  876. setResult(result, inter);
  877. if (times_int >= 0) {
  878. result->type = R_restart;
  879. result->times = times_int;
  880. }
  881. return result->type;
  882. }
  883. ResultType returnCode(FUNC){
  884. setResultCore(result);
  885. if (st->u.return_code.value == NULL) {
  886. setResult(result, inter);
  887. goto set_result;
  888. }
  889. if (operationSafeInterStatement(CFUNC(st->u.return_code.value, var_list, result, belong)))
  890. return result->type;
  891. set_result:
  892. result->type = R_func;
  893. return result->type;
  894. }
  895. ResultType yieldCode(FUNC){
  896. setResultCore(result);
  897. if (st->u.yield_code.value == NULL) {
  898. setResult(result, inter);
  899. goto set_result;
  900. }
  901. if (operationSafeInterStatement(CFUNC(st->u.yield_code.value, var_list, result, belong)))
  902. return result->type;
  903. set_result:
  904. result->type = R_yield;
  905. return result->type;
  906. }
  907. ResultType raiseCode(FUNC){
  908. setResultCore(result);
  909. if (st->u.raise_code.value == NULL) {
  910. setResult(result, inter);
  911. goto set_result;
  912. }
  913. if (operationSafeInterStatement(CFUNC(st->u.raise_code.value, var_list, result, belong)))
  914. return result->type;
  915. set_result:
  916. result->type = R_error;
  917. result->error = connectError(makeError(L"RaiseException", L"Exception was raise by user", st->line, st->code_file), result->error);
  918. return result->type;
  919. }
  920. ResultType assertCode(FUNC){
  921. bool result_;
  922. LinkValue *opt;
  923. setResultCore(result);
  924. if (inter->data.assert_run == assert_ignore) { // 不执行断言
  925. setResult(result, inter);
  926. return result->type;
  927. }
  928. if (operationSafeInterStatement(CFUNC(st->u.raise_code.value, var_list, result, belong)) || inter->data.assert_run == assert_run)
  929. return result->type;
  930. GET_RESULT(opt, result);
  931. result_ = checkBool(opt, st->line, st->code_file, CNEXT_NT);
  932. gc_freeTmpLink(&opt->gc_status);
  933. if (!CHECK_RESULT(result))
  934. return result->type;
  935. else if (result_)
  936. setResult(result, inter);
  937. else
  938. setResultErrorSt(E_AssertException, L"Assertion check error", true, st, CNEXT_NT);
  939. return result->type;
  940. }
  941. ResultType gotoLabel(FUNC){
  942. int times_int = 0;
  943. wchar_t *label = NULL;
  944. setResultCore(result);
  945. if (operationSafeInterStatement(CFUNC(st->u.goto_.label, var_list, result, belong)))
  946. return result->type;
  947. if (!isType(result->value->value, V_str)) {
  948. setResultErrorSt(E_TypeException, ONLY_ACC(label name, V_str), true, st, CNEXT_NT);
  949. return result->type;
  950. }
  951. label = memWidecpy(result->value->value->data.str.str);
  952. freeResult(result);
  953. if (st->u.goto_.times == NULL)
  954. goto not_times;
  955. if (operationSafeInterStatement(CFUNC(st->u.goto_.times, var_list, result, belong))) {
  956. memFree(label);
  957. return result->type;
  958. }
  959. if (!checkNumber(CNEXT)) {
  960. memFree(label);
  961. return result->type;
  962. }
  963. times_int = (int)result->value->value->data.int_.num;
  964. freeResult(result);
  965. not_times:
  966. if (st->u.goto_.return_ == NULL)
  967. setResult(result, inter);
  968. else if (operationSafeInterStatement(CFUNC(st->u.goto_.return_, var_list, result, belong))) {
  969. memFree(label);
  970. return result->type;
  971. }
  972. result->times = times_int;
  973. result->type = R_goto;
  974. result->label = label;
  975. return result->type;
  976. }
  977. ResultType runLabel(FUNC) {
  978. LinkValue *goto_value;
  979. GET_RESULT(goto_value, result); // goto的值通过result传入, 因此不能进行setResultCore
  980. if (st->u.label_.as != NULL)
  981. assCore(st->u.label_.as, goto_value, false, false, CNEXT_NT);
  982. gc_freeTmpLink(&goto_value->gc_status);
  983. if (st->u.label_.as != NULL && !CHECK_RESULT(result))
  984. return result->type;
  985. freeResult(result);
  986. if (st->u.label_.command != NULL)
  987. operationSafeInterStatement(CFUNC(st->u.label_.command, var_list, result, belong));
  988. else
  989. setResult(result, inter);
  990. return result->type;
  991. }