瀏覽代碼

cls定义类方法

SongZihuan 5 年之前
父節點
當前提交
0353719391
共有 5 個文件被更改,包括 20 次插入11 次删除
  1. 2 1
      README.md
  2. 1 5
      inter/interpreter.c
  3. 1 0
      paser/lexical.c
  4. 14 4
      paser/syntax.c
  5. 2 1
      paser/token.h

+ 2 - 1
README.md

@@ -55,6 +55,7 @@ return x n  函数返回值x,返回n层
 * 修改了解析器中的status的成员的意义,修复了call back的bug[具体参见commit message]。
 * 修改了解析器中的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方法)。
 * 使用function定义静态方法(def)可以定义auto方法(如果是class内则定义为action方法,否则定义为def方法)。
-* 优先级bug:调用类方法只能用(xxx.zzz)()的方式,为优先级错误[已经修复]。(至今)
+* 优先级bug:调用类方法只能用(xxx.zzz)()的方式,为优先级错误[已经修复]。
+* 使用cls来定义类方法,并且使用类调用对象方法的时候不会填入self
 ## 关于GWARF
 ## 关于GWARF
 最后更新时间 : 2020年05月05日 广州
 最后更新时间 : 2020年05月05日 广州

+ 1 - 5
inter/interpreter.c

@@ -2211,15 +2211,11 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
             // 赋值self
             // 赋值self
             GWARF_result father;
             GWARF_result father;
             if(func_->is_class == action){
             if(func_->is_class == action){
-                if(get.father != NULL){
+                if(get.father != NULL && get.father->type == OBJECT_value){
                     father.value = *(get.father);
                     father.value = *(get.father);
                     assignment_statement_core(tmp_x->u.var, old_var_list, the_var, father, true);
                     assignment_statement_core(tmp_x->u.var, old_var_list, the_var, father, true);
                     tmp_x = tmp_x->next;  // get the next to iter
                     tmp_x = tmp_x->next;  // get the next to iter
                 }
                 }
-                else{
-                    printf("Warning!!![4]\n");
-                    // TODO:: 抛出错误
-                }
             }
             }
             else if(func_->is_class == cls){
             else if(func_->is_class == cls){
                 if(get.father != NULL){
                 if(get.father != NULL){

+ 1 - 0
paser/lexical.c

@@ -173,6 +173,7 @@ int paser(int *index, p_status *status){
         match_text(p, global_paser[INCLUDE_PASER], "include");
         match_text(p, global_paser[INCLUDE_PASER], "include");
         match_text(p, global_paser[SVAR_PASER], "$");
         match_text(p, global_paser[SVAR_PASER], "$");
         match_text(p, global_paser[FUNC_PASER], "function");
         match_text(p, global_paser[FUNC_PASER], "function");
+        match_text(p, global_paser[CLS_PASER], "cls");
 
 
         *index = check_list(global_paser, p, status);  // 检查解析结果
         *index = check_list(global_paser, p, status);  // 检查解析结果
 
 

+ 14 - 4
paser/syntax.c

@@ -127,7 +127,7 @@ void command(p_status *old_status, token_node *list){  // 多项式
         get_stop_token(status, list);
         get_stop_token(status, list);
         push_statement(statement_base, new_token);
         push_statement(statement_base, new_token);
     }
     }
-    else if(left.type == DEF_PASER || left.type == FUNC_PASER || left.type == CLASS_PASER){
+    else if(left.type == DEF_PASER || left.type == FUNC_PASER || left.type == CLASS_PASER || left.type == CLS_PASER){
         fprintf(status_log, "[info][grammar]  (command)back one token to (def_class)\n");
         fprintf(status_log, "[info][grammar]  (command)back one token to (def_class)\n");
         back_one_token(list, left);
         back_one_token(list, left);
         get_base_token(&status, list, def_class, new_token);
         get_base_token(&status, list, def_class, new_token);
@@ -451,7 +451,7 @@ void def_class(p_status *status, token_node *list){
     parameter *p_list;
     parameter *p_list;
 
 
     def_t = pop_node(list);
     def_t = pop_node(list);
-    if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER || def_t.type == CLASS_PASER){
+    if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER || def_t.type == CLASS_PASER || def_t.type == CLS_PASER){
         p_status new_status;
         p_status new_status;
         new_status = *status;
         new_status = *status;
         new_status.not_match_tuple = true;
         new_status.not_match_tuple = true;
@@ -490,12 +490,22 @@ void def_class(p_status *status, token_node *list){
         }
         }
 
 
         statement *def_tmp =  make_statement();
         statement *def_tmp =  make_statement();
-        if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER){
+        if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER || def_t.type == CLS_PASER){
             def_tmp->type = def;
             def_tmp->type = def;
             def_tmp->code.def.var = name_t.data.statement_value;
             def_tmp->code.def.var = name_t.data.statement_value;
             def_tmp->code.def.parameter_list = p_list;
             def_tmp->code.def.parameter_list = p_list;
             def_tmp->code.def.done = block_t.data.statement_value;
             def_tmp->code.def.done = block_t.data.statement_value;
-            def_tmp->code.def.type = (def_t.type == DEF_PASER) ? auto_func : function;
+            switch(def_t.type){
+                case DEF_PASER:
+                def_tmp->code.def.type = auto_func;
+                break;
+                case CLS_PASER:
+                def_tmp->code.def.type = cls;
+                break;
+                case FUNC_PASER:
+                def_tmp->code.def.type = function;
+                break;
+            }
         }
         }
         else{
         else{
             def_tmp->type = set_class;
             def_tmp->type = set_class;

+ 2 - 1
paser/token.h

@@ -3,7 +3,7 @@
 
 
 #include "../inter/interpreter.h"
 #include "../inter/interpreter.h"
 
 
-#define MAX_PASER_SIZE 86
+#define MAX_PASER_SIZE 87
 #define INT_PASER 0
 #define INT_PASER 0
 #define DOUBLE_PASER 1
 #define DOUBLE_PASER 1
 #define ENTER_PASER 2
 #define ENTER_PASER 2
@@ -90,6 +90,7 @@
 #define COMMENT_PASER 83
 #define COMMENT_PASER 83
 #define SVAR_PASER 84
 #define SVAR_PASER 84
 #define FUNC_PASER 85
 #define FUNC_PASER 85
+#define CLS_PASER 86
 
 
 // 获取并返回一个token
 // 获取并返回一个token
 #define get_pop_token(status,list,new_token) \
 #define get_pop_token(status,list,new_token) \