Browse Source

内置函数调用优化

SongZihuan 5 years ago
parent
commit
8bbed76127
4 changed files with 46 additions and 38 deletions
  1. BIN
      gwarf
  2. 1 1
      gwarf.c
  3. 38 33
      gwarf_interpreter/interprete.h
  4. 7 4
      gwarf_interpreter/interpreter.c

BIN
gwarf


+ 1 - 1
gwarf.c

@@ -6,7 +6,7 @@ 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);
-    login_official(the_var);  // 注册官方函数
+    login_official(the_var, official_func);  // 注册官方函数
     parser("/home/songzihuan/test.gwf");
     printf("----start run----\n");
     traverse_global(global_inter->global_code, the_var);

+ 38 - 33
gwarf_interpreter/interprete.h

@@ -4,34 +4,6 @@
 #define bool int
 #define read_statement_list(the_statement, the_var) read_statement(the_statement, the_var, NULL)
 
-typedef enum{
-    customize = 1,  // func by user
-    official,  // func by gwarf
-} func_type;
-
-typedef enum{
-    printf_func = 1,  // print_func
-} official_func_type;
-
-typedef struct func{
-    func_type type;
-    official_func_type official_func;
-    struct parameter *parameter_list;  // def parameter
-    struct statement *done;  // def to do
-    struct var_list *the_var;  // func会记录the_var,因为不同地方调用var如果var链不统一那就会很乱
-    int is_class; 
-} func;
-
-typedef struct class_object{
-    struct var_list *out_var;  // 外部the_var list
-    struct var_list *the_var;  // 记录class_object的  -- 相当与cls
-} class_object;
-
-typedef struct the_object{
-    struct var_list *cls;  // 记录class_object的  -- 相当与cls
-    struct var_list *the_var;  // 记录class_object的实例  -- 相当与self
-} the_object;
-
 // the type of data(GWARF_value)
 typedef enum{
     NUMBER_value = 1,
@@ -53,9 +25,9 @@ typedef struct GWARF_value{
         int int_value;
         bool bool_value;
         char *string;  // STRING
-        func *func_value;
-        class_object *class_value;
-        the_object *object_value;
+        struct func *func_value;
+        struct class_object *class_value;
+        struct the_object *object_value;
     } value;
 } GWARF_value;
 
@@ -326,6 +298,36 @@ if_list *append_elif(if_list *, if_list *);
 GWARF_result traverse(statement *, var_list *, bool);
 GWARF_result traverse_global(statement *, var_list *);
 
+//------- class/object/func
+typedef enum{
+    customize = 1,  // func by user
+    official,  // func by gwarf
+} func_type;
+
+typedef enum{
+    printf_func = 1,  // print_func
+} official_func_type;
+
+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 parameter *parameter_list;  // def parameter
+    struct statement *done;  // def to do
+    struct var_list *the_var;  // func会记录the_var,因为不同地方调用var如果var链不统一那就会很乱
+    int is_class; 
+} func;
+
+typedef struct class_object{
+    struct var_list *out_var;  // 外部the_var list
+    struct var_list *the_var;  // 记录class_object的  -- 相当与cls
+} class_object;
+
+typedef struct the_object{
+    struct var_list *cls;  // 记录class_object的  -- 相当与cls
+    struct var_list *the_var;  // 记录class_object的实例  -- 相当与self
+} the_object;
+
 //------- inter func
 inter *get_inter();
 
@@ -347,5 +349,8 @@ parameter *add_parameter_value(statement *, parameter *);
 inter *global_inter;
 statement_list *statement_base;
 
-void login_official_func(int, int, var_list *, char *);
-void login_official(var_list *);
+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));
+
+// 内置函数
+GWARF_result official_func(func *, parameter *, var_list *);

+ 7 - 4
gwarf_interpreter/interpreter.c

@@ -1246,7 +1246,7 @@ GWARF_result call_back(statement *the_statement, var_list *the_var){  // the fun
             puts("----stop start func----");
         }
         else{
-            result = official_func(func_, tmp_s, the_var);
+            result = func_->paser(func_, tmp_s, the_var);
         }
         the_var = free_var_list(the_var);  // free the new var
     }
@@ -1840,7 +1840,7 @@ inter *get_inter(){
 }
 
 // ------official func
-void login_official_func(int type, int is_class, var_list *the_var, char *name){  // 注册单个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
     GWARF_result func_value;
     func *func_tmp = malloc(sizeof(func));
 
@@ -1850,13 +1850,14 @@ void login_official_func(int type, int is_class, var_list *the_var, char *name){
     func_tmp->type = official;
     func_tmp->official_func = type;
     func_tmp->is_class = is_class;
+    func_tmp->paser = paser;
 
     func_value.value.type = FUNC_value;
     func_value.value.value.func_value = func_tmp;
     assigment_func(name, func_value, the_var, 0);  // 注册函数到指定的位置
 }
 
-void login_official(var_list *the_var){
+void login_official(var_list *the_var, GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var)){
     // {{official_func_type, is_class}}
     int a[][2] = {{1,0}};
     // {login_name}
@@ -1864,10 +1865,12 @@ void login_official(var_list *the_var){
 
     int lenth = sizeof(a)/sizeof(a[0]);
     for(int i = 0;i < lenth;i+=1){
-        login_official_func(a[i][0], a[i][1], the_var, name[i]);
+        login_official_func(a[i][0], a[i][1], the_var, name[i], paser);
     }
 }
 
+
+// global 全局内置函数解析器
 GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var){
     GWARF_result return_value;
     switch (the_func->official_func)