Browse Source

扩大赋值范围svar

SongZihuan 5 years ago
parent
commit
65735843f9
6 changed files with 200 additions and 36 deletions
  1. 113 13
      inter/interpreter.c
  2. 9 2
      inter/interpreter.h
  3. 1 0
      paser/lexical.c
  4. 1 0
      paser/paser.h
  5. 72 20
      paser/syntax.c
  6. 4 1
      paser/token.h

+ 113 - 13
inter/interpreter.c

@@ -119,6 +119,7 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             break;
         }
         case call:
+            puts("tag 11");
             return_value = call_back(the_statement, the_var);
             break;
         case while_cycle:
@@ -279,6 +280,53 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             }
             break;
         }
+        case base_svar:{    // because the var tmp, we should ues a {} to make a block[name space] for the tmp var;
+            int from = 0;
+            if(the_statement->code.base_svar.from == NULL){
+                from = 0;
+            }
+            else{
+                GWARF_result tmp_result, tmp_object = traverse(the_statement->code.base_svar.from, the_var, false);
+                if(is_error(&tmp_object)){  // Name Error错误
+                    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;
+                    }
+                }
+            }
+            GWARF_result eq_object = traverse(the_statement->code.base_svar.var, the_var, false);
+            if(is_error(&eq_object)){
+                return eq_object;
+            }
+            else if(is_space(&eq_object)){
+                return eq_object;
+            }
+            char *str = to_str_dict(eq_object.value, the_var).value.string;
+            printf("str = %s\n", str);
+
+            var *tmp = find_var(the_var, from, str);
+            if(tmp == NULL){  // 输出name error[共两处会输出]
+                char *tmp = malloc((size_t)( 21 + strlen(str) ));
+                sprintf(tmp, "name not found [%s]\n", str);
+                return_value = to_error(tmp, "NameException", the_var);
+            }
+            else{
+                return_value.value = tmp->value;  // get_var
+            }
+            break;
+        }
         case point:{
             GWARF_result tmp_result = traverse((the_statement->code).point.base_var, the_var, false);
             if(is_error(&tmp_result)){  // Name Error错误
@@ -369,8 +417,10 @@ 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;
-
+            puts("tag 11");
+            printf("the_statement->code.def.var = %d\n", the_statement->code.def.var->type);
             assignment_statement_core(the_statement->code.def.var, the_var, the_login_var, func_value, true);  // 注册函数到指定的位置
+            puts("tag 12");
             // 无返回值
             break;
         }
@@ -1667,9 +1717,7 @@ GWARF_result assignment_statement_core(statement *the_statement, var_list *the_v
     value.u = statement_end;
     value.value.type = NULL_value;
     value.value.value.int_value = 0;
-    if(the_statement->type != base_var && the_statement->type != point && the_statement->type != down){
-        goto the_else;  // 非法的赋值语句
-    }
+    value.base_name = NULL;  // 默认是NULL
     if(!self){  // 函数声明的时候使用self
         if(right_result.value.type == OBJECT_value || right_result.value.type == CLASS_value){  // 比如a = q, q是一个object, 若他的__assignment__方法返回的是数字5, 那么a的赋值就相当与a = 5了而不是a = q
             right_result = get__assignment__(&(right_result.value), the_var);
@@ -1709,6 +1757,7 @@ GWARF_result assignment_statement_core(statement *the_statement, var_list *the_v
         }
 
         value = assignment_func(left, right_result, login_var, from);
+        value.base_name = left;
     }
     else if(the_statement->type == point){  // 通过point赋值
         GWARF_result tmp_result = traverse(the_statement->code.point.base_var, login_var, false);  // 不用取value
@@ -1787,19 +1836,65 @@ GWARF_result assignment_statement_core(statement *the_statement, var_list *the_v
             }
         }
         else{
-            goto the_else;
+            the_else: value = to_error("Bad Assignment", "AssignmentException", the_var);  // 赋值错误
+            puts("Bad Assignment");
+        }
+    }
+    else if(the_statement->type == base_svar){  // 通过base_var赋值
+        int from = 0;
+        if(the_statement->code.base_svar.from == NULL){
+            from = 0;
+        }
+        else{
+            GWARF_result tmp_result, tmp_object = traverse(the_statement->code.base_svar.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;
+            }
+        }
+
+        GWARF_result eq_object = traverse(the_statement->code.base_svar.var, the_var, false);
+        if(is_error(&eq_object)){
+            return eq_object;
+        }
+        else if(is_space(&eq_object)){
+            return eq_object;
         }
+        char *str = to_str_dict(eq_object.value, the_var).value.string;
+        value = assignment_func(str, right_result, login_var, from);
+        value.base_name = str;  // str来自value,本身就是malloc申请的内存
     }
-    else{ 
-        the_else: value = to_error("Bad Assignment", "AssignmentException", the_var);  // 赋值错误
-        puts("Bad Assignment");
+    else{  // 非标准赋值
+        GWARF_result eq_object = traverse(the_statement, the_var, false);
+        if(is_error(&eq_object)){
+            return eq_object;
+        }
+        else if(is_space(&eq_object)){
+            return eq_object;
+        }
+        char *str = to_str_dict(eq_object.value, the_var).value.string;
+        value = assignment_func(str, right_result, login_var, 0);
+        value.base_name = str;
     }
     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错误
+    if(is_error(&get)){
         // puts("STOP:: Name No Found!");
         return get;
     }
@@ -1821,7 +1916,6 @@ GWARF_result login_var(var_list *the_var, var_list *old_var_list, parameter *tmp
     var_list *tmp_var = make_var_base(make_hash_var());  // 为1-模式准备
 
     while(1){
-        puts("again");
         if ((tmp_x == NULL)&&(tmp_s == NULL)){  // the last
             break;
         }
@@ -1914,9 +2008,9 @@ GWARF_result login_var(var_list *the_var, var_list *old_var_list, parameter *tmp
                     else{
                         GWARF_result tmp_x_var = traverse(tmp_x->u.var, tmp_var, false);  // 使用tmp_x->u.var在tmp_var中获取变量的值
                         if((!is_error(&tmp_x_var)) && (!is_space(&tmp_x_var))){  // 如果找到了,就赋值
-                            assignment_statement(tmp_x->u.var, old_var_list, the_var, tmp_x_var);
-                            if(tmp_x->u.var->type = base_var){
-                                del_var_var_list(tmp_var, 0, tmp_x->u.var->code.base_var.var_name);  // 从中删除变量
+                            GWARF_result tmp = assignment_statement(tmp_x->u.var, old_var_list, the_var, tmp_x_var);
+                            if(tmp.base_name != NULL){  // 删除变量
+                                del_var_var_list(tmp_var, 0, tmp.base_name);  // 从中删除变量
                             }
                         }
                         else if(tmp_x->type == name_value){  // 没找到就检查是否为name_value类型,给默认值
@@ -1924,6 +2018,7 @@ GWARF_result login_var(var_list *the_var, var_list *old_var_list, parameter *tmp
                             if(is_error(&tmp) || is_space(&tmp)){
                                 return tmp;
                             }
+                            puts("tag 4");
                             assignment_statement(tmp_x->u.var, old_var_list, the_var, tmp);
                         }
                         else{
@@ -1982,6 +2077,7 @@ GWARF_result login_var(var_list *the_var, var_list *old_var_list, parameter *tmp
             // 放入list中
             GWARF_result dict_tmp;
             dict_tmp.value = to_object(parameter_to_dict(tmp_s, old_var_list), old_var_list);  // 把所有name_value变成dict
+            puts("tag 5");
             assignment_statement(tmp_x->u.var, old_var_list, the_var, dict_tmp);
             assignment_type = 1;  // 进入根据实参赋值模式
             tmp_x = NULL;  // 已经到最后一个了
@@ -1993,6 +2089,7 @@ GWARF_result login_var(var_list *the_var, var_list *old_var_list, parameter *tmp
                 break;
             }
             assignment_type = 1;
+            puts("tag 6");
             assignment_statement(tmp_s->u.var, old_var_list, tmp_var, tmp);  // 先赋值到tmp_var上
             tmp_s = tmp_s->next;
         }
@@ -2018,6 +2115,7 @@ GWARF_result login_var(var_list *the_var, var_list *old_var_list, parameter *tmp
             // 放入list中
             GWARF_result list_tmp;
             list_tmp.value = to_object(parameter_to_list(tmp_s, old_var_list), old_var_list);  // 把所有only_value变成list
+            puts("tag 7");
             assignment_statement(tmp_x->u.var, old_var_list, the_var, list_tmp);
             assignment_type = 1;  // 进入根据实参赋值模式
             tmp_x = tmp_x->next;
@@ -2033,6 +2131,7 @@ GWARF_result login_var(var_list *the_var, var_list *old_var_list, parameter *tmp
             }
         }
         else if(assignment_type == 0){
+            puts("tag 8");
             assignment_statement(tmp_x->u.var, old_var_list, the_var, tmp);
             tmp_x = tmp_x->next;  // get the next to iter
             tmp_s = tmp_s->next;
@@ -2061,6 +2160,7 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
             if(func_->is_class){
                 if(get.father != NULL){
                     father.value = *(get.father);
+                    puts("tag 9");
                     assignment_statement_core(tmp_x->u.var, old_var_list, the_var, father, true);
                     tmp_x = tmp_x->next;  // get the next to iter
                 }

+ 9 - 2
inter/interpreter.h

@@ -121,6 +121,7 @@ typedef struct statement{
         assert_e,
         chose_exp,
         pack_eq,
+        base_svar,
     } type;  // the statement type
 
     union
@@ -197,6 +198,11 @@ typedef struct statement{
             struct statement *from;  // from where [double->int]
         } base_var;
 
+        struct{
+            struct statement *var;  // return var
+            struct statement *from;  // from where [double->int]
+        } base_svar;
+
         struct{
             struct statement *base_var;  // a.b --> a
             struct statement *child_var;  // a.b --> b
@@ -356,8 +362,6 @@ typedef struct statement{
 // ------------------------- result value
 
 typedef struct GWARF_result{
-    GWARF_value value;
-    GWARF_value *father;  // a.b --> a
     enum{
         return_def=1,
         statement_end,
@@ -371,8 +375,11 @@ typedef struct GWARF_result{
         code_rewent,
         error,
     } u;  // the result type[from where]
+    GWARF_value value;
+    GWARF_value *father;  // a.b --> a
     int return_times;  // return用
     char *error_info;  // 输出的错误信息
+    char *base_name;  // 返回名字
 } GWARF_result;
 
 // ------------------------- default_var [记录默认变量[层]] 用于default语句

+ 1 - 0
paser/lexical.c

@@ -171,6 +171,7 @@ int paser(int *index, p_status *status){
         match_text(p, global_paser[POINT_PASER], ".");
         match_text(p, global_paser[IMPORT_PASER], "import");
         match_text(p, global_paser[INCLUDE_PASER], "include");
+        match_text(p, global_paser[SVAR_PASER], "$");
 
         *index = check_list(global_paser, p, status);  // 检查解析结果
 

+ 1 - 0
paser/paser.h

@@ -9,6 +9,7 @@ typedef struct p_status
 {
     bool is_parameter;  // parameter模式不匹配 = 
     bool is_func;  // func模式不匹配 ()
+    bool is_call;
     bool is_list;  // 不匹配参数的POW和name_value
     bool is_dict;  // 不匹配参数的MUL和非only_value选项以及用:取代=
     bool is_left;  // 是否为最左边的公式

+ 72 - 20
paser/syntax.c

@@ -44,6 +44,7 @@ void lambda_(p_status *status, token_node *list);
 void attribute(p_status *status, token_node *list);
 void import_include(p_status *status, token_node *list);
 void chose_exp_(p_status *status, token_node *list);
+void svar_token(p_status *status, token_node *list);
 void paser_error(char *text);
 
 /*
@@ -629,6 +630,7 @@ void formal_parameter(p_status *status, token_node *list){  // 因试分解
         p_status new_status;
         new_status = *status;
         new_status.is_parameter = true;
+        printf("status->is_parameter = %d\n", new_status.is_parameter);
         get_base_token(&new_status, list, top_exp, next);
         new_status.is_parameter = false;
         if(next.type != NON_top_exp){
@@ -648,9 +650,9 @@ void formal_parameter(p_status *status, token_node *list){  // 因试分解
             }
             p_status new_status;
             new_status = *status;
-            if(status->is_peq) new_status.is_parameter = false;
-            get_right_token(&new_status, list, top_exp, value_token);
             if(status->is_peq) new_status.is_parameter = true;
+            get_right_token(&new_status, list, top_exp, value_token);
+            if(status->is_peq) new_status.is_parameter = false;
             if(value_token.type != NON_top_exp){
                 paser_error("Don't get a top_exp");
                 return;
@@ -1326,7 +1328,7 @@ void hide_list(p_status *status, token_node *list){
         back_one_token(list, exp);
         return;
     }
-    if(status->is_func + status->is_left + status->is_parameter + status->is_for + status->is_list + status->is_dict == 0){  // 几个会用到逗号的选项先排除
+    if(status->is_func + status->is_left + status->is_parameter + status->is_for + status->is_list + status->is_dict + status->is_call == 0){  // 几个会用到逗号的选项先排除
         token comma;
         get_pop_token(status,list,comma);
         if(comma.type == COMMA_PASER){
@@ -1603,12 +1605,12 @@ void bool_not(p_status *status, token_node *list){
 }
 
 void compare(p_status *status, token_node *list){  // 多项式
-    fprintf(status_log, "[info][grammar]  mode status: polynomial\n");
+    fprintf(status_log, "[info][grammar]  mode status: compare\n");
     token left, right, symbol, new_token;
 
     left = pop_node(list);  // 先弹出一个token   检查token的类型:区分是模式1,还是模式2/3
     if(left.type == NON_compare){  // 模式2/3
-        fprintf(status_log, "[info][grammar]  (polynomial)reduce right\n");
+        fprintf(status_log, "[info][grammar]  (compare)reduce right\n");
         get_pop_token(status, list, symbol);
         if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
            symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER){  // 模式2/3
@@ -1647,14 +1649,14 @@ void compare(p_status *status, token_node *list){  // 多项式
             return compare(status, list);  // 回调自己
         }
         else{  // 递归跳出
-            fprintf(status_log, "[info][grammar]  (polynomial)out\n");
+            fprintf(status_log, "[info][grammar]  (compare)out\n");
             back_one_token(list, left);
             back_again(list, symbol);
             return;
         }
     }
     else{  // 模式1
-        fprintf(status_log, "[info][grammar]  (polynomial)back one token to (factor)\n");
+        fprintf(status_log, "[info][grammar]  (compare)back one token to (bit_notor)\n");
         back_one_token(list, left);
         get_base_token(status, list, bit_notor, new_token);
         if(new_token.type != NON_bit_notor){
@@ -1672,12 +1674,12 @@ bit_notor : bit_or
           | bit_notor BITOR bit_or
 */
 void bit_notor(p_status *status, token_node *list){  // 因试分解
-    fprintf(status_log, "[info][grammar]  mode status: bit_or\n");
+    fprintf(status_log, "[info][grammar]  mode status: bit_notor\n");
     token left, right, symbol, new_token;
 
     left = pop_node(list);  // 先弹出一个token   检查token的类型:区分是模式1,还是模式2/3
     if(left.type == NON_bit_notor){  // 模式2/3
-        fprintf(status_log, "[info][grammar]  (bit_or)reduce right\n");
+        fprintf(status_log, "[info][grammar]  (bit_notor)reduce right\n");
         get_pop_token(status, list, symbol);
 
         if(symbol.type == BITNOTOR_PASER){  // 模式2/3
@@ -1708,7 +1710,7 @@ void bit_notor(p_status *status, token_node *list){  // 因试分解
         }
     }
     else{  // 模式1
-        fprintf(status_log, "[info][grammar]  (bit_or)back one token to (bit_and)\n");
+        fprintf(status_log, "[info][grammar]  (bit_notor)back one token to (bit_and)\n");
         back_one_token(list, left);
         get_base_token(status, list, bit_or, new_token);
         if(new_token.type != NON_bit_or){
@@ -1835,12 +1837,12 @@ bit_move : power
          | bit_move BITLEFT factor
 */
 void bit_move(p_status *status, token_node *list){  // 因试分解
-    fprintf(status_log, "[info][grammar]  mode status: factor\n");
+    fprintf(status_log, "[info][grammar]  mode status: bit_move\n");
     token left, right, symbol, new_token;
 
     left = pop_node(list);  // 先弹出一个token   检查token的类型:区分是模式1,还是模式2/3
     if(left.type == NON_bit_move){  // 模式2/3
-        fprintf(status_log, "[info][grammar]  (factor)reduce right\n");
+        fprintf(status_log, "[info][grammar]  (bit_move)reduce right\n");
         get_pop_token(status, list, symbol);
 
         if(symbol.type == BITRIGHT_PASER || symbol.type == BITLEFT_PASER){  // 模式2/3
@@ -1875,7 +1877,7 @@ void bit_move(p_status *status, token_node *list){  // 因试分解
         }
     }
     else{  // 模式1
-        fprintf(status_log, "[info][grammar]  (bit_move)back one token to (factor)\n");
+        fprintf(status_log, "[info][grammar]  (bit_move)back one token to (polynomial)\n");
         back_one_token(list, left);
         get_base_token(status, list, polynomial, new_token);
         if(new_token.type != NON_polynomial){
@@ -2233,7 +2235,7 @@ attribute : bit_or
           | attribute POINT bit_or
 */
 void attribute(p_status *status, token_node *list){  // 因试分解
-    fprintf(status_log, "[info][grammar]  mode status: bit_or\n");
+    fprintf(status_log, "[info][grammar]  mode status: attribute\n");
     token left, right, symbol, new_token;
 
     left = pop_node(list);  // 先弹出一个token   检查token的类型:区分是模式1,还是模式2/3
@@ -2388,11 +2390,13 @@ void call_back_(p_status *status, token_node *list){  // 因试分解
         get_pop_token(status, list, symbol);
 
         if(symbol.type == LB_PASER && !status->is_func){
-
             get_pop_token(status, list, rb_t);
             if(rb_t.type != RB_PASER){  // 带参数
                 back_again(list, rb_t);
-                get_right_token(status,list,formal_parameter,parameter_t);
+                p_status new_status = *status;  // 继承file_p等值
+                reset_status(new_status);  // 不会影响 *staus
+                new_status.is_call = true;  // 括号内忽略回车
+                get_right_token(&new_status,list,formal_parameter,parameter_t);
                 if(parameter_t.type != NON_parameter){
                     paser_error("Don't get formal_parameter");
                 }
@@ -2487,6 +2491,17 @@ void element(p_status *status, token_node *list){  // 数字归约
         add_node(list, new_token);
         return;
     }
+    else if(gett.type == SVAR_PASER){  // a
+        back_one_token(list, gett);
+        get_base_token(status, list, svar_token, new_token);
+        if(new_token.type != NON_svar){
+            back_one_token(list, new_token);  // 往回[不匹配类型]
+            return;
+        }
+        new_token.type = NON_element;
+        add_node(list, new_token);
+        return;
+    }
     else if(gett.type == LP_PASER){  // dict
         back_one_token(list, gett);
         p_status new_status = *status;  // 继承file_p等值
@@ -2532,6 +2547,15 @@ void element(p_status *status, token_node *list){  // 数字归约
                 new_token.data.statement_value->code.base_var.from = exp_token.data.statement_value;
                 goto back;
             }
+            else if(tmp_var.type == SVAR_PASER){  // a
+                back_one_token(list, tmp_var);
+                get_base_token(status, list, svar_token, new_token);
+                if(new_token.type != NON_svar){
+                    paser_error("Don't get svar");
+                }
+                new_token.data.statement_value->code.base_svar.from = exp_token.data.statement_value;
+                goto back;
+            }
             else{
                 // back again连用的时候要倒过来使用
                 /* 原因:
@@ -2692,6 +2716,38 @@ void dict_(p_status *status, token_node *list){  // 数字归约
     }
 }
 
+void svar_token(p_status *status, token_node *list){  // 数字归约
+    fprintf(status_log, "[info][grammar]  mode status: svar_token\n");
+    token gett, var_t, new_token;
+
+    gett = pop_node(list);  // 取得一个token
+    if(gett.type == SVAR_PASER){  // var类型
+        new_token.type = NON_svar;
+
+        get_right_token(status, list, element, var_t);
+        if(var_t.type != NON_element){
+            paser_error("Don't get element[1]");
+        }
+
+        statement *code_tmp =  make_statement();
+        code_tmp->type = base_svar;
+        code_tmp->code.base_svar.var = var_t.data.statement_value;
+        code_tmp->code.base_svar.from = NULL;
+        
+        new_token.data.statement_value = code_tmp;
+        new_token.data_type = statement_value;
+
+        fprintf(status_log, "[info][grammar]  (svar_token)out\n");
+        add_node(list, new_token);  // 压入节点
+        return;
+    }
+    else{  // 不是期望值
+        fprintf(status_log, "[info][grammar]  (svar_token)back one token\n");
+        back_one_token(list, gett);
+        return;
+    }
+}
+
 /*
 var_token : VAR
 */
@@ -2703,10 +2759,6 @@ void var_token(p_status *status, token_node *list){  // 数字归约
     if(gett.type == VAR_PASER){  // var类型
         new_token.type = NON_base_var;
 
-        GWARF_value tmp_value;
-        tmp_value.type = INT_value;
-        tmp_value.value.int_value = atoi(gett.data.text);
-
         statement *code_tmp =  make_statement();
         code_tmp->type = base_var;
         code_tmp->code.base_var.var_name = gett.data.text;

+ 4 - 1
paser/token.h

@@ -3,7 +3,7 @@
 
 #include "../inter/interpreter.h"
 
-#define MAX_PASER_SIZE 84
+#define MAX_PASER_SIZE 85
 #define INT_PASER 0
 #define DOUBLE_PASER 1
 #define ENTER_PASER 2
@@ -88,6 +88,7 @@
 #define IMPORT_PASER 81
 #define INCLUDE_PASER 82
 #define COMMENT_PASER 83
+#define SVAR_PASER 84
 
 // 获取并返回一个token
 #define get_pop_token(status,list,new_token) \
@@ -220,6 +221,7 @@ typedef enum token_type
     FSUB = FSUB_PASER,
     LAMBDA = LAMBDA_PASER,
     POINT = POINT_PASER,
+    SVAR = SVAR_PASER,
 
     // 特殊符号
     BAD_token = -2,
@@ -270,6 +272,7 @@ typedef enum token_type
     NON_point = -46,
     NON_import = -47,
     NON_chose = -48,
+    NON_svar = -49,
 } token_type;
 
 typedef union token_data