浏览代码

运算符重载

SongZihuan 5 年之前
父节点
当前提交
dcdcdc6e81
共有 5 个文件被更改,包括 92 次插入4 次删除
  1. 47 1
      inter/interpreter.c
  2. 2 0
      inter/interpreter.h
  3. 1 0
      paser/lexical.c
  4. 39 2
      paser/syntax.c
  5. 3 1
      paser/token.h

+ 47 - 1
inter/interpreter.c

@@ -1798,6 +1798,11 @@ GWARF_result operation_func(statement *the_statement, var_list *the_var, var_lis
             value = assignment_statement_eq(the_statement->code.operation.left_exp, the_var, login_var, right_result, global_inter);
             break;
         }
+        case IS_func:
+            get_left_result;
+            get_right_result;
+            value = is_func(left_result, right_result, the_var, global_inter);
+            break;
         case EQUAL_func:
             get_left_result;
             get_right_result;
@@ -2446,7 +2451,6 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
                 out:
                 ;
             }
-            puts("WFWFSSSSSSSSSSSSSSSSSSSSSs");
             if(tmp_s->next == NULL){
                 puts("tmp_s->next == NULL");
             }
@@ -4129,6 +4133,48 @@ GWARF_result equal_func(GWARF_result left_result, GWARF_result right_result, var
     return_back: return return_value;
 }
 
+// ---------  IS
+GWARF_result is_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var, inter *global_inter){
+    GWARF_result return_value = GWARF_result_reset;
+    unsigned long int left, right;
+    if(left_result.value.type == OBJECT_value){  // 调用左div方法
+        left = (unsigned long int)left_result.value.value.object_value;
+    }
+    else if(left_result.value.type == CLASS_value){
+        left = (unsigned long int)left_result.value.value.class_value;
+    }
+    else if(left_result.value.type == FUNC_value){
+        left = (unsigned long int)left_result.value.value.func_value;
+    }
+    else if(left_result.value.type == NULL_value){
+        left = (unsigned long int)0;
+    }
+    NotSupportCul();
+
+    if(right_result.value.type == OBJECT_value){  // 调用左div方法
+        right = (unsigned long int)right_result.value.value.object_value;
+    }
+    else if(right_result.value.type == CLASS_value){
+        right = (unsigned long int)right_result.value.value.class_value;
+    }
+    else if(right_result.value.type == FUNC_value){
+        right = (unsigned long int)right_result.value.value.func_value;
+    }
+    else if(right_result.value.type == NULL_value){
+        right = (unsigned long int)0;
+    }
+    NotSupportCul();
+    return_value.value.type = BOOL_value;
+    if(left == right){
+        return_value.value.value.bool_value = true;
+    }
+    else{
+        return_value.value.value.bool_value = false;
+    }
+    
+    return_back: return return_value;
+}
+
 // --------- traverse[iter]
 GWARF_result traverse(statement *the_statement, var_list *the_var, bool new, inter *global_inter){  // traverse the statement
     statement *tmp = the_statement;

+ 2 - 0
inter/interpreter.h

@@ -205,6 +205,7 @@ typedef struct statement{
                 BOOLIS_func,  // <=>
                 BOOLSAND_func,  // and
                 BOOLSOR_func,  // or
+                IS_func,  // a is b
             } type;
             struct statement *right_exp;  // the right exp
             struct statement *left_exp;  // the left exp
@@ -598,6 +599,7 @@ GWARF_result log_func(GWARF_result, GWARF_result, var_list *, inter *);
 GWARF_result sqrt_func(GWARF_result, GWARF_result, var_list *, inter *);
 GWARF_result assignment_func(char *, GWARF_result, var_list *, int, int);  // 不需要inter
 GWARF_result equal_func(GWARF_result, GWARF_result, var_list *, int, inter *);
+GWARF_result is_func(GWARF_result, GWARF_result, var_list *, inter *);
 GWARF_result negative_func(GWARF_result, var_list *, inter *);
 GWARF_result assignment_statement_core(statement *, var_list *, var_list *, GWARF_result, bool, int, inter *);
 GWARF_result assignment_statement_value(statement *, var_list *, var_list *, GWARF_value, inter *);

+ 1 - 0
paser/lexical.c

@@ -194,6 +194,7 @@ int paser(int *index, p_status *status){
         match_text(p, global_paser[PUBLIC_PASER], "public");
         match_text(p, global_paser[PRIVATE_PASER], "private");
         match_text(p, global_paser[FINALLY_PASER], "finally");
+        match_text(p, global_paser[IS_PASER], "is");
 
         *index = check_list(global_paser, p, status);  // 检查解析结果
 

+ 39 - 2
paser/syntax.c

@@ -1846,8 +1846,9 @@ void compare(p_status *status, token_node *list){  // 多项式
         get_pop_token(status, list, symbol);
         if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
            symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER ||
-           symbol.type == ISLEFT_PASER || symbol.type == ISRIGHT_PASER || symbol.type == BOOLIS_PASER||
-           symbol.type == ILEFT_PASER || symbol.type == IRIGHT_PASER || symbol.type == BOOLNOTOR_PASER){
+           symbol.type == ISLEFT_PASER || symbol.type == ISRIGHT_PASER || symbol.type == BOOLIS_PASER ||
+           symbol.type == ILEFT_PASER || symbol.type == IRIGHT_PASER || symbol.type == BOOLNOTOR_PASER ||
+           symbol.type == IS_PASER){
             get_right_token(status, list, bit_notor, right);  // 回调右边
             if(right.type != NON_bit_notor){
                 paser_error("Don't get a bit_notor");
@@ -1894,6 +1895,8 @@ void compare(p_status *status, token_node *list){  // 多项式
                 break;
             case BOOLNOTOR_PASER:
                 code_tmp->code.operation.type = BOOLNOTOR_func;
+            case IS_PASER:
+                code_tmp->code.operation.type = IS_func;
                 break;
             }
             code_tmp->code.operation.left_exp = left.data.statement_value;
@@ -2536,6 +2539,40 @@ void call_back_(p_status *status, token_node *list){  // 因试分解
             add_node(list, new_token);  // 压入节点[弹出3个压入1个]
             return call_back_(status, list);  // 回调自己
         }
+        else if(symbol.type == VAR_PASER || symbol.type == SVAR_PASER){  // 运算符重载,如  yyy add xxx
+            back_one_token(list, symbol);
+            token call_func, second_args;
+            get_base_token(status, list, element, call_func);
+            if(call_func.type != NON_element){
+                paser_error("Don't get the element");
+            }
+
+            get_right_token(status, list, attribute, second_args);
+            if(second_args.type != NON_point){
+                paser_error("Don't get the attribute[1]");
+            }
+
+            parameter *first, *second;
+            first = make_parameter_value(left.data.statement_value);
+            first->u.var = first->u.value;
+
+            second = make_parameter_value(second_args.data.statement_value);
+            second->u.var = second->u.value;
+            first->next = second;
+
+            new_token.type = NON_call;
+            new_token.data_type = statement_value;
+
+            statement *code_tmp =  make_statement();
+            code_tmp->type = call;
+            code_tmp->code.call.func = call_func.data.statement_value;
+            code_tmp->code.call.parameter_list = first;
+            
+            new_token.data.statement_value = code_tmp;
+            add_node(list, new_token);  // 压入节点[弹出3个压入1个]
+            return call_back_(status, list);  // 回调自己            
+
+        }
         else{  // 递归跳出
             // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
             fprintf(status_log, "[info][grammar]  (call_back_)out\n");

+ 3 - 1
paser/token.h

@@ -3,7 +3,7 @@
 
 #include "../inter/interpreter.h"
 
-#define MAX_PASER_SIZE 107
+#define MAX_PASER_SIZE 108
 #define INT_PASER 0
 #define DOUBLE_PASER 1
 #define ENTER_PASER 2
@@ -111,6 +111,7 @@
 #define BOOLSAND_PASER 104
 #define BOOLSOR_PASER 105
 #define FINALLY_PASER 106
+#define IS_PASER 107
 
 // 获取并返回一个token
 #define get_pop_token(status,list,new_token) \
@@ -266,6 +267,7 @@ typedef enum token_type
     BOOLSAND = BOOLSAND_PASER,
     BOOLSOR = BOOLSOR_PASER,
     FINALLY = FINALLY_PASER,
+    IS = IS_PASER,
 
     // 特殊符号
     BAD_token = -2,