sys_lib.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "__ofunc.h"
  2. static ResultType vm_setNowRunCore(O_FUNC, bool type){
  3. LinkValue *function_value = NULL;
  4. ArgumentParser ap[] = {{.type=name_value, .name=L"func", .must=1, .long_arg=false},
  5. {.must=-1}};
  6. setResultCore(result);
  7. {
  8. parserArgumentUnion(ap, arg, CNEXT_NT);
  9. if (!CHECK_RESULT(result))
  10. return result->type;
  11. freeResult(result);
  12. }
  13. function_value = ap[0].value;
  14. function_value->value->data.function.function_data.run = type;
  15. result->value = function_value;
  16. gc_addTmpLink(&result->value->gc_status);
  17. result->type = R_opt;
  18. return R_opt;
  19. }
  20. static ResultType vm_isnowrun(O_FUNC){
  21. return vm_setNowRunCore(CO_FUNC(arg, var_list, result, belong), true);
  22. }
  23. static ResultType vm_disnowrun(O_FUNC){
  24. return vm_setNowRunCore(CO_FUNC(arg, var_list, result, belong), false);
  25. }
  26. static ResultType vm_quit(O_FUNC){
  27. if (arg != NULL)
  28. setResultError(E_ArgumentException, MANY_ARG, LINEFILE, true, CNEXT_NT);
  29. else
  30. setResultError(E_QuitException, L"vmcore quit()", LINEFILE, true, CNEXT_NT);
  31. return R_error;
  32. }
  33. static ResultType vm_setAssert(O_FUNC, enum AssertRunType type){
  34. setResultCore(result);
  35. if (arg != NULL) {
  36. setResultError(E_ArgumentException, MANY_ARG, LINEFILE, true, CNEXT_NT);
  37. return R_error;
  38. }
  39. inter->data.assert_run = type;
  40. setResult(result, inter);
  41. return result->type;
  42. }
  43. static ResultType vm_assertignore(O_FUNC){
  44. return vm_setAssert(CO_FUNC(arg, var_list, result, belong), assert_ignore);
  45. }
  46. static ResultType vm_assertrun(O_FUNC){
  47. return vm_setAssert(CO_FUNC(arg, var_list, result, belong), assert_run);
  48. }
  49. static ResultType vm_assertraise(O_FUNC){
  50. return vm_setAssert(CO_FUNC(arg, var_list, result, belong), assert_raise);
  51. }
  52. static ResultType vm_selfunCore(O_FUNC, bool type){
  53. LinkValue *function_value = NULL;
  54. ArgumentParser ap[] = {{.type=name_value, .name=L"func", .must=1, .long_arg=false},
  55. {.must=-1}};
  56. bool push;
  57. setResultCore(result);
  58. {
  59. parserArgumentUnion(ap, arg, CNEXT_NT);
  60. if (!CHECK_RESULT(result))
  61. return result->type;
  62. freeResult(result);
  63. }
  64. function_value = ap[0].value;
  65. push = function_value->value->data.function.function_data.push;
  66. if (push && !type) { // 原本是push, 现在设定为非push
  67. function_value->value->data.function.function_data.push = false;
  68. if (function_value->value->object.out_var != NULL)
  69. function_value->value->object.out_var = pushVarList(function_value->value->object.out_var, inter);
  70. } else if (!push && type) {
  71. if (function_value->value->object.out_var != NULL)
  72. function_value->value->object.out_var = popVarList(function_value->value->object.out_var);
  73. }
  74. result->value = function_value;
  75. gc_addTmpLink(&result->value->gc_status);
  76. result->type = R_opt;
  77. return R_opt;
  78. }
  79. static ResultType vm_selfun(O_FUNC){
  80. return vm_selfunCore(CO_FUNC(arg, var_list, result, belong), false);
  81. }
  82. static ResultType vm_nselfun(O_FUNC){
  83. return vm_selfunCore(CO_FUNC(arg, var_list, result, belong), true);
  84. }
  85. static ResultType vm_setfreemode(O_FUNC, bool type){
  86. setResultCore(result);
  87. if (arg != NULL) {
  88. setResultError(E_ArgumentException, MANY_ARG, LINEFILE, true, CNEXT_NT);
  89. return R_error;
  90. }
  91. inter->data.free_mode = type;
  92. setResult(result, inter);
  93. return result->type;
  94. }
  95. static ResultType vm_freemode(O_FUNC){
  96. return vm_setfreemode(CO_FUNC(arg, var_list, result, belong), true);
  97. }
  98. static ResultType vm_nfreemode(O_FUNC){
  99. return vm_setfreemode(CO_FUNC(arg, var_list, result, belong), false);
  100. }
  101. static ResultType vm_opt_mode(O_FUNC, enum OptMode mode){
  102. setResultCore(result);
  103. if (arg != NULL) {
  104. setResultError(E_ArgumentException, MANY_ARG, LINEFILE, true, CNEXT_NT);
  105. return R_error;
  106. }
  107. inter->data.opt_mode = mode;
  108. setResult(result, inter);
  109. return result->type;
  110. }
  111. static ResultType vm_free_opt(O_FUNC){
  112. return vm_opt_mode(CO_FUNC(arg, var_list, result, belong), om_free);
  113. }
  114. static ResultType vm_normal_opt(O_FUNC){
  115. return vm_opt_mode(CO_FUNC(arg, var_list, result, belong), om_normal);
  116. }
  117. static ResultType vm_simple_opt(O_FUNC){
  118. return vm_opt_mode(CO_FUNC(arg, var_list, result, belong), om_simple);
  119. }
  120. static ResultType vm_exec(O_FUNC){
  121. ArgumentParser ap[] = {{.type=name_value, .name=L"cm", .must=1, .long_arg=false},
  122. {.type=name_value, .name=L"var", .must=0, .long_arg=false},
  123. {.type=name_value, .name=L"out", .must=0, .long_arg=false},
  124. {.must=-1}};
  125. LinkValue *str;
  126. LinkValue *var;
  127. LinkValue *out;
  128. Statement *new_st;
  129. VarList *run;
  130. setResultCore(result);
  131. parserArgumentUnion(ap, arg, CNEXT_NT);
  132. if (!CHECK_RESULT(result))
  133. return result->type;
  134. freeResult(result);
  135. str = ap[0].value;
  136. var = ap[1].value;
  137. out = ap[2].value;
  138. if (str->value->type != V_str) {
  139. setResultError(E_TypeException, ONLY_ACC(cm, str), LINEFILE, true, CNEXT_NT);
  140. return result->type;
  141. }
  142. if (var != NULL && var->value->type != V_dict) {
  143. setResultError(E_TypeException, ONLY_ACC(var, dict), LINEFILE, true, CNEXT_NT);
  144. return R_error;
  145. }
  146. if (out != NULL) {
  147. if (out->value->type != V_bool) {
  148. setResultError(E_TypeException, ONLY_ACC(out, bool), LINEFILE, true, CNEXT_NT);
  149. return R_error;
  150. } else if (var == NULL) {
  151. setResultError(E_TypeException, L"missing parameters: var", LINEFILE, true, CNEXT_NT);
  152. return R_error;
  153. }
  154. } else
  155. out = false;
  156. {
  157. ParserMessage *pm = makeParserMessageStr(str->value->data.str.str, false);
  158. new_st = makeStatement(0, "exec");
  159. parserCommandList(pm, inter, true, new_st);
  160. if (pm->status == int_error) {
  161. setResultError(E_KeyInterrupt, KEY_INTERRUPT, LINEFILE, true, CNEXT_NT);
  162. return R_error;
  163. } else if (pm->status != success) {
  164. wchar_t *wcs_message = memStrToWcs(pm->status_message, false);
  165. setResultError(E_TypeException, wcs_message, LINEFILE, true, CNEXT_NT);
  166. memFree(wcs_message);
  167. return R_error;
  168. }
  169. freeParserMessage(pm, true);
  170. }
  171. if (var != NULL) {
  172. run = makeVarList(inter, false, var->value->data.dict.dict);
  173. if (out)
  174. run->next = var_list;
  175. } else
  176. run = var_list;
  177. includeSafeInterStatement(CFUNC(new_st, run, result, belong));
  178. freeStatement(new_st);
  179. if (var != NULL)
  180. freeVarList(run);
  181. return result->type;
  182. }
  183. void registered(R_FUNC){
  184. NameFunc tmp[] = {{L"is_now_run", vm_isnowrun, fp_no_, .var=nfv_notpush},
  185. {L"dis_now_run", vm_disnowrun, fp_no_, .var=nfv_notpush},
  186. {L"quit", vm_quit, fp_no_, .var=nfv_notpush},
  187. {L"exec", vm_exec, fp_no_, .var=nfv_notpush},
  188. {L"assert_ignore", vm_assertignore, fp_no_, .var=nfv_notpush},
  189. {L"assert_run", vm_assertrun, fp_no_, .var=nfv_notpush},
  190. {L"assert_raise", vm_assertraise, fp_no_, .var=nfv_notpush},
  191. {L"self_fun", vm_selfun, fp_no_, .var=nfv_notpush},
  192. {L"no_self_fun", vm_nselfun, fp_no_, .var=nfv_notpush},
  193. {L"free_mode", vm_freemode, fp_no_, .var=nfv_notpush},
  194. {L"normal_mode", vm_nfreemode, fp_no_, .var=nfv_notpush},
  195. {L"free_opt", vm_free_opt, fp_no_, .var=nfv_notpush},
  196. {L"normal_opt", vm_normal_opt, fp_no_, .var=nfv_notpush},
  197. {L"simple_opt", vm_simple_opt, fp_no_, .var=nfv_notpush},
  198. {NULL, NULL}};
  199. iterBaseNameFunc(tmp, belong, CFUNC_CORE(var_list));
  200. }