Browse Source

feat: 函数定义支持do关键词

SongZihuan 4 years ago
parent
commit
5515fadbe7

+ 1 - 0
VirtulMathCore/include/statement.h

@@ -90,6 +90,7 @@ struct Statement{
         struct {
         struct {
             struct Statement *name;
             struct Statement *name;
             struct Statement *function;
             struct Statement *function;
+            struct Statement *first_do;
             struct Parameter *parameter;
             struct Parameter *parameter;
             struct DecorationStatement *decoration;
             struct DecorationStatement *decoration;
         } set_function;
         } set_function;

+ 5 - 0
VirtulMathCore/parser/grammar.c

@@ -514,6 +514,11 @@ void parserDo(PASERSSIGNATURE){
                     goto error_;
                     goto error_;
                 st->u.for_branch.first_do = do_code;
                 st->u.for_branch.first_do = do_code;
                 break;
                 break;
+            case MATHER_DEF:
+                if (!callChildStatement(CALLPASERSSIGNATURE, parserDef, FUNCTION, &st, "Don't get a function def after do"))
+                    goto error_;
+                st->u.set_function.first_do = do_code;
+                break;
             case MATHER_DO: {
             case MATHER_DO: {
                 long int tmp_line = delToken(pm);
                 long int tmp_line = delToken(pm);
                 if (readBackToken(pm) != MATHER_WHILE){
                 if (readBackToken(pm) != MATHER_WHILE){

+ 19 - 5
VirtulMathCore/src/runcall.c

@@ -58,14 +58,28 @@ ResultType setClass(INTER_FUNCTIONSIG) {
 
 
 ResultType setFunction(INTER_FUNCTIONSIG) {
 ResultType setFunction(INTER_FUNCTIONSIG) {
     LinkValue *tmp = NULL;
     LinkValue *tmp = NULL;
-    Value *function_value = NULL;
-    VarList *function_var = NULL;
+    Value *func_value = NULL;
+    VarList *func_out_var = NULL;
     setResultCore(result);
     setResultCore(result);
 
 
-    function_var = copyVarList(var_list, false, inter);
-    function_value = makeVMFunctionValue(st->u.set_function.function, st->u.set_function.parameter, function_var, inter);
-    tmp = makeLinkValue(function_value, belong, inter);
+    func_out_var = copyVarList(var_list, false, inter);
+    func_value = makeVMFunctionValue(st->u.set_function.function, st->u.set_function.parameter, func_out_var, inter);
+    tmp = makeLinkValue(func_value, belong, inter);
     gc_addTmpLink(&tmp->gc_status);
     gc_addTmpLink(&tmp->gc_status);
+
+    {
+        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;
+        functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.set_function.first_do, func_value->object.var, result, tmp));
+        func_value->object.var->next = var_backup;
+        inter->data.default_pt_type = pt_type_bak;
+        if (result->type != yield_return && !CHECK_RESULT(result))
+            goto error_;
+        freeResult(result);
+    }
+
     if (st->u.set_function.decoration != NULL){
     if (st->u.set_function.decoration != NULL){
         setDecoration(st->u.set_function.decoration, tmp, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         setDecoration(st->u.set_function.decoration, tmp, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
         if (!CHECK_RESULT(result))
         if (!CHECK_RESULT(result))

+ 3 - 0
VirtulMathCore/src/statement.c

@@ -156,6 +156,7 @@ Statement *makeFunctionStatement(Statement *name, Statement *function, Parameter
     tmp->u.set_function.function = function;
     tmp->u.set_function.function = function;
     tmp->u.set_function.parameter = pt;
     tmp->u.set_function.parameter = pt;
     tmp->u.set_function.decoration = NULL;
     tmp->u.set_function.decoration = NULL;
+    tmp->u.set_function.first_do = NULL;
     return tmp;
     return tmp;
 }
 }
 
 
@@ -390,6 +391,7 @@ void freeStatement(Statement *st){
                 freeStatement(st->u.set_function.function);
                 freeStatement(st->u.set_function.function);
                 freeParameter(st->u.set_function.parameter, true);
                 freeParameter(st->u.set_function.parameter, true);
                 freeDecorationStatement(st->u.set_function.decoration);
                 freeDecorationStatement(st->u.set_function.decoration);
+                freeStatement(st->u.set_function.first_do);
                 break;
                 break;
             case set_class:
             case set_class:
                 freeStatement(st->u.set_class.name);
                 freeStatement(st->u.set_class.name);
@@ -551,6 +553,7 @@ Statement *copyStatementCore(Statement *st){
             new->u.set_function.function = copyStatement(st->u.set_function.function);
             new->u.set_function.function = copyStatement(st->u.set_function.function);
             new->u.set_function.parameter = copyParameter(st->u.set_function.parameter);
             new->u.set_function.parameter = copyParameter(st->u.set_function.parameter);
             new->u.set_function.decoration = copyDecorationStatement(st->u.set_function.decoration);
             new->u.set_function.decoration = copyDecorationStatement(st->u.set_function.decoration);
+            new->u.set_function.first_do = copyStatement(st->u.set_function.first_do);
             break;
             break;
         case set_class:
         case set_class:
             new->u.set_class.name = copyStatement(st->u.set_class.name);
             new->u.set_class.name = copyStatement(st->u.set_class.name);