Quellcode durchsuchen

refactor: 提取了部分函数, 缩短函数长度

把关于yield的操作封装为函数
涉及的功能由:
function, block, if, while, for, try, with
SongZihuan vor 4 Jahren
Ursprung
Commit
adf0e0e98e

+ 1 - 0
VirtulMathCore/include/run.h

@@ -22,6 +22,7 @@ bool ifBranchSafeInterStatement(INTER_FUNCTIONSIG);
 bool functionSafeInterStatement(INTER_FUNCTIONSIG);
 bool blockSafeInterStatement(INTER_FUNCTIONSIG);
 bool cycleBranchSafeInterStatement(INTER_FUNCTIONSIG);
+bool withBranchSafeInterStatement(INTER_FUNCTIONSIG);
 bool tryBranchSafeInterStatement(INTER_FUNCTIONSIG);
 Statement *checkLabel(Statement *base, wchar_t *label);
 

+ 1 - 0
VirtulMathCore/include/statement.h

@@ -221,6 +221,7 @@ struct Statement{
                 LinkValue *value;
                 LinkValue *_exit_;
                 LinkValue *_enter_;
+                LinkValue *with_belong;
             } with_;
             struct{
                 LinkValue *iter;

+ 1 - 1
VirtulMathCore/ofunc/src/list.c

@@ -23,7 +23,7 @@ ResultType tuple_list_newCore(OFFICAL_FUNCTIONSIG, enum ListType type){
         value->value->data.list.list[value->value->data.list.size - 1] = at->data.value;
     }
 
-    run_init(value, arg, 0, "list/tuple.new", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+    run_init(value, NULL, 0, "list/tuple.new", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
     return result->type;
 }
 

+ 10 - 17
VirtulMathCore/src/__run.c

@@ -107,29 +107,22 @@ wchar_t *getNameFromValue(Value *value, struct Inter *inter) {
     }
 }
 
-bool popStatementVarList(Statement *funtion_st, VarList **function_var, VarList *out_var, Inter *inter){
+/**
+ * 获取Statement的VarList(若yield), 否则push out_var(若不为yield)
+ * @param return_ VarList的返回值
+ * @return 是否位yield模式
+ */
+bool popYieldVarList(Statement *st, VarList **return_, VarList *out_var, Inter *inter){
     bool yield_run;
-    if ((yield_run = funtion_st->info.have_info)) {
-        *function_var = funtion_st->info.var_list;
-        (*function_var)->next = out_var;
+    if ((yield_run = st->info.have_info)) {
+        *return_ = st->info.var_list;
+        (*return_)->next = out_var;
     }
     else
-        *function_var = pushVarList(out_var, inter);
+        *return_ = pushVarList(out_var, inter);
     return yield_run;
 }
 
-void newFunctionYield(Statement *funtion_st, Statement *node, VarList *new_var, Inter *inter){
-    new_var->next = NULL;
-    gc_freeze(inter, new_var, NULL, true);
-    funtion_st->info.var_list = new_var;
-    funtion_st->info.node = node->type == yield_code ? node->next : node;
-    funtion_st->info.have_info = true;
-}
-
-void updateFunctionYield(Statement *function_st, Statement *node){
-    function_st->info.node = node->type == yield_code ? node->next : node;
-    function_st->info.have_info = true;
-}
 ResultType setFunctionArgument(Argument **arg, Argument **base, LinkValue *_func, fline line, char *file, int pt_sep, INTER_FUNCTIONSIG_NOT_ST){
     Argument *tmp = NULL;
     LinkValue *self;

+ 1 - 10
VirtulMathCore/src/include/__run.h

@@ -9,16 +9,7 @@ ResultType getBaseVarInfo(wchar_t **name, int *times, INTER_FUNCTIONSIG);
 ResultType getBaseSVarInfo(wchar_t **name, int *times, INTER_FUNCTIONSIG);
 ResultType getVarInfo(wchar_t **name, int *times, INTER_FUNCTIONSIG);
 
-bool popStatementVarList(Statement *funtion_st, VarList **function_var, VarList *out_var, Inter *inter);
-
-void newFunctionYield(Statement *funtion_st, Statement *node, VarList *new_var, Inter *inter);
-void updateFunctionYield(Statement *function_st, Statement *node);
-
-void updateBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, enum StatementInfoStatus status);
-void newWithBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, VarList *new_var, enum StatementInfoStatus status,
-                        Inter *inter, LinkValue *value, LinkValue *_exit_, LinkValue *_enter_);
-void newForBranchYield(Statement *branch_st, Statement *node, StatementList *sl_node, VarList *new_var, enum StatementInfoStatus status,
-                       Inter *inter, LinkValue *iter);
+bool popYieldVarList(Statement *st, VarList **return_, VarList *out_var, Inter *inter);
 
 ResultType setFunctionArgument(struct Argument **arg, struct Argument **base, LinkValue *_func, fline line, char *file, int pt_sep, INTER_FUNCTIONSIG_NOT_ST);
 void freeFunctionArgument(Argument *arg, Argument *base);

+ 10 - 0
VirtulMathCore/src/run.c

@@ -297,6 +297,16 @@ bool cycleBranchSafeInterStatement(INTER_FUNCTIONSIG){
     return true;
 }
 
+bool withBranchSafeInterStatement(INTER_FUNCTIONSIG){
+    ResultType type;
+    type = iterStatement(CALL_INTER_FUNCTIONSIG(st, var_list, result, belong));
+    if (RUN_TYPE(type))
+        return false;
+    if (type == R_restart || type == R_goto)
+        result->times--;
+    return true;
+}
+
 bool tryBranchSafeInterStatement(INTER_FUNCTIONSIG){
     ResultType type;
     type = iterStatement(CALL_INTER_FUNCTIONSIG(st, var_list, result, belong));

Datei-Diff unterdrückt, da er zu groß ist
+ 479 - 358
VirtulMathCore/src/runbranch.c


+ 55 - 37
VirtulMathCore/src/runcall.c

@@ -234,67 +234,85 @@ static ResultType callCFunction(LinkValue *function_value, Argument *arg, long i
     return result->type;
 }
 
-static ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int line, char *file, int pt_sep, INTER_FUNCTIONSIG_NOT_ST) {
+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, INTER_FUNCTIONSIG_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, INTER_FUNCTIONSIG_NOT_ST) {
+    Argument *bak;
     VarList *var_func = NULL;
     Statement *st_func = NULL;
-    Argument *bak;
-    Parameter *pt_func = function_value->value->data.function.pt;
+    Parameter *pt_func = func_value->value->data.function.pt;
     bool yield_run = false;
     setResultCore(result);
-    st_func = function_value->value->data.function.function;
+    st_func = func_value->value->data.function.function;
 
     if (st_func == NULL) {
         setResult(result, inter);
         return result->type;
     }
 
-    gc_addTmpLink(&function_value->gc_status);
-    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等于空的时候为内联函数
+    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, function_value, line, file, pt_sep, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
+    setFunctionArgument(&arg, &bak, func_value, line, file, pt_sep, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
     if (!CHECK_RESULT(result))
         goto return_;
     freeResult(result);
+
     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));
+    setParameterCore(line, file, arg, pt_func, var_func, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, func_value->belong));
     freeFunctionArgument(arg, bak);
     gc_freeTmpLink(&var_func->hashtable->gc_status);
-    if (!CHECK_RESULT(result)) {
-        gc_freeze(inter, var_list, var_func, false);
-        st_func = function_value->value->data.function.function;
-        if (yield_run)
-            freeRunInfo(st_func);
-        else
-            popVarList(var_func);
+
+    if (!CHECK_RESULT(result))
         goto return_;
-    }
 
     freeResult(result);
-    functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(st_func, var_func, result, function_value->belong));
-    gc_freeze(inter, var_list, var_func, false);
+    functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(st_func, var_func, result, func_value->belong));
 
-    st_func = function_value->value->data.function.function;  // TODO-szh yield 提取函数
-    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_func, inter);
-            result->type = R_opt;
-            result->is_yield = true;
-        }
-        else
-            popVarList(var_func);
     return_:
-    gc_freeTmpLink(&function_value->gc_status);
+    gc_freeze(inter, var_list, var_func, false);
+    setFunctionResult(func_value, yield_run, result, CALL_INTER_FUNCTIONSIG_CORE(var_func));
+    gc_freeTmpLink(&func_value->gc_status);
     return result->type;
 }
 

+ 31 - 11
VirtulMathCore/src/runoperation.c

@@ -45,30 +45,50 @@ ResultType operationStatement(INTER_FUNCTIONSIG) {
     return result->type;
 }
 
-ResultType blockOperation(INTER_FUNCTIONSIG) {
-    Statement *info_st = st->u.operation.left;
-    bool yield_run;
-    if ((yield_run = popStatementVarList(st, &var_list, var_list, inter)))
-        info_st = st->info.node;
-    blockSafeInterStatement(CALL_INTER_FUNCTIONSIG(info_st, var_list, result, belong));
-    if (result->type == R_error)
-        return result->type;
-    else if (yield_run) {
+static void updateBlockYield(Statement *block_st, Statement *node){
+    block_st->info.node = node->type == yield_code ? node->next : node;
+    block_st->info.have_info = true;
+}
+
+static void newBlockYield(Statement *block_st, Statement *node, VarList *new_var, Inter *inter){
+    new_var->next = NULL;
+    gc_freeze(inter, new_var, NULL, true);
+    block_st->info.var_list = new_var;
+    block_st->info.node = node->type == yield_code ? node->next : node;
+    block_st->info.have_info = true;
+}
+
+static void setBlockResult(Statement *st, bool yield_run, Result *result, INTER_FUNCTIONSIG_CORE) {
+    if (yield_run) {
         if (result->type == R_yield){
-            updateFunctionYield(st, result->node);
+            updateBlockYield(st, result->node);
             result->type = R_opt;
+            result->is_yield = true;
         }
         else
             freeRunInfo(st);
     }
     else {
         if (result->type == R_yield){
-            newFunctionYield(st, result->node, var_list, inter);
+            newBlockYield(st, result->node, var_list, inter);
             result->type = R_opt;
+            result->is_yield = true;
         }
         else
             popVarList(var_list);
     }
+}
+
+ResultType blockOperation(INTER_FUNCTIONSIG) {
+    Statement *info_st = st->u.operation.left;
+    bool yield_run;
+    if ((yield_run = popYieldVarList(st, &var_list, var_list, inter)))
+        info_st = st->info.node;
+    blockSafeInterStatement(CALL_INTER_FUNCTIONSIG(info_st, var_list, result, belong));
+    if (result->type == R_error)
+        return result->type;
+    else
+        setBlockResult(st, yield_run, result, CALL_INTER_FUNCTIONSIG_CORE(var_list));
     if (CHECK_RESULT(result) && st->aut != auto_aut)
         result->value->aut = st->aut;
     return result->type;

+ 4 - 0
VirtulMathCore/src/statement.c

@@ -20,6 +20,7 @@ void setRunInfo(Statement *st){
     st->info.branch.with_.value = NULL;
     st->info.branch.with_._exit_ = NULL;
     st->info.branch.with_._enter_ = NULL;
+    st->info.branch.with_.with_belong = NULL;
     st->info.branch.for_.iter = NULL;
 }
 
@@ -34,6 +35,8 @@ void freeRunInfo(Statement *st) {
         gc_freeTmpLink(&st->info.branch.with_._exit_->gc_status);
     if (st->info.branch.with_._enter_ != NULL)
         gc_freeTmpLink(&st->info.branch.with_._enter_->gc_status);
+    if (st->info.branch.with_.with_belong != NULL)
+        gc_freeTmpLink(&st->info.branch.with_.with_belong->gc_status);
     if (st->info.branch.for_.iter != NULL)
         gc_freeTmpLink(&st->info.branch.for_.iter->gc_status);
     setRunInfo(st);
@@ -515,6 +518,7 @@ Statement *copyStatement(Statement *st){
 
 Statement *copyStatementCore(Statement *st){
     Statement *new = makeStatement(st->line, st->code_file);
+    // copyStatement的时候不会复制runInfo的信息
     new->type = st->type;
     new->aut = st->aut;
     new->next = NULL;

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.