Explorar el Código

feat: 增加两种函数类型

函数定义时就绑定belong
当函数类型为cls时,特殊参数:cls的值直接为函数绑定的belong

link #6
SongZihuan hace 4 años
padre
commit
75496606fd

+ 3 - 0
VirtulMathCore/gc/gc.c

@@ -92,6 +92,9 @@ static void gc_iterValue(Value *value){
         case dict:
             gc_iterHashTable(value->data.dict.dict);
             break;
+        case function:
+            gc_iterLinkValue(value->data.function.function_data.cls);
+            break;
         default:
             break;
     }

+ 3 - 0
VirtulMathCore/include/value.h

@@ -73,10 +73,13 @@ struct Function{
             object_static_,  // self参数不允许class
             class_static_,  // self参数允许一切,但转换为类
             all_static_, // self参数允许一切
+            cls_static_,  // 使用function自带的cls作为参数
             object_free_,  // 同object_static_但不包含func参数
             class_free_,  // 同object_static_但不包含func参数
             all_free_,  // 允许class或者object
+            cls_free_,  // 使用function自带的cls作为参数
         } pt_type;
+        LinkValue *cls;
     } function_data;
 };
 

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

@@ -1,7 +1,8 @@
 #include "__ofunc.h"
 
-static void setFunctionData(Value *value, Inter *inter) {
+static void setFunctionData(Value *value, LinkValue *cls, Inter *inter) {
     value->data.function.function_data.pt_type = inter->data.default_pt_type;
+    value->data.function.function_data.cls = cls;
 }
 
 ResultType function_new(OFFICAL_FUNCTIONSIG){
@@ -28,7 +29,7 @@ ResultType function_new(OFFICAL_FUNCTIONSIG){
     value->value->data.function.function = NULL;
     value->value->data.function.pt = NULL;
     value->value->data.function.of = NULL;
-    setFunctionData(value->value, inter);
+    setFunctionData(value->value, ap->value, inter);
 
     switch (init_new(value, arg, "function.new", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong))) {
         case 1:

+ 10 - 0
VirtulMathCore/ofunc/src/sys.c

@@ -62,6 +62,14 @@ ResultType vm_setMethodCore(OFFICAL_FUNCTIONSIG, enum FunctionPtType type){
     return operation_return;
 }
 
+ResultType vm_clsfreemethod(OFFICAL_FUNCTIONSIG){
+    return vm_setMethodCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), cls_free_);
+}
+
+ResultType vm_clsmethod(OFFICAL_FUNCTIONSIG){
+    return vm_setMethodCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), cls_static_);
+}
+
 ResultType vm_freemethod(OFFICAL_FUNCTIONSIG){
     return vm_setMethodCore(CALL_OFFICAL_FUNCTION(arg, var_list, result, belong), free_);
 }
@@ -112,6 +120,8 @@ void registeredSysFunction(REGISTERED_FUNCTIONSIG){
                       {L"objectmethod", vm_objectfreemethod, free_},
                       {L"simplemethod", vm_allfreemethod, free_},
                       {L"simplestaticmethod", vm_allstaticmethod, free_},
+                      {L"clsmethod", vm_clsfreemethod, free_},
+                      {L"clsstaticmethod", vm_clsmethod, free_},
                       {L"quit", vm_quit, free_},
                       {NULL, NULL}};
     iterBaseNameFunc(tmp, belong, CALL_INTER_FUNCTIONSIG_CORE(var_list));

+ 3 - 1
VirtulMathCore/src/__run.c

@@ -140,7 +140,7 @@ ResultType setFunctionArgument(Argument **arg, Argument **base, LinkValue *_func
     switch (pt_sep) {
         case 0:
             func = _func;
-            self = _func->belong;
+            self = pt_type == cls_free_ || pt_type == cls_static_ ? _func->value->data.function.function_data.cls : _func->belong;
             *base = *arg;
             break;
         case 1: {
@@ -208,6 +208,7 @@ ResultType setFunctionArgument(Argument **arg, Argument **base, LinkValue *_func
                 tmp->next = *arg;
             *arg = tmp;
             break;
+        case cls_static_:
         case all_static_:
             tmp = makeValueArgument(func);
             tmp->next = makeValueArgument(self);
@@ -248,6 +249,7 @@ ResultType setFunctionArgument(Argument **arg, Argument **base, LinkValue *_func
                 *arg = tmp;
             }
             break;
+        case cls_free_:
         case all_free_:
             tmp = makeValueArgument(self);
             tmp->next = *arg;

+ 3 - 0
VirtulMathCore/src/value.c

@@ -93,6 +93,7 @@ Value *makeVMFunctionValue(Statement *st, Parameter *pt, INTER_FUNCTIONSIG_NOT_S
     tmp = result->value->value;
     tmp->data.function.function = copyStatement(st);
     tmp->data.function.pt = copyParameter(pt);
+    tmp->data.function.function_data.cls = belong;
     for (VarList *vl = tmp->object.out_var, *vl_next; vl != NULL; vl = vl_next) {
         vl_next = vl->next;
         freeVarList(vl);
@@ -112,6 +113,7 @@ Value *makeCFunctionValue(OfficialFunction of, fline line, char *file, INTER_FUN
     tmp->data.function.type = c_function;
     tmp->data.function.of = of;
     tmp->data.function.function_data.pt_type = inter->data.default_pt_type;
+    tmp->data.function.function_data.cls = belong;
     for (VarList *vl = tmp->object.out_var, *vl_next; vl != NULL; vl = vl_next) {
         vl_next = vl->next;
         freeVarList(vl);
@@ -142,6 +144,7 @@ LinkValue *makeCFunctionFromOf(OfficialFunction of, LinkValue *func, OfficialFun
     return_->value->data.function.type = c_function;
     return_->value->data.function.of = of;
     return_->value->data.function.function_data.pt_type = inter->data.default_pt_type;
+    return_->value->data.function.function_data.cls = belong;
     for (VarList *vl = return_->value->object.out_var; vl != NULL; vl = freeVarList(vl))
         PASS;
     return_->value->object.out_var = copyVarList(var_list, false, inter);