Browse Source

fix & feat: 加入object类, 修改point赋值运算冻结问题

加入object类
point赋值运算冻结未解冻的bug修复
SongZihuan 4 years ago
parent
commit
b44f72a63b
8 changed files with 61 additions and 10 deletions
  1. 2 0
      include/inter.h
  2. 1 0
      include/ofunc.h
  3. 8 0
      ofunc/include/object.h
  4. 32 0
      ofunc/src/object.c
  5. 12 6
      src/inter.c
  6. 3 1
      src/ofunc.c
  7. 1 1
      src/runoperation.c
  8. 2 2
      src/value.c

+ 2 - 0
include/inter.h

@@ -12,6 +12,8 @@ struct Inter{
 
     struct VarList *var_list;
     struct InterData{
+        struct Value *object;
+        struct Value *none;
         FILE *debug;
         FILE *error;
         char *log_dir;  // 记录log文件夹的位置

+ 1 - 0
include/ofunc.h

@@ -2,6 +2,7 @@
 #define VIRTUALMATH_OFUNC_H
 #include "__macro.h"
 #include "io.h"
+#include "object.h"
 
 struct Argument;
 struct VarList;

+ 8 - 0
ofunc/include/object.h

@@ -0,0 +1,8 @@
+#ifndef VIRTUALMATH_OBJECT_H
+#define VIRTUALMATH_OBJECT_H
+#include "__macro.h"
+
+typedef enum ResultType ResultType;
+void registeredObject(RegisteredFunctionSig);
+void makeBaseObject(Inter *inter);
+#endif //VIRTUALMATH_OBJECT_H

+ 32 - 0
ofunc/src/object.c

@@ -0,0 +1,32 @@
+#include "__ofunc.h"
+
+ResultType object_new_(OfficialFunctionSig){
+    setResultBase(result, inter, father);
+    printf("object.__new__\n");
+    return result->type;
+}
+
+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));
+    object_var->next = NULL;
+
+    name = setStrVarName("object", false, CALL_INTER_FUNCTIONSIG_CORE(var_list));
+    name_ = makeLinkValue(makeStringValue(name, inter), father, inter);
+    addFromVarList(name, name_, 0, makeLinkValue(object, father, inter), CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
+    memFree(name);
+    gc_freeTmpLink(&object->gc_status);
+}
+
+void makeBaseObject(Inter *inter){
+    Value *object = makeClassValue(copyVarList(inter->var_list, false, inter), inter, NULL);
+    gc_addStatementLink(&object->gc_status);
+    inter->data.object = object;
+}

+ 12 - 6
src/inter.c

@@ -2,7 +2,6 @@
 
 Inter *makeInter(char *debug, LinkValue *father) {
     Inter *tmp = memCalloc(1, sizeof(Inter));
-    Value *none_value = NULL;
     LinkValue *base_father = NULL;
     setBaseInterData(tmp);
     tmp->base = NULL;
@@ -28,8 +27,10 @@ Inter *makeInter(char *debug, LinkValue *father) {
         tmp->data.debug = stdout;
         tmp->data.error = stderr;
     }
-    none_value = makeNoneValue(tmp);  // 注册None值
-    gc_addStatementLink(&none_value->gc_status);
+
+    makeBaseObject(tmp);
+    tmp->data.none = makeNoneValue(tmp);
+    gc_addStatementLink(&tmp->data.none->gc_status);
 
     base_father = makeLinkValue(makeObject(tmp, copyVarList(tmp->var_list, false, tmp), NULL, NULL), father, tmp);
     gc_addStatementLink(&base_father->gc_status);
@@ -40,6 +41,8 @@ Inter *makeInter(char *debug, LinkValue *father) {
 }
 
 void setBaseInterData(struct Inter *inter){
+    inter->data.object = NULL;
+    inter->data.none = NULL;
     inter->data.var_str_prefix = memStrcpy("str_");
     inter->data.var_num_prefix = memStrcpy("num_");
     inter->data.var_defualt = memStrcpy("default_var");
@@ -49,6 +52,8 @@ void setBaseInterData(struct Inter *inter){
 }
 
 void freeBaseInterData(struct Inter *inter){
+    gc_freeStatementLink(&inter->data.none->gc_status);
+    gc_freeStatementLink(&inter->data.object->gc_status);
     memFree(inter->data.var_num_prefix);
     memFree(inter->data.var_str_prefix);
     memFree(inter->data.var_defualt);
@@ -99,9 +104,13 @@ void mergeInter(Inter *new, Inter *base){
     LinkValue **base_linkValue = NULL;
     HashTable **base_hash = NULL;
     Var **base_var = NULL;
+
     gc_freeStatementLink(&new->base_father->gc_status);
     new->base_father = NULL;
 
+    freeVarList(new->var_list);
+    freeBaseInterData(new);
+
     for (base_value = &base->base; *base_value != NULL; base_value = &(*base_value)->gc_next)
             PASS;
     for (base_linkValue = &base->link_base; *base_linkValue != NULL; base_linkValue = &(*base_linkValue)->gc_next)
@@ -115,9 +124,6 @@ void mergeInter(Inter *new, Inter *base){
     *base_linkValue = new->link_base;
     *base_hash = new->hash_base;
     *base_var = new->base_var;
-
-    freeVarList(new->var_list);
-    freeBaseInterData(new);
     memFree(new);
 }
 

+ 3 - 1
src/ofunc.c

@@ -1,6 +1,8 @@
 #include "__virtualmath.h"
 
-static Registered base_func_list[] = {registeredIOFunction, NULL};
+static Registered base_func_list[] = {registeredIOFunction,
+                                      registeredObject,
+                                      NULL};
 
 void registeredBaseFunction(struct LinkValue *father, Inter *inter){
     for (Registered *list = base_func_list; *list != NULL; list++)

+ 1 - 1
src/runoperation.c

@@ -318,7 +318,7 @@ ResultType pointAss(Statement *name, LinkValue *value, INTER_FUNCTIONSIG_NOT_ST)
         pointAss(name->u.operation.right, value, CALL_INTER_FUNCTIONSIG_NOT_ST(object, result, father));
     else
         assCore(name->u.operation.right, value, CALL_INTER_FUNCTIONSIG_NOT_ST(object, result, father));
-    gc_freeze(inter, var_list, object, true);
+    gc_freeze(inter, var_list, object, false);
 
     freeResult(&left);
     return result->type;

+ 2 - 2
src/value.c

@@ -27,12 +27,12 @@ Value *makeObject(Inter *inter, VarList *object, VarList *out_var, FatherValue *
 
 Value *makeNoneValue(Inter *inter) {
     Value *tmp;
-    if (inter->base == NULL) {
+    if (inter->data.none == NULL) {
         tmp = makeObject(inter, NULL, NULL, NULL);
         tmp->type = none;
     }
     else
-        tmp = inter->base;
+        tmp = inter->data.none;
     return tmp;
 }