Explorar el Código

setup函数和inline函数

SongZihuan hace 5 años
padre
commit
a3397d18c0
Se han modificado 6 ficheros con 102 adiciones y 15 borrados
  1. 5 3
      README.md
  2. 34 9
      inter/interpreter.c
  3. 2 0
      inter/interpreter.h
  4. 2 0
      paser/lexical.c
  5. 52 2
      paser/syntax.c
  6. 7 1
      paser/token.h

+ 5 - 3
README.md

@@ -53,11 +53,13 @@ return x n  函数返回值x,返回n层
 * GWARF立项和完成了基本结构。
 * GWARF重写了PASER解析器。
 * 修改了解析器中的status的成员的意义,修复了call back的bug[具体参见commit message]。
-* 虚解包,允许语法:a,b,\* = 1,2,3,4,5,6。也支持形参表,但不支持实参表。
+* 虚解包,允许语法:``a,b,\* = 1,2,3,4,5,6``。也支持形参表,但不支持实参表。
 * 使用function定义静态方法(def)可以定义auto方法(如果是class内则定义为action方法,否则定义为def方法)。
-* 优先级bug:调用类方法只能用(xxx.zzz)()的方式,为优先级错误[已经修复]。
+* 优先级bug:调用类方法只能用``(xxx.zzz)()``的方式,为优先级错误[已经修复]。
 * 使用cls来定义类方法,并且使用类调用对象方法的时候不会填入self。
 * 使用点运算符读取成员的时候会越界访问外部变量。[已经修复]
-* 设置了函数静态空间,允许对函数使用point运算符
+* 设置了函数静态空间,允许对函数使用point运算符。
+* 设置了setup函数,函数可以使用setup关键词进行初始化。
+* 设置inline函数,函数执行的时候将不会产生单独的``var_list``,也不会记录``var_list``。
 ## 关于GWARF
 最后更新时间 : 2020年05月05日 广州

+ 34 - 9
inter/interpreter.c

@@ -119,6 +119,7 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             break;
         }
         case call:
+            puts("statr call");
             return_value = call_back(the_statement, the_var);
             break;
         case while_cycle:
@@ -346,7 +347,10 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
                 return_value = traverse((the_statement->code).point.child_var, base_the_var.value.object_value->the_var, false);
             }
             else if(base_the_var.type == FUNC_value){
+                var_list *old_tmp = base_the_var.value.func_value->self->next;
+                base_the_var.value.func_value->self->next = NULL;  // 暂时断链
                 return_value = traverse((the_statement->code).point.child_var, base_the_var.value.func_value->self, false);
+                base_the_var.value.func_value->self->next = old_tmp;  // 恢复
             }
             else{  // 其他类型
                 goto the_break;
@@ -404,12 +408,27 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
         case def:{
             GWARF_result func_value;
             func *func_tmp = malloc(sizeof(func));
-
             func_tmp->done = the_statement->code.def.done;
             func_tmp->parameter_list = the_statement->code.def.parameter_list;
-            func_tmp->the_var = copy_var_list(the_var);
             func_tmp->self = make_var_base(make_hash_var());
+            if(the_statement->code.def.is_inline){  // inline函数
+                func_tmp->the_var = NULL;
+                goto make_func;  // 跳过其他设置
+            }
 
+            func_tmp->the_var = copy_var_list(the_var);
+            if(the_statement->code.def.setup != NULL){  // 存在setup的内容
+                append_by_var_list(func_tmp->self, func_tmp->the_var);
+                GWARF_result tmp;
+                tmp = traverse(the_statement->code.def.setup, func_tmp->self, false);
+                if(is_error(&tmp) || is_space(&tmp)){
+                    return_value = tmp;
+                    goto func_break;
+                }
+                func_tmp->the_var = func_tmp->self;
+            }
+
+            make_func:
             func_tmp->type = customize;  // func by user
             if((the_login_var != the_var && the_statement->code.def.type == auto_func) || the_statement->code.def.type == action){  // 定义为类方法
                 func_tmp->is_class = action;
@@ -427,7 +446,7 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             printf("the_statement->code.def.var = %d\n", the_statement->code.def.var->type);
             assignment_statement_core(the_statement->code.def.var, the_var, the_login_var, func_value, true);  // 注册函数到指定的位置
             // 无返回值
-            break;
+            func_break: break;
         }
         case lambda_func:{
             func *func_tmp = malloc(sizeof(func));
@@ -2186,13 +2205,17 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
     if(get.value.type == FUNC_value){
         func *func_ = get.value.value.func_value;
         parameter *tmp_x = func_->parameter_list;
-        the_var = func_->the_var;
+        bool new_var_list = false;
+        if(func_->the_var != NULL){
+            the_var = func_->the_var;  // inline则不需要设置
+            new_var_list = true;
+        }
         // tmp_x:形参,tmp_s:实参
 
-        // printf("----address = %d----\n", the_var);
-        hash_var *tmp = make_hash_var();  // base_var
-        the_var = append_var_list(tmp, the_var);
-        // printf("----new address = %d----\n", the_var);
+        if(new_var_list){
+            hash_var *tmp = make_hash_var();  // base_var
+            the_var = append_var_list(tmp, the_var);
+        }
 
         if(func_->type == customize){  // 用户定义的方法
             // 赋值self
@@ -2250,7 +2273,9 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
         else{
             result = func_->paser(func_, tmp_s, the_var, get, old_var_list);
         }
-        the_var = free_var_list(the_var);  // free the new var
+        if(new_var_list){
+            the_var = free_var_list(the_var);  // free the new var
+        }
         goto back;
     }
     else if(get.value.type == CLASS_value){  // 生成实例

+ 2 - 0
inter/interpreter.h

@@ -290,12 +290,14 @@ typedef struct statement{
             parameter *parameter_list;  // def parameter
             struct statement *done;  // def to do
             struct statement *var;  // 方法的名字
+            struct statement *setup;  // setup to do
             enum {
                 function,
                 action,
                 cls,
                 auto_func,
             } type;  // 静态函数[function] or 实例函数[action] or 类方法[cls] or 默认[def]
+            bool is_inline;  // inline函数不设单独的func_var空间
         } def;
 
         struct{

+ 2 - 0
paser/lexical.c

@@ -175,6 +175,8 @@ int paser(int *index, p_status *status){
         match_text(p, global_paser[FUNC_PASER], "function");
         match_text(p, global_paser[CLS_PASER], "cls");
         match_text(p, global_paser[ACTION_PASER], "action");
+        match_text(p, global_paser[SETUP_PASER], "setup");
+        match_text(p, global_paser[INLINE_PASER], "inline");
 
         *index = check_list(global_paser, p, status);  // 检查解析结果
 

+ 52 - 2
paser/syntax.c

@@ -127,7 +127,7 @@ void command(p_status *old_status, token_node *list){  // 多项式
         get_stop_token(status, list);
         push_statement(statement_base, new_token);
     }
-    else if(left.type == DEF_PASER || left.type == FUNC_PASER || left.type == CLASS_PASER || left.type == CLS_PASER || left.type == ACTION_PASER){
+    else if(left.type == DEF_PASER || left.type == FUNC_PASER || left.type == CLASS_PASER || left.type == CLS_PASER || left.type == ACTION_PASER || left.type == SETUP_PASER || left.type == INLINE_PASER){
         fprintf(status_log, "[info][grammar]  (command)back one token to (def_class)\n");
         back_one_token(list, left);
         get_base_token(&status, list, def_class, new_token);
@@ -451,7 +451,55 @@ void def_class(p_status *status, token_node *list){
     parameter *p_list;
 
     def_t = pop_node(list);
-    if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER || def_t.type == CLASS_PASER || def_t.type == CLS_PASER || def_t.type == ACTION_PASER){
+    if(def_t.type == SETUP_PASER){  // 动态函数
+        get_right_token(status,list,block_,block_t);  // 获取初始化信息
+        if(block_t.type != NON_block){  // 不是表达式
+            paser_error("Don't get '{'");
+        }
+        token next, main_func;
+        again:
+        get_pop_token(status, list, next);
+        if(next.type == DEF_PASER || next.type == FUNC_PASER || next.type == CLS_PASER || next.type == ACTION_PASER){  // 许可的loop
+            back_one_token(list, next);
+            get_base_token(status, list, def_class, main_func);  // 不需要safe_get_token
+            if(main_func.type != NON_def){
+                goto error;
+            }
+            new_token = main_func;
+            new_token.data.statement_value->code.def.setup = block_t.data.statement_value;  // 设置setup
+        }
+        else if(next.type == ENTER_PASER){
+            goto again;
+        }
+        else{
+            error: paser_error("Don't get main func for setup");
+        }
+        new_token.type = NON_def;
+        new_token.data_type = statement_value;
+        add_node(list, new_token);  // 压入节点[弹出3个压入1个]
+        return;
+    }
+    if(def_t.type == INLINE_PASER){  // 动态函数
+        token next, main_func;
+        get_pop_token(status, list, next);
+        if(next.type == DEF_PASER || next.type == FUNC_PASER || next.type == CLS_PASER || next.type == ACTION_PASER){  // 许可的loop
+            back_one_token(list, next);
+            get_base_token(status, list, def_class, main_func);  // 不需要safe_get_token
+            if(main_func.type != NON_def){
+                goto error_inline;
+            }
+            new_token = main_func;
+            new_token.data.statement_value->code.def.is_inline = true;  // 设置setup
+        }
+        else{
+            error_inline: paser_error("Don't func for inline func");
+        }
+        new_token.type = NON_def;
+        new_token.data_type = statement_value;
+        add_node(list, new_token);  // 压入节点[弹出3个压入1个]
+        return;
+    }
+    else if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER || def_t.type == CLASS_PASER || def_t.type == CLS_PASER || def_t.type == ACTION_PASER){
         p_status new_status;
         new_status = *status;
         new_status.not_match_tuple = true;
@@ -495,6 +543,8 @@ void def_class(p_status *status, token_node *list){
             def_tmp->code.def.var = name_t.data.statement_value;
             def_tmp->code.def.parameter_list = p_list;
             def_tmp->code.def.done = block_t.data.statement_value;
+            def_tmp->code.def.setup = NULL;
+            def_tmp->code.def.is_inline = false;
             switch(def_t.type){
                 case DEF_PASER:
                 def_tmp->code.def.type = auto_func;

+ 7 - 1
paser/token.h

@@ -3,7 +3,7 @@
 
 #include "../inter/interpreter.h"
 
-#define MAX_PASER_SIZE 88
+#define MAX_PASER_SIZE 90
 #define INT_PASER 0
 #define DOUBLE_PASER 1
 #define ENTER_PASER 2
@@ -92,6 +92,8 @@
 #define FUNC_PASER 85
 #define CLS_PASER 86
 #define ACTION_PASER 87
+#define SETUP_PASER 88
+#define INLINE_PASER 89
 
 // 获取并返回一个token
 #define get_pop_token(status,list,new_token) \
@@ -225,6 +227,10 @@ typedef enum token_type
     LAMBDA = LAMBDA_PASER,
     POINT = POINT_PASER,
     SVAR = SVAR_PASER,
+    FUNC = FUNC_PASER,
+    CLS = CLS_PASER,
+    ACTION = ACTION_PASER,
+    SETUP = SETUP_PASER,
 
     // 特殊符号
     BAD_token = -2,