Browse Source

初步实现dict

SongZihuan 5 years ago
parent
commit
deb16864c2
8 changed files with 732 additions and 367 deletions
  1. BIN
      gwarf
  2. 1 0
      gwarf.c
  3. 294 2
      inter/cfunc.c
  4. 5 0
      inter/interpreter.c
  5. 27 1
      inter/interpreter.h
  6. 0 1
      inter/var.c
  7. 17 0
      paser/gwarf_yacc.y
  8. 388 363
      paser/y.tab.c

BIN
gwarf


+ 1 - 0
gwarf.c

@@ -38,6 +38,7 @@ void login(var_list *the_var){
     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_BaseException = BaseException_login_official(the_var, BaseException_official_func, tmp_object->the_var);

+ 294 - 2
inter/cfunc.c

@@ -84,6 +84,12 @@ GWARF_value to_object(GWARF_value value, var_list *the_var){  // 把GWARF_value
             func_result.value = tmp->value;
         }
     }
+    else if(value.type == DICT_value){
+        tmp = find_var(the_var, 0, "dict");
+        if(tmp != NULL){
+            func_result.value = tmp->value;
+        }
+    }
     else{
         return value;
     }
@@ -1824,12 +1830,34 @@ GWARF_result list_official_func(func *the_func, parameter *tmp_s, var_list *the_
         default:
             break;
     }
-    if(the_func->official_func == __slice__func){
-    }
     return_result: return return_value;
 }
 
 
+GWARF_value to_dict(GWARF_value value, var_list *the_var){
+    if((value.type == DICT_value)){
+        return value;  // 直接返回数据
+    }
+
+    GWARF_value return_number;
+    return_number.type = DICT_value;
+
+    if(value.type == OBJECT_value){  // 调用__value__方法
+        return_number = to_list(get__value__(&value, the_var).value, the_var);  // 递归
+    }
+    else{
+        // 生成一个空的DICT
+        return_number.type = DICT_value;
+        return_number.value.dict_value = malloc(sizeof(the_dict));
+        return_number.value.dict_value->index = 0;
+        return_number.value.dict_value->dict_value = make_hash_var();
+        return_number.value.dict_value->name_list = malloc(sizeof(dict_key));
+        return_number.value.dict_value->name_list->key = "";
+        return_number.value.dict_value->name_list->next = NULL;
+    }
+    return return_number;
+}
+
 GWARF_value to_list(GWARF_value value, var_list *the_var){
     if((value.type == LIST_value)){
         return value;  // 直接返回数据
@@ -1880,6 +1908,270 @@ GWARF_value parameter_to_list(parameter *tmp_s, var_list *the_var){  // 把param
     return return_list;
 }
 
+GWARF_value parameter_to_dict(parameter *tmp_s, var_list *the_var){  // 把parameter转换为list
+    GWARF_value return_dict;
+    return_dict.type = DICT_value;
+    return_dict.value.dict_value = malloc(sizeof(the_dict));
+    return_dict.value.dict_value->index = 0;
+    return_dict.value.dict_value->dict_value = make_hash_var();
+    return_dict.value.dict_value->name_list = malloc(sizeof(dict_key));
+    return_dict.value.dict_value->name_list->key = "";
+    return_dict.value.dict_value->name_list->next = NULL;
+    
+    int index = 0;
+    GWARF_result result_tmp;
+    while(1){
+        if(tmp_s == NULL){
+            break;
+        }
+        if(tmp_s->type != name_value){
+            goto next;  // 跳过这一个
+        }
+        result_tmp = traverse(tmp_s->u.value, the_var, false);  // 不需要取__value__
+        if(is_error(&result_tmp)){  // Name Error错误
+            goto next;  // 直接指向下一个
+        }
+        else if(is_space(&result_tmp)){
+            goto next;
+        }
+
+        int status = login_node(tmp_s->u.name, result_tmp.value, return_dict.value.dict_value->dict_value);  // 插入
+        dict_key *tmp_dict_name = return_dict.value.dict_value->name_list;
+        while (1){  // 迭代
+            if(!strcmp(tmp_dict_name->key, tmp_s->u.name)){  // 已经存在
+                break;
+            }
+            else if(tmp_dict_name->next == NULL){
+                tmp_dict_name->next = malloc(sizeof(dict_key));
+                tmp_dict_name->next->next = NULL;
+                tmp_dict_name->next->key = malloc(sizeof(tmp_s->u.name));
+                strcpy(tmp_dict_name->next->key, tmp_s->u.name);  // 复制key
+                index += 1;  // 不存在才+1
+                break;
+            }
+            tmp_dict_name = tmp_dict_name->next;
+        }
+        next: tmp_s = tmp_s->next;  // 指向下一个
+    }
+    return_dict.value.dict_value->index = index;
+    return return_dict;
+}
+
+class_object *dict_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("dict", class_value, the_var, 0);  // 注册class 的 位置
+    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 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 dict_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 __init__func:{  // printf something
+            if(tmp_s == NULL){  // 生成空列表
+                GWARF_result tmp_result;
+                GWARF_value dict_tmp;
+                dict_tmp.type = DICT_value;
+                dict_tmp.value.dict_value = malloc(sizeof(the_dict));
+                dict_tmp.value.dict_value->index = 0;
+                dict_tmp.value.dict_value->dict_value = make_hash_var();
+                dict_tmp.value.dict_value->name_list = malloc(sizeof(dict_key));
+                dict_tmp.value.dict_value->name_list->key = "";
+                dict_tmp.value.dict_value->name_list->next = NULL;
+                tmp_result.value = dict_tmp;
+                assignment_func("value", tmp_result, login_var, 0);  // 注册到self
+                return_value.u = statement_end;  // __init__没有return
+            }
+            else{
+                GWARF_result tmp, tmp_result = traverse(tmp_s->u.value, out_var, false);
+                if(is_error(&tmp_result)){  // Name Error错误
+                    return_value = tmp_result;
+                    goto return_result;
+                }
+                else if(is_space(&tmp_result)){
+                    return_value = tmp_result;
+                    goto return_result;
+                }
+                tmp.value = to_dict(tmp_result.value, out_var);  // 只有一个参数[要针对不同数据类型对此处作出处理]
+                assignment_func("value", tmp, login_var, 0);  // 注册到self
+                return_value.u = statement_end;  // __init__没有return
+            }
+
+            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
+            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;
+            if(tmp == NULL){
+                iter_index = 0;
+            }
+            else{
+                iter_index = to_int(tmp->value, out_var).value.int_value;
+            }
+
+            tmp = find_var(login_var, 0, "value");
+            len = tmp->value.value.dict_value->index;
+            if(iter_index >= len){
+                return_value = to_error("Max Iter", "IterException", the_var);
+            }
+            else{
+                dict_key *tmp_dict_key = tmp->value.value.dict_value->name_list->next;  // 忽略第一个点
+                for(int i = 0;i < iter_index;i += 1){
+                    if(tmp_dict_key == NULL){  // to_error
+                        return_value = to_error("Max Iter", "IterException", the_var);
+                        goto next_break;  // 
+                    }
+                    tmp_dict_key = tmp_dict_key->next;
+                }
+
+                var *find_var = find_node(tmp_dict_key->key, tmp->value.value.dict_value->dict_value);
+                if(find_var == NULL){  // not found
+                    printf("key = '%s'\n", tmp_dict_key->key);
+                    return_value = to_error("Dict key Not Found", "NameException", out_var);
+                }
+                else{
+                    return_value.value = find_var->value;
+                    GWARF_result iter_value;
+                    iter_value.value.type = INT_value;
+                    iter_value.value.value.int_value = iter_index + 1;
+                    assignment_func("iter_value", iter_value, login_var, 0);  // 注册到self
+                }
+            }
+
+            next_break: break;
+        }
+        case __down__func:{  // return index
+            var *tmp = find_var(login_var, 0, "value");
+            if(tmp != NULL){
+                GWARF_result get_value, tmp_result = traverse(tmp_s->u.value, out_var, false);
+                if(is_error(&tmp_result)){  // Name Error错误
+                    return_value = tmp_result;
+                    goto break_down;
+                }
+                else if(is_space(&tmp_result)){
+                    return_value = tmp_result;
+                    goto break_down;
+                }
+                GWARF_value base_the_var = tmp_result.value;  // 只有一个参数
+                get_value = get__value__(&base_the_var, the_var);
+                get_value.value = to_str(get_value.value, out_var);
+                
+                var *find_var = find_node(get_value.value.value.string, tmp->value.value.dict_value->dict_value);
+                if(find_var == NULL){  // not found
+                    return_value = to_error("Dict key Not Found", "NameException", out_var);
+                }
+                else{
+                    return_value.value = find_var->value;
+                }
+            }
+            else{
+                return_value.value.type = NULL_value;
+                return_value.value.value.int_value = 0;
+            }
+            break_down: break;
+        }
+        case __set__func:{  // return index
+            var *tmp = find_var(login_var, 0, "value");
+            if(tmp != NULL){
+                GWARF_result get_value, tmp_result = traverse(tmp_s->u.value, out_var, false);
+                if(is_error(&tmp_result)){  // Name Error错误
+                    return_value = tmp_result;
+                    goto return_result;
+                }
+                else if(is_space(&tmp_result)){
+                    return_value = tmp_result;
+                    goto return_result;
+                }
+                GWARF_value base_the_var = tmp_result.value;  // 只有一个参数
+                get_value = get__value__(&base_the_var, the_var);
+                get_value.value = to_str(get_value.value, out_var);
+
+                tmp_s = tmp_s->next;
+                GWARF_result new_value = traverse(tmp_s->u.value, out_var, false);
+                if(is_error(&new_value)){  // Name Error错误
+                    return_value = new_value;
+                    goto return_result;
+                }
+                else if(is_space(&new_value)){
+                    return_value = new_value;
+                    goto return_result;
+                }
+                login_node(get_value.value.value.string, new_value.value, tmp->value.value.dict_value->dict_value);  // 插入
+                dict_key *tmp_dict_name = tmp->value.value.dict_value->name_list;
+                while (1){  // 迭代
+                    if(!strcmp(tmp_dict_name->key, get_value.value.value.string)){  // 已经存在
+                        break;
+                    }
+                    else if(tmp_dict_name->next == NULL){
+                        tmp_dict_name->next = malloc(sizeof(dict_key));
+                        tmp_dict_name->next->next = NULL;
+                        tmp_dict_name->next->key = malloc(sizeof(get_value.value.value.string));
+                        strcpy(tmp_dict_name->next->key, get_value.value.value.string);  // 复制key
+                        tmp->value.value.dict_value->index += 1;  // 不存在才+1
+                        break;
+                    }
+                    tmp_dict_name = tmp_dict_name->next;
+                }
+                
+            }
+            return_value.value.type = NULL_value;
+            return_value.value.value.int_value = 0;
+            break;
+        }
+        default:
+            break;
+    }
+    return_result: return return_value;
+}
+
 GWARF_result get__assignment__(GWARF_value *base_the_var, var_list *the_var){  // 获取__assignment__
     GWARF_result tmp = run_func_core(base_the_var, the_var, "__assignment__", true);
     if(is_error(&tmp) || is_space(&tmp)){

+ 5 - 0
inter/interpreter.c

@@ -179,6 +179,9 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
         case base_list:  // get value[所有字面量均为这个表达式]
             return_value.value = parameter_to_list(the_statement->code.base_list.value, the_var);  // code
             break;
+        case base_dict:  // get value[所有字面量均为这个表达式]
+            return_value.value = parameter_to_dict(the_statement->code.base_dict.value, the_var);  // code
+            break;
         case slice:{  // get value[所有字面量均为这个表达式]
             GWARF_result tmp_result = traverse((the_statement->code).slice.base_var, the_var, false), get;  // 把a[1:2:3]的a取出来
             if(is_error(&tmp_result)){  // Name Error错误
@@ -1767,6 +1770,8 @@ GWARF_result assignment_statement(statement *the_statement, var_list *the_var, v
                 parameter *tmp = pack_value_parameter(child_value.value);
                 tmp->next = pack_value_parameter(right_result.value);
                 value = call_back_core(get, the_var, tmp);
+            }
+            else{
                 goto the_else;
             }
         }

+ 27 - 1
inter/interpreter.h

@@ -1,6 +1,6 @@
 #ifndef _INTERPRETER_H
 #define _INTERPRETER_H
-#define MAX_SIZE (1024 ^ 2)
+#define MAX_SIZE (1024)
 
 #define malloc(size) safe_malloc(size)
 #define free(p) p=safe_free(p)
@@ -25,6 +25,7 @@ typedef enum{
     CLASS_value,  // 对象 [7]
     OBJECT_value,  // 实例 [8]
     LIST_value,  // 列表类型 [只允许系统使用] [9]
+    DICT_value,  // 字典类型 [只允许系统使用] [10]
 } GWARF_value_type;
 
 // all value is GWARF_value
@@ -40,6 +41,7 @@ typedef struct GWARF_value{
         struct class_object *class_value;
         struct the_object *object_value;
         struct the_list *list_value;
+        struct the_dict *dict_value;
     } value;
 } GWARF_value;
 
@@ -84,6 +86,7 @@ typedef struct statement{
         base_var,  // return var value by name
         base_value,  // return an GWARF_value
         base_list,  // return an GWARF_value->LIST_value
+        base_dict,  // return an GWARF_value->DICT_value
         while_cycle,  // while
         for_cycle,
         if_branch,  // if
@@ -209,6 +212,10 @@ typedef struct statement{
             parameter *value;  // [1,2,3,4] -> to_list
         } base_list;
 
+        struct{
+            parameter *value;  // [1,2,3,4] -> to_list
+        } base_dict;
+
         struct{
             struct statement *base_var;  // a[1:2:3] -> a
             parameter *value;  // a[1:2:3] -> 1 2 3
@@ -471,6 +478,19 @@ typedef struct the_list  // 列表类型
     int index;  // the max index
 } the_list;
 
+typedef struct the_dict  // 列表类型
+{
+    struct hash_var *dict_value;  // 列表类型
+    int index;  // the max index
+    struct dict_key *name_list;  // 值插入的顺序
+} the_dict;
+
+typedef struct dict_key  // dict的key类型
+{
+    char *key;
+    struct dict_key *next;
+} dict_key;
+
 // 函数声明
 GWARF_result operation_func(statement *, var_list *, var_list *);
 GWARF_result while_func(statement *, var_list *);
@@ -517,7 +537,9 @@ GWARF_value to_double(GWARF_value value, var_list *the_var);
 GWARF_value to_str(GWARF_value value, var_list *the_var);
 GWARF_value to_bool_(GWARF_value value, var_list *the_var);
 GWARF_value to_list(GWARF_value value, var_list *the_var);
+GWARF_value to_dict(GWARF_value value, var_list *the_var);
 GWARF_value parameter_to_list(parameter *tmp_s, var_list *the_var);
+GWARF_value parameter_to_dict(parameter *tmp_s, var_list *the_var);
 bool to_bool(GWARF_value);
 
 GWARF_result get__value__(GWARF_value *, var_list *);
@@ -568,6 +590,10 @@ GWARF_result bool_official_func(func *the_func, parameter *tmp_s, var_list *the_
 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);
 
+// dict内置类
+class_object *dict_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
+GWARF_result dict_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
+
 // 错误内置类
 class_object *BaseException_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
 GWARF_result BaseException_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);

+ 0 - 1
inter/var.c

@@ -110,7 +110,6 @@ unsigned int time33(char *key){
 int login_node(char *name, GWARF_value value, hash_var *the_hash_var){
     unsigned int index = time33(name);
     var *base_node = the_hash_var->hash[index];  // 根据下标拿base节点
-
     if(base_node == NULL){  // 生成基本节点
         the_hash_var->hash[index] = make_var();
         base_node = the_hash_var->hash[index];

+ 17 - 0
paser/gwarf_yacc.y

@@ -706,6 +706,23 @@ base_value
 
         $$ = code_tmp;
     }
+    | LP arguments RP
+    {
+        parameter *tmp;
+        tmp = malloc(sizeof(parameter));  // get an address for base var
+        tmp->next = NULL;
+        statement *statement_tmp = malloc(sizeof(statement));
+        statement_tmp->type = base_dict;
+        statement_tmp->code.base_dict.value = $2;
+        tmp->u.value = statement_tmp;
+
+        statement *code_tmp =  make_statement();
+        code_tmp->type = call;
+        code_tmp->code.call.func = pack_call_name("dict", NULL);
+        code_tmp->code.call.parameter_list = tmp;
+
+        $$ = code_tmp;
+    }
     | NULL_token
     {
         // NULL代表空值,是GWARF_value

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


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