Kaynağa Gözat

新增tuple

SongZihuan 5 yıl önce
ebeveyn
işleme
8bd16118cc
5 değiştirilmiş dosya ile 108 ekleme ve 44 silme
  1. 4 2
      gwarf.c
  2. 67 33
      inter/cfunc.c
  3. 3 0
      inter/interpreter.c
  4. 9 0
      inter/interpreter.h
  5. 25 9
      paser/syntax.c

+ 4 - 2
gwarf.c

@@ -37,8 +37,10 @@ void login(var_list *the_var){
     double_login_official(the_var, double_official_func, tmp_gobject->the_var);  // 注册double
     str_login_official(the_var, str_official_func, tmp_gobject->the_var);  // 注册str
     bool_login_official(the_var, bool_official_func, tmp_gobject->the_var);  // 注册bool
-    list_login_official(the_var, list_official_func, tmp_gobject->the_var);  // 注册list
-    dict_login_official(the_var, dict_official_func, tmp_gobject->the_var);  // 注册dict
+    
+    class_object *tmp_tuple = tuple_login_official(the_var, tuple_official_func, tmp_gobject->the_var);
+    list_login_official(the_var, list_official_func, tmp_tuple->the_var);  // 注册list
+    dict_login_official(the_var, dict_official_func, tmp_tuple->the_var);  // 注册dict
 
     // 注册错误类型
     class_object *tmp_BaseException = BaseException_login_official(the_var, BaseException_official_func, tmp_object->the_var);

+ 67 - 33
inter/cfunc.c

@@ -97,6 +97,21 @@ GWARF_value to_object(GWARF_value value, var_list *the_var){  // 把GWARF_value
 }
 
 
+GWARF_value to_tuple(GWARF_value value, var_list *the_var){  // 把GWARF_value封装成objct
+    GWARF_result func_result;
+    func_result.u = statement_end;
+    var *tmp;
+    tmp = find_var(the_var, 0, "tuple");
+    if(tmp != NULL){
+        func_result.value = tmp->value;
+    }
+    else{
+        return value;
+    }
+    return call_back_core(func_result, the_var, pack_value_parameter(value)).value;
+}
+
+
 GWARF_result get_object(parameter *tmp_s, char *name, var_list *the_var){  // 生成一个object
     GWARF_result func_result;
     func_result.u = statement_end;
@@ -1748,8 +1763,7 @@ GWARF_value to_bool_(GWARF_value value, var_list *the_var){
     return return_number;
 }
 
-
-class_object *list_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){
+class_object *tuple_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){
     // 创建对象[空对象]
     puts("----set class----");
     GWARF_result class_value;
@@ -1758,12 +1772,12 @@ class_object *list_login_official(var_list *the_var, GWARF_result (*paser)(func
     class_value.value.type = CLASS_value;
     class_value.value.value.class_value = class_tmp;
 
-    assignment_func("list", class_value, the_var, 0);  // 注册class 的 位置
+    assignment_func("tuple", class_value, the_var, 0);  // 注册class 的 位置
     puts("----stop set class----");
 
     // 注册函数
-    int a[][2] = {{2,1},{23,1},{24,1},{25,1},{26,1},{27,1},{28,1}};
-    char *name[] = {"__init__", "__len__", "__down__", "__set__", "__slice__", "__iter__", "__next__"};  //  __len__是获取长度,__down__是获取下值,__set__是设置值,__slice__是切片
+    int a[][2] = {{2,1},{23,1},{24,1},{26,1},{27,1},{28,1}};
+    char *name[] = {"__init__", "__len__", "__down__", "__slice__", "__iter__", "__next__"};  //  __len__是获取长度,__down__是获取下值,__slice__是切片
 
     int lenth = sizeof(a)/sizeof(a[0]);
     for(int i = 0;i < lenth;i+=1){
@@ -1772,7 +1786,7 @@ class_object *list_login_official(var_list *the_var, GWARF_result (*paser)(func
     return class_tmp;
 }
 
-GWARF_result list_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){  // out_var是外部环境, the_var是self内部环境
+GWARF_result tuple_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){  // out_var是外部环境, the_var是self内部环境
     GWARF_result return_value;
     var_list *login_var;
     return_value.u = return_def;
@@ -1939,6 +1953,51 @@ GWARF_result list_official_func(func *the_func, parameter *tmp_s, var_list *the_
             }
             break;
         }
+        default:
+            break;
+    }
+    return_result: return return_value;
+}
+
+class_object *list_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){
+    // 创建对象[空对象]
+    puts("----set class----");
+    GWARF_result class_value;
+    class_object *class_tmp = make_object(the_var, father_var_list);
+
+    class_value.value.type = CLASS_value;
+    class_value.value.value.class_value = class_tmp;
+
+    assignment_func("list", class_value, the_var, 0);  // 注册class 的 位置
+    puts("----stop set class----");
+
+    // 注册函数
+    int a[][2] = {{25,1}};
+    char *name[] = {"__set__"};  //  继承tuple
+
+    int lenth = sizeof(a)/sizeof(a[0]);
+    for(int i = 0;i < lenth;i+=1){
+        login_official_func(a[i][0], a[i][1], class_tmp->the_var, name[i], paser);
+    }
+    return class_tmp;
+}
+
+GWARF_result list_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){  // out_var是外部环境, the_var是self内部环境
+    GWARF_result return_value;
+    var_list *login_var;
+    return_value.u = return_def;
+    return_value.return_times = 0;
+    if(father.father->type == CLASS_value){  // is class so that can use "."
+        login_var = father.father->value.class_value->the_var;
+    }
+    else if(father.father->type == OBJECT_value){
+        login_var = father.father->value.object_value->the_var;
+    }
+    else{
+        printf("NO login, father type = %d\n", father.father->type);
+    }
+    switch (the_func->official_func)
+    {
         case __set__func:{  // return index
             var *tmp = find_var(login_var, 0, "value");
             if(tmp != NULL){
@@ -2124,9 +2183,7 @@ GWARF_value parameter_to_dict(parameter *tmp_s, var_list *the_var){  // 把param
             before->next = after;
             goto next;  // 跳过这一个
         }
-        puts("[tag 7]");
         if(tmp_s->type != name_value){
-            puts("[tag 4]");
             goto next;  // 跳过这一个
         }
         result_tmp = traverse(tmp_s->u.value, the_var, false);  // 不需要取__value__
@@ -2138,22 +2195,15 @@ GWARF_value parameter_to_dict(parameter *tmp_s, var_list *the_var){  // 把param
         }
         char *key;
         if(tmp_s->u.var->type == base_var){
-            puts("[tag 11.2]");
             size_t size = (size_t)(13 + strlen(tmp_s->u.var->code.base_var.var_name));
             key = (char *)malloc(size);
             snprintf(key, size, "str_%s", tmp_s->u.var->code.base_var.var_name);
-            puts("[tag 8.1]");
         }
         else{
-            puts("[tag 10.2]");
             GWARF_result key_tmp = traverse(tmp_s->u.var, the_var, 0);
             key = to_str_dict(key_tmp.value, the_var).value.string;
-            puts("[tag 8.2]");
         }
-        puts("[tag 9]");
-        printf("result_tmp.value.type = %d\n",result_tmp.value.type);
         login_node(key, result_tmp.value, return_dict.value.dict_value->dict_value);  // 插入
-        printf("NEW KEY = %s, type = %d\n", key, result_tmp.value.type);
         dict_key *tmp_dict_name = return_dict.value.dict_value->name_list;
         while (1){  // 迭代
             if(!strcmp(tmp_dict_name->key, key)){  // 已经存在
@@ -2172,7 +2222,6 @@ GWARF_value parameter_to_dict(parameter *tmp_s, var_list *the_var){  // 把param
         next: tmp_s = tmp_s->next;  // 指向下一个
     }
     return_dict.value.dict_value->index = index;
-    puts("[tag 8]");
     return return_dict;
 }
 
@@ -2189,8 +2238,8 @@ class_object *dict_login_official(var_list *the_var, GWARF_result (*paser)(func
     puts("----stop set class----");
 
     // 注册函数
-    int a[][2] = {{2,1},{23,1},{24,1},{25,1},{27,1},{28,1}};
-    char *name[] = {"__init__", "__len__", "__down__", "__set__", "__iter__", "__next__"};  //  __len__是获取长度,__down__是获取下值,__set__是设置值,__slice__是切片
+    int a[][2] = {{2,1},{24,1},{25,1},{28,1}};
+    char *name[] = {"__init__", "__down__", "__set__", "__next__"};  //  继承tuple
 
     int lenth = sizeof(a)/sizeof(a[0]);
     for(int i = 0;i < lenth;i+=1){
@@ -2260,21 +2309,6 @@ GWARF_result dict_official_func(func *the_func, parameter *tmp_s, var_list *the_
             assignment_func("iter_value", iter_value, login_var, 0);  // 注册到self
             break;
         }
-        case __len__func:{  // return index
-            var *tmp = find_var(login_var, 0, "value");
-            return_value.value.type = INT_value;
-            return_value.value.value.int_value = tmp->value.value.list_value->index;
-            break;
-        }
-        case __iter__func:{  // return self
-            GWARF_result iter_value;
-            iter_value.value.type = INT_value;
-            iter_value.value.value.int_value = 0;
-            assignment_func("iter_value", iter_value, login_var, 0);  // 注册到self
-
-            return_value.value = *(father.father);
-            break;
-        }
         case __next__func:{  // return index
             var *tmp = find_var(login_var, 0, "iter_value");
             int iter_index, len;

+ 3 - 0
inter/interpreter.c

@@ -179,6 +179,9 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             }
             // base_value返回字面量 -> 主要返回object类型,还会返回GWARF_value的其他类型供生成object类型
             break;
+        case base_tuple:  // get value[所有字面量均为这个表达式]
+            return_value.value = to_tuple(parameter_to_list(the_statement->code.base_tuple.value, the_var), the_var);  // code
+            break;
         case base_list:  // get value[所有字面量均为这个表达式]
             return_value.value = to_object(parameter_to_list(the_statement->code.base_list.value, the_var), the_var);  // code
             break;

+ 9 - 0
inter/interpreter.h

@@ -85,6 +85,7 @@ typedef struct statement{
         operation,  // such as + - * /
         base_var,  // return var value by name
         base_value,  // return an GWARF_value
+        base_tuple,
         base_list,  // return an GWARF_value->LIST_value
         base_dict,  // return an GWARF_value->DICT_value
         while_cycle,  // while
@@ -213,6 +214,10 @@ typedef struct statement{
             parameter *value;  // [1,2,3,4] -> to_list
         } base_list;
 
+        struct{
+            parameter *value;  // [1,2,3,4] -> to_tuple
+        } base_tuple;
+
         struct{
             parameter *right;  // 实参
             parameter *left;  // 形参
@@ -600,6 +605,10 @@ GWARF_result str_official_func(func *the_func, parameter *tmp_s, var_list *the_v
 class_object *bool_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
 GWARF_result bool_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
 
+// list内置类
+class_object *tuple_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
+GWARF_result tuple_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
+
 // list内置类
 class_object *list_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
 GWARF_result list_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);

+ 25 - 9
paser/syntax.c

@@ -1158,11 +1158,20 @@ void eq_number(p_status *status, token_node *list){  // 因试分解
                 
                 status->is_peq = true;
                 get_base_token(status,list,formal_parameter,p_left);
+                if(p_left.type != NON_parameter){
+                    paser_error("Dont get formal_parameter");
+                }
                 status->is_peq = false;
                 
                 get_pop_token(status, list, eq_t);
-                if(eq_t.type != EQ_PASER){
-                    paser_error("Don't get '='[1]");
+                if(eq_t.type != EQ_PASER){  // 变成hide_list
+                    back_again(list, eq_t);
+                    statement *code_tmp =  make_statement();
+                    code_tmp->type = base_tuple;
+                    code_tmp->code.base_tuple.value = p_left.data.parameter_list;
+                    new_token.data.statement_value = code_tmp;
+                    new_token.data_type = statement_value;
+                    goto return_back;
                 }
 
                 get_pop_token(status,list,tmp);
@@ -1197,6 +1206,7 @@ void eq_number(p_status *status, token_node *list){  // 因试分解
                 back_again(list, comma);  // 回退comma 使用back_again目的是让new_token在comma之前
             }
         }
+        return_back:
         new_token.type = NON_eq;
         add_node(list, new_token);
         return eq_number(status, list);  // 回调自己
@@ -1229,8 +1239,8 @@ void hide_list(p_status *status, token_node *list){
                 paser_error("Don't get formal_parameter");
             }
             statement *code_tmp =  make_statement();
-            code_tmp->type = base_list;
-            code_tmp->code.base_list.value = new_token.data.parameter_list;
+            code_tmp->type = base_tuple;
+            code_tmp->code.base_tuple.value = new_token.data.parameter_list;
             exp.data.statement_value = code_tmp;
             exp.data_type = statement_value;
         }
@@ -1868,7 +1878,8 @@ void factor(p_status *status, token_node *list){  // 因试分解
 
 /*
 negative : bit_not
-         | BITNOT bit_not
+         | SUB_PASER bit_not
+         | ADD_PASER bit_not
 */
 void negative(p_status *status, token_node *list){
     fprintf(status_log, "[info][grammar]  mode status: negative\n");
@@ -1893,13 +1904,18 @@ void negative(p_status *status, token_node *list){
         code_tmp->code.operation.right_exp = right.data.statement_value;
         new_token.data.statement_value = code_tmp;
         add_node(list, new_token);  // 压入节点[弹出3个压入1个]
-        return;  // 回调自己
+        return;
     }
     else{  // 模式1
         fprintf(status_log, "[info][grammar]  (negative)back one token to (bit_not)\n");
-        back_one_token(list, left);
-        get_base_token(status, list, bit_not, new_token);
-        if(new_token.type != NON_bit_not){
+        if(left.type != ADD_PASER){  // 取正运算符
+            back_one_token(list, left);
+            get_base_token(status, list, bit_not, new_token);
+        }
+        else{  // 需要safe_get_token
+            get_right_token(status, list, negative, new_token);
+        }
+        if(new_token.type != NON_bit_not && new_token.type != NON_negative){
             back_one_token(list, new_token);  // 往回[不匹配类型]
             return;
         }