Bläddra i källkod

修复str的bug和内存越界

SongZihuan 5 år sedan
förälder
incheckning
223b668ecd
5 ändrade filer med 70 tillägg och 5 borttagningar
  1. 1 0
      inter/interpreter.c
  2. 2 1
      paser/lex.h
  3. 9 3
      paser/lexical.c
  4. 54 0
      paser/syntax.c
  5. 4 1
      paser/token.h

+ 1 - 0
inter/interpreter.c

@@ -810,6 +810,7 @@ GWARF_result import_func(statement *the_statement, var_list *the_var){
     
     login(new_the_var);
     
+    printf("ad = %s\n", file.value.string);
     parser(file.value.string);
     printf("----start run----\n");
     traverse_global(global_inter->global_code, new_the_var);

+ 2 - 1
paser/lex.h

@@ -26,9 +26,10 @@
         paser->text = (char *)malloc(sizeof(char)); \
     } \
     else{ \
-        paser->text = (char *)realloc(paser->text, sizeof(char) * (len + 1)); \
+        paser->text = (char *)realloc(paser->text, sizeof(char) * (len + 2)); \
     } \
     paser->text[len] = p; \
+    paser->text[len + 1] = '\0'; \
     } while(0)
 
 #define CHECK_END(paser) \

+ 9 - 3
paser/lexical.c

@@ -164,6 +164,8 @@ int paser(int *index, p_status *status){
         match_text(p, global_paser[ASSERT_PASER], "assert");
         match_text_s(p, global_paser[LAMBDA_PASER], "->");
         match_text(p, global_paser[POINT_PASER], ".");
+        match_text(p, global_paser[IMPORT_PASER], "import");
+        match_text(p, global_paser[INCLUDE_PASER], "include");
 
         *index = check_list(global_paser, p, status);  // 检查解析结果
 
@@ -305,12 +307,13 @@ void match_double(char p, word_paser *paser){  // 匹配一个double
                 paser->status = 1;
             }
             else{
-                paser->text = (char *)realloc(paser->text, sizeof(char) * (strlen(paser->text) + 1));
+                paser->text = (char *)realloc(paser->text, sizeof(char) * (len + 2));
                 if(p == '.'){
                     paser->status = 2; 
                 }
             }
             paser->text[len] = p;
+            paser->text[len + 1] = '\0';
         }
         else{
             if(paser->status == 2){  // 必须进入小数模式才可以
@@ -342,8 +345,9 @@ void match_str(char p, word_paser *paser){  // 匹配一个var字符串
         }
         else{
             if((p != '\'' && paser->status == 1) || (p != '\"' && paser->status == 2)){  // 匹配到最后一个
-                paser->text = (char *)realloc(paser->text, sizeof(char) * (len + 1));
+                paser->text = (char *)realloc(paser->text, sizeof(char) * (len + 2));
                 paser->text[len] = p;
+                paser->text[len + 1] = '\0';
             }
             else{
                 paser->status = WAIT_END;
@@ -360,6 +364,7 @@ void match_var(char p, word_paser *paser){  // 匹配一个var
             if(p == '_' || (p <= 'z' && p >= 'a')){
                 paser->text = (char *)malloc(sizeof(char));
                 paser->text[len] = p;
+                paser->text[len + 1] = '\0';
                 paser->status = 1;
             }
             else{
@@ -368,8 +373,9 @@ void match_var(char p, word_paser *paser){  // 匹配一个var
         }
         else{
             if(p == '_' || (p <= 'z' && p >= 'a') || (p <= '9' && p >= '0')){
-                paser->text = (char *)realloc(paser->text, sizeof(char) * (len + 1));
+                paser->text = (char *)realloc(paser->text, sizeof(char) * (len + 2));
                 paser->text[len] = p;
+                paser->text[len + 1] = '\0';
             }
             else{
                 paser->status = S_END;

+ 54 - 0
paser/syntax.c

@@ -42,6 +42,7 @@ void out_exception(p_status *status, token_node *list);
 void self_exp(p_status *status, token_node *list);
 void lambda_(p_status *status, token_node *list);
 void attribute(p_status *status, token_node *list);
+void import_include(p_status *status, token_node *list);
 void paser_error(char *text);
 
 /*
@@ -149,6 +150,14 @@ void command(p_status *old_status, token_node *list){  // 多项式
         get_stop_token(status, list);
         push_statement(statement_base, new_token);
     }
+    else if(left.type == IMPORT_PASER || left.type == INCLUDE_PASER){
+        fprintf(status_log, "[info][grammar]  (command)back one token to (import_include)\n");
+        back_one_token(list, left);
+        get_base_token(&status, list, import_include, new_token);
+
+        get_stop_token(status, list);
+        push_statement(statement_base, new_token);
+    }
     else if(left.type == RETURN_PASER){
         fprintf(status_log, "[info][grammar]  (command)back one token to (return_)\n");
         back_one_token(list, left);
@@ -663,6 +672,51 @@ void formal_parameter(p_status *status, token_node *list){  // 因试分解
     }
 }
 
+/*
+import “aaa” as AA 和 include 语句
+*/
+void import_include(p_status *status, token_node *list){
+    fprintf(status_log, "[info][grammar]  mode status: while_\n");
+    token type_t, str_t, as_t, var_t, new_token;
+    type_t = pop_node(list);
+    if(type_t.type == IMPORT_PASER || type_t.type == INCLUDE_PASER){
+        get_right_token(status,list,top_exp,str_t);
+        if(str_t.type != NON_top_exp){  // 不是表达式
+            paser_error("Don't get top_exp");
+        }
+
+        statement *tmp =  make_statement();
+        if(type_t.type == IMPORT_PASER){  // import才有as top_exp
+            get_pop_token(status, list, as_t);
+            if(as_t.type != AS_PASER){
+                paser_error("Don't get as");
+            }
+
+            get_right_token(status,list,top_exp,var_t);
+            if(var_t.type != NON_top_exp){  // 不是表达式
+                paser_error("Don't get top_exp");
+            }
+            tmp->type = import_class;
+            tmp->code.import_class.file = str_t.data.statement_value;
+            tmp->code.import_class.var = var_t.data.statement_value;
+        }
+        else{
+            tmp->type = include_import;
+            tmp->code.include_import.file = str_t.data.statement_value;
+        }
+
+        new_token.type = NON_import;
+        new_token.data_type = statement_value;
+        new_token.data.statement_value = tmp;
+        add_node(list, new_token);  // 压入节点[弹出3个压入1个]
+        return;
+    }
+    else{
+        back_one_token(list, type_t);
+        return;
+    }
+}
+
 /*
 while_ : WHILE LB top_exp RB block
 */

+ 4 - 1
paser/token.h

@@ -3,7 +3,7 @@
 
 #include "../inter/interpreter.h"
 
-#define MAX_PASER_SIZE 81
+#define MAX_PASER_SIZE 83
 #define INT_PASER 0
 #define DOUBLE_PASER 1
 #define ENTER_PASER 2
@@ -85,6 +85,8 @@
 #define FSUB_PASER 78
 #define LAMBDA_PASER 79
 #define POINT_PASER 80
+#define IMPORT_PASER 81
+#define INCLUDE_PASER 82
 
 // 获取并返回一个token
 #define get_pop_token(status,list,new_token) \
@@ -265,6 +267,7 @@ typedef enum token_type
     NON_self_exp = -44,
     NON_lambda = -45,
     NON_point = -46,
+    NON_import = -47,
 } token_type;
 
 typedef union token_data