Переглянути джерело

feat: 增加函数__call__为类/对象的回调函数

SongZihuan 4 роки тому
батько
коміт
0933505aa3
10 змінених файлів з 33 додано та 29 видалено
  1. 1 0
      include/inter.h
  2. 1 0
      include/ofunc.h
  3. 6 4
      include/run.h
  4. 1 0
      main.c
  5. 0 13
      src/__run.c
  6. 0 1
      src/include/__run.h
  7. 2 0
      src/inter.c
  8. 20 9
      src/runcall.c
  9. 1 1
      src/runoperation.c
  10. 1 1
      src/value.c

+ 1 - 0
include/inter.h

@@ -30,6 +30,7 @@ struct Inter{
         char *var_defualt;
         char *object_init;
         char *object_new;
+        char *object_call;
         char *object_enter;
         char *object_exit;
         char *object_add;

+ 1 - 0
include/ofunc.h

@@ -16,6 +16,7 @@
 struct Argument;
 struct VarList;
 struct FatherValue;
+struct Inter;
 
 struct NameFunc{
     char *name;

+ 6 - 4
include/run.h

@@ -13,6 +13,7 @@ typedef struct StatementList StatementList;
 typedef struct Inter Inter;
 typedef struct VarList VarList;
 typedef struct Parameter Parameter;
+typedef struct Argument Argument;
 typedef struct DecorationStatement DecorationStatement;
 typedef ResultType (*VarInfo)(char **name, int *times, INTER_FUNCTIONSIG);
 
@@ -31,11 +32,12 @@ ResultType setFunction(INTER_FUNCTIONSIG);
 ResultType setLambda(INTER_FUNCTIONSIG);
 ResultType callBack(INTER_FUNCTIONSIG);
 
-ResultType callBackCore(LinkValue *function_value, struct Argument *arg, long line, char *file, INTER_FUNCTIONSIG_NOT_ST);
+ResultType callBackCore(LinkValue *function_value, Argument *arg, long line, char *file, INTER_FUNCTIONSIG_NOT_ST);
 ResultType callBackCorePt(LinkValue *function_value, Parameter *pt, long line, char *file, INTER_FUNCTIONSIG_NOT_ST);
-ResultType callClass(LinkValue *class_value, struct Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST);
-ResultType callVMFunction(LinkValue *function_value, struct Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST);
-ResultType callCFunction(LinkValue *function_value, struct Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST);
+ResultType callClass(LinkValue *class_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST);
+ResultType callObject(LinkValue *object_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST);
+ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST);
+ResultType callCFunction(LinkValue *function_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST);
 
 ResultType setDecoration(DecorationStatement *ds, LinkValue *value, INTER_FUNCTIONSIG_NOT_ST);
 ResultType getVar(INTER_FUNCTIONSIG, VarInfo var_info);

+ 1 - 0
main.c

@@ -24,6 +24,7 @@ int main(int argc, char *argv[]) {
  * argument 设定 tmp_link
  * __call__ 设定
  * __var__ 设定
+ * __del__ 设定
  * 下标和切片
  * 官方函数
  * 官方类

+ 0 - 13
src/__run.c

@@ -110,19 +110,6 @@ void updateFunctionYield(Statement *function_st, Statement *node){
     function_st->info.have_info = true;
 }
 
-void freeFunctionYield(Statement *function_st, Inter *inter){  // TODO-szh 去除该函数
-    function_st->info.var_list->next = NULL;
-    gc_freeze(inter, function_st->info.var_list, NULL, false);
-    freeVarList(function_st->info.var_list);
-    function_st->info.var_list = NULL;
-    function_st->info.have_info = false;
-    function_st->info.node = NULL;
-}
-
-Statement *getRunInfoStatement(Statement *funtion_st){  // TODO-szh 去除该函数
-    return funtion_st->info.node;
-}
-
 ResultType setFunctionArgument(Argument **arg, LinkValue *function_value, long line, char *file, INTER_FUNCTIONSIG_NOT_ST){
     Argument *tmp = NULL;
     enum FunctionPtType pt_type = function_value->value->data.function.function_data.pt_type;

+ 0 - 1
src/include/__run.h

@@ -21,7 +21,6 @@ bool popStatementVarList(Statement *funtion_st, VarList **function_var, VarList
 
 void newFunctionYield(Statement *funtion_st, Statement *node, VarList *new_var, Inter *inter);
 void updateFunctionYield(Statement *function_st, Statement *node);
-void freeFunctionYield(Statement *function_st, Inter *inter);
 
 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,

+ 2 - 0
src/inter.c

@@ -62,6 +62,7 @@ void setBaseInterData(struct Inter *inter){
     inter->data.object_sub = memStrcpy("__sub__");
     inter->data.object_mul = memStrcpy("__mul__");
     inter->data.object_div = memStrcpy("__div__");
+    inter->data.object_call = memStrcpy("__call__");
     inter->data.default_pt_type = free_;
 
 }
@@ -87,6 +88,7 @@ void freeBaseInterData(struct Inter *inter){
     memFree(inter->data.object_sub);
     memFree(inter->data.object_mul);
     memFree(inter->data.object_div);
+    memFree(inter->data.object_call);
 
     memFree(inter->data.log_dir);
     if (inter->data.log_dir != NULL) {

+ 20 - 9
src/runcall.c

@@ -138,13 +138,10 @@ ResultType callBackCore(LinkValue *function_value, Argument *arg, long line, cha
         callCFunction(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
     else if (function_value->value->type == class)
         callClass(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
-    else{
-        setResultError(result, inter, "TypeException", "Object is not callable", line, file, father, true);
-        goto return_;
-    }
+    else
+        callObject(function_value, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
     setResultError(result, inter, NULL, NULL, line, file, father, false);
 
-    return_:
     gc_freeTmpLink(&function_value->gc_status);
     return result->type;
 }
@@ -154,7 +151,6 @@ ResultType callClass(LinkValue *class_value, Argument *arg, long int line, char
     setResultCore(result);
 
     if (_new_ != NULL){
-        _new_->father = class_value;
         gc_addTmpLink(&_new_->gc_status);
         callBackCore(_new_, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
         gc_freeTmpLink(&_new_->gc_status);
@@ -165,6 +161,21 @@ ResultType callClass(LinkValue *class_value, Argument *arg, long int line, char
     return result->type;
 }
 
+ResultType callObject(LinkValue *object_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
+    LinkValue *_call_ = findAttributes(inter->data.object_call, false, object_value, inter);
+    setResultCore(result);
+
+    if (_call_ != NULL){
+        gc_addTmpLink(&_call_->gc_status);
+        callBackCore(_call_, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
+        gc_freeTmpLink(&_call_->gc_status);
+    }
+    else
+        setResultError(result, inter, "TypeException", "Object is not callable", line, file, father, true);
+
+    return result->type;
+}
+
 ResultType callCFunction(LinkValue *function_value, Argument *arg, long int line, char *file, INTER_FUNCTIONSIG_NOT_ST){
     VarList *function_var = NULL;
     OfficialFunction of = NULL;
@@ -201,7 +212,7 @@ ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int lin
     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, inter)))
-        funtion_st = getRunInfoStatement(funtion_st);
+        funtion_st = funtion_st->info.node;
 
     gc_freeze(inter, var_list, function_var, true);
 
@@ -218,7 +229,7 @@ ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int lin
         gc_freeze(inter, var_list, function_var, false);
         funtion_st = function_value->value->data.function.function;
         if (yield_run)
-            freeFunctionYield(funtion_st, inter);
+            freeRunInfo(funtion_st);
         else
             popVarList(function_var);
         goto return_;
@@ -235,7 +246,7 @@ ResultType callVMFunction(LinkValue *function_value, Argument *arg, long int lin
             result->type = operation_return;
         }
         else
-            freeFunctionYield(funtion_st, inter);
+            freeRunInfo(funtion_st);
     else
         if (result->type == yield_return){
             newFunctionYield(funtion_st, result->node, function_var, inter);

+ 1 - 1
src/runoperation.c

@@ -58,7 +58,7 @@ ResultType blockOperation(INTER_FUNCTIONSIG) {
             result->type = operation_return;
         }
         else
-            freeFunctionYield(st, inter);
+            freeRunInfo(st);
     }
     else {
         if (result->type == yield_return){

+ 1 - 1
src/value.c

@@ -1,4 +1,4 @@
-#include "__virtualmath.h"
+#include "__run.h"
 
 Value *makeObject(Inter *inter, VarList *object, VarList *out_var, FatherValue *father) {
     Value *tmp, *list_tmp = inter->base;