瀏覽代碼

赋值,import等语法更开放

SongZihuan 5 年之前
父節點
當前提交
b5618be639
共有 5 個文件被更改,包括 384 次插入501 次删除
  1. 二進制
      gwarf
  2. 128 200
      inter/interpreter.c
  3. 6 8
      inter/interpreter.h
  4. 14 53
      paser/gwarf_yacc.y
  5. 236 240
      paser/y.tab.c

二進制
gwarf


+ 128 - 200
inter/interpreter.c

@@ -343,33 +343,7 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             func_value.value.type = FUNC_value;
             func_value.value.value.func_value = func_tmp;
 
-            int from;
-            if(the_statement->code.def.from == NULL){
-                from = 0;
-            }
-            else{
-                GWARF_result tmp_result, tmp_object = traverse(the_statement->code.def.from, the_var, false);
-                if(is_error(&tmp_object)){  // Name Error错误
-                    // puts("STOP:: Name No Found!");
-                    from = 0;
-                }
-                else if(is_space(&tmp_object)){
-                    from = 0;
-                }
-                else{
-                    tmp_result = get__value__(&(tmp_object.value), the_var);  // 从object中提取value
-                    if(tmp_result.value.type == INT_value){
-                        from = tmp_result.value.value.int_value;
-                    }
-                    else if(tmp_result.value.type == NUMBER_value){
-                        from = (int)tmp_result.value.value.double_value;
-                    }
-                    else{
-                        from = 0;
-                    }
-                }
-            }
-            assigment_func(the_statement->code.def.name, func_value, login_var, from);  // 注册函数到指定的位置
+            assigment_statement(the_statement->code.def.var, the_var, login_var, func_value);  // 注册函数到指定的位置
             // 无返回值
             break;
         }
@@ -432,34 +406,7 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
                 tmp = tmp->next;
             }
 
-            int from;
-            if(the_statement->code.set_class.from == NULL){
-                from = 0;
-            }
-            else{
-                GWARF_result tmp_result, tmp_object = traverse(the_statement->code.set_class.from, the_var, false);
-                if(is_error(&tmp_object)){  // Name Error错误
-                    // puts("STOP:: Name No Found!");
-                    from = 0;
-                }
-                else if(is_space(&tmp_object)){
-                    from = 0;
-                }
-                else{
-                    tmp_result = get__value__(&(tmp_object.value), the_var);  // 从object中提取value
-                    if(tmp_result.value.type == INT_value){
-                        from = tmp_result.value.value.int_value;
-                    }
-                    else if(tmp_result.value.type == NUMBER_value){
-                        from = (int)tmp_result.value.value.double_value;
-                    }
-                    else{
-                        from = 0;
-                    }
-                }
-            }
-
-            assigment_func(the_statement->code.set_class.name, class_value, login_var, from);  // 注册class 的 位置
+            assigment_statement(the_statement->code.set_class.var, the_var,login_var, class_value);  // 注册class 的 位置
             puts("----stop set class----");
             // 无返回值
             break;
@@ -806,7 +753,6 @@ GWARF_result import_func(statement *the_statement, var_list *the_var){
     GWARF_result return_value;
     statement *file_statement = the_statement->code.import_class.file;
     
-    char *name = the_statement->code.import_class.name;
     GWARF_value file = to_str(traverse(file_statement, the_var, false).value, the_var);
 
     // TODO:: 使用链表来存储
@@ -834,35 +780,8 @@ GWARF_result import_func(statement *the_statement, var_list *the_var){
     class_tmp->the_var = new_the_var;  // make class var list
     class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var));  // make class var list with out var
 
-    int from;
-    if(the_statement->code.import_class.from == NULL){
-        from = 0;
-    }
-    else{
-        GWARF_result tmp_result, tmp_object = traverse(the_statement->code.import_class.from, the_var, false);
-        if(is_error(&tmp_object)){  // Name Error错误
-            // puts("STOP:: Name No Found!");
-            from = 0;
-        }
-        else if(is_space(&tmp_object)){
-            from = 0;
-        }
-        else{
-            tmp_result = get__value__(&(tmp_object.value), the_var);  // 从object中提取value
-            if(tmp_result.value.type == INT_value){
-                from = tmp_result.value.value.int_value;
-            }
-            else if(tmp_result.value.type == NUMBER_value){
-                from = (int)tmp_result.value.value.double_value;
-            }
-            else{
-                from = 0;
-            }
-        }
-    }
-
     import_result.value.value.class_value = class_tmp;
-    assigment_func(name, import_result, the_var, from);
+    assigment_statement(the_statement->code.import_class.var, the_var, the_var, import_result);
 
     return_value.u = statement_end;
     return_value.value.type = NULL_value;
@@ -1204,7 +1123,7 @@ GWARF_result try_func(statement *the_statement, var_list *the_var){  // read the
     // restart操作[和continue效果相同]
 
     if(is_error(&value)){  // 遇到错误->执行except语句[不需要再检查break...]
-        assigment_func(the_statement->code.try_code.name, value, the_var, 0);
+        assigment_statement(the_statement->code.try_code.var, the_var, the_var, value);
         puts("----except----");
         value = traverse(the_statement->code.try_code.except, the_var, false);
         puts("----stop except----");
@@ -1322,8 +1241,6 @@ GWARF_result forin_func(statement *the_statement, var_list *the_var){  // read t
     GWARF_result value;
     var *tmp = make_var();  // base_var
     the_var = append_var_list(tmp, the_var);
-
-    char *name = the_statement->code.for_in_cycle.name;
     
     GWARF_result tmp_result = traverse(the_statement->code.for_in_cycle.iter, the_var, false);  // 取得迭代器
     if(is_error(&tmp_result)){  // Name Error错误
@@ -1347,7 +1264,7 @@ GWARF_result forin_func(statement *the_statement, var_list *the_var){  // read t
             break;  // goto return_value;
         }
         else{
-            assigment_func(name, tmp_next, the_var, 0);  // 赋值
+            assigment_statement(the_statement->code.for_in_cycle.var, the_var, the_var,tmp_next);  // 赋值
         }
         restart_again: 
         puts("----for in----");
@@ -1529,118 +1446,7 @@ GWARF_result operation_func(statement *the_statement, var_list *the_var, var_lis
             value = negative_func(right_result, the_var);
             break;
         case ASSIGMENT_func:{  // because the var char, we should ues a {} to make a block[name space] for the tmp var;
-            if((the_statement->code.operation.left_exp)->type == base_var){  // 通过base_var赋值
-                char *left = (the_statement->code.operation.left_exp)->code.base_var.var_name;  // get var name but not value
-                int from = 0;
-                if((the_statement->code.operation.left_exp)->code.base_var.from == NULL){
-                    from = 0;
-                }
-                else{
-                    GWARF_result tmp_result, tmp_object = traverse((the_statement->code.operation.left_exp)->code.base_var.from, the_var, false);
-                    if(is_error(&tmp_object)){  // Name Error错误
-                        // puts("STOP:: Name No Found!");
-                        return tmp_object;
-                    }
-                    else if(is_space(&tmp_object)){
-                        return tmp_object;
-                    }
-                    tmp_result = get__value__(&(tmp_object.value), the_var);  // 从object中提取value
-                    if(tmp_result.value.type == INT_value){
-                        from = tmp_result.value.value.int_value;
-                    }
-                    else if(tmp_result.value.type == NUMBER_value){
-                        from = (int)tmp_result.value.value.double_value;
-                    }
-                    else{
-                        from = 0;
-                    }
-                }
-
-                value = assigment_func(left, right_result, login_var, from);
-            }
-            else if((the_statement->code.operation.left_exp)->type == point){  // 通过point赋值
-                GWARF_result tmp_result = traverse((the_statement->code.operation.left_exp)->code.point.base_var, the_var, false);  // 不用取value
-                if(is_error(&tmp_result)){  // Name Error错误
-                    // puts("STOP:: Name No Found!");
-                    return tmp_result;
-                }
-                else if(is_space(&tmp_result)){
-                    return tmp_result;
-                }
-                GWARF_value base_the_var = tmp_result.value;  // 不用取value
-                if(((the_statement->code.operation.left_exp)->code.point.child_var)->type == base_var){
-                    char *left = ((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.var_name;
-                    int from = 0;
-                    if(((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.from == NULL){
-                        from = 0;
-                    }
-                    else{
-                        GWARF_result tmp_result, tmp_object = traverse(((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.from, the_var, false);
-                        if(is_error(&tmp_object)){  // Name Error错误
-                            // puts("STOP:: Name No Found!");
-                            return tmp_object;
-                        }
-                        else if(is_space(&tmp_object)){
-                            return tmp_object;
-                        }
-                        tmp_result = get__value__(&(tmp_object.value), the_var);  // 从object中提取value
-                        if(tmp_result.value.type == INT_value){
-                            from = tmp_result.value.value.int_value;
-                        }
-                        else if(tmp_result.value.type == NUMBER_value){
-                            from = (int)tmp_result.value.value.double_value;
-                        }
-                        else{
-                            from = 0;
-                        }
-                    }
-                    value = assigment_func(left, right_result, base_the_var.value.object_value->the_var, from);
-                }
-                else{
-                    goto the_else;
-                }
-            }
-            else if((the_statement->code.operation.left_exp)->type == down){  // 通过down赋值
-                GWARF_result tmp_result = traverse((the_statement->code.operation.left_exp)->code.down.base_var, the_var, false), get;  // 不用取value
-                if(is_error(&tmp_result)){  // Name Error错误
-                    // puts("STOP:: Name No Found!");
-                    return tmp_result;
-                }
-                else if(is_space(&tmp_result)){
-                    return tmp_result;
-                }
-                GWARF_value base_the_var = tmp_result.value;  // 不用取value
-                if(base_the_var.type == CLASS_value){  // is class so that can use "."
-                    GWARF_result child_value = traverse((the_statement->code.operation.left_exp)->code.down.child_var, the_var, false);  // 作为参数
-                    var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__set__");
-                    if(tmp != NULL){
-                        get.value = tmp->value;
-                        get.father = &base_the_var;  // 设置father
-                        parameter *tmp = pack_value_parameter(child_value.value);
-                        tmp->next = pack_value_parameter(right_result.value);
-                        value = call_back_core(get, the_var, tmp);
-                        goto the_else;
-                    }
-                }
-                else if(base_the_var.type == OBJECT_value){
-                    GWARF_result child_value = traverse((the_statement->code.operation.left_exp)->code.down.child_var, the_var, false);  // 作为参数
-                    var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__set__");
-                    if(tmp != NULL){
-                        get.value = tmp->value;
-                        get.father = &base_the_var;  // 设置father
-                        parameter *tmp = pack_value_parameter(child_value.value);
-                        tmp->next = pack_value_parameter(right_result.value);
-                        value = call_back_core(get, the_var, tmp);
-                        goto the_else;
-                    }
-                }
-                else{
-                    goto the_else;
-                }
-            }
-            else{ 
-                the_else: puts("Bad Assigment");
-            }
+            value = assigment_statement(the_statement->code.operation.left_exp, the_var, login_var, right_result);
             break;
         }
         case EQUAL_func:
@@ -1678,6 +1484,128 @@ GWARF_result operation_func(statement *the_statement, var_list *the_var, var_lis
     return value;
 }
 
+GWARF_result assigment_statement(statement *the_statement, var_list *the_var, var_list *login_var, GWARF_result right_result){
+    GWARF_result value;
+    value.u = statement_end;
+    value.value.type = NULL_value;
+    value.value.value.int_value = 0;
+
+
+    if(the_statement->type == base_var){  // 通过base_var赋值
+        char *left = the_statement->code.base_var.var_name;  // get var name but not value
+        int from = 0;
+        if(the_statement->code.base_var.from == NULL){
+            from = 0;
+        }
+        else{
+            GWARF_result tmp_result, tmp_object = traverse(the_statement->code.base_var.from, the_var, false);
+            if(is_error(&tmp_object)){  // Name Error错误
+                // puts("STOP:: Name No Found!");
+                return tmp_object;
+            }
+            else if(is_space(&tmp_object)){
+                return tmp_object;
+            }
+            tmp_result = get__value__(&(tmp_object.value), the_var);  // 从object中提取value
+            if(tmp_result.value.type == INT_value){
+                from = tmp_result.value.value.int_value;
+            }
+            else if(tmp_result.value.type == NUMBER_value){
+                from = (int)tmp_result.value.value.double_value;
+            }
+            else{
+                from = 0;
+            }
+        }
+
+        value = assigment_func(left, right_result, login_var, from);
+    }
+    else if(the_statement->type == point){  // 通过point赋值
+        GWARF_result tmp_result = traverse(the_statement->code.point.base_var, the_var, false);  // 不用取value
+        if(is_error(&tmp_result)){  // Name Error错误
+            // puts("STOP:: Name No Found!");
+            return tmp_result;
+        }
+        else if(is_space(&tmp_result)){
+            return tmp_result;
+        }
+        GWARF_value base_the_var = tmp_result.value;  // 不用取value
+        if((the_statement->code.point.child_var)->type == base_var){
+            char *left = (the_statement->code.point.child_var)->code.base_var.var_name;
+            int from = 0;
+            if((the_statement->code.point.child_var)->code.base_var.from == NULL){
+                from = 0;
+            }
+            else{
+                GWARF_result tmp_result, tmp_object = traverse((the_statement->code.point.child_var)->code.base_var.from, the_var, false);
+                if(is_error(&tmp_object)){  // Name Error错误
+                    // puts("STOP:: Name No Found!");
+                    return tmp_object;
+                }
+                else if(is_space(&tmp_object)){
+                    return tmp_object;
+                }
+                tmp_result = get__value__(&(tmp_object.value), the_var);  // 从object中提取value
+                if(tmp_result.value.type == INT_value){
+                    from = tmp_result.value.value.int_value;
+                }
+                else if(tmp_result.value.type == NUMBER_value){
+                    from = (int)tmp_result.value.value.double_value;
+                }
+                else{
+                    from = 0;
+                }
+            }
+            value = assigment_func(left, right_result, base_the_var.value.object_value->the_var, from);
+        }
+        else{
+            goto the_else;
+        }
+    }
+    else if(the_statement->type == down){  // 通过down赋值
+        GWARF_result tmp_result = traverse(the_statement->code.down.base_var, the_var, false), get;  // 不用取value
+        if(is_error(&tmp_result)){  // Name Error错误
+            // puts("STOP:: Name No Found!");
+            return tmp_result;
+        }
+        else if(is_space(&tmp_result)){
+            return tmp_result;
+        }
+        GWARF_value base_the_var = tmp_result.value;  // 不用取value
+        if(base_the_var.type == CLASS_value){  // is class so that can use "."
+            GWARF_result child_value = traverse(the_statement->code.down.child_var, the_var, false);  // 作为参数
+            var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__set__");
+            if(tmp != NULL){
+                get.value = tmp->value;
+                get.father = &base_the_var;  // 设置father
+                parameter *tmp = pack_value_parameter(child_value.value);
+                tmp->next = pack_value_parameter(right_result.value);
+                value = call_back_core(get, the_var, tmp);
+                goto the_else;
+            }
+        }
+        else if(base_the_var.type == OBJECT_value){
+            GWARF_result child_value = traverse(the_statement->code.down.child_var, the_var, false);  // 作为参数
+            var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__set__");
+            if(tmp != NULL){
+                get.value = tmp->value;
+                get.father = &base_the_var;  // 设置father
+                parameter *tmp = pack_value_parameter(child_value.value);
+                tmp->next = pack_value_parameter(right_result.value);
+                value = call_back_core(get, the_var, tmp);
+                goto the_else;
+            }
+        }
+        else{
+            goto the_else;
+        }
+    }
+    else{ 
+        the_else: puts("Bad Assigment");
+    }
+    return value;
+}
+
 GWARF_result call_back(statement *the_statement, var_list *the_var){  // the func for add and call from read_statement_list
     GWARF_result get = traverse(the_statement->code.call.func, the_var, false), result;
     if(is_error(&get)){  // Name Error错误

+ 6 - 8
inter/interpreter.h

@@ -215,10 +215,9 @@ typedef struct statement{
         } code_block;
 
         struct{
-            char *name;
             parameter *parameter_list;  // def parameter
             struct statement *done;  // def to do
-            struct statement *from;  // from where
+            struct statement *var;  // from where
         } def;
 
         struct{
@@ -232,17 +231,16 @@ typedef struct statement{
         } return_code;
 
         struct{
-            char *name;  // class name
             struct statement *done;  // class to do
             parameter *father_list;  // 继承
-            struct statement *from;  // from where [double->int]
+            struct statement *var;  // from where [double->int]
         } set_class;
 
         struct
         {
             struct statement *try;
             struct statement *except;
-            char *name;  // as var
+            struct statement *var;  // as var
         } try_code;
 
         struct
@@ -259,8 +257,7 @@ typedef struct statement{
         struct
         {
             struct statement *file;  // get address for file
-            char *name;  // as name
-            struct statement *from;  // from where
+            struct statement *var;  // as name
         } import_class;
 
         struct
@@ -270,7 +267,7 @@ typedef struct statement{
 
         struct
         {
-            char *name;  // for i in a -> i
+            struct statement *var;  // for i in a -> i
             struct statement *iter;  // for i in a -> a
             struct statement *done;  // for while to do
         } for_in_cycle;
@@ -426,6 +423,7 @@ GWARF_result sqrt_func(GWARF_result, GWARF_result, var_list *);
 GWARF_result assigment_func(char *, GWARF_result, var_list *, int);
 GWARF_result equal_func(GWARF_result, GWARF_result, var_list *, int);
 GWARF_result negative_func(GWARF_result, var_list *);
+GWARF_result assigment_statement(statement *, var_list *, var_list *, GWARF_result);
 
 double sqrt_(double, double);
 double log_(double, double);

+ 14 - 53
paser/gwarf_yacc.y

@@ -695,22 +695,14 @@ for_exp
         statement_base = append_statement_list(for_tmp->code.for_cycle.done, statement_base);  // new statement_base (FILO)
         $$ = for_tmp;
     }
-    | FOR LB base_var_ IN top_exp RB
+    | FOR LB element IN top_exp RB
     {
         statement *for_tmp =  make_statement();
         for_tmp->type = for_in_cycle;
-        for_tmp->type = for_in_cycle;
         for_tmp->code.for_in_cycle.iter = $5;
-
-        for_tmp->code.for_in_cycle.name = malloc(sizeof($3->code.base_var.var_name));
-        char *name_tmp = for_tmp->code.for_in_cycle.name;
-        strcpy(name_tmp, $3->code.base_var.var_name);
-
+        for_tmp->code.for_in_cycle.var = $3;
         for_tmp->code.for_in_cycle.done = make_statement();
         statement_base = append_statement_list(for_tmp->code.for_in_cycle.done, statement_base);  // new statement_base (FILO)
-
-        free($3->code.base_var.var_name);  // 实际上会内存泄露[from没有被释放]
-        free($3);
         $$ = for_tmp;
     }
     ;
@@ -726,20 +718,13 @@ include_exp
     ;
 
 import_exp
-    : IMPORT top_exp AS base_var_
+    : IMPORT top_exp AS element
     {
         statement *import_tmp =  make_statement();
         import_tmp->type = import_class;
         import_tmp->code.import_class.file = $2;
 
-        import_tmp->code.import_class.name = malloc(sizeof($4->code.base_var.var_name));
-        char *name_tmp = import_tmp->code.import_class.name;
-        strcpy(name_tmp, $4->code.base_var.var_name);
-
-        import_tmp->code.import_class.from = $4->code.base_var.from;
-
-        free($4->code.base_var.var_name);  // 实际上会内存泄露[from没有被释放]
-        free($4);
+        import_tmp->code.import_class.var = $4;
 
         $$ = import_tmp;
     }
@@ -772,13 +757,11 @@ try_block
     ;
 
 try_exp
-    : try_token block EXCEPT AS base_var_
+    : try_token block EXCEPT AS element
     {
         statement_base = free_statement_list(statement_base);  // out statement_base (FILO)
 
-        $1->code.try_code.name = malloc(sizeof($5->code.base_var.var_name));
-        char *name_tmp = $1->code.try_code.name;
-        strcpy(name_tmp, $5->code.base_var.var_name);        
+        $1->code.try_code.var = $5;  
 
         $1->code.try_code.except = make_statement();
         statement_base = append_statement_list($1->code.try_code.except, statement_base);  // new statement_base (FILO)
@@ -825,42 +808,32 @@ class_block
     ;
 
 class_exp
-    : CLASS  base_var_ LB RB
+    : CLASS  element LB RB
     {   
         //无参数方法
         statement *class_tmp =  make_statement();
         class_tmp->type = set_class;
 
-        class_tmp->code.set_class.from = $2->code.base_var.from;
-        class_tmp->code.set_class.name = malloc(sizeof($2->code.base_var.var_name));
-        char *name_tmp = class_tmp->code.set_class.name;
-        strcpy(name_tmp, $2->code.base_var.var_name);
+        class_tmp->code.set_class.var = $2;
 
         class_tmp->code.set_class.father_list = NULL;
         class_tmp->code.set_class.done = make_statement();
         statement_base = append_statement_list(class_tmp->code.set_class.done, statement_base);  // new statement_base (FILO)
 
-        free($2->code.base_var.var_name);
-        free($2);
         $$ = class_tmp;
     }
-    | CLASS  base_var_ LB arguments RB
+    | CLASS  element LB arguments RB
     {   
         //无参数方法
         statement *class_tmp =  make_statement();
         class_tmp->type = set_class;
 
-        class_tmp->code.set_class.name = malloc(sizeof($2->code.base_var.var_name));
-        char *name_tmp = class_tmp->code.set_class.name;
-        strcpy(name_tmp, $2->code.base_var.var_name);
-        class_tmp->code.set_class.from = $2->code.base_var.from;
+        class_tmp->code.set_class.var = $2;
 
         class_tmp->code.set_class.done = make_statement();
         class_tmp->code.set_class.father_list = $4;  // set father
         statement_base = append_statement_list(class_tmp->code.set_class.done, statement_base);  // new statement_base (FILO)
 
-        free($2->code.base_var.var_name);
-        free($2);
         $$ = class_tmp;
     }
     ;
@@ -873,41 +846,29 @@ def_block
     ;
 
 def_exp
-    : DEF  base_var_ LB RB
+    : DEF  element LB RB
     {   
         //无参数方法
         statement *def_tmp =  make_statement();
         def_tmp->type = def;
 
-        def_tmp->code.def.name = malloc(sizeof($2->code.base_var.var_name));
-        char *name_tmp = def_tmp->code.def.name;
-        strcpy(name_tmp, $2->code.base_var.var_name);
-
-        def_tmp->code.def.from = $2->code.base_var.from;
+        def_tmp->code.def.var = $2;
         def_tmp->code.def.parameter_list = NULL;
         def_tmp->code.def.done = make_statement();
         statement_base = append_statement_list(def_tmp->code.def.done, statement_base);  // new statement_base (FILO)
 
-        free($2->code.base_var.var_name);  // 实际上会内存泄露[from没有被释放]
-        free($2);
         $$ = def_tmp;
     }
-    | DEF  base_var_ LB formal_parameter RB
+    | DEF  element LB formal_parameter RB
     {   
         statement *def_tmp =  make_statement();
         def_tmp->type = def;
 
-        def_tmp->code.def.name = malloc(sizeof($2->code.base_var.var_name));
-        char *name_tmp = def_tmp->code.def.name;
-        strcpy(name_tmp, $2->code.base_var.var_name);
-
-        def_tmp->code.def.from = $2->code.base_var.from;
+        def_tmp->code.def.var = $2;
         def_tmp->code.def.parameter_list = $4;
         def_tmp->code.def.done = make_statement();
         statement_base = append_statement_list(def_tmp->code.def.done, statement_base);  // new statement_base (FILO)
 
-        free($2->code.base_var.var_name);
-        free($2);
         $$ = def_tmp;
     }
     ;

File diff suppressed because it is too large
+ 236 - 240
paser/y.tab.c


Some files were not shown because too many files changed in this diff