Bladeren bron

修复了内置函数无self属性的错误,以及被批量替换为fprintf的snprintf和sprintf修正

SongZihuan 5 jaren geleden
bovenliggende
commit
e460cb5aa6
4 gewijzigde bestanden met toevoegingen van 20 en 25 verwijderingen
  1. 3 0
      demo/6.gwf
  2. 5 3
      inter/cfunc.c
  3. 12 21
      inter/interpreter.c
  4. 0 1
      paser/syntax.c

+ 3 - 0
demo/6.gwf

@@ -0,0 +1,3 @@
+a = [1,2,3,5,6]
+a.__iter__.__next__
+

+ 5 - 3
inter/cfunc.c

@@ -173,6 +173,8 @@ void login_official_func(int type, int is_class, var_list *the_var, char *name,
     func_tmp->is_class = is_class;
     func_tmp->is_class = is_class;
     func_tmp->is_lambda = false;
     func_tmp->is_lambda = false;
     func_tmp->paser = paser;
     func_tmp->paser = paser;
+    func_tmp->self = make_var_base(make_hash_var());
+    func_tmp->self->tag = run_object;
 
 
     func_value.value.type = FUNC_value;
     func_value.value.type = FUNC_value;
     func_value.value.value.func_value = func_tmp;
     func_value.value.value.func_value = func_tmp;
@@ -278,7 +280,7 @@ GWARF_result object_official_func(func *the_func, parameter *tmp_s, var_list *th
             size = (size_t)(9 + len_intx(ad));
             size = (size_t)(9 + len_intx(ad));
             return_value.value.type = STRING_value;
             return_value.value.type = STRING_value;
             return_value.value.value.string = (char *)malloc(size);
             return_value.value.value.string = (char *)malloc(size);
-            fprintf(inter_info, return_value.value.value.string, size, "<-%u->", ad);
+            snprintf(return_value.value.value.string, size, "<-%u->", ad);
             break;
             break;
         }
         }
         case __assignment__func:
         case __assignment__func:
@@ -2210,7 +2212,7 @@ GWARF_result parameter_to_dict(parameter *tmp_s, var_list *the_var, inter *globa
         if(tmp_s->u.var->type == base_var){  // 设置key
         if(tmp_s->u.var->type == base_var){  // 设置key
             size_t size = (size_t)(13 + strlen(tmp_s->u.var->code.base_var.var_name));
             size_t size = (size_t)(13 + strlen(tmp_s->u.var->code.base_var.var_name));
             key = (char *)malloc(size);
             key = (char *)malloc(size);
-            fprintf(inter_info, key, size, "str_%s", tmp_s->u.var->code.base_var.var_name);
+            snprintf(key, size, "str_%s", tmp_s->u.var->code.base_var.var_name);
         }
         }
         else{
         else{
             GWARF_result key_tmp = traverse(tmp_s->u.var, the_var, 0, global_inter);
             GWARF_result key_tmp = traverse(tmp_s->u.var, the_var, 0, global_inter);
@@ -2493,7 +2495,7 @@ GWARF_result run_func_core(GWARF_value *base_the_var, var_list *the_var, char *n
         }
         }
         else{
         else{
             char *tmp = malloc((size_t)( 21 + strlen(name)) );
             char *tmp = malloc((size_t)( 21 + strlen(name)) );
-             fprintf(inter_info, tmp, "Name Not Found [%s]\n", name);
+            sprintf(tmp, "Name Not Found [%s]\n", name);
             reight_tmp = to_error(tmp, "NameException", global_inter);
             reight_tmp = to_error(tmp, "NameException", global_inter);
             goto return_result;
             goto return_result;
         }
         }

+ 12 - 21
inter/interpreter.c

@@ -287,25 +287,25 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             if(tmp == NULL){
             if(tmp == NULL){
                 var_error:
                 var_error:
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
-                 fprintf(inter_info, str_tmp, "name not found [%s]\n", the_statement->code.base_var.var_name);
+                sprintf(str_tmp, "name not found [%s]", the_statement->code.base_var.var_name);
                 return_value = to_error(str_tmp, "NameException",global_inter);
                 return_value = to_error(str_tmp, "NameException",global_inter);
             }
             }
             else if(tmp->lock == protect && the_statement->code.base_var.lock_token == public_token){  // 权限不够
             else if(tmp->lock == protect && the_statement->code.base_var.lock_token == public_token){  // 权限不够
                 // 企图使用public权限访问protect
                 // 企图使用public权限访问protect
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
-                 fprintf(inter_info, str_tmp, "var is protect [%s]\n", the_statement->code.base_var.var_name);
+                sprintf(str_tmp, "var is protect [%s]", the_statement->code.base_var.var_name);
                 return_value = to_error(str_tmp, "VarException",global_inter);
                 return_value = to_error(str_tmp, "VarException",global_inter);
             }
             }
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == protect && index > max_object && index > max_class){
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == protect && index > max_object && index > max_class){
                 // 企图使用不合法的protect权限访问private
                 // 企图使用不合法的protect权限访问private
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
-                 fprintf(inter_info, str_tmp, "var is private [%s]\n", the_statement->code.base_var.var_name);
+                sprintf(str_tmp, "var is private [%s]", the_statement->code.base_var.var_name);
                 return_value = to_error(str_tmp, "VarException",global_inter);
                 return_value = to_error(str_tmp, "VarException",global_inter);
             }
             }
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == public_token){
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == public_token){
                 // 企图使用public权限访问private
                 // 企图使用public权限访问private
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
-                 fprintf(inter_info, str_tmp, "var is private [%s]\n", the_statement->code.base_var.var_name);
+                sprintf(str_tmp, "var is private [%s]", the_statement->code.base_var.var_name);
                 return_value = to_error(str_tmp, "VarException",global_inter);
                 return_value = to_error(str_tmp, "VarException",global_inter);
             }
             }
             else{
             else{
@@ -374,22 +374,22 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             if(tmp == NULL){
             if(tmp == NULL){
                 svar_error:
                 svar_error:
                 str_tmp = malloc((size_t)( 21 + strlen(str) ));
                 str_tmp = malloc((size_t)( 21 + strlen(str) ));
-                 fprintf(inter_info, str_tmp, "name not found [%s]\n", str);
+                sprintf(str_tmp, "name not found [%s]\n", str);
                 return_value = to_error(str_tmp, "NameException",global_inter);
                 return_value = to_error(str_tmp, "NameException",global_inter);
             }
             }
             else if(tmp->lock == protect && the_statement->code.base_var.lock_token == public_token){  // 权限不够
             else if(tmp->lock == protect && the_statement->code.base_var.lock_token == public_token){  // 权限不够
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
-                 fprintf(inter_info, str_tmp, "var is protect [%s]\n", the_statement->code.base_var.var_name);
+                sprintf(str_tmp, "var is protect [%s]\n", the_statement->code.base_var.var_name);
                 return_value = to_error(str_tmp, "VarException",global_inter);
                 return_value = to_error(str_tmp, "VarException",global_inter);
             }
             }
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == protect && index > max_object && index > max_class){
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == protect && index > max_object && index > max_class){
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
-                 fprintf(inter_info, str_tmp, "var is private [%s]\n", the_statement->code.base_var.var_name);
+                sprintf(str_tmp, "var is private [%s]\n", the_statement->code.base_var.var_name);
                 return_value = to_error(str_tmp, "VarException",global_inter);
                 return_value = to_error(str_tmp, "VarException",global_inter);
             }
             }
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == public_token){
             else if(tmp->lock == private && the_statement->code.base_var.lock_token == public_token){
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
                 str_tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
-                 fprintf(inter_info, str_tmp, "var is private [%s]\n", the_statement->code.base_var.var_name);
+                sprintf(str_tmp, "var is private [%s]\n", the_statement->code.base_var.var_name);
                 return_value = to_error(str_tmp, "VarException",global_inter);
                 return_value = to_error(str_tmp, "VarException",global_inter);
             }
             }
             else{
             else{
@@ -442,7 +442,8 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
             else if(base_the_var.type == NULL_value){
             else if(base_the_var.type == NULL_value){
                 goto the_break;  // NULL的point运算返回NULL (感觉起来NULL也是一个类)
                 goto the_break;  // NULL的point运算返回NULL (感觉起来NULL也是一个类)
             }
             }
-            else{  // 其他类型 解释器错误
+            else{
+                return_value = to_error("Get a Not Support Type", "TypeException", global_inter);
                 goto the_break;
                 goto the_break;
             }
             }
             if(is_error(&return_value) || is_space(&return_value)){  // Name Error错误
             if(is_error(&return_value) || is_space(&return_value)){  // Name Error错误
@@ -479,7 +480,6 @@ GWARF_result read_statement(statement *the_statement, var_list *the_var, var_lis
                     get.father = &base_the_var;  // 设置father
                     get.father = &base_the_var;  // 设置father
                     fputs("case down\n", inter_info);
                     fputs("case down\n", inter_info);
                     return_value = call_back_core(get, the_var, (the_statement->code).down.child_var, global_inter);
                     return_value = call_back_core(get, the_var, (the_statement->code).down.child_var, global_inter);
-                    // fprintf(inter_info, "return_value.u = %d\n", return_value.u);
                 }
                 }
                 else{
                 else{
                     return_value = to_error("Don't Support Down Number", "TypeException", global_inter);
                     return_value = to_error("Don't Support Down Number", "TypeException", global_inter);
@@ -1012,7 +1012,6 @@ GWARF_result if_func(if_list *if_base, var_list *the_var, inter *global_inter){
             // TODO::可以检查值类型,如果不正确则是解释器错误
             // TODO::可以检查值类型,如果不正确则是解释器错误
             if(value.u == code_restarted){
             if(value.u == code_restarted){
                 if(value.value.type != INT_value){
                 if(value.value.type != INT_value){
-                    // fprintf(inter_info, "Code Warrning: Bad Type Number for restart(ed), reset to zero");
                     value.value.type = INT_value;
                     value.value.type = INT_value;
                     value.value.value.int_value = 0;
                     value.value.value.int_value = 0;
                 }
                 }
@@ -1030,7 +1029,6 @@ GWARF_result if_func(if_list *if_base, var_list *the_var, inter *global_inter){
             // continued操作
             // continued操作
             if(value.u == code_continued){
             if(value.u == code_continued){
                 if(value.value.type != INT_value){
                 if(value.value.type != INT_value){
-                    // fprintf(inter_info, "Code Warrning: Bad Type Number for continue(ed), reset to zero");
                     value.value.type = INT_value;
                     value.value.type = INT_value;
                     value.value.value.int_value = 0;
                     value.value.value.int_value = 0;
                 }
                 }
@@ -1048,7 +1046,6 @@ GWARF_result if_func(if_list *if_base, var_list *the_var, inter *global_inter){
             // broken操作
             // broken操作
             if(value.u == code_broken){
             if(value.u == code_broken){
                 if(value.value.type != INT_value){
                 if(value.value.type != INT_value){
-                    // fprintf(inter_info, "Code Warrning: Bad Type Number for break(broken), reset to zero");
                     value.value.type = INT_value;
                     value.value.type = INT_value;
                     value.value.value.int_value = 0;
                     value.value.value.int_value = 0;
                 }
                 }
@@ -1094,7 +1091,6 @@ GWARF_result if_func(if_list *if_base, var_list *the_var, inter *global_inter){
                 // restarted操作
                 // restarted操作
                 if(value.u == code_restarted){
                 if(value.u == code_restarted){
                     if(value.value.type != INT_value){
                     if(value.value.type != INT_value){
-                        // fprintf(inter_info, "Code Warrning: Bad Type Number for restart(ed), reset to zero");
                         value.value.type = INT_value;
                         value.value.type = INT_value;
                         value.value.value.int_value = 0;
                         value.value.value.int_value = 0;
                     }
                     }
@@ -1112,7 +1108,6 @@ GWARF_result if_func(if_list *if_base, var_list *the_var, inter *global_inter){
                 // continued操作 [设在在rewent和rego前面]
                 // continued操作 [设在在rewent和rego前面]
                 if(value.u == code_continued){
                 if(value.u == code_continued){
                     if(value.value.type != INT_value){
                     if(value.value.type != INT_value){
-                        // fprintf(inter_info, "Code Warrning: Bad Type Number for continue(ed), reset to zero");
                         value.value.type = INT_value;
                         value.value.type = INT_value;
                         value.value.value.int_value = 0;
                         value.value.value.int_value = 0;
                     }
                     }
@@ -1130,7 +1125,6 @@ GWARF_result if_func(if_list *if_base, var_list *the_var, inter *global_inter){
                 // broken操作
                 // broken操作
                 if(value.u == code_broken){
                 if(value.u == code_broken){
                     if(value.value.type != INT_value){
                     if(value.value.type != INT_value){
-                        // fprintf(inter_info, "Code Warrning: Bad Type Number for break(broken), reset to zero");
                         value.value.type = INT_value;
                         value.value.type = INT_value;
                         value.value.value.int_value = 0;
                         value.value.value.int_value = 0;
                     }
                     }
@@ -2579,10 +2573,8 @@ GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_
             the_var = func_->the_var;
             the_var = func_->the_var;
             // tmp_x:形参,tmp_s:实参
             // tmp_x:形参,tmp_s:实参
 
 
-            // // fprintf(inter_info, "----address = %d----\n", the_var);
             hash_var *tmp = make_hash_var();  // base_var
             hash_var *tmp = make_hash_var();  // base_var
             the_var = append_var_list(tmp, the_var);
             the_var = append_var_list(tmp, the_var);
-            // // fprintf(inter_info, "----new address = %d----\n", the_var);
 
 
             if(func_->type == customize){  // 用户定义的方法
             if(func_->type == customize){  // 用户定义的方法
                 if(func_->is_class == action){
                 if(func_->is_class == action){
@@ -4270,8 +4262,8 @@ GWARF_result traverse_global(statement *the_statement, var_list *the_var, inter
         }
         }
         result = read_statement_list(tmp, the_var, global_inter);
         result = read_statement_list(tmp, the_var, global_inter);
         if(result.u == error){  // Name Error错误
         if(result.u == error){  // Name Error错误
-            fprintf(inter_info, "%s\n", result.error_info);
-            printf("%s\n", result.error_info);
+            fprintf(inter_info, "Exception: %s\n", result.error_info);
+            printf("Exception: %s\n", result.error_info);
             break;
             break;
         }
         }
         tmp = tmp->next;
         tmp = tmp->next;
@@ -4295,7 +4287,6 @@ GWARF_result traverse_get_value(statement *the_statement, var_list *the_var, var
         result = read_statement(tmp, the_var, NULL, out_var, lock, global_inter);
         result = read_statement(tmp, the_var, NULL, out_var, lock, global_inter);
         if(result.u == error){  // Name Error错误
         if(result.u == error){  // Name Error错误
             fprintf(inter_info, "%s\n", result.error_info);
             fprintf(inter_info, "%s\n", result.error_info);
-            printf("%s\n", result.error_info);
             break;
             break;
         }
         }
         else if(result.u == return_def && result.return_times != 0){  // return def
         else if(result.u == return_def && result.return_times != 0){  // return def

+ 0 - 1
paser/syntax.c

@@ -2604,7 +2604,6 @@ void attribute(p_status *status, token_node *list){  // 因试分解
     if(left.type == NON_point){  // 模式2/3
     if(left.type == NON_point){  // 模式2/3
         fprintf(status_log, "[info][grammar]  (attribute)reduce right\n");
         fprintf(status_log, "[info][grammar]  (attribute)reduce right\n");
         get_pop_token(status, list, symbol);
         get_pop_token(status, list, symbol);
-
         if(symbol.type == POINT_PASER){  // 模式2/3
         if(symbol.type == POINT_PASER){  // 模式2/3
             get_right_token(status, list, call_down, right);  // 回调右边
             get_right_token(status, list, call_down, right);  // 回调右边
             if(right.type != NON_call_down){
             if(right.type != NON_call_down){