浏览代码

list生成和读取

SongZihuan 5 年之前
父节点
当前提交
dcaf692c21
共有 7 个文件被更改,包括 686 次插入259 次删除
  1. 二进制
      gwarf
  2. 2 1
      gwarf.c
  3. 202 0
      inter/cfunc.c
  4. 139 6
      inter/interpreter.c
  5. 30 1
      inter/interpreter.h
  6. 25 0
      paser/gwarf_yacc.y
  7. 288 251
      paser/y.tab.c

二进制
gwarf


+ 2 - 1
gwarf.c

@@ -19,7 +19,7 @@ int main(){
     statement_base = make_statement_base(global_inter->global_code);
     GWARF_result father;
 
-    login_official(the_var, official_func);  // 注册官方函数
+    login_official(the_var, official_func);  // 注册内置函数
 
     class_object *tmp_object = object_login_official(the_var, object_official_func);  // 注册oobject
     class_object *tmp_gobject = gobject_login_official(the_var, gobject_official_func, tmp_object->the_var);  // 注册goobject
@@ -28,6 +28,7 @@ int main(){
     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
 
     parser("/home/songzihuan/test.gwf");
     printf("----start run----\n");

+ 202 - 0
inter/cfunc.c

@@ -1122,6 +1122,208 @@ 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){
+    // 创建对象[空对象]
+    puts("----set class----");
+    GWARF_result class_value;
+    class_object *class_tmp = malloc(sizeof(class_object));
+
+    class_tmp->the_var = make_var_base(make_var());  // make class var list
+    if(father_var_list != NULL){
+        append_by_var_list(class_tmp->the_var, father_var_list);  // 一切类都需要继承object类[包括set class如果tmp_s == NULL则需要继承object]
+    }
+    class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var));  // make class var list with out var
+    class_value.value.type = CLASS_value;
+    class_value.value.value.class_value = class_tmp;
+
+    assigment_func("list", class_value, the_var, 0);  // 注册class 的 位置
+    puts("----stop set class----");
+
+    // 注册函数
+    int a[][2] = {{2,1},{23,1},{24,1},{25,1}};
+    char *name[] = {"__init__", "__len__", "__down__", "__set__"};  //  __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 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 __init__func:{  // printf something
+            if(tmp_s == NULL){  // 生成空列表
+                GWARF_result tmp_result;
+                GWARF_value list_tmp;
+                list_tmp.type = LIST_value;
+                list_tmp.value.list_value = malloc(sizeof(the_list));
+                list_tmp.value.list_value->index = 0;
+                list_tmp.value.list_value->list_value = malloc((size_t)0);
+                tmp_result.value = list_tmp;
+                assigment_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(tmp_result.u == name_no_found){  // 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_list(tmp_result.value, out_var);  // 只有一个参数[要针对不同数据类型对此处作出处理]
+                assigment_func("value", tmp, login_var, 0);  // 注册到self
+                return_value.u = statement_end;  // __init__没有return
+            }
+            break;
+        }
+        case __len__func:{  // return index
+            var *tmp = find_var(login_var, 0, "value");
+            if(tmp != NULL){
+                return_value.value.type = INT_value;
+                return_value.value.value.int_value = (int)(sizeof(tmp->value.value.list_value->list_value) / sizeof(GWARF_value));  // 获取长度
+            }
+            else{
+                return_value.value.type = INT_value;
+                return_value.value.value.int_value = 0;
+            }
+            break;
+        }
+        case __down__func:{  // return index
+            var *tmp = find_var(login_var, 0, "value");
+            int len = (int)(sizeof(tmp->value.value.list_value->list_value) / sizeof(GWARF_value));
+            if(tmp != NULL){
+                GWARF_result get_value, tmp_result = traverse(tmp_s->u.value, out_var, false);
+                if(tmp_result.u == name_no_found){  // 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_int(get_value.value, out_var);
+
+                return_value.value = tmp->value.value.list_value->list_value[get_value.value.value.int_value];
+            }
+            else{
+                return_value.value.type = INT_value;
+                return_value.value.value.int_value = 0;
+            }
+            break;
+        }
+        case __set__func:{  // return index
+            var *tmp = find_var(login_var, 0, "value");
+            int len = (int)(sizeof(tmp->value.value.list_value->list_value) / sizeof(GWARF_value));
+            if(tmp != NULL){
+                GWARF_result get_value, tmp_result = traverse(tmp_s->u.value, out_var, false);
+                if(tmp_result.u == name_no_found){  // 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_int(get_value.value, out_var);
+
+                tmp_s = tmp_s->next;
+                GWARF_result new_value = traverse(tmp_s->u.value, out_var, false);
+                if(new_value.u == name_no_found){  // Name Error错误
+                    return_value = new_value;
+                    goto return_result;
+                }
+                else if(is_space(&new_value)){
+                    return_value = new_value;
+                    goto return_result;
+                }
+
+                tmp->value.value.list_value->list_value[get_value.value.value.int_value] = new_value.value;
+                tmp->value.value.list_value->index += 1;
+            }
+            else{
+                return_value.value.type = INT_value;
+                return_value.value.value.int_value = 0;
+            }
+            break;
+        }
+        default:
+            break;
+    }
+    return_result: return return_value;
+}
+
+
+GWARF_value to_list(GWARF_value value, var_list *the_var){
+    if((value.type == LIST_value)){
+        return value;  // 直接返回数据
+    }
+
+    GWARF_value return_number;
+    return_number.type = LIST_value;
+
+    if(value.type == OBJECT_value){  // 调用__value__方法
+        return_number = to_list(get__value__(&value, the_var).value, the_var);  // 递归
+    }
+    else{
+        return_number.value.list_value = malloc(sizeof(the_list));
+        return_number.value.list_value->index = 1;
+        return_number.value.list_value->list_value = malloc(sizeof(GWARF_value));
+        return_number.value.list_value->list_value[0] = value;  // 保存value
+    }
+    return return_number;
+}
+
+GWARF_value parameter_to_list(parameter *tmp_s, var_list *the_var){  // 把parameter转换为list
+    GWARF_value return_list;
+    return_list.type = LIST_value;
+    return_list.value.list_value = malloc(sizeof(the_list));
+    return_list.value.list_value->list_value = malloc(0);
+    int index = 0;
+    GWARF_result result_tmp;
+    while(1){
+        if(tmp_s == NULL){
+            break;
+        }
+        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;
+        }
+        index += 1;
+        return_list.value.list_value->list_value = realloc(return_list.value.list_value->list_value, sizeof(GWARF_value) * index);  // 申请新空间
+        return_list.value.list_value->list_value[index - 1] = result_tmp.value;  // 保存value
+        next: tmp_s = tmp_s->next;  // 指向下一个
+    }
+    return_list.value.list_value->index = index;
+    return return_list;
+}
+
+
 GWARF_result get__value__(GWARF_value *base_the_var, var_list *the_var){  // 用于计算的get__value__统一核心
     GWARF_result tmp = run_func(base_the_var, the_var, "__value__");
     if(tmp.u == name_no_found){

+ 139 - 6
inter/interpreter.c

@@ -143,6 +143,9 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             }
             // base_value返回字面量 -> 主要返回object类型,还会返回GWARF_value的其他类型供生成object类型
             break;
+        case base_list:  // get value[所有字面量均为这个表达式]
+            return_value.value = parameter_to_list(the_statement->code.base_list.value, the_var);  // code
+            break;
         case base_var:{    // 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_var.from == NULL){
@@ -198,7 +201,6 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             break;
         }
         case point:{
-            puts("----point----");
             GWARF_result tmp_result = traverse((the_statement->code).point.base_var, the_var, false);
             if(is_error(&tmp_result)){  // Name Error错误
                 // puts("STOP:: Name No Found!");
@@ -212,11 +214,9 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
 
             GWARF_value base_the_var = tmp_result.value;
             if(base_the_var.type == CLASS_value){  // is class so that can use "."
-                puts("func: point");
                 return_value = traverse((the_statement->code).point.child_var, base_the_var.value.class_value->the_var, false);
             }
             else if(base_the_var.type == OBJECT_value){
-                puts("func: point");
                 return_value = traverse((the_statement->code).point.child_var, base_the_var.value.object_value->the_var, false);
             }
             else{  // 其他类型
@@ -232,10 +232,60 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
 
             return_value.father = malloc(sizeof(return_value.father));  // 记录father的值
             *(return_value.father) = base_the_var;
-            puts("----stop point----");
             return_value.value = to_object(return_value.value, the_var);
             the_break: break;
         }
+        case down:{
+            GWARF_result tmp_result = traverse((the_statement->code).down.base_var, the_var, false), get;
+            if(is_error(&tmp_result)){  // Name Error错误
+                // puts("STOP:: Name No Found!");
+                return_value = tmp_result;
+                goto the_break;
+            }
+            else if(is_space(&tmp_result)){
+                return_value = tmp_result;
+                goto the_break;
+            }
+
+            GWARF_value base_the_var = tmp_result.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, "__down__");
+                if(tmp != NULL){
+                    get.value = tmp->value;
+                    get.father = &base_the_var;  // 设置father
+                    return_value = call_back_core(get, the_var, pack_value_parameter(child_value.value));
+                    goto the_break_down;
+                }
+            }
+            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, "__down__");
+                if(tmp != NULL){
+                    get.value = tmp->value;
+                    get.father = &base_the_var;  // 设置father
+                    return_value = call_back_core(get, the_var, pack_value_parameter(child_value.value));
+                    goto the_break_down;
+                }
+            }
+            else{  // 其他类型
+                goto the_break_down;
+            }
+
+            if(is_error(&return_value)){  // Name Error错误
+                // puts("STOP:: Name No Found!");
+                goto the_break_down;
+            }
+            else if(is_space(&return_value)){
+                goto the_break_down;
+            }
+
+            return_value.father = malloc(sizeof(return_value.father));  // 记录father的值
+            *(return_value.father) = base_the_var;
+            return_value.value = to_object(return_value.value, the_var);
+            the_break_down: break;
+        }
         case def:{
             GWARF_result func_value;
             func *func_tmp = malloc(sizeof(func));
@@ -1356,6 +1406,89 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
         result.u = statement_end;
         result.value = tmp;
     }
+    else if(get.value.type == OBJECT_value){  // 调用__call__方法
+        // 执行__init__
+        var *call_tmp = find_var(get.value.value.object_value->the_var, 0, "__call__");
+        if(call_tmp != NULL){  // 找到了__init__
+            func *func_ = call_tmp->value.value.func_value;
+            parameter *tmp_x = func_->parameter_list;
+            the_var = func_->the_var;
+            // tmp_x:形参,tmp_s:实参
+
+            // // printf("----address = %d----\n", the_var);
+            var *tmp = make_var();  // base_var
+            the_var = append_var_list(tmp, the_var);
+            // // printf("----new address = %d----\n", the_var);
+
+            if(func_->type == customize){  // 用户定义的方法
+                if(tmp_x == NULL){
+                    puts("No tmp_x");
+                    goto no_tmp_x_call;  // 无形参
+                }
+                GWARF_result father;
+                father.value.type = OBJECT_value;
+                father.value.value.object_value = get.value.value.object_value;
+                if(func_->is_class  == 1){
+                    assigment_func(tmp_x->u.name, father, the_var, 0);
+                    if (tmp_x->next == NULL){  // the last
+                        goto no_tmp_x_call;
+                    }
+                    tmp_x = tmp_x->next;  // get the next to iter
+                }
+                while(1){
+                    GWARF_result tmp = traverse(tmp_s->u.value, the_var, false);
+                    if(is_error(&tmp)){  // Name Error错误
+                        // puts("STOP:: Name No Found!");
+                        the_var = free_var_list(the_var);  // free the new var
+                        return tmp;
+                    }
+                    else if(is_space(&tmp)){
+                        the_var = free_var_list(the_var);  // free the new var
+                        return tmp;
+                    }
+                    assigment_func(tmp_x->u.name, tmp, the_var, 0);
+                    if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){  // the last
+                        break;
+                    }
+                    tmp_x = tmp_x->next;  // get the next to iter
+                    tmp_s = tmp_s->next;
+                }
+                no_tmp_x_call: 
+                puts("----start func----");
+                {
+                    GWARF_result tmp = traverse(func_->done, the_var, false);  // 执行func_value->done
+                    if(is_error(&tmp)){  // Name Error错误
+                        // puts("STOP:: Name No Found!");\
+                        the_var = free_var_list(the_var);  // free the new var
+                        return tmp;
+                    }
+                    else if(is_space(&tmp)){
+                        the_var = free_var_list(the_var);  // free the new var
+                        return tmp;
+                    }
+                }
+                puts("----stop start func----");
+            }
+            else{
+                GWARF_result tmp_get;
+                GWARF_value father;
+                father.type = OBJECT_value;
+                father.value.object_value = get.value.value.object_value;
+                tmp_get.father = &father;
+                GWARF_result tmp = func_->paser(func_, tmp_s, the_var, tmp_get, old_var_list);   // 返回值不记录
+                if(is_error(&tmp)){  // Name Error错误
+                    the_var = free_var_list(the_var);  // free the new var
+                    // puts("STOP:: Name No Found!");
+                    return tmp;
+                }
+                else if(is_space(&tmp)){
+                    the_var = free_var_list(the_var);  // free the new var
+                    return tmp;
+                }
+            }
+            the_var = free_var_list(the_var);  // free the new var
+        }
+    }
     if(result.u == return_def){
         if(result.return_times <= 0){
             result.u = statement_end;
@@ -2104,8 +2237,8 @@ GWARF_result traverse(statement *the_statement, var_list *the_var, bool new){  /
     GWARF_result result, result2;
     if(the_statement == NULL){
         result.u = statement_end;  // 正常设置[正常语句结束]
-        result.value.type = NUMBER_value;  // 默认设置
-        result.value.value.double_value = 0;  // 默认设置
+        result.value.type = NULL_value;  // 默认设置
+        result.value.value.int_value = 0;  // 默认设置
         goto return_back;
     }
     bool lock = false;

+ 30 - 1
inter/interpreter.h

@@ -21,6 +21,7 @@ typedef enum{
     FUNC_value,  // 函数
     CLASS_value,  // 对象
     OBJECT_value,  // 实例
+    LIST_value,  // 列表类型
 } GWARF_value_type;
 
 // all value is GWARF_value
@@ -35,6 +36,7 @@ typedef struct GWARF_value{
         struct func *func_value;
         struct class_object *class_value;
         struct the_object *object_value;
+        struct the_list *list_value;
     } value;
 } GWARF_value;
 
@@ -63,7 +65,8 @@ typedef struct statement{
         start=1,  // for base statement
         operation,  // such as + - * /
         base_var,  // return var value by name
-        base_value,  // return an number or number
+        base_value,  // return an GWARF_value
+        base_list,  // return an GWARF_value->LIST_value
         while_cycle,  // while
         for_cycle,
         if_branch,  // if
@@ -82,6 +85,7 @@ typedef struct statement{
         def,  // func
         call,  // func()
         point,  // a.b  注:返回变量同时返回the_var链表[func 用于回调]
+        down,  // a[b]  注:返回变量同时返回the_var链表[func 用于回调]
         return_code,
         set_class,  // class aaa; b = aaa() is ```call```
     } type;  // the statement type
@@ -136,10 +140,19 @@ typedef struct statement{
             struct statement *child_var;  // a.b --> b
         } point;
 
+        struct{
+            struct statement *base_var;  // a[b] --> a
+            struct statement *child_var;  // a[b] --> b
+        } down;
+
         struct{
             GWARF_value value;  // return value
         } base_value;
 
+        struct{
+            parameter *value;  // return value
+        } base_list;
+
         struct{
             struct statement *times;  // 层数
         } break_cycle;
@@ -302,6 +315,9 @@ typedef enum{
     __sqrtr__func = 20,
     __subr__func = 21,
     __divr__func = 22,
+    __len__func = 23,
+    __down__func = 24,
+    __set__func = 25,
 } official_func_type;
 
 typedef struct func{
@@ -324,6 +340,12 @@ typedef struct the_object{
     struct var_list *the_var;  // 记录class_object的实例  -- 相当与self
 } the_object;
 
+typedef struct the_list  // 列表类型
+{
+    GWARF_value *list_value;  // 列表类型
+    int index;  // the max index
+} the_list;
+
 // 函数声明
 GWARF_result operation_func(statement *, var_list *, var_list *);
 GWARF_result while_func(statement *, var_list *);
@@ -351,6 +373,8 @@ GWARF_value to_int(GWARF_value, var_list *the_var);
 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 parameter_to_list(parameter *tmp_s, var_list *the_var);
 bool to_bool(GWARF_value);
 
 GWARF_result get__value__(GWARF_value *, var_list *);
@@ -393,7 +417,12 @@ 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 *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);
+
 bool is_space(GWARF_result *);
+bool is_error(GWARF_result *tmp);
 
 if_list *make_base_if();
 if_list *make_if(statement *, statement *);

+ 25 - 0
paser/gwarf_yacc.y

@@ -327,6 +327,14 @@ element
         code_tmp->code.point.child_var = $3;
         $$ = code_tmp; 
     }
+    | element LI element RI
+    {
+        statement *code_tmp =  make_statement();
+        code_tmp->type = down;
+        code_tmp->code.down.base_var = $1;
+        code_tmp->code.down.child_var = $3;
+        $$ = code_tmp; 
+    }
     | LB top_exp RB
     {
         $$ = $2;
@@ -395,6 +403,23 @@ base_value
         code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
         $$ = code_tmp;
     }
+    | LI arguments RI
+    {
+        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_list;
+        statement_tmp->code.base_list.value = $2;
+        tmp->u.value = statement_tmp;
+
+        statement *code_tmp =  make_statement();
+        code_tmp->type = call;
+        code_tmp->code.call.func = pack_call_name("list", NULL);
+        code_tmp->code.call.parameter_list = tmp;
+
+        $$ = code_tmp;
+    }
     | NULL_token
     {
         // NULL代表空值,是GWARF_value

文件差异内容过多而无法显示
+ 288 - 251
paser/y.tab.c


部分文件因为文件数量过多而无法显示