Przeglądaj źródła

feat: 支持内联函数

支持定义内联函数
可以通过function(x)定义简单函数
x = function(y)
等价于
def x(){
return y
}

link #6
SongZihuan 4 lat temu
rodzic
commit
2448645c80
2 zmienionych plików z 41 dodań i 24 usunięć
  1. 14 3
      VirtulMathCore/ofunc/src/function.c
  2. 27 21
      VirtulMathCore/src/runcall.c

+ 14 - 3
VirtulMathCore/ofunc/src/function.c

@@ -41,18 +41,29 @@ ResultType function_new(OFFICAL_FUNCTIONSIG){
     return result->type;
 }
 
-
-// TODO-szh 函数执行的时候,function的st为NULL的时候, 直接返回null
 ResultType function_init(OFFICAL_FUNCTIONSIG){
     ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
                            {.type=only_value, .must=0, .long_arg=false},
                            {.must=-1}};
+    LinkValue *func;
     setResultCore(result);
     parserArgumentUnion(ap, arg, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
     if (!CHECK_RESULT(result))
         return result->type;
     freeResult(result);
-    // TODO-szh 添加操作行为
+    if ((func = ap[0].value)->value->type != function) {
+        setResultError(E_TypeException, INSTANCE_ERROR(function), 0, "function", true,
+                       CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+        return error_return;
+    }
+
+    if (ap[1].value != NULL) {
+        Statement *return_ = makeBaseLinkValueStatement(ap[1].value, 0, "sys");
+        func->value->data.function.function = makeReturnStatement(return_, 0, "sys");
+        func->value->data.function.function_data.pt_type = free_;
+        func->value->data.function.type = vm_function;
+    }
+
     setResult(result, inter, belong);
     return result->type;
 }

+ 27 - 21
VirtulMathCore/src/runcall.c

@@ -248,57 +248,63 @@ ResultType callCFunction(LinkValue *function_value, Argument *arg, long int line
 }
 
 ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
-    VarList *function_var = NULL;
-    Statement *funtion_st = NULL;
+    VarList *var_func = NULL;
+    Statement *st_func = NULL;
     Argument *bak = arg;
-    Parameter *func_pt = function_value->value->data.function.pt;
+    Parameter *pt_func = function_value->value->data.function.pt;
     bool yield_run = false;
     setResultCore(result);
+    st_func = function_value->value->data.function.function;
+
+    if (st_func == NULL) {
+        setResult(result, inter, belong);
+        return result->type;
+    }
+
     gc_addTmpLink(&function_value->gc_status);
-    funtion_st = function_value->value->data.function.function;
-    if ((yield_run = popStatementVarList(funtion_st, &function_var, (function_value->value->object.out_var != NULL ? function_value->value->object.out_var : var_list), inter)))
-        funtion_st = funtion_st->info.node;
+    if ((yield_run = popStatementVarList(st_func, &var_func, (function_value->value->object.out_var != NULL ? function_value->value->object.out_var : var_list), inter)))  // 当out_var等于空的时候为内联函数
+        st_func = st_func->info.node;
 
-    gc_freeze(inter, var_list, function_var, true);
+    gc_freeze(inter, var_list, var_func, true);
 
     setFunctionArgument(&arg, function_value, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
     if (!CHECK_RESULT(result))
         goto return_;
 
     freeResult(result);
-    gc_addTmpLink(&function_var->hashtable->gc_status);
-    setParameterCore(line, file, arg, func_pt, function_var, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, function_value->belong));
+    gc_addTmpLink(&var_func->hashtable->gc_status);
+    setParameterCore(line, file, arg, pt_func, var_func, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, function_value->belong));
     freeFunctionArgument(arg, bak);
-    gc_freeTmpLink(&function_var->hashtable->gc_status);
+    gc_freeTmpLink(&var_func->hashtable->gc_status);
     if (!CHECK_RESULT(result)) {
-        gc_freeze(inter, var_list, function_var, false);
-        funtion_st = function_value->value->data.function.function;
+        gc_freeze(inter, var_list, var_func, false);
+        st_func = function_value->value->data.function.function;
         if (yield_run)
-            freeRunInfo(funtion_st);
+            freeRunInfo(st_func);
         else
-            popVarList(function_var);
+            popVarList(var_func);
         goto return_;
     }
 
     freeResult(result);
-    functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(funtion_st, function_var, result, function_value->belong));
-    gc_freeze(inter, var_list, function_var, false);
+    functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(st_func, var_func, result, function_value->belong));
+    gc_freeze(inter, var_list, var_func, false);
 
-    funtion_st = function_value->value->data.function.function;
+    st_func = function_value->value->data.function.function;
     if (yield_run)
         if (result->type == yield_return){
-            updateFunctionYield(funtion_st, result->node);
+            updateFunctionYield(st_func, result->node);
             result->type = operation_return;
         }
         else
-            freeRunInfo(funtion_st);
+            freeRunInfo(st_func);
     else
         if (result->type == yield_return){
-            newFunctionYield(funtion_st, result->node, function_var, inter);
+            newFunctionYield(st_func, result->node, var_func, inter);
             result->type = operation_return;
         }
         else
-            popVarList(function_var);
+            popVarList(var_func);
     return_:
     gc_freeTmpLink(&function_value->gc_status);
     return result->type;