run.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "__run.h"
  2. /**
  3. * 运行单个statement
  4. * @param st
  5. * @param inter
  6. * @param var_list
  7. * @return
  8. */
  9. ResultType runStatement(FUNC) {
  10. ResultType type = R_not;
  11. setResultCore(result);
  12. gc_addTmpLink(&belong->gc_status);
  13. switch (st->type) {
  14. case base_value:
  15. type = getBaseValue(CNEXT);
  16. break;
  17. case base_var:
  18. type = getVar(CNEXT, getBaseVarInfo);
  19. break;
  20. case base_svar:
  21. type = getVar(CNEXT, getBaseSVarInfo);
  22. break;
  23. case base_list:
  24. type = getList(CNEXT);
  25. break;
  26. case base_dict:
  27. type = getDict(CNEXT);
  28. break;
  29. case base_lambda:
  30. type = setLambda(CNEXT);
  31. break;
  32. case operation:
  33. type = operationStatement(CNEXT);
  34. break;
  35. case set_class:
  36. type = setClass(CNEXT);
  37. break;
  38. case set_function:
  39. type = setFunction(CNEXT);
  40. break;
  41. case slice_:
  42. type = elementSlice(CNEXT);
  43. break;
  44. case call_function:
  45. type = callBack(CNEXT);
  46. break;
  47. case if_branch:
  48. type = ifBranch(CNEXT);
  49. break;
  50. case while_branch:
  51. type = whileBranch(CNEXT);
  52. break;
  53. case for_branch:
  54. type = forBranch(CNEXT);
  55. break;
  56. case with_branch:
  57. type = withBranch(CNEXT);
  58. break;
  59. case try_branch:
  60. type = tryBranch(CNEXT);
  61. break;
  62. case break_cycle:
  63. type = breakCycle(CNEXT);
  64. break;
  65. case continue_cycle:
  66. type = continueCycle(CNEXT);
  67. break;
  68. case rego_if:
  69. type = regoIf(CNEXT);
  70. break;
  71. case restart:
  72. type = restartCode(CNEXT);
  73. break;
  74. case return_code:
  75. type = returnCode(CNEXT);
  76. break;
  77. case yield_code:
  78. type = yieldCode(CNEXT);
  79. break;
  80. case raise_code:
  81. type = raiseCode(CNEXT);
  82. break;
  83. case include_file:
  84. type = includeFile(CNEXT);
  85. break;
  86. case import_file:
  87. type = importFile(CNEXT);
  88. break;
  89. case from_import_file:
  90. type = fromImportFile(CNEXT);
  91. break;
  92. case default_var:
  93. type = setDefault(CNEXT);
  94. break;
  95. case assert:
  96. type = assertCode(CNEXT);
  97. break;
  98. case goto_:
  99. type = gotoLabel(CNEXT);
  100. break;
  101. case del_:
  102. type = delOperation(CNEXT);
  103. break;
  104. default:
  105. setResult(result, inter);
  106. break;
  107. }
  108. if (RUN_TYPE(type) && result->value->aut == auto_aut)
  109. result->value->aut = st->aut;
  110. result->node = st;
  111. #if START_GC
  112. gc_freeTmpLink(&belong->gc_status);
  113. gc_run(inter, var_list, 1, 2, 0, var_list, belong, result->value);
  114. #endif
  115. return type;
  116. }
  117. static bool checkSignal(fline line, char *file, FUNC_NT) {
  118. if (is_KeyInterrupt == signal_appear){
  119. is_KeyInterrupt = signal_reset;
  120. setResultError(E_KeyInterrupt, KEY_INTERRUPT, line, file, true, CNEXT_NT);
  121. return true;
  122. }
  123. return false;
  124. }
  125. static bool gotoStatement(Statement **next, FUNC) {
  126. Statement *label_st = checkLabel(st, result->label);
  127. if (label_st == NULL){
  128. setResultErrorSt(E_GotoException, L"Don't find label", true, st, CNEXT_NT);
  129. return false;
  130. }
  131. runLabel(CFUNC(label_st, var_list, result, belong));
  132. if (!CHECK_RESULT(result))
  133. return false;
  134. *next = label_st->next;
  135. return true;
  136. }
  137. /**
  138. * 局部程序运行statement
  139. * @param st
  140. * @param inter
  141. * @param var_list
  142. * @return
  143. */
  144. ResultType iterStatement(FUNC) {
  145. Statement *base;
  146. ResultType type;
  147. void *bak = NULL;
  148. setResultCore(result);
  149. if (st == NULL){
  150. setResult(result, inter);
  151. return result->type;
  152. }
  153. is_KeyInterrupt = signal_reset;
  154. bak = signal(SIGINT, signalStopInter);
  155. gc_addTmpLink(&belong->gc_status);
  156. do {
  157. base = st;
  158. if (checkSignal(base->line, base->code_file, CNEXT_NT)) {
  159. type = result->type;
  160. break;
  161. }
  162. while (base != NULL) {
  163. freeResult(result);
  164. type = runStatement(CFUNC(base, var_list, result, belong));
  165. if (checkSignal(base->line, base->code_file, CNEXT_NT)) {
  166. type = result->type;
  167. break;
  168. }
  169. if (type == R_goto && result->times == 0){
  170. if (!gotoStatement(&base, CNEXT)) {
  171. type = result->type;
  172. break;
  173. }
  174. }
  175. else if (!RUN_TYPE(type))
  176. break;
  177. else
  178. base = base->next;
  179. }
  180. } while (type == R_restart && result->times == 0);
  181. if (type == R_not || type == R_restart)
  182. setResultOperationNone(result, inter, belong);
  183. result->node = base;
  184. #if START_GC
  185. gc_freeTmpLink(&belong->gc_status);
  186. gc_run(inter, var_list, 1, 2, 0, var_list, belong, result->value);
  187. #endif
  188. signal(SIGINT, bak);
  189. return result->type;
  190. }
  191. /**
  192. * 全局程序运行statement
  193. * @param inter
  194. * @return
  195. */
  196. ResultType globalIterStatement(Result *result, Inter *inter, Statement *st) {
  197. ResultType type;
  198. VarList *var_list = NULL;
  199. Statement *base;
  200. LinkValue *belong = inter->base_belong;
  201. void *bak = NULL;
  202. if (st == NULL){
  203. setResult(result, inter);
  204. return result->type;
  205. }
  206. is_KeyInterrupt = signal_reset;
  207. bak = signal(SIGINT, signalStopInter);
  208. gc_addTmpLink(&belong->gc_status);
  209. do {
  210. base = st;
  211. var_list = inter->var_list;
  212. if (checkSignal(base->line, base->code_file, CNEXT_NT)) {
  213. type = result->type;
  214. break;
  215. }
  216. while (base != NULL) {
  217. freeResult(result);
  218. type = runStatement(CFUNC(base, var_list, result, belong));
  219. if (checkSignal(base->line, base->code_file, CNEXT_NT)) {
  220. type = result->type;
  221. break;
  222. }
  223. if (type == R_goto){
  224. if (!gotoStatement(&base, CNEXT)) {
  225. type = result->type;
  226. break;
  227. }
  228. }
  229. else if (!RUN_TYPE(type))
  230. break;
  231. else
  232. base = base->next;
  233. }
  234. } while (type == R_restart && result->times == 0);
  235. if (type != R_error && type != R_func)
  236. setResultOperationNone(result, inter, belong);
  237. result->node = base;
  238. #if START_GC
  239. gc_freeTmpLink(&belong->gc_status);
  240. gc_run(inter, var_list, 1, 2, 0, var_list, belong, result->value);
  241. #endif
  242. signal(SIGINT, bak);
  243. return result->type;
  244. }
  245. // 若需要中断执行, 则返回true
  246. bool operationSafeInterStatement(FUNC){
  247. ResultType type;
  248. type = iterStatement(CNEXT);
  249. if (RUN_TYPE(type))
  250. return false;
  251. else if (type != return_code && type != R_error)
  252. setResultErrorSt(E_ResultException, L"Operation get not support result type", true, st, CNEXT_NT);
  253. return true;
  254. }
  255. bool ifBranchSafeInterStatement(FUNC){
  256. ResultType type;
  257. type = iterStatement(CNEXT);
  258. if (RUN_TYPE(type))
  259. return false;
  260. if (type == R_rego){
  261. result->times--;
  262. if (result->times < 0)
  263. return false;
  264. }
  265. if (type == R_restart || type == R_goto)
  266. result->times--;
  267. return true;
  268. }
  269. bool cycleBranchSafeInterStatement(FUNC){
  270. ResultType type;
  271. type = iterStatement(CNEXT);
  272. if (RUN_TYPE(type))
  273. return false;
  274. if (type == R_break || type == R_continue){
  275. result->times--;
  276. if (result->times < 0)
  277. return false;
  278. }
  279. if (type == R_restart || type == R_goto)
  280. result->times--;
  281. return true;
  282. }
  283. bool withBranchSafeInterStatement(FUNC){
  284. ResultType type;
  285. type = iterStatement(CNEXT);
  286. if (RUN_TYPE(type))
  287. return false;
  288. if (type == R_restart || type == R_goto)
  289. result->times--;
  290. return true;
  291. }
  292. bool tryBranchSafeInterStatement(FUNC){
  293. ResultType type;
  294. type = iterStatement(CNEXT);
  295. if (RUN_TYPE(type))
  296. return false;
  297. if (type == R_restart || type == R_goto)
  298. result->times--;
  299. return true;
  300. }
  301. bool functionSafeInterStatement(FUNC){
  302. ResultType type;
  303. type = iterStatement(CNEXT);
  304. if (type == R_error || result->type == R_yield)
  305. return true;
  306. else if (type == R_func)
  307. result->type = R_opt;
  308. else
  309. result->type = R_not;
  310. return false;
  311. }
  312. bool includeSafeInterStatement(FUNC){
  313. iterStatement(CNEXT);
  314. return !CHECK_RESULT(result);
  315. }
  316. bool blockSafeInterStatement(FUNC){
  317. ResultType type;
  318. type = iterStatement(CNEXT);
  319. if (type == R_error || type == R_yield)
  320. return true;
  321. result->type = R_opt;
  322. return false;
  323. }
  324. Statement *checkLabel(Statement *base, wchar_t *label){
  325. for (PASS; base != NULL; base = base->next)
  326. if (base->type == label_ && eqWide(base->u.label_.label, label))
  327. return base;
  328. return NULL;
  329. }
  330. bool is_quitExc(LinkValue *value, Inter *inter) {
  331. return value->value == inter->data.quit_exc->value || checkAttribution(value->value, inter->data.quit_exc->value);
  332. }