diff options
Diffstat (limited to '15/main.y')
-rw-r--r-- | 15/main.y | 34 |
1 files changed, 24 insertions, 10 deletions
@@ -6,12 +6,15 @@ | |||
6 | 6 | ||
7 | void apply(char); | 7 | void apply(char); |
8 | void load_num(char); | 8 | void load_num(char); |
9 | void load_var(char); | 9 | void load_var(char *); |
10 | void store_var(char); | 10 | void store_var(char *); |
11 | 11 | ||
12 | int yyerror(char *); | 12 | int yyerror(char *); |
13 | int yylex(); | 13 | int yylex(); |
14 | int reg_count=0; | 14 | int reg_count=0; |
15 | |||
16 | char cur_str[100]; | ||
17 | |||
15 | struct instruction { | 18 | struct instruction { |
16 | char op1; | 19 | char op1; |
17 | char op2; | 20 | char op2; |
@@ -19,21 +22,32 @@ struct instruction { | |||
19 | }; | 22 | }; |
20 | %} | 23 | %} |
21 | 24 | ||
22 | %token IDENT NUM | 25 | %token IDENT NUM NL |
23 | %type expr | 26 | %type expr |
24 | %left '+' | 27 | %left '+' |
25 | %left '*''/' | 28 | %left '*''/' |
26 | %left '-' | 29 | %left '-' |
27 | %% | 30 | %% |
28 | 31 | ||
29 | statement: IDENT '=' expr ';' { store_var((char)$1); } | 32 | start: statement NL start |
33 | | NL | ||
34 | |||
35 | statement: IDENT { | ||
36 | strcpy(cur_str, yytext); | ||
37 | } '=' expr { | ||
38 | printf("ST %s, R%d\n", cur_str, reg_count-1); | ||
39 | } | ||
30 | ; | 40 | ; |
31 | 41 | ||
32 | expr: expr '+' expr {apply('+');} | 42 | expr: expr '+' expr {apply('+');} |
33 | | expr '-' expr {apply('-');} | 43 | | expr '-' expr {apply('-');} |
34 | | expr '*' expr {apply('*');} | 44 | | expr '*' expr {apply('*');} |
35 | | NUM {load_num((char)$1); $$=(char)$1;} | 45 | | NUM { |
36 | | IDENT {load_var((char)$1); $$=(char)$1;} | 46 | printf("LD R%d, #%c\n", reg_count++, atoi(yytext)); |
47 | } | ||
48 | | IDENT { | ||
49 | printf("LD R%d, %s\n", reg_count++, yytext); | ||
50 | } | ||
37 | ; | 51 | ; |
38 | 52 | ||
39 | %% | 53 | %% |
@@ -55,12 +69,12 @@ void load_num(char num) { | |||
55 | printf("LD R%d, #%c\n", reg_count++, num); | 69 | printf("LD R%d, #%c\n", reg_count++, num); |
56 | } | 70 | } |
57 | 71 | ||
58 | void load_var(char var) { | 72 | void load_var(char *var) { |
59 | printf("LD R%d, %c\n", reg_count++, var); | 73 | printf("LD R%d, %s\n", reg_count++, var); |
60 | } | 74 | } |
61 | 75 | ||
62 | void store_var(char var) { | 76 | void store_var(char* var) { |
63 | printf("ST %c, R%d\n", var, reg_count-1); | 77 | printf("ST %s, R%d\n", var, reg_count-1); |
64 | } | 78 | } |
65 | 79 | ||
66 | int yyerror(char *s) { | 80 | int yyerror(char *s) { |