-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang.y
190 lines (144 loc) · 4.79 KB
/
golang.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Reference: http://dinosaur.compilertools.net/bison/bison_5.html
*/
%{
#include<stdio.h>
#include<stdlib.h>
#include "enums.h"
%}
%union {
char val[1024];
TokensT Token; // For managing tokens like '{'
OperationsT Operation; // For refering to operation like '+'
RelopsT Relop; // For refering to operations like '=='
TypesT Types; // For returning types during variable declaration
KeywordsT Keywords; // For returning Keywords like if for further processing
ExpressionT Expression;
codeBlock *code;
}
%token <Token> LRPAREN RRPAREN LCPAREN RCPAREN LSPAREN RSPAREN
%token <Token> SEMICOLON DOUBLEQUOTE
%token <Operation> ADD SUB MUL DIV
%token <Relop> RELOP
%token <Types> TYPE
// %token <Keywords> FUNC IF ELSE PRINT
%token <val> NUM /* Simple long integer number */
%token <val> FLT /* Simple double precision number */
%token <val> VAR /* Variable and Function */
%token <keyword> PACKAGE IMPORT FUNC IF ELSE PRINT WHILE
%type <Expression> EXPRESSION
%type <code> input Statement SimpleStmt Assignment IncDecStmt PrintStmt While Condition Block IfStmt
%right EQUAL ASSIGN
%right RELOP
%left SUB ADD
%left MUL DIV //'\%'
%left NEG INCREMENT DECREMENT /* Negation--unary minus */
//%right '^' /* Exponentiation */
/* Grammar follows */
%%
Program: input { Concat(FINAL_CODE, $1); }
;
NewLine: '\n'
;
OptionalBlankLines:
| NewLine
;
input: { $$ = Assign(""); }
| Statement input { Concat($1, $2); $$ = $1; }
;
Statement: Block NewLine { $$ = $1; }
| While NewLine { $$ = $1; }
| IfStmt NewLine { $$ = $1; }
| SimpleStmt OptionalBlankLines { $$ = $1; }
;
Condition: EXPRESSION RELOP EXPRESSION { $$ = ConCode($1, $3, $2); }
;
While: WHILE LRPAREN Condition RRPAREN OptionalBlankLines Block { $$ = WhileBlock($3, $6); }
;
IfStmt: IF LRPAREN Condition RRPAREN OptionalBlankLines Block { $$ = IFBlock($3, $6, NULL); }
| IF LRPAREN Condition RRPAREN OptionalBlankLines Block ELSE OptionalBlankLines Block { $$ = IFBlock($3, $6, $9); }
| IF LRPAREN Condition RRPAREN OptionalBlankLines Block ELSE IfStmt { $$ = IFBlock($3, $6, $8); }
;
Block: LCPAREN OptionalBlankLines input RCPAREN { Concat($3, BlockClean()); $$=$3; }
;
SimpleStmt: Assignment Terminator { $$ = $1; }
| PrintStmt Terminator { $$ = $1; }
| IncDecStmt Terminator { $$ = $1; }
;
Terminator: NewLine
| SEMICOLON
;
PrintStmt: PRINT EXPRESSION { $$ = PrintExp($2); }
;
Assignment: VAR ASSIGN EXPRESSION { $$ = VarAssign($1, $3, 0);}
| VAR EQUAL EXPRESSION { $$ = VarAssign($1, $3, 1);}
;
IncDecStmt: VAR INCREMENT { $$ = VarOp($1, Add); }
| VAR DECREMENT { $$ = VarOp($1, Sub); }
;
EXPRESSION: NUM { $$ = DecConst($1, Int); }
| FLT { $$ = DecConst($1, Flt); }
| VAR { $$ = LoadVar($1, 0); }
| EXPRESSION ADD EXPRESSION { $$ = ExpOp($1, $3, Add); }
| EXPRESSION SUB EXPRESSION { $$ = ExpOp($1, $3, Sub); }
| EXPRESSION MUL EXPRESSION { $$ = ExpOp($1, $3, Mul); }
| EXPRESSION DIV EXPRESSION { $$ = ExpOp($1, $3, Div); }
| LRPAREN EXPRESSION RRPAREN { $$ = $2; }
;
/* End of grammar */
%%
int main ()
{
// Init globals
// Empty string to FINAL_CODE
FINAL_CODE = Assign(".data\n\n.text\n\n");
// Defining the global StackTop
StackTop = (StackNode *)calloc(1,sizeof(StackNode));
StackTop->id = 0;
StackTop->offset = 0;
StackTop->constant.Type = Int;
StackTop->constant.num = 0;
StackTop->prev = NULL;
StackTop->next = NULL;
StackTop->Group = Constant;
//
// Init LoopTop
LoopAdd();
//
// Set label counter to 0
label_count = 0;
//
// Set reg_rot
reg_rot = 0;
//
// Set Level
Level = 0;
//
// Register Array
for (int i = 0; i < NUM_REG; ++i)
{
TArray[i].id = 0;
TArray[i].snode = NULL;
TArray[i].load_level = Level;
TArray[i].access_level = Level;
TArray[i].Group = Codeblock;
}
// Output file pointer
FILE *fp;
if (yyparse()==0)
{
Concat(FINAL_CODE, Assign("j END\n\nPRINT:\nli $v0,1\nsyscall\nli $v0,11\nli $a0, 10\nsyscall\njr $ra\n\nEND:\nnop"));
fp=fopen("asmb.asm","w");
fprintf(fp ,"%s\n", FINAL_CODE->code);
fclose(fp);
// printf("%s\n", FINAL_CODE->code);
}
else
{
printf("Unrecoverable compilation error\n");
}
}
void yyerror (char *s) /* Called by yyparse on error */
{
printf ("%s\n", s);
}