aboutsummaryrefslogtreecommitdiff
path: root/15/main.y
diff options
context:
space:
mode:
Diffstat (limited to '15/main.y')
-rw-r--r--15/main.y34
1 files changed, 24 insertions, 10 deletions
diff --git a/15/main.y b/15/main.y
index 46075c2..40b7ba2 100644
--- a/15/main.y
+++ b/15/main.y
@@ -6,12 +6,15 @@
6 6
7void apply(char); 7void apply(char);
8void load_num(char); 8void load_num(char);
9void load_var(char); 9void load_var(char *);
10void store_var(char); 10void store_var(char *);
11 11
12int yyerror(char *); 12int yyerror(char *);
13int yylex(); 13int yylex();
14int reg_count=0; 14int reg_count=0;
15
16char cur_str[100];
17
15struct instruction { 18struct 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
29statement: IDENT '=' expr ';' { store_var((char)$1); } 32start: statement NL start
33 | NL
34
35statement: 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
32expr: expr '+' expr {apply('+');} 42expr: 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
58void load_var(char var) { 72void 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
62void store_var(char var) { 76void 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
66int yyerror(char *s) { 80int yyerror(char *s) {