runbranch.c 35 KB

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