runcall.c 13 KB

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