aboutsummaryrefslogtreecommitdiff
path: root/15
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-06-18 05:30:04 +0100
committerAkshay <[email protected]>2021-06-18 05:30:04 +0100
commit015cbbfb9dd7a08355c25b3a3ea93621b2a64c57 (patch)
treea910adc208ee1fd54a4566d74380a164f4b03f12 /15
parentf9f62e75f3b234b5daf3bc25e995f9f0f56ca0a2 (diff)
yay it worksHEADmaster
Diffstat (limited to '15')
-rw-r--r--15/main.l4
-rw-r--r--15/main.y34
2 files changed, 26 insertions, 12 deletions
diff --git a/15/main.l b/15/main.l
index ffcb97e..1011cdb 100644
--- a/15/main.l
+++ b/15/main.l
@@ -9,12 +9,12 @@ extern int yylval;
9 yylval=yytext[0]; 9 yylval=yytext[0];
10 return NUM; 10 return NUM;
11}; 11};
12[a-zA-Z]+? { 12[a-zA-Z_][a-zA-Z0-9]* {
13 yylval=yytext[0]; 13 yylval=yytext[0];
14 return IDENT; 14 return IDENT;
15} 15}
16. return yytext[0]; 16. return yytext[0];
17\n return 0; 17\n return NL;
18%% 18%%
19 19
20int yywrap() { 20int yywrap() {
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) {