فهرست منبع

feat & fix: 优化.运算符对father的设定, 修复findVar错误

.运算符调整father前加入检查:
    1. child结果(result)的father若等于原father(left)则不作调整
    2. 在满足(1)之后, 仅若child结果(result)的father属原father(left)的father(object.father)做调整
修复了findVar中使用指针优化后,循环条件存在未使用间接访问操作符错误
SongZihuan 4 سال پیش
والد
کامیت
810b37e5f3
10فایلهای تغییر یافته به همراه49 افزوده شده و 26 حذف شده
  1. 3 3
      gc/freeze.c
  2. 1 1
      gc/gc.c
  3. 3 3
      include/gc.h
  4. 1 1
      include/value.h
  5. 5 3
      src/inter.c
  6. 3 3
      src/run.c
  7. 3 3
      src/runcall.c
  8. 15 6
      src/runoperation.c
  9. 14 2
      src/value.c
  10. 1 1
      src/var.c

+ 3 - 3
gc/freeze.c

@@ -12,7 +12,7 @@ void gc_freezeHashTable(HashTable *ht, bool is_lock){
     gc_IterAlready(&ht->gc_status);
 }
 
-void iterFreezeVarList(VarList *freeze, VarList *base, bool is_lock){
+void gc_iterFreezeVarList(VarList *freeze, VarList *base, bool is_lock){
     for (PASS; freeze != NULL; freeze = freeze->next){
         if (!comparVarList(freeze, base))
             gc_freezeHashTable(freeze->hashtable, is_lock);
@@ -26,9 +26,9 @@ void iterFreezeVarList(VarList *freeze, VarList *base, bool is_lock){
  * @param base
  * @param is_lock
  */
-void runFREEZE(Inter *inter, VarList *freeze, VarList *base, bool is_lock){
+void gc_freeze(Inter *inter, VarList *freeze, VarList *base, bool is_lock){
 #if START_GC
     gc_resetBase(inter);
-    iterFreezeVarList(freeze, base, is_lock);
+    gc_iterFreezeVarList(freeze, base, is_lock);
 #endif
 }

+ 1 - 1
gc/gc.c

@@ -120,7 +120,7 @@ void gc_freeBase(Inter *inter){
 #endif
 }
 
-void runGC(Inter *inter, int var_list, int link_value, int value, ...){
+void gc_run(Inter *inter, int var_list, int link_value, int value, ...){
 #if START_GC
     gc_resetBase(inter);
     va_list arg;

+ 3 - 3
include/gc.h

@@ -39,9 +39,9 @@ void gc_iterHashTable(struct HashTable *ht);
 void gc_iterVar(struct Var *var);
 void gc_varList(struct VarList *vl);
 
-void iterFreezeVarList(struct VarList *freeze, struct VarList *base, bool is_lock);
-void runFREEZE(struct Inter *inter, struct VarList *freeze, struct VarList *base, bool is_lock);
+void gc_iterFreezeVarList(struct VarList *freeze, struct VarList *base, bool is_lock);
+void gc_freeze(struct Inter *inter, struct VarList *freeze, struct VarList *base, bool is_lock);
 
-void runGC(struct Inter *inter, int var_list, int link_value, int value, ...);
+void gc_run(struct Inter *inter, int var_list, int link_value, int value, ...);
 
 #endif //VIRTUALMATH_GC_H

+ 1 - 1
include/value.h

@@ -146,5 +146,5 @@ FatherValue *copyFatherValue(FatherValue *value);
 FatherValue *freeFatherValue(FatherValue *value);
 FatherValue *connectFatherValue(FatherValue *base, FatherValue *back);
 FatherValue *connectSafeFatherValue(FatherValue *base, FatherValue *back);
-
+bool checkAttribution(Value *self, Value *father);
 #endif //VIRTUALMATH_VALUE_H

+ 5 - 3
src/inter.c

@@ -89,9 +89,11 @@ void freeBaseInterData(struct Inter *inter){
 void freeInter(Inter *inter, bool self){
     freeBase(inter, return_);
 
-    printLinkValueGC("\n\nprintLinkValueGC TAG : freeInter", inter);
-    printValueGC("\nprintValueGC TAG : freeInter", inter);
-    printVarGC("\nprintVarGC TAG : freeInter", inter);
+    if (getc(stdin) == '1') {
+        printLinkValueGC("\n\nprintLinkValueGC TAG : freeInter", inter);
+        printValueGC("\nprintValueGC TAG : freeInter", inter);
+        printVarGC("\nprintVarGC TAG : freeInter", inter);
+    }
 
     freeStatement(inter->statement);  // Statement放在Value前面释放, 因为base_value的释放需要处理gc_status
     freeVarList(inter->var_list, true);

+ 3 - 3
src/run.c

@@ -77,7 +77,7 @@ ResultType runStatement(INTER_FUNCTIONSIG) {
 
     if (result->value->aut == auto_aut)
         result->value->aut = st->aut;
-    runGC(inter, 1, 0, 0, var_list);
+    gc_run(inter, 1, 0, 0, var_list);
     return type;
 }
 
@@ -110,7 +110,7 @@ ResultType iterStatement(INTER_FUNCTIONSIG) {
     if (type == not_return || type == restart_return)
         setResultOperationNone(result, inter, father);
 
-    runGC(inter, 1, 0, 0, var_list);
+    gc_run(inter, 1, 0, 0, var_list);
     return result->type;
 }
 
@@ -137,7 +137,7 @@ ResultType globalIterStatement(Inter *inter, Result *result) {
     if (type != error_return && type != function_return)
         setResultOperationNone(result, inter, father);
 
-    runGC(inter, 1, 0, 0, var_list);
+    gc_run(inter, 1, 0, 0, var_list);
     return result->type;
 }
 

+ 3 - 3
src/runcall.c

@@ -120,12 +120,12 @@ ResultType callFunction(LinkValue *function_value, Parameter *parameter, INTER_F
 
     function_var = pushVarList(function_value->value->object.out_var, inter);
     gc_addTmpLink(&function_var->hashtable->gc_status);
-    runFREEZE(inter, var_list, function_var, true);
+    gc_freeze(inter, var_list, function_var, true);
 
     setParameter(parameter, function_value->value->data.function.pt, function_var, function_value, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, father));
     if (!run_continue(result)) {
         gc_addTmpLink(&function_var->hashtable->gc_status);
-        runFREEZE(inter, var_list, function_var, false);
+        gc_freeze(inter, var_list, function_var, false);
         popVarList(function_var);
         goto return_;
     }
@@ -134,7 +134,7 @@ ResultType callFunction(LinkValue *function_value, Parameter *parameter, INTER_F
     functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(function_value->value->data.function.function, function_var, result, function_value));
 
     gc_freeTmpLink(&function_var->hashtable->gc_status);
-    runFREEZE(inter, var_list, function_var, false);
+    gc_freeze(inter, var_list, function_var, false);
     popVarList(function_var);
 
     return_:

+ 15 - 6
src/runoperation.c

@@ -164,18 +164,27 @@ ResultType pointOperation(INTER_FUNCTIONSIG) {
         PASS;
     out_var->next = left->value->object.out_var;
 
-    runFREEZE(inter, var_list, object, true);
-    operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.operation.right, object, result, left));  // TODO-szh father设定 (添加参数规定若获得var的位置位于times后则自动设置father)
+    gc_freeze(inter, var_list, object, true);
+    operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.operation.right, object, result, left));
     if (!run_continue(result))
         goto return_;
     else if ((left->aut == public_aut || left->aut == auto_aut) && (result->value->aut != public_aut && result->value->aut != auto_aut))
         setResultError(result, inter, "PermissionsException", "Wrong Permissions: access variables as public", st, father, true);
     else if ((left->aut == protect_aut) && (result->value->aut == private_aut))
         setResultError(result, inter, "PermissionsException", "Wrong Permissions: access variables as protect", st, father, true);
-    result->value->father = left;  // 保留原father.father,为left.father
 
+    if (result->value->father->value != left->value && checkAttribution(left->value, result->value->father->value)) {
+        /**
+         * 若获得的值的father是left的father, 则将获得值的father调整为left (拷贝)
+         */
+        LinkValue *return_value = copyLinkValue(result->value, inter);
+        return_value->father = left;
+        gc_freeTmpLink(&result->value->gc_status);
+        gc_addTmpLink(&return_value->gc_status);
+        result->value = return_value;
+    }
     return_:
-    runFREEZE(inter, var_list, object, false);
+    gc_freeze(inter, var_list, object, false);
     if (out_var != NULL)
         out_var->next = NULL;
     gc_freeTmpLink(&left->gc_status);
@@ -257,12 +266,12 @@ ResultType pointAss(Statement *name, LinkValue *value, INTER_FUNCTIONSIG_NOT_ST)
     setResultCore(result);
 
     VarList *object = left.value->value->object.var;
-    runFREEZE(inter, var_list, object, true);
+    gc_freeze(inter, var_list, object, true);
     if (name->u.operation.right->type == OPERATION && name->u.operation.right->u.operation.OperationType == OPT_POINT)
         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));
-    runFREEZE(inter, var_list, object, true);
+    gc_freeze(inter, var_list, object, true);
 
     freeResult(&left);
     return result->type;

+ 14 - 2
src/value.c

@@ -387,12 +387,24 @@ FatherValue *connectSafeFatherValue(FatherValue *base, FatherValue *back){
     FatherValue **last_node = &base;
     if (back == NULL)
         goto reutrn_;
-    for (PASS; *last_node != NULL; ){
+    for (PASS; *last_node != NULL; )
         if ((*last_node)->value->value == back->value->value)
             *last_node = freeFatherValue(*last_node);
         else
             last_node = &(*last_node)->next;
-    }
     *last_node = back;
     reutrn_: return base;
 }
+
+/**
+ * 检查 father 是否为 self 的父亲
+ * @param self
+ * @param father
+ * @return
+ */
+bool checkAttribution(Value *self, Value *father){
+    for (FatherValue *self_father = self->object.father; self_father != NULL; self_father = self_father->next)
+        if (self_father->value->value == father)
+            return true;
+    return false;
+}

+ 1 - 1
src/var.c

@@ -127,7 +127,7 @@ LinkValue *findVar(char *name, VarList *var_list, bool del_var) {
     LinkValue *tmp = NULL;
     HASH_INDEX index = time33(name);
 
-    for (Var **base = &var_list->hashtable->hashtable[index]; base != NULL; base = &(*base)->next){
+    for (Var **base = &var_list->hashtable->hashtable[index]; *base != NULL; base = &(*base)->next){
         if (eqString((*base)->name, name)){
             tmp = (*base)->value;
             if (del_var)