runcall.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "__run.h"
  2. ResultType setClass(INTER_FUNCTIONSIG) {
  3. Argument *call = NULL;
  4. LinkValue *tmp = NULL;
  5. Inherit *class_belong = NULL;
  6. VarList *belong_var = NULL;
  7. enum FunctionPtType pt_type_bak = inter->data.default_pt_type;
  8. setResultCore(result);
  9. call = getArgument(st->u.set_class.father, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  10. if (!run_continue(result))
  11. goto error_;
  12. class_belong = setFather(call);
  13. freeArgument(call, false);
  14. tmp = makeLinkValue(makeClassValue(copyVarList(var_list, false, inter), inter, class_belong), belong, inter);
  15. gc_addTmpLink(&tmp->gc_status);
  16. belong_var = tmp->value->object.var->next;
  17. tmp->value->object.var->next = var_list;
  18. freeResult(result);
  19. inter->data.default_pt_type = object_free_;
  20. functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.set_class.st, tmp->value->object.var, result, tmp));
  21. inter->data.default_pt_type = pt_type_bak;
  22. tmp->value->object.var->next = belong_var;
  23. if (!run_continue(result))
  24. goto error_;
  25. freeResult(result);
  26. if (st->u.set_class.decoration != NULL){
  27. setDecoration(st->u.set_class.decoration, tmp, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  28. if (!run_continue(result))
  29. goto error_;
  30. gc_freeTmpLink(&tmp->gc_status);
  31. tmp = result->value;
  32. result->value = NULL;
  33. freeResult(result);
  34. }
  35. assCore(st->u.set_class.name, tmp, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  36. if (run_continue(result))
  37. setResult(result, inter, belong);
  38. gc_freeTmpLink(&tmp->gc_status);
  39. return result->type;
  40. error_:
  41. gc_freeTmpLink(&tmp->gc_status);
  42. setResultErrorSt(result, inter, NULL, NULL, st, belong, false);
  43. return result->type;
  44. }
  45. ResultType setFunction(INTER_FUNCTIONSIG) {
  46. LinkValue *tmp = NULL;
  47. Value *function_value = NULL;
  48. VarList *function_var = NULL;
  49. setResultCore(result);
  50. function_var = copyVarList(var_list, false, inter);
  51. function_value = makeVMFunctionValue(st->u.set_function.function, st->u.set_function.parameter, function_var, inter);
  52. tmp = makeLinkValue(function_value, belong, inter);
  53. gc_addTmpLink(&tmp->gc_status);
  54. if (st->u.set_function.decoration != NULL){
  55. setDecoration(st->u.set_function.decoration, tmp, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  56. if (!run_continue(result))
  57. goto error_;
  58. gc_freeTmpLink(&tmp->gc_status);
  59. tmp = result->value;
  60. result->value = NULL;
  61. freeResult(result);
  62. }
  63. assCore(st->u.set_function.name, tmp, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  64. if (!run_continue(result))
  65. goto error_;
  66. setResult(result, inter, belong);
  67. gc_freeTmpLink(&tmp->gc_status);
  68. return result->type;
  69. error_:
  70. gc_freeTmpLink(&tmp->gc_status);
  71. return result->type;
  72. }
  73. ResultType setLambda(INTER_FUNCTIONSIG) {
  74. Value *function_value = NULL;
  75. VarList *function_var = NULL;
  76. setResultCore(result);
  77. result->type = operation_return;
  78. function_var = copyVarList(var_list, false, inter);
  79. {
  80. Statement *resunt_st = makeReturnStatement(st->u.base_lambda.function, st->line, st->code_file);
  81. function_value = makeVMFunctionValue(resunt_st, st->u.base_lambda.parameter, function_var, inter);
  82. resunt_st->u.return_code.value = NULL;
  83. freeStatement(resunt_st);
  84. }
  85. result->value = makeLinkValue(function_value, belong, inter);
  86. gc_addTmpLink(&result->value->gc_status);
  87. return result->type;
  88. }
  89. ResultType elementSlice(INTER_FUNCTIONSIG) {
  90. LinkValue *element = NULL;
  91. LinkValue *_func_ = NULL;
  92. char *func_name = NULL;
  93. setResultCore(result);
  94. if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.slice_.element, var_list, result, belong)))
  95. return result->type;
  96. element = result->value;
  97. result->value = NULL;
  98. freeResult(result);
  99. func_name = st->u.slice_.type == SliceType_down_ ? inter->data.object_down : inter->data.object_slice;
  100. _func_ = findAttributes(func_name, false, element, inter);
  101. if (_func_ != NULL){
  102. gc_addTmpLink(&_func_->gc_status);
  103. callBackCorePt(_func_, st->u.slice_.index, st->line, st->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  104. gc_freeTmpLink(&_func_->gc_status);
  105. }
  106. else
  107. setResultErrorSt(result, inter, "TypeException", "Don't find __down__/__slice__", st, belong, true);
  108. gc_freeTmpLink(&element->gc_status);
  109. return result->type;
  110. }
  111. ResultType callBack(INTER_FUNCTIONSIG) {
  112. LinkValue *function_value = NULL;
  113. setResultCore(result);
  114. if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.call_function.function, var_list, result, belong)))
  115. return result->type;
  116. function_value = result->value;
  117. result->value = NULL;
  118. freeResult(result);
  119. callBackCorePt(function_value, st->u.call_function.parameter, st->line, st->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  120. gc_freeTmpLink(&function_value->gc_status);
  121. return result->type;
  122. }
  123. ResultType callBackCorePt(LinkValue *function_value, Parameter *pt, long line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  124. Argument *arg = NULL;
  125. setResultCore(result);
  126. gc_addTmpLink(&function_value->gc_status);
  127. arg = getArgument(pt, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  128. if (!run_continue(result))
  129. goto return_;
  130. freeResult(result);
  131. callBackCore(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  132. return_:
  133. gc_freeTmpLink(&function_value->gc_status);
  134. freeArgument(arg, false);
  135. return result->type;
  136. }
  137. ResultType callBackCore(LinkValue *function_value, Argument *arg, long line, char *file, INTER_FUNCTIONSIG_NOT_ST){
  138. setResultCore(result);
  139. gc_addTmpLink(&function_value->gc_status);
  140. if (function_value->value->type == function && function_value->value->data.function.type == vm_function)
  141. callVMFunction(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  142. else if (function_value->value->type == function && function_value->value->data.function.type == c_function)
  143. callCFunction(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  144. else if (function_value->value->type == class)
  145. callClass(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  146. else
  147. callObject(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  148. setResultError(result, inter, NULL, NULL, line, file, belong, false);
  149. gc_freeTmpLink(&function_value->gc_status);
  150. return result->type;
  151. }
  152. ResultType callClass(LinkValue *class_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  153. LinkValue *_new_ = findAttributes(inter->data.object_new, false, class_value, inter);
  154. setResultCore(result);
  155. if (_new_ != NULL){
  156. gc_addTmpLink(&_new_->gc_status);
  157. callBackCore(_new_, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  158. gc_freeTmpLink(&_new_->gc_status);
  159. }
  160. else
  161. setResultError(result, inter, "ClassException", "Don't find __new__", line, file, belong, true);
  162. return result->type;
  163. }
  164. ResultType callObject(LinkValue *object_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  165. LinkValue *_call_ = findAttributes(inter->data.object_call, false, object_value, inter);
  166. setResultCore(result);
  167. if (_call_ != NULL){
  168. gc_addTmpLink(&_call_->gc_status);
  169. callBackCore(_call_, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  170. gc_freeTmpLink(&_call_->gc_status);
  171. }
  172. else
  173. setResultError(result, inter, "TypeException", "Object is not callable", line, file, belong, true);
  174. return result->type;
  175. }
  176. ResultType callCFunction(LinkValue *function_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST){
  177. VarList *function_var = NULL;
  178. OfficialFunction of = NULL;
  179. Argument *bak = arg;
  180. setResultCore(result);
  181. gc_addTmpLink(&function_value->gc_status);
  182. setFunctionArgument(&arg, function_value, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  183. if (!run_continue(result))
  184. goto return_;
  185. of = function_value->value->data.function.of;
  186. function_var = function_value->value->object.out_var;
  187. gc_freeze(inter, var_list, function_var, true);
  188. freeResult(result);
  189. of(CALL_OfficialFunction(arg, function_var, result, function_value->belong));
  190. gc_freeze(inter, var_list, function_var, false);
  191. freeFunctionArgument(arg, bak);
  192. return_:
  193. gc_freeTmpLink(&function_value->gc_status);
  194. return result->type;
  195. }
  196. ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  197. VarList *function_var = NULL;
  198. Statement *funtion_st = NULL;
  199. Argument *bak = arg;
  200. Parameter *func_pt = function_value->value->data.function.pt;
  201. bool yield_run = false;
  202. setResultCore(result);
  203. gc_addTmpLink(&function_value->gc_status);
  204. funtion_st = function_value->value->data.function.function;
  205. if ((yield_run = popStatementVarList(funtion_st, &function_var, function_value->value->object.out_var, inter)))
  206. funtion_st = funtion_st->info.node;
  207. gc_freeze(inter, var_list, function_var, true);
  208. setFunctionArgument(&arg, function_value, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  209. if (!run_continue(result))
  210. goto return_;
  211. freeResult(result);
  212. gc_addTmpLink(&function_var->hashtable->gc_status);
  213. setParameterCore(line, file, arg, func_pt, function_var, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, function_value->belong));
  214. freeFunctionArgument(arg, bak);
  215. gc_freeTmpLink(&function_var->hashtable->gc_status);
  216. if (!run_continue(result)) {
  217. gc_freeze(inter, var_list, function_var, false);
  218. funtion_st = function_value->value->data.function.function;
  219. if (yield_run)
  220. freeRunInfo(funtion_st);
  221. else
  222. popVarList(function_var);
  223. goto return_;
  224. }
  225. freeResult(result);
  226. functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(funtion_st, function_var, result, function_value->belong));
  227. gc_freeze(inter, var_list, function_var, false);
  228. funtion_st = function_value->value->data.function.function;
  229. if (yield_run)
  230. if (result->type == yield_return){
  231. updateFunctionYield(funtion_st, result->node);
  232. result->type = operation_return;
  233. }
  234. else
  235. freeRunInfo(funtion_st);
  236. else
  237. if (result->type == yield_return){
  238. newFunctionYield(funtion_st, result->node, function_var, inter);
  239. result->type = operation_return;
  240. }
  241. else
  242. popVarList(function_var);
  243. return_:
  244. gc_freeTmpLink(&function_value->gc_status);
  245. return result->type;
  246. }
  247. ResultType setDecoration(DecorationStatement *ds, LinkValue *value, INTER_FUNCTIONSIG_NOT_ST) {
  248. LinkValue *decall = NULL;
  249. Parameter *pt = NULL;
  250. setResultCore(result);
  251. gc_addTmpLink(&value->gc_status);
  252. for (PASS; ds != NULL; ds = ds->next){
  253. freeResult(result);
  254. if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(ds->decoration, var_list, result, belong)))
  255. break;
  256. pt = makeValueParameter(makeBaseLinkValueStatement(value, ds->decoration->line, ds->decoration->code_file));
  257. decall = result->value;
  258. result->value = NULL;
  259. freeResult(result);
  260. callBackCorePt(decall, pt, ds->decoration->line, ds->decoration->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  261. gc_freeTmpLink(&decall->gc_status);
  262. freeParameter(pt, true);
  263. if (!run_continue(result))
  264. break;
  265. gc_freeTmpLink(&value->gc_status);
  266. value = result->value;
  267. gc_addTmpLink(&value->gc_status);
  268. }
  269. gc_freeTmpLink(&value->gc_status);
  270. return result->type;
  271. }