Browse Source

实现代码多层次

SongZihuan 5 years ago
parent
commit
2ad27f992f
9 changed files with 315 additions and 197 deletions
  1. BIN
      gwarf
  2. 3 1
      gwarf.c
  3. 14 0
      gwarf_interpreter/interprete.h
  4. 42 0
      gwarf_interpreter/interpreter.c
  5. 2 1
      paser/gwarf_lex.l
  6. 26 8
      paser/gwarf_yacc.y
  7. 81 74
      paser/lex.yy.c
  8. 146 112
      paser/y.tab.c
  9. 1 1
      paser/y.tab.h

BIN
gwarf


+ 3 - 1
gwarf.c

@@ -4,8 +4,10 @@
 
 int main(){
     global_inter = get_inter();  // 拿全局解释器[并声明全局变量]
-    parser();
     var_list *the_var = make_var_base(global_inter->global_var);
+    statement_base = make_statement_base(global_inter->global_code);
+
+    parser("/home/songzihuan/test.gwf");
     printf("----start run----\n");
     traverse(global_inter->global_code, the_var,false);
     printf("code end...\n");

+ 14 - 0
gwarf_interpreter/interprete.h

@@ -87,6 +87,13 @@ typedef struct var_list{
     struct var_list *next;
 } var_list;
 
+// ------------------------- inter paser [记录每一层变量code的链表]
+
+typedef struct statement_list{
+    statement *statement_base;
+    struct statement_list *next;
+} statement_list;
+
 // ------------------------- inter
 
 typedef struct{
@@ -105,6 +112,12 @@ void del_var(char *, var *);
 statement *make_statement();
 statement *append_statement(statement *, statement*);
 
+statement_list *make_statement_list();
+statement_list *make_statement_base(statement *);
+statement_list *append_statement_list(statement *, statement_list *);
+statement *find_statement_list(int, statement_list *);
+statement_list *free_statement_list(statement_list *);
+
 //------- run func
 GWARF_result traverse(statement *, var_list *, bool);
 
@@ -118,3 +131,4 @@ char *yytext;
 
 // main
 inter *global_inter;
+statement_list *statement_base;

+ 42 - 0
gwarf_interpreter/interpreter.c

@@ -172,6 +172,48 @@ void add_var(var_list *var_base,int from, char *name, GWARF_value value){  // ad
     append_var(name, value, start->var_base);
 }
 
+// ------------------------- statement_list
+statement_list *make_statement_list(){  // make a empty var_list node
+    statement_list *tmp;
+    tmp = malloc(sizeof(statement_list));  // get an address for base var
+    tmp->next = NULL;
+    tmp->statement_base = NULL;
+    return tmp;
+}
+
+statement_list *make_statement_base(statement *gloabl_code){
+    statement_list *tmp = make_statement_list();
+    tmp->statement_base = gloabl_code;
+    return tmp;
+}
+
+statement_list *append_statement_list(statement *statement_base, statement_list *statment_list_base){  // make var_list[FILO]
+    statement_list *tmp = make_statement_list();
+    tmp->statement_base = statement_base;
+    tmp->next = statment_list_base;
+    return tmp;
+}
+
+statement *find_statement_list(int from, statement_list *statment_list_base){  // find var by func get_var in var_list[iter to find]
+    statement_list *start = statment_list_base;
+    for(int i = 0;i < from;i+= 1){
+        if(start->next = NULL){
+            break;
+        }
+        start = start->next;
+    }
+    return start->statement_base;
+}
+
+statement_list *free_statement_list(statement_list *statment_list_base){  // make var_list[FILO]
+    statement_list *tmp = statment_list_base->next;
+    if(tmp != NULL){
+        free(statment_list_base);
+        return tmp;
+    }
+    return statment_list_base;
+}
+
 // ------------------------- run code
 
 GWARF_result read_statement_list(statement *the_statement, var_list *the_var){  // read the statement list with case to run by func

+ 2 - 1
paser/gwarf_lex.l

@@ -8,7 +8,7 @@
 
 <INITIAL>"(" {return LB;}
 <INITIAL>")" {return RB;}
-<INITIAL>"{" {return LP;}
+<INITIAL>[\n]*\{[\n]* {return LP;}
 <INITIAL>"}" {return RP;}
 
 <INITIAL>">=" {return MOREEQ;}
@@ -38,6 +38,7 @@
 
 <INITIAL>" " ;
 <INITIAL>\n {return STOP;}
+<INITIAL>";" {return STOP;}
 <INITIAL>. {printf("text = [%s];\n", yytext);}
 
 <COMMENT>\n {BEGIN INITIAL;}

+ 26 - 8
paser/gwarf_yacc.y

@@ -3,7 +3,6 @@
     #include"lex.yy.c"
     #include"../gwarf_interpreter/interprete.h"
     extern int yylex (void);
-    statement *the_global_code, *now_code;
 %}
 
 %union{
@@ -20,19 +19,22 @@
 %%
 command_block
     : command_list
+    | while_block
     ;
 
 command_list
     : command
     {
         if($1 != NULL){
-            append_statement(now_code, $1);
+            statement *tmp = find_statement_list(0, statement_base);
+            append_statement(tmp, $1);
         }
     }
     | command_list command
     {   
         if($2 != NULL){
-            append_statement(global_inter->global_code, $2);
+            statement *tmp = find_statement_list(0, statement_base);
+            append_statement(tmp, $2);
         }
     }
     ;
@@ -198,18 +200,34 @@ base_var_
     }
     ;
 
+while_block
+    : while_exp block
+    {
+        printf("start-1\n");
+    }
+
+while_exp
+    : WHILE LB top_exp RB
+    {
+        printf("start-2\n");
+    }
+
+block
+    : LP command_list RP STOP
+    {
+        printf("start-0\n");
+    }
+
 %%
 int yyerror(char const *str)
 {
-    fprintf(stderr, "parser error near %s\n", yytext);
+    fprintf(stderr, "parser error near %s ;\n", yytext, yytext);
     return 0;
 }
 
-int parser(void)
+int parser(char *file)
 {
-    the_global_code = global_inter->global_code;
-    now_code = the_global_code;
-    yyin = fopen("/home/songzihuan/test.gwf","r");
+    yyin = fopen(file,"r");
     yyparse();
     return 0;
 }

+ 81 - 74
paser/lex.yy.c

@@ -351,8 +351,8 @@ static void yynoreturn yy_fatal_error ( const char* msg  );
 	(yy_hold_char) = *yy_cp; \
 	*yy_cp = '\0'; \
 	(yy_c_buf_p) = yy_cp;
-#define YY_NUM_RULES 33
-#define YY_END_OF_BUFFER 34
+#define YY_NUM_RULES 34
+#define YY_END_OF_BUFFER 35
 /* This struct is not used in this scanner,
    but its presence is necessary. */
 struct yy_trans_info
@@ -360,13 +360,14 @@ struct yy_trans_info
 	flex_int32_t yy_verify;
 	flex_int32_t yy_nxt;
 	};
-static const flex_int16_t yy_accept[51] =
+static const flex_int16_t yy_accept[55] =
     {   0,
-        0,    0,    0,    0,    0,    0,   34,   25,   24,   23,
-       25,   20,   18,   19,    2,    3,   15,   13,   14,   16,
-       21,   21,   10,   12,    9,   22,   17,   22,    4,    5,
-       28,   26,   27,   32,   31,   30,   29,    8,    0,   21,
-        7,   11,    6,   22,   22,   21,   22,   22,    1,    0
+        0,    0,    0,    0,    0,    0,   35,   26,   24,   23,
+       26,   20,   18,   19,    2,    3,   15,   13,   14,   16,
+       21,   21,   25,   10,   12,    9,   22,   17,   22,    4,
+        5,   29,   27,   28,   33,   32,   31,   30,    0,    4,
+        8,    0,   21,    7,   11,    6,   22,   22,    4,   21,
+       22,   22,    1,    0
     } ;
 
 static const YY_CHAR yy_ec[256] =
@@ -376,15 +377,15 @@ static const YY_CHAR yy_ec[256] =
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    3,    4,    5,    6,    1,    1,    1,    7,    8,
         9,   10,   11,    1,   12,   13,   14,   15,   16,   16,
-       16,   16,   16,   16,   16,   16,   16,    1,    1,   17,
-       18,   19,    1,    1,   20,   20,   20,   20,   20,   20,
-       20,   20,   20,   20,   20,   20,   20,   20,   20,   20,
-       20,   20,   20,   20,   20,   20,   20,   20,   20,   20,
-        1,    1,    1,   21,   20,    1,   20,   20,   20,   20,
-
-       22,   20,   20,   23,   24,   20,   20,   25,   20,   20,
-       20,   20,   20,   20,   20,   20,   20,   20,   26,   20,
-       20,   20,   27,    1,   28,    1,    1,    1,    1,    1,
+       16,   16,   16,   16,   16,   16,   16,    1,   17,   18,
+       19,   20,    1,    1,   21,   21,   21,   21,   21,   21,
+       21,   21,   21,   21,   21,   21,   21,   21,   21,   21,
+       21,   21,   21,   21,   21,   21,   21,   21,   21,   21,
+        1,    1,    1,   22,   21,    1,   21,   21,   21,   21,
+
+       23,   21,   21,   24,   25,   21,   21,   26,   21,   21,
+       21,   21,   21,   21,   21,   21,   21,   21,   27,   21,
+       21,   21,   28,    1,   29,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
@@ -401,60 +402,60 @@ static const YY_CHAR yy_ec[256] =
         1,    1,    1,    1,    1
     } ;
 
-static const YY_CHAR yy_meta[29] =
+static const YY_CHAR yy_meta[30] =
     {   0,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
-        1,    1,    1,    1,    2,    2,    1,    1,    1,    2,
-        1,    2,    2,    2,    2,    2,    1,    1
+        1,    1,    1,    1,    2,    2,    1,    1,    1,    1,
+        2,    1,    2,    2,    2,    2,    2,    1,    1
     } ;
 
-static const flex_int16_t yy_base[54] =
+static const flex_int16_t yy_base[58] =
     {   0,
-        0,    0,   27,   28,   30,   34,   62,   63,   63,   63,
-       43,   63,   63,   63,   63,   63,   63,   63,   63,   63,
-       63,   27,   42,   41,   40,    0,   63,   34,   63,   63,
-       63,   63,   63,   63,   63,   63,   63,   63,   29,   33,
-       63,   63,   63,    0,   32,   35,   22,   16,    0,   63,
-       51,   53,   29
+        0,    0,   28,   29,   31,   35,   70,   71,   30,   71,
+       45,   71,   71,   71,   71,   71,   71,   71,   71,   71,
+       71,   28,   71,   44,   43,   42,    0,   71,   36,   57,
+       71,   71,   71,   71,   71,   71,   71,   71,   37,   55,
+       71,   30,   34,   71,   71,   71,    0,   31,   53,   36,
+       28,   30,    0,   71,   65,   67,   46
     } ;
 
-static const flex_int16_t yy_def[54] =
+static const flex_int16_t yy_def[58] =
     {   0,
-       50,    1,   51,   51,   52,   52,   50,   50,   50,   50,
-       50,   50,   50,   50,   50,   50,   50,   50,   50,   50,
-       50,   50,   50,   50,   50,   53,   50,   53,   50,   50,
-       50,   50,   50,   50,   50,   50,   50,   50,   50,   50,
-       50,   50,   50,   53,   53,   50,   53,   53,   53,    0,
-       50,   50,   50
+       54,    1,   55,   55,   56,   56,   54,   54,   54,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,   54,   54,
+       54,   54,   54,   54,   54,   54,   57,   54,   57,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,   54,   54,
+       54,   54,   54,   54,   54,   54,   57,   57,   54,   54,
+       57,   57,   57,    0,   54,   54,   54
     } ;
 
-static const flex_int16_t yy_nxt[92] =
+static const flex_int16_t yy_nxt[101] =
     {   0,
         8,    9,   10,   11,   12,   13,   14,   15,   16,   17,
        18,   19,    8,   20,   21,   22,   23,   24,   25,   26,
-       27,   26,   26,   26,   26,   28,   29,   30,   32,   32,
-       44,   35,   33,   33,   36,   35,   37,   49,   36,   39,
-       37,   40,   40,   46,   46,   39,   48,   40,   40,   46,
-       46,   31,   31,   34,   34,   47,   45,   43,   42,   41,
-       38,   50,    7,   50,   50,   50,   50,   50,   50,   50,
-       50,   50,   50,   50,   50,   50,   50,   50,   50,   50,
-       50,   50,   50,   50,   50,   50,   50,   50,   50,   50,
-       50
+       27,   28,   27,   27,   27,   27,   29,   30,   31,   33,
+       33,   39,   36,   34,   34,   37,   36,   38,   39,   37,
+       42,   38,   43,   43,   50,   50,   42,   47,   43,   43,
+       50,   50,   53,   52,   49,   51,   49,   40,   49,   48,
+       46,   45,   44,   41,   40,   32,   32,   35,   35,   54,
+        7,   54,   54,   54,   54,   54,   54,   54,   54,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,   54,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,   54,   54
 
     } ;
 
-static const flex_int16_t yy_chk[92] =
+static const flex_int16_t yy_chk[101] =
     {   0,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
-        1,    1,    1,    1,    1,    1,    1,    1,    3,    4,
-       53,    5,    3,    4,    5,    6,    5,   48,    6,   22,
-        6,   22,   22,   39,   39,   40,   47,   40,   40,   46,
-       46,   51,   51,   52,   52,   45,   28,   25,   24,   23,
-       11,    7,   50,   50,   50,   50,   50,   50,   50,   50,
-       50,   50,   50,   50,   50,   50,   50,   50,   50,   50,
-       50,   50,   50,   50,   50,   50,   50,   50,   50,   50,
-       50
+        1,    1,    1,    1,    1,    1,    1,    1,    1,    3,
+        4,    9,    5,    3,    4,    5,    6,    5,   39,    6,
+       22,    6,   22,   22,   42,   42,   43,   57,   43,   43,
+       50,   50,   52,   51,   49,   48,   40,    9,   30,   29,
+       26,   25,   24,   11,   39,   55,   55,   56,   56,    7,
+       54,   54,   54,   54,   54,   54,   54,   54,   54,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,   54,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,   54,   54
 
     } ;
 
@@ -476,9 +477,9 @@ char *yytext;
 #line 2 "gwarf_lex.l"
     #include<stdio.h>
     #include"y.tab.h"
-#line 479 "lex.yy.c"
+#line 480 "lex.yy.c"
 
-#line 481 "lex.yy.c"
+#line 482 "lex.yy.c"
 
 #define INITIAL 0
 #define COMMENT 1
@@ -699,7 +700,7 @@ YY_DECL
 	{
 #line 6 "gwarf_lex.l"
 
-#line 702 "lex.yy.c"
+#line 703 "lex.yy.c"
 
 	while ( /*CONSTCOND*/1 )		/* loops until end-of-file is reached */
 		{
@@ -726,13 +727,13 @@ yy_match:
 			while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
 				{
 				yy_current_state = (int) yy_def[yy_current_state];
-				if ( yy_current_state >= 51 )
+				if ( yy_current_state >= 55 )
 					yy_c = yy_meta[yy_c];
 				}
 			yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
 			++yy_cp;
 			}
-		while ( yy_base[yy_current_state] != 63 );
+		while ( yy_base[yy_current_state] != 71 );
 
 yy_find_action:
 		yy_act = yy_accept[yy_current_state];
@@ -772,6 +773,7 @@ YY_RULE_SETUP
 {return RB;}
 	YY_BREAK
 case 4:
+/* rule 4 can match eol */
 YY_RULE_SETUP
 #line 11 "gwarf_lex.l"
 {return LP;}
@@ -886,15 +888,15 @@ YY_RULE_SETUP
 case 25:
 YY_RULE_SETUP
 #line 41 "gwarf_lex.l"
-{printf("text = [%s];\n", yytext);}
+{return STOP;}
 	YY_BREAK
 case 26:
-/* rule 26 can match eol */
 YY_RULE_SETUP
-#line 43 "gwarf_lex.l"
-{BEGIN INITIAL;}
+#line 42 "gwarf_lex.l"
+{printf("text = [%s];\n", yytext);}
 	YY_BREAK
 case 27:
+/* rule 27 can match eol */
 YY_RULE_SETUP
 #line 44 "gwarf_lex.l"
 {BEGIN INITIAL;}
@@ -902,12 +904,12 @@ YY_RULE_SETUP
 case 28:
 YY_RULE_SETUP
 #line 45 "gwarf_lex.l"
-;
+{BEGIN INITIAL;}
 	YY_BREAK
 case 29:
 YY_RULE_SETUP
-#line 47 "gwarf_lex.l"
-{BEGIN INITIAL;}
+#line 46 "gwarf_lex.l"
+;
 	YY_BREAK
 case 30:
 YY_RULE_SETUP
@@ -915,28 +917,33 @@ YY_RULE_SETUP
 {BEGIN INITIAL;}
 	YY_BREAK
 case 31:
-/* rule 31 can match eol */
 YY_RULE_SETUP
 #line 49 "gwarf_lex.l"
+{BEGIN INITIAL;}
+	YY_BREAK
+case 32:
+/* rule 32 can match eol */
+YY_RULE_SETUP
+#line 50 "gwarf_lex.l"
 {
     yylval.string_value = yytext;
     return STRING;
     }
 	YY_BREAK
-case 32:
+case 33:
 YY_RULE_SETUP
-#line 53 "gwarf_lex.l"
+#line 54 "gwarf_lex.l"
 {
     yylval.string_value = yytext;
     return STRING;
     }
 	YY_BREAK
-case 33:
+case 34:
 YY_RULE_SETUP
-#line 57 "gwarf_lex.l"
+#line 58 "gwarf_lex.l"
 ECHO;
 	YY_BREAK
-#line 939 "lex.yy.c"
+#line 946 "lex.yy.c"
 case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(COMMENT):
 case YY_STATE_EOF(STRING_TEXT):
@@ -1235,7 +1242,7 @@ static int yy_get_next_buffer (void)
 		while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
 			{
 			yy_current_state = (int) yy_def[yy_current_state];
-			if ( yy_current_state >= 51 )
+			if ( yy_current_state >= 55 )
 				yy_c = yy_meta[yy_c];
 			}
 		yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
@@ -1263,11 +1270,11 @@ static int yy_get_next_buffer (void)
 	while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
 		{
 		yy_current_state = (int) yy_def[yy_current_state];
-		if ( yy_current_state >= 51 )
+		if ( yy_current_state >= 55 )
 			yy_c = yy_meta[yy_c];
 		}
 	yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
-	yy_is_jam = (yy_current_state == 50);
+	yy_is_jam = (yy_current_state == 54);
 
 		return yy_is_jam ? 0 : yy_current_state;
 }
@@ -1943,7 +1950,7 @@ void yyfree (void * ptr )
 
 #define YYTABLES_NAME "yytables"
 
-#line 57 "gwarf_lex.l"
+#line 58 "gwarf_lex.l"
 
 int yywrap(void) {
     return 1;

+ 146 - 112
paser/y.tab.c

@@ -72,9 +72,8 @@
     #include"lex.yy.c"
     #include"../gwarf_interpreter/interprete.h"
     extern int yylex (void);
-    statement *the_global_code, *now_code;
 
-#line 78 "y.tab.c"
+#line 77 "y.tab.c"
 
 # ifndef YY_CAST
 #  ifdef __cplusplus
@@ -172,14 +171,14 @@ extern int yydebug;
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 union YYSTYPE
 {
-#line 9 "gwarf_yacc.y"
+#line 8 "gwarf_yacc.y"
 
     int int_value;
     double double_value;
     char *string_value;
     void *statement_value;
 
-#line 183 "y.tab.c"
+#line 182 "y.tab.c"
 
 };
 typedef union YYSTYPE YYSTYPE;
@@ -496,18 +495,18 @@ union yyalloc
 #endif /* !YYCOPY_NEEDED */
 
 /* YYFINAL -- State number of the termination state.  */
-#define YYFINAL  16
+#define YYFINAL  20
 /* YYLAST -- Last index in YYTABLE.  */
-#define YYLAST   37
+#define YYLAST   59
 
 /* YYNTOKENS -- Number of terminals.  */
 #define YYNTOKENS  24
 /* YYNNTS -- Number of nonterminals.  */
-#define YYNNTS  11
+#define YYNNTS  14
 /* YYNRULES -- Number of rules.  */
-#define YYNRULES  26
+#define YYNRULES  30
 /* YYNSTATES -- Number of states.  */
-#define YYNSTATES  43
+#define YYNSTATES  54
 
 #define YYUNDEFTOK  2
 #define YYMAXUTOK   278
@@ -556,9 +555,10 @@ static const yytype_int8 yytranslate[] =
   /* YYRLINE[YYN] -- Source line where rule number YYN was defined.  */
 static const yytype_uint8 yyrline[] =
 {
-       0,    22,    22,    26,    32,    41,    45,    52,    56,    68,
-      69,    78,    87,    96,   105,   114,   126,   127,   136,   148,
-     149,   158,   170,   171,   172,   179,   190
+       0,    21,    21,    22,    26,    33,    43,    47,    54,    58,
+      70,    71,    80,    89,    98,   107,   116,   128,   129,   138,
+     150,   151,   160,   172,   173,   174,   181,   192,   204,   210,
+     216
 };
 #endif
 
@@ -571,7 +571,8 @@ static const char *const yytname[] =
   "DIV", "MUL", "EQ", "LESS", "MORE", "RB", "LB", "RP", "LP", "WHILE",
   "STOP", "POW", "EQUAL", "MOREEQ", "LESSEQ", "NOTEQ", "$accept",
   "command_block", "command_list", "command", "top_exp", "third_number",
-  "second_number", "first_number", "element", "base_number", "base_var_", YY_NULLPTR
+  "second_number", "first_number", "element", "base_number", "base_var_",
+  "while_block", "while_exp", "block", YY_NULLPTR
 };
 #endif
 
@@ -586,7 +587,7 @@ static const yytype_int16 yytoknum[] =
 };
 # endif
 
-#define YYPACT_NINF (-18)
+#define YYPACT_NINF (-7)
 
 #define yypact_value_is_default(Yyn) \
   ((Yyn) == YYPACT_NINF)
@@ -600,11 +601,12 @@ static const yytype_int16 yytoknum[] =
      STATE-NUM.  */
 static const yytype_int8 yypact[] =
 {
-      -1,   -18,   -18,     2,   -18,    12,    -1,   -18,    18,     3,
-       4,    10,   -18,   -18,    27,     9,   -18,   -18,   -18,     2,
-       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
-     -18,     4,   -18,     4,     4,     4,     4,     4,    10,    10,
-     -18,   -18,   -18
+       3,    -7,    -7,    37,    10,    -7,    12,    11,    -7,    -3,
+      16,    -2,     1,    -7,    -7,    20,    -7,    15,    22,    37,
+      -7,    -7,    -7,    37,    37,    37,    37,    37,    37,    37,
+      37,    37,    37,    37,    11,    -7,    -7,    28,    -2,    -7,
+      -2,    -2,    -2,    -2,    -2,     1,     1,    -7,    -7,    -7,
+       8,    -7,    25,    -7
 };
 
   /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
@@ -612,25 +614,26 @@ static const yytype_int8 yypact[] =
      means the default is an error.  */
 static const yytype_int8 yydefact[] =
 {
-       0,    25,    26,     0,     5,     0,     2,     3,     0,     7,
-       9,    16,    19,    22,    23,     0,     1,     4,     6,     0,
-       0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
-      24,    12,    23,    11,    10,    13,    14,    15,    17,    18,
-      21,    20,     8
+       0,    26,    27,     0,     0,     6,     0,     2,     4,     0,
+       8,    10,    17,    20,    23,    24,     3,     0,     0,     0,
+       1,     5,     7,     0,     0,     0,     0,     0,     0,     0,
+       0,     0,     0,     0,     0,    28,    25,     0,    13,    24,
+      12,    11,    14,    15,    16,    18,    19,    22,    21,     9,
+       0,    29,     0,    30
 };
 
   /* YYPGOTO[NTERM-NUM].  */
 static const yytype_int8 yypgoto[] =
 {
-     -18,   -18,   -18,    22,    -2,   -18,    11,   -17,    -7,   -18,
-       0
+      -7,    -7,    14,    -6,    -1,    -7,    29,    17,    27,    -7,
+       0,    -7,    -7,    -7
 };
 
   /* YYDEFGOTO[NTERM-NUM].  */
 static const yytype_int8 yydefgoto[] =
 {
-      -1,     5,     6,     7,     8,     9,    10,    11,    12,    13,
-      32
+      -1,     6,     7,     8,     9,    10,    11,    12,    13,    14,
+      39,    16,    17,    35
 };
 
   /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM.  If
@@ -638,45 +641,52 @@ static const yytype_int8 yydefgoto[] =
      number is the opposite.  If YYTABLE_NINF, syntax error.  */
 static const yytype_int8 yytable[] =
 {
-      14,    15,     1,    14,     2,     1,    14,     2,    38,    39,
-      25,    26,    16,     3,    19,    20,     3,     4,    27,    28,
-      40,    41,    30,    21,    22,    23,    24,    42,    17,    14,
-      31,    33,    34,    35,    36,    37,    18,    29
+      15,    21,    18,    15,    29,    30,     1,    15,     2,    31,
+      32,     1,    20,     2,     1,    22,     2,     3,    37,    15,
+       4,     5,     3,    52,    19,     3,     5,    23,    24,     5,
+      33,    34,    49,    15,    15,    36,    25,    26,    27,    28,
+       1,    51,     2,    53,    21,     0,    45,    46,    50,     0,
+      15,     3,    38,    40,    41,    42,    43,    44,    47,    48
 };
 
 static const yytype_int8 yycheck[] =
 {
-       0,     3,     3,     3,     5,     3,     6,     5,    25,    26,
-       6,     7,     0,    14,    11,    12,    14,    18,     8,     9,
-      27,    28,    13,    20,    21,    22,    23,    29,     6,    29,
-      19,    20,    21,    22,    23,    24,    18,    10
+       0,     7,     3,     3,     6,     7,     3,     7,     5,     8,
+       9,     3,     0,     5,     3,    18,     5,    14,    19,    19,
+      17,    18,    14,    15,    14,    14,    18,    11,    12,    18,
+      10,    16,    33,    33,    34,    13,    20,    21,    22,    23,
+       3,    13,     5,    18,    50,    -1,    29,    30,    34,    -1,
+      50,    14,    23,    24,    25,    26,    27,    28,    31,    32
 };
 
   /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
      symbol of state STATE-NUM.  */
 static const yytype_int8 yystos[] =
 {
-       0,     3,     5,    14,    18,    25,    26,    27,    28,    29,
-      30,    31,    32,    33,    34,    28,     0,    27,    18,    11,
-      12,    20,    21,    22,    23,     6,     7,     8,     9,    10,
-      13,    30,    34,    30,    30,    30,    30,    30,    31,    31,
-      32,    32,    28
+       0,     3,     5,    14,    17,    18,    25,    26,    27,    28,
+      29,    30,    31,    32,    33,    34,    35,    36,    28,    14,
+       0,    27,    18,    11,    12,    20,    21,    22,    23,     6,
+       7,     8,     9,    10,    16,    37,    13,    28,    30,    34,
+      30,    30,    30,    30,    30,    31,    31,    32,    32,    28,
+      26,    13,    15,    18
 };
 
   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
 static const yytype_int8 yyr1[] =
 {
-       0,    24,    25,    26,    26,    27,    27,    28,    28,    29,
-      29,    29,    29,    29,    29,    29,    30,    30,    30,    31,
-      31,    31,    32,    32,    32,    33,    34
+       0,    24,    25,    25,    26,    26,    27,    27,    28,    28,
+      29,    29,    29,    29,    29,    29,    29,    30,    30,    30,
+      31,    31,    31,    32,    32,    32,    33,    34,    35,    36,
+      37
 };
 
   /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.  */
 static const yytype_int8 yyr2[] =
 {
-       0,     2,     1,     1,     2,     1,     2,     1,     3,     1,
-       3,     3,     3,     3,     3,     3,     1,     3,     3,     1,
-       3,     3,     1,     1,     3,     1,     1
+       0,     2,     1,     1,     1,     2,     1,     2,     1,     3,
+       1,     3,     3,     3,     3,     3,     3,     1,     3,     3,
+       1,     3,     3,     1,     1,     3,     1,     1,     2,     4,
+       4
 };
 
 
@@ -1371,52 +1381,54 @@ yyreduce:
   YY_REDUCE_PRINT (yyn);
   switch (yyn)
     {
-  case 3:
+  case 4:
 #line 27 "gwarf_yacc.y"
     {
         if((yyvsp[0].statement_value) != NULL){
-            append_statement(now_code, (yyvsp[0].statement_value));
+            statement *tmp = find_statement_list(0, statement_base);
+            append_statement(tmp, (yyvsp[0].statement_value));
         }
     }
-#line 1382 "y.tab.c"
+#line 1393 "y.tab.c"
     break;
 
-  case 4:
-#line 33 "gwarf_yacc.y"
+  case 5:
+#line 34 "gwarf_yacc.y"
     {   
         if((yyvsp[0].statement_value) != NULL){
-            append_statement(global_inter->global_code, (yyvsp[0].statement_value));
+            statement *tmp = find_statement_list(0, statement_base);
+            append_statement(tmp, (yyvsp[0].statement_value));
         }
     }
-#line 1392 "y.tab.c"
+#line 1404 "y.tab.c"
     break;
 
-  case 5:
-#line 42 "gwarf_yacc.y"
+  case 6:
+#line 44 "gwarf_yacc.y"
     {
         (yyval.statement_value) = NULL;
     }
-#line 1400 "y.tab.c"
+#line 1412 "y.tab.c"
     break;
 
-  case 6:
-#line 46 "gwarf_yacc.y"
+  case 7:
+#line 48 "gwarf_yacc.y"
     {
         (yyval.statement_value) = (yyvsp[-1].statement_value);
     }
-#line 1408 "y.tab.c"
+#line 1420 "y.tab.c"
     break;
 
-  case 7:
-#line 53 "gwarf_yacc.y"
+  case 8:
+#line 55 "gwarf_yacc.y"
     {
         (yyval.statement_value) = (yyvsp[0].statement_value);
     }
-#line 1416 "y.tab.c"
+#line 1428 "y.tab.c"
     break;
 
-  case 8:
-#line 57 "gwarf_yacc.y"
+  case 9:
+#line 59 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1425,11 +1437,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1429 "y.tab.c"
+#line 1441 "y.tab.c"
     break;
 
-  case 10:
-#line 70 "gwarf_yacc.y"
+  case 11:
+#line 72 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1438,11 +1450,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1442 "y.tab.c"
+#line 1454 "y.tab.c"
     break;
 
-  case 11:
-#line 79 "gwarf_yacc.y"
+  case 12:
+#line 81 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1451,11 +1463,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1455 "y.tab.c"
+#line 1467 "y.tab.c"
     break;
 
-  case 12:
-#line 88 "gwarf_yacc.y"
+  case 13:
+#line 90 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1464,11 +1476,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1468 "y.tab.c"
+#line 1480 "y.tab.c"
     break;
 
-  case 13:
-#line 97 "gwarf_yacc.y"
+  case 14:
+#line 99 "gwarf_yacc.y"
         {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1477,11 +1489,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1481 "y.tab.c"
+#line 1493 "y.tab.c"
     break;
 
-  case 14:
-#line 106 "gwarf_yacc.y"
+  case 15:
+#line 108 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1490,11 +1502,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1494 "y.tab.c"
+#line 1506 "y.tab.c"
     break;
 
-  case 15:
-#line 115 "gwarf_yacc.y"
+  case 16:
+#line 117 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1503,11 +1515,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1507 "y.tab.c"
+#line 1519 "y.tab.c"
     break;
 
-  case 17:
-#line 128 "gwarf_yacc.y"
+  case 18:
+#line 130 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1516,11 +1528,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1520 "y.tab.c"
+#line 1532 "y.tab.c"
     break;
 
-  case 18:
-#line 137 "gwarf_yacc.y"
+  case 19:
+#line 139 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1529,11 +1541,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1533 "y.tab.c"
+#line 1545 "y.tab.c"
     break;
 
-  case 20:
-#line 150 "gwarf_yacc.y"
+  case 21:
+#line 152 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1542,11 +1554,11 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1546 "y.tab.c"
+#line 1558 "y.tab.c"
     break;
 
-  case 21:
-#line 159 "gwarf_yacc.y"
+  case 22:
+#line 161 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = operation;
@@ -1555,19 +1567,19 @@ yyreduce:
         code_tmp->code.operation.right_exp = (yyvsp[0].statement_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1559 "y.tab.c"
+#line 1571 "y.tab.c"
     break;
 
-  case 24:
-#line 173 "gwarf_yacc.y"
+  case 25:
+#line 175 "gwarf_yacc.y"
     {
         (yyval.statement_value) = (yyvsp[-1].statement_value);
     }
-#line 1567 "y.tab.c"
+#line 1579 "y.tab.c"
     break;
 
-  case 25:
-#line 180 "gwarf_yacc.y"
+  case 26:
+#line 182 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->type = base_value;
@@ -1575,11 +1587,11 @@ yyreduce:
         code_tmp->code.base_value.value.value.double_value = (yyvsp[0].double_value);
         (yyval.statement_value) = code_tmp;
     }
-#line 1579 "y.tab.c"
+#line 1591 "y.tab.c"
     break;
 
-  case 26:
-#line 191 "gwarf_yacc.y"
+  case 27:
+#line 193 "gwarf_yacc.y"
     {
         statement *code_tmp =  make_statement();
         code_tmp->code.base_var.var_name = malloc(sizeof((yyvsp[0].string_value)));
@@ -1588,11 +1600,35 @@ yyreduce:
         strcpy(name_tmp, (yyvsp[0].string_value));
         (yyval.statement_value) = code_tmp;
     }
-#line 1592 "y.tab.c"
+#line 1604 "y.tab.c"
+    break;
+
+  case 28:
+#line 205 "gwarf_yacc.y"
+    {
+        printf("start-1\n");
+    }
+#line 1612 "y.tab.c"
+    break;
+
+  case 29:
+#line 211 "gwarf_yacc.y"
+    {
+        printf("start-2\n");
+    }
+#line 1620 "y.tab.c"
+    break;
+
+  case 30:
+#line 217 "gwarf_yacc.y"
+    {
+        printf("start-0\n");
+    }
+#line 1628 "y.tab.c"
     break;
 
 
-#line 1596 "y.tab.c"
+#line 1632 "y.tab.c"
 
       default: break;
     }
@@ -1824,19 +1860,17 @@ yyreturn:
 #endif
   return yyresult;
 }
-#line 201 "gwarf_yacc.y"
+#line 221 "gwarf_yacc.y"
 
 int yyerror(char const *str)
 {
-    fprintf(stderr, "parser error near %s\n", yytext);
+    fprintf(stderr, "parser error near %s ;\n", yytext, yytext);
     return 0;
 }
 
-int parser(void)
+int parser(char *file)
 {
-    the_global_code = global_inter->global_code;
-    now_code = the_global_code;
-    yyin = fopen("/home/songzihuan/test.gwf","r");
+    yyin = fopen(file,"r");
     yyparse();
     return 0;
 }

+ 1 - 1
paser/y.tab.h

@@ -99,7 +99,7 @@ extern int yydebug;
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 union YYSTYPE
 {
-#line 9 "gwarf_yacc.y"
+#line 8 "gwarf_yacc.y"
 
     int int_value;
     double double_value;