1
0
Эх сурвалжийг харах

feat: 允许设定函数的隐式形参类型

SongZihuan 4 жил өмнө
parent
commit
82d9d9596a

+ 7 - 0
include/value.h

@@ -50,6 +50,13 @@ struct Value{
             struct Statement *function;
             struct Parameter *pt;
             OfficialFunction of;
+            struct {
+                enum {
+                    static_,
+                    object_static_,
+                    class_static_,
+                } pt_type;
+            } function_data;
         } function;
         struct List{
             enum ListType{

+ 1 - 1
ofunc/include/__ofunc.h

@@ -5,7 +5,7 @@
 #include "__run.h"
 
 
-void registeredFunctionCore(OfficialFunction of, char *name, struct LinkValue *father, INTER_FUNCTIONSIG_CORE);
+LinkValue *registeredFunctionCore(OfficialFunction of, char *name, struct LinkValue *father, INTER_FUNCTIONSIG_CORE);
 void iterNameFunc(NameFunc list[],struct LinkValue *father, INTER_FUNCTIONSIG_CORE);
 
 #endif //VIRTUALMATH___OFUNC_H

+ 2 - 1
ofunc/src/__ofunc.c

@@ -1,6 +1,6 @@
 #include "__ofunc.h"
 
-void registeredFunctionCore(OfficialFunction of, char *name, struct LinkValue *father, INTER_FUNCTIONSIG_CORE) {
+LinkValue *registeredFunctionCore(OfficialFunction of, char *name, struct LinkValue *father, INTER_FUNCTIONSIG_CORE) {
     LinkValue *value = NULL;
     LinkValue *name_ = NULL;
     char *var_name = setStrVarName(name, false, CALL_INTER_FUNCTIONSIG_CORE(var_list));
@@ -8,6 +8,7 @@ void registeredFunctionCore(OfficialFunction of, char *name, struct LinkValue *f
     value = makeLinkValue(makeCFunctionValue(of, var_list, inter), father, inter);
     addFromVarList(var_name, name_, 0, value, CALL_INTER_FUNCTIONSIG_CORE(var_list));
     memFree(var_name);
+    return value;
 }
 
 void iterNameFunc(NameFunc list[], LinkValue *father, INTER_FUNCTIONSIG_CORE){

+ 4 - 2
ofunc/src/object.c

@@ -45,13 +45,15 @@ ResultType object_new_(OfficialFunctionSig){
 void registeredObject(RegisteredFunctionSig){
     Value *object = inter->data.object;
     VarList *object_var = object->object.var;
-    NameFunc tmp[] = {{"__new__", object_new_}, {NULL, NULL}};
     LinkValue *name_ = NULL;
     char *name = NULL;
     gc_addTmpLink(&object->gc_status);
 
     object_var->next = inter->var_list;
-    iterNameFunc(tmp, father, CALL_INTER_FUNCTIONSIG_CORE(object_var));
+    {
+        LinkValue *new = registeredFunctionCore(object_new_, "__new__", father, CALL_INTER_FUNCTIONSIG_CORE(object_var));
+        new->value->data.function.function_data.pt_type = class_static_;
+    }
     object_var->next = NULL;
 
     name = setStrVarName("object", false, CALL_INTER_FUNCTIONSIG_CORE(var_list));

+ 23 - 4
src/__run.c

@@ -131,14 +131,33 @@ ResultType setFunctionArgument(Argument **arg, LinkValue *function_value, long l
         return error_return;
     }
     tmp = makeValueArgument(function_value);
-    tmp->next = makeValueArgument(function_value->father);
-    tmp->next->next = *arg;
+    switch (function_value->value->data.function.function_data.pt_type) {
+        case static_:
+            tmp->next = *arg;
+            break;
+        case class_static_:
+            tmp->next = makeValueArgument(function_value->father);
+            tmp->next->next = *arg;
+            break;
+        case object_static_:
+            if (function_value->father->value->type == class)
+                tmp->next = *arg;
+            else {
+                tmp->next = makeValueArgument(function_value->father);
+                tmp->next->next = *arg;
+            }
+            break;
+    }
     *arg = tmp;
     setResultBase(result, inter, father);
     return result->type;
 }
 
-void freeFunctionArgument(Argument *arg){
-    arg->next->next = NULL;
+void freeFunctionArgument(Argument *arg, Argument *base) {
+    for (Argument *tmp = arg; tmp != NULL && tmp->next != NULL; tmp = tmp->next)
+        if (tmp->next == base) {
+            tmp->next = NULL;
+            break;
+        }
     freeArgument(arg, true);
 }

+ 1 - 1
src/include/__run.h

@@ -28,6 +28,6 @@ void newWithBranchYield(Statement *branch_st, Statement *node, StatementList *sl
                         Inter *inter, LinkValue *value, LinkValue *_exit_, LinkValue *_enter_);
 
 ResultType setFunctionArgument(struct Argument **arg, LinkValue *function_value, long line, char *file, INTER_FUNCTIONSIG_NOT_ST);
-void freeFunctionArgument(struct Argument *arg);
+void freeFunctionArgument(Argument *arg, Argument *base);
 
 #endif //VIRTUALMATH___RUN_H

+ 4 - 2
src/runcall.c

@@ -169,6 +169,7 @@ ResultType callClass(LinkValue *class_value, Argument *arg, long int line, char
 ResultType callCFunction(LinkValue *function_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST){
     VarList *function_var = NULL;
     OfficialFunction of = NULL;
+    Argument *bak = arg;
     setResultCore(result);
     gc_addTmpLink(&function_value->gc_status);
 
@@ -184,7 +185,7 @@ ResultType callCFunction(LinkValue *function_value, Argument *arg, long int line
     of(CALL_OfficialFunction(arg, function_var, result, function_value->father));
 
     gc_freeze(inter, var_list, function_var, false);
-    freeFunctionArgument(arg);
+    freeFunctionArgument(arg, bak);
 
     return_:
     gc_freeTmpLink(&function_value->gc_status);
@@ -194,6 +195,7 @@ 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;
+    Argument *bak = arg;
     Parameter *func_pt = function_value->value->data.function.pt;
     bool yield_run = false;
     setResultCore(result);
@@ -211,7 +213,7 @@ ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int lin
     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->father));
-    freeFunctionArgument(arg);
+    freeFunctionArgument(arg, bak);
     gc_freeTmpLink(&function_var->hashtable->gc_status);
     if (!run_continue(result)) {
         gc_freeze(inter, var_list, function_var, false);

+ 6 - 0
src/value.c

@@ -71,6 +71,10 @@ Value *makeStringValue(char *str, Inter *inter) {
 }
 
 
+void setFunctionData(Value *value) {
+    value->data.function.function_data.pt_type = object_static_;
+}
+
 Value *makeVMFunctionValue(Statement *st, Parameter *pt, VarList *var_list, Inter *inter) {
     Value *tmp;
     tmp = makeObject(inter, NULL, var_list, NULL);
@@ -79,6 +83,7 @@ Value *makeVMFunctionValue(Statement *st, Parameter *pt, VarList *var_list, Inte
     tmp->data.function.function = copyStatement(st);
     tmp->data.function.pt = copyParameter(pt);
     tmp->data.function.of = NULL;
+    setFunctionData(tmp);
     return tmp;
 }
 
@@ -90,6 +95,7 @@ Value *makeCFunctionValue(OfficialFunction of, VarList *var_list, Inter *inter)
     tmp->data.function.function = NULL;
     tmp->data.function.pt = NULL;
     tmp->data.function.of = of;
+    setFunctionData(tmp);
     return tmp;
 }