Quellcode durchsuchen

新增内置对象text(demo)

SongZihuan vor 5 Jahren
Ursprung
Commit
042aeed5e6
4 geänderte Dateien mit 94 neuen und 33 gelöschten Zeilen
  1. BIN
      gwarf
  2. 2 0
      gwarf.c
  3. 8 4
      gwarf_interpreter/interprete.h
  4. 84 29
      gwarf_interpreter/interpreter.c

BIN
gwarf


+ 2 - 0
gwarf.c

@@ -6,7 +6,9 @@ int main(){
     global_inter = get_inter();  // 拿全局解释器[并声明全局变量]
     var_list *the_var = make_var_base(global_inter->global_var);
     statement_base = make_statement_base(global_inter->global_code);
+    GWARF_result father;
     login_official(the_var, official_func);  // 注册官方函数
+    text_login_official(the_var, text_official_func);
     parser("/home/songzihuan/test.gwf");
     printf("----start run----\n");
     traverse_global(global_inter->global_code, the_var);

+ 8 - 4
gwarf_interpreter/interprete.h

@@ -311,7 +311,7 @@ typedef enum{
 typedef struct func{
     func_type type;
     official_func_type official_func;
-    struct GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var);
+    struct GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var, GWARF_result);
     struct parameter *parameter_list;  // def parameter
     struct statement *done;  // def to do
     struct var_list *the_var;  // func会记录the_var,因为不同地方调用var如果var链不统一那就会很乱
@@ -349,8 +349,12 @@ parameter *add_parameter_value(statement *, parameter *);
 inter *global_inter;
 statement_list *statement_base;
 
-void login_official_func(int type, int is_class, var_list *the_var, char *name, GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var));
-void login_official(var_list *the_var, GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var));
+void login_official_func(int type, int is_class, var_list *the_var, char *name, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result));
+void login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result));
 
 // 内置函数
-GWARF_result official_func(func *, parameter *, var_list *);
+GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father);
+
+// text内置类
+void text_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result));
+GWARF_result text_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father);

+ 84 - 29
gwarf_interpreter/interpreter.c

@@ -19,7 +19,6 @@ GWARF_result if_func(if_list *, var_list *);
 GWARF_result for_func(statement *, var_list *);
 GWARF_result negative_func(GWARF_result, var_list *);
 GWARF_result call_back(statement *, var_list *);
-GWARF_result official_func(func *, parameter *, var_list *);
 
 int get_var_list_len(var_list *);
 var_list *copy_var_list(var_list *);
@@ -1215,8 +1214,8 @@ GWARF_result call_back(statement *the_statement, var_list *the_var){  // the fun
                 goto no_tmp_x;  // 无形参
             }
             GWARF_result father;
-            father.value = *(get.father);
             if(func_->is_class  == 1){
+                father.value = *(get.father);
                 assigment_func(tmp_x->u.name, father, the_var, 0);
                 if (tmp_x->next == NULL){  // the last
                     goto no_tmp_x;
@@ -1246,7 +1245,11 @@ GWARF_result call_back(statement *the_statement, var_list *the_var){  // the fun
             puts("----stop start func----");
         }
         else{
-            result = func_->paser(func_, tmp_s, the_var);
+            GWARF_result father;
+            if(func_->is_class  == 1){
+                father.value = *(get.father);
+            }
+            result = func_->paser(func_, tmp_s, the_var, father);
         }
         the_var = free_var_list(the_var);  // free the new var
     }
@@ -1271,33 +1274,42 @@ GWARF_result call_back(statement *the_statement, var_list *the_var){  // the fun
             the_var = append_var_list(tmp, the_var);
             printf("----new address = %d----\n", the_var);
 
-            if(tmp_x == NULL){
-                puts("No tmp_x");
-                goto no_tmp_x_init;  // 无形参
-            }
-            GWARF_result father;
-            father.value.type = OBJECT_value;
-            father.value.value.object_value = object_tmp;
-            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_init;
+            if(func_->type == customize){  // 用户定义的方法
+                if(tmp_x == NULL){
+                    puts("No tmp_x");
+                    goto no_tmp_x_init;  // 无形参
                 }
-                tmp_x = tmp_x->next;  // get the next to iter
+                GWARF_result father;
+                father.value.type = OBJECT_value;
+                father.value.value.object_value = object_tmp;
+                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_init;
+                    }
+                    tmp_x = tmp_x->next;  // get the next to iter
+                }
+                while(1){
+                    GWARF_result tmp = traverse(tmp_s->u.value, the_var, false);
+                    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_init: 
+                puts("----start func----");
+                traverse(func_->done, the_var, false);  // 执行func_value->done
+                puts("----stop start func----");
             }
-            while(1){
-                GWARF_result tmp = traverse(tmp_s->u.value, the_var, false);
-                assigment_func(tmp_x->u.name, tmp, the_var, 0);
-                if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){  // the last
-                    break;
+            else{
+                GWARF_result father;
+                if(func_->is_class  == 1){
+                    father.value = *(get.father);
                 }
-                tmp_x = tmp_x->next;  // get the next to iter
-                tmp_s = tmp_s->next;
+                result = func_->paser(func_, tmp_s, the_var, father);
             }
-            no_tmp_x_init: 
-            puts("----start func----");
-            traverse(func_->done, the_var, false);  // 执行func_value->done
-            puts("----stop start func----");
             the_var = free_var_list(the_var);  // free the new var
         }
         // 记录返回值
@@ -1840,7 +1852,7 @@ inter *get_inter(){
 }
 
 // ------official func
-void login_official_func(int type, int is_class, var_list *the_var, char *name, GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var)){  // 注册单个official func
+void login_official_func(int type, int is_class, var_list *the_var, char *name, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result)){  // 注册单个official func
     GWARF_result func_value;
     func *func_tmp = malloc(sizeof(func));
 
@@ -1857,7 +1869,7 @@ void login_official_func(int type, int is_class, var_list *the_var, char *name,
     assigment_func(name, func_value, the_var, 0);  // 注册函数到指定的位置
 }
 
-void login_official(var_list *the_var, GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var)){
+void login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result)){
     // {{official_func_type, is_class}}
     int a[][2] = {{1,0}};
     // {login_name}
@@ -1871,7 +1883,7 @@ void login_official(var_list *the_var, GWARF_result (*paser)(struct func *, stru
 
 
 // global 全局内置函数解析器
-GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var){
+GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father){
     GWARF_result return_value;
     switch (the_func->official_func)
     {
@@ -1926,3 +1938,46 @@ GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var){
     }
     return_result: return return_value;
 }
+
+
+// 注册test 对象
+void text_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result)){
+    // 创建对象[空对象]
+    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
+    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("text", class_value, the_var, 0);  // 注册class 的 位置
+    puts("----stop set class----");
+
+    // 注册函数
+    // {{official_func_type, is_class}}
+    int a[][2] = {{1,0}};
+    // {login_name}
+    char *name[] = {"print"};
+
+    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);
+    }
+}
+
+// text 全局内置函数解析器
+GWARF_result text_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father){
+    GWARF_result return_value;
+    switch (the_func->official_func)
+    {
+    case printf_func:{  // printf something
+        printf("text.print\n");  // 换行
+        break;
+    }
+    default:
+        break;
+    }
+    return_result: return return_value;
+}