runbranch.c 33 KB

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