123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- #include "__run.h"
- ResultType setClass(FUNC) {
- Argument *call = NULL;
- LinkValue *tmp = NULL;
- Inherit *class_inherit = NULL;
- setResultCore(result);
- call = getArgument(st->u.set_class.father, false, CNEXT_NT);
- if (!CHECK_RESULT(result))
- goto error_return;
- class_inherit = setFather(call);
- freeArgument(call, false);
- tmp = makeLinkValue(makeClassValue(var_list, inter, class_inherit), belong, inter);
- gc_addTmpLink(&tmp->gc_status);
- freeResult(result);
- {
- enum FunctionPtType pt_type_bak = inter->data.default_pt_type;
- VarList *var_backup = tmp->value->object.var->next;
- inter->data.default_pt_type = object_free_;
- tmp->value->object.var->next = var_list;
- gc_freeze(inter, var_backup, NULL, true);
- // 运行类定义的时候需要调整belong
- functionSafeInterStatement(CFUNC(st->u.set_class.st, tmp->value->object.var, result, tmp));
- gc_freeze(inter, var_backup, NULL, false);
- tmp->value->object.var->next = var_backup;
- inter->data.default_pt_type = pt_type_bak;
- if (result->type != R_yield && !CHECK_RESULT(result))
- goto error_;
- freeResult(result);
- }
- if (st->u.set_class.decoration != NULL){
- setDecoration(st->u.set_class.decoration, tmp, CNEXT_NT);
- if (!CHECK_RESULT(result))
- goto error_;
- gc_freeTmpLink(&tmp->gc_status);
- tmp = result->value;
- result->value = NULL;
- freeResult(result);
- }
- assCore(st->u.set_class.name, tmp, false, true, CNEXT_NT);
- if (CHECK_RESULT(result))
- setResult(result, inter);
- gc_freeTmpLink(&tmp->gc_status);
- return result->type;
- error_:
- gc_freeTmpLink(&tmp->gc_status);
- error_return:
- setResultErrorSt(E_BaseException, NULL, false, st, CNEXT_NT);
- return result->type;
- }
- ResultType setFunction(FUNC) {
- LinkValue *func = NULL;
- setResultCore(result);
- makeVMFunctionValue(st->u.set_function.function, st->u.set_function.parameter, CNEXT_NT);
- if (!CHECK_RESULT(result))
- return result->type;
- func = result->value;
- result->value = NULL;
- freeResult(result);
- {
- enum FunctionPtType pt_type_bak = inter->data.default_pt_type;
- VarList *var_backup = func->value->object.var->next;
- inter->data.default_pt_type = object_free_;
- func->value->object.var->next = var_list;
- // 运行函数初始化模块的时候需要调整belong
- functionSafeInterStatement(CFUNC(st->u.set_function.first_do, func->value->object.var, result, func));
- func->value->object.var->next = var_backup;
- inter->data.default_pt_type = pt_type_bak;
- if (result->type != R_yield && !CHECK_RESULT(result))
- goto error_;
- freeResult(result);
- }
- if (st->u.set_function.decoration != NULL){
- setDecoration(st->u.set_function.decoration, func, CNEXT_NT);
- if (!CHECK_RESULT(result))
- goto error_;
- gc_freeTmpLink(&func->gc_status);
- func = result->value;
- result->value = NULL;
- freeResult(result);
- }
- assCore(st->u.set_function.name, func, false, true, CNEXT_NT);
- if (CHECK_RESULT(result)) // 若没有出现错误则设定none
- setResult(result, inter);
- error_:
- gc_freeTmpLink(&func->gc_status);
- return result->type;
- }
- ResultType setLambda(FUNC) {
- Statement *resunt_st = NULL;
- setResultCore(result);
- resunt_st = makeReturnStatement(st->u.base_lambda.function, st->line, st->code_file);
- makeVMFunctionValue(resunt_st, st->u.base_lambda.parameter, CNEXT_NT);
- resunt_st->u.return_code.value = NULL;
- freeStatement(resunt_st);
- return result->type;
- }
- ResultType elementSlice(FUNC) {
- LinkValue *element = NULL;
- LinkValue *_func_ = NULL;
- wchar_t *func_name = NULL;
- setResultCore(result);
- if (operationSafeInterStatement(CFUNC(st->u.slice_.element, var_list, result, belong)))
- return result->type;
- element = result->value;
- result->value = NULL;
- freeResult(result);
- func_name = st->u.slice_.type == SliceType_down_ ? inter->data.object_down : inter->data.object_slice;
- _func_ = findAttributes(func_name, false, LINEFILE, true, CFUNC_NT(var_list, result, element));
- if (!CHECK_RESULT(result))
- goto return_;
- freeResult(result);
- if (_func_ != NULL){
- gc_addTmpLink(&_func_->gc_status);
- callBackCorePt(_func_, st->u.slice_.index, st->line, st->code_file, CNEXT_NT);
- gc_freeTmpLink(&_func_->gc_status);
- }
- else
- setResultErrorSt(E_TypeException, OBJ_NOTSUPPORT(__down__/__slice__), true, st, CNEXT_NT);
- return_:
- gc_freeTmpLink(&element->gc_status);
- return result->type;
- }
- ResultType callBack(FUNC) {
- LinkValue *function_value = NULL;
- setResultCore(result);
- if (operationSafeInterStatement(CFUNC(st->u.call_function.function, var_list, result, belong)))
- return result->type;
- function_value = result->value;
- result->value = NULL;
- freeResult(result);
- callBackCorePt(function_value, st->u.call_function.parameter, st->line, st->code_file, CNEXT_NT);
- gc_freeTmpLink(&function_value->gc_status);
- return result->type;
- }
- ResultType callBackCorePt(LinkValue *function_value, Parameter *pt, long line, char *file, FUNC_NT) {
- Argument *arg = NULL;
- int pt_sep =1;
- bool sep = false;
- setResultCore(result);
- gc_addTmpLink(&function_value->gc_status);
- arg = getArgument(pt, false, CNEXT_NT);
- if (!CHECK_RESULT(result))
- goto return_;
- for (Parameter *tmp = pt; tmp != NULL; tmp = tmp->next, pt_sep++) {
- if (tmp->data.is_sep) {
- sep = true;
- break;
- }
- }
- freeResult(result);
- callBackCore(function_value, arg, line, file, sep ? pt_sep : 0, CNEXT_NT);
- return_:
- gc_freeTmpLink(&function_value->gc_status);
- freeArgument(arg, false);
- return result->type;
- }
- static ResultType callClass(LinkValue *class_value, Argument *arg, fline line, char *file, int pt_sep, FUNC_NT) {
- LinkValue *_new_;
- setResultCore(result);
- gc_addTmpLink(&class_value->gc_status);
- _new_ = findAttributes(inter->data.object_new, false, LINEFILE, true, CFUNC_NT(var_list, result, class_value));
- if (!CHECK_RESULT(result))
- goto return_;
- freeResult(result);
- if (_new_ != NULL){
- gc_addTmpLink(&_new_->gc_status);
- callBackCore(_new_, arg, line, file, pt_sep, CNEXT_NT);
- gc_freeTmpLink(&_new_->gc_status);
- }
- else
- setResultError(E_TypeException, OBJ_NOTSUPPORT(new(__new__)), line, file, true, CNEXT_NT);
- return_:
- gc_freeTmpLink(&class_value->gc_status);
- return result->type;
- }
- static ResultType callObject(LinkValue *object_value, Argument *arg, fline line, char *file, int pt_sep, FUNC_NT) {
- LinkValue *_call_;
- setResultCore(result);
- gc_addTmpLink(&object_value->gc_status);
- _call_ = findAttributes(inter->data.object_call, false, LINEFILE, true, CFUNC_NT(var_list, result, object_value));
- if (!CHECK_RESULT(result))
- goto return_;
- freeResult(result);
- if (_call_ != NULL){
- gc_addTmpLink(&_call_->gc_status);
- callBackCore(_call_, arg, line, file, pt_sep, CNEXT_NT);
- gc_freeTmpLink(&_call_->gc_status);
- }
- else
- setResultError(E_TypeException, OBJ_NOTSUPPORT(call(__call__)), line, file, true, CNEXT_NT);
- return_:
- gc_freeTmpLink(&object_value->gc_status);
- return result->type;
- }
- static ResultType callCFunction(LinkValue *function_value, Argument *arg, long int line, char *file, int pt_sep, FUNC_NT){
- VarList *function_var = NULL;
- OfficialFunction of = NULL;
- Argument *bak;
- setResultCore(result);
- gc_addTmpLink(&function_value->gc_status);
- setFunctionArgument(&arg, &bak, function_value, line, file, pt_sep, CNEXT_NT);
- if (!CHECK_RESULT(result))
- goto return_;
- of = function_value->value->data.function.of;
- function_var = pushVarList(function_value->value->object.out_var != NULL ? function_value->value->object.out_var : var_list, inter);
- gc_freeze(inter, var_list, function_var, true);
- freeResult(result);
- of(CO_FUNC(arg, function_var, result, function_value));
- if (result->type == R_func)
- result->type = R_opt;
- else if (result->type != R_opt && result->type != R_error)
- setResult(result, inter);
- gc_freeze(inter, var_list, function_var, false);
- popVarList(function_var);
- freeFunctionArgument(arg, bak);
- return_: gc_freeTmpLink(&function_value->gc_status);
- return result->type;
- }
- static ResultType callFFunction(LinkValue *function_value, Argument *arg, long int line, char *file, int pt_sep, FUNC_NT){
- ffi_cif cif;
- setResultCore(result);
- gc_addTmpLink(&function_value->gc_status);
- ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 0, &ffi_type_void, NULL);
- ffi_call(&cif, function_value->value->data.function.ffunc, NULL, NULL);
- setResult(result, inter);
- gc_freeTmpLink(&function_value->gc_status);
- return result->type;
- }
- static void updateFunctionYield(Statement *func_st, Statement *node){
- func_st->info.node = node->type == yield_code ? node->next : node;
- func_st->info.have_info = true;
- }
- static void newFunctionYield(Statement *func_st, Statement *node, VarList *new_var, Inter *inter){
- new_var->next = NULL;
- gc_freeze(inter, new_var, NULL, true);
- func_st->info.var_list = new_var;
- func_st->info.node = node->type == yield_code ? node->next : node;
- func_st->info.have_info = true;
- }
- static void setFunctionResult(LinkValue *func_value, bool yield_run, Result *result, FUNC_CORE) {
- Statement *st_func = func_value->value->data.function.function;
- if (yield_run) {
- if (result->type == R_yield) {
- updateFunctionYield(st_func, result->node);
- result->type = R_opt;
- result->is_yield = true;
- } else
- freeRunInfo(st_func);
- } else {
- if (result->type == R_yield) {
- newFunctionYield(st_func, result->node, var_list, inter);
- result->type = R_opt;
- result->is_yield = true;
- } else
- popVarList(var_list);
- }
- }
- static ResultType callVMFunction(LinkValue *func_value, Argument *arg, long int line, char *file, int pt_sep, FUNC_NT) {
- Argument *bak;
- VarList *var_func = NULL;
- Statement *st_func = NULL;
- Parameter *pt_func = func_value->value->data.function.pt;
- bool yield_run = false;
- setResultCore(result);
- st_func = func_value->value->data.function.function;
- if (st_func == NULL) {
- setResult(result, inter);
- return result->type;
- }
- gc_addTmpLink(&func_value->gc_status);
- {
- VarList *out_var;
- if (func_value->value->object.out_var == NULL)
- out_var = var_list; // 当out_var等于空的时候为内联函数
- else
- out_var = func_value->value->object.out_var;
- yield_run = popYieldVarList(st_func, &var_func, out_var, inter);
- }
- if (yield_run)
- st_func = st_func->info.node;
- gc_freeze(inter, var_list, var_func, true);
- setFunctionArgument(&arg, &bak, func_value, line, file, pt_sep, CNEXT_NT);
- if (!CHECK_RESULT(result))
- goto return_;
- freeResult(result);
- gc_addTmpLink(&var_func->hashtable->gc_status);
- setParameterCore(line, file, arg, pt_func, var_func, CFUNC_NT(var_list, result, func_value));
- freeFunctionArgument(arg, bak);
- gc_freeTmpLink(&var_func->hashtable->gc_status);
- if (!CHECK_RESULT(result))
- goto return_;
- freeResult(result);
- functionSafeInterStatement(CFUNC(st_func, var_func, result, func_value)); // 运行函数的时候, belong调整为函数本身
- return_:
- gc_freeze(inter, var_list, var_func, false);
- setFunctionResult(func_value, yield_run, result, CFUNC_CORE(var_func));
- gc_freeTmpLink(&func_value->gc_status);
- return result->type;
- }
- ResultType callBackCore(LinkValue *function_value, Argument *arg, fline line, char *file, int pt_sep, FUNC_NT) {
- setResultCore(result);
- gc_addTmpLink(&function_value->gc_status);
- if (function_value->value->type == V_func) {
- switch (function_value->value->data.function.type) {
- case vm_func:
- callVMFunction(function_value, arg, line, file, pt_sep, CNEXT_NT);
- break;
- case c_func:
- callCFunction(function_value, arg, line, file, pt_sep, CNEXT_NT);
- break;
- case f_func:
- callFFunction(function_value, arg, line, file, pt_sep, CNEXT_NT);
- break;
- default:
- setResultError(E_SystemException, L"function type error", line, file, true, CNEXT_NT);
- goto return_;
- }
- } else if (function_value->value->type == V_class)
- callClass(function_value, arg, line, file, pt_sep, CNEXT_NT);
- else
- callObject(function_value, arg, line, file, pt_sep, CNEXT_NT);
- setResultError(E_BaseException, NULL, line, file, false, CNEXT_NT);
- return_:
- gc_freeTmpLink(&function_value->gc_status);
- return result->type;
- }
- ResultType setDecoration(DecorationStatement *ds, LinkValue *value, FUNC_NT) {
- LinkValue *decall = NULL;
- Parameter *pt = NULL;
- setResultCore(result);
- gc_addTmpLink(&value->gc_status);
- for (PASS; ds != NULL; ds = ds->next){
- freeResult(result);
- if (operationSafeInterStatement(CFUNC(ds->decoration, var_list, result, belong)))
- break;
- pt = makeValueParameter(makeBaseLinkValueStatement(value, ds->decoration->line, ds->decoration->code_file));
- decall = result->value;
- result->value = NULL;
- freeResult(result);
- callBackCorePt(decall, pt, ds->decoration->line, ds->decoration->code_file, CNEXT_NT);
- gc_freeTmpLink(&decall->gc_status);
- freeParameter(pt, true);
- if (!CHECK_RESULT(result))
- break;
- gc_freeTmpLink(&value->gc_status);
- value = result->value;
- gc_addTmpLink(&value->gc_status);
- }
- gc_freeTmpLink(&value->gc_status);
- return result->type;
- }
|