-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_handler.c
executable file
·263 lines (247 loc) · 7.43 KB
/
parser_handler.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "parser_handler.h"
extern parser_node* parser;
extern FILE* output;
void result_push_back(char *content) {
parse_node* target = (parse_node*)malloc(sizeof(parse_node));
memset(target->content, 0, sizeof(char) * 1024);
strcpy(target->content, content);
parse_node* head = parser->parse_result;
if (!head) {
parser->parse_result = target;
return;
}
while (head->next) {
head = head->next;
}
head->next = target;
}
void result_push_front(char *content) {
parse_node* target = (parse_node*)malloc(sizeof(parse_node));
memset(target->content, 0, sizeof(char) * 1024);
strcpy(target->content, content);
parse_node* head = parser->parse_result;
if (!head) {
parser->parse_result = target;
return;
}
target->next = parser->parse_result;
parser->parse_result = target;
}
void dump_parse_result() {
if (parser->error_num > 0) {
return;
}
// initialize the output file pointer
FILE* output = fopen("compiler_hw3.j", "w");
if (output == NULL) {
printf("Error opening file!\n");
exit(1);
}
// output the class info first
char text[1024] = ".class public compiler_hw3\n.super java/lang/Object";
fprintf(output, "%s\n", text);
parse_node* iter = parser->parse_result;
parse_node* prev = NULL;
while(iter) {
prev = iter;
fprintf(output, "%s\n", iter->content);
iter = iter->next;
prev->next = NULL;
free(prev);
}
parser->parse_result = NULL;
fclose(output);
}
void function_parse_output_dec() {
// push front the function declaration
if (strcmp(parser->keep_function_name, "main") == 0) {
result_push_back(".method public static main([Ljava/lang/String;)V");
} else {
char temp[1024] = {0};
sprintf(temp, ".method public static %s(", parser->keep_function_name);
for (int i = 0 ; i < strlen(parser->formal_parameters); ++i) {
temp[strlen(temp)] = parser->formal_parameters[i];
}
temp[strlen(temp)] = ')';
temp[strlen(temp)] = parser->keep_function_type;
result_push_back(temp);
}
// push front the stack size declaration
result_push_back(".limit locals 50");
result_push_back(".limit stack 50");
}
void global_variable_parse_output() {
// global variable declaration
if (parser->scope_level == 0) {
char temp[1024];
memset(temp, 0, sizeof(char) * 1024);
if (parser->data_type == 'S') {
sprintf(temp, ".field public static %s %s",
parser->keep_init_variable_name, "Ljava/lang/String");
} else {
sprintf(temp, ".field public static %s %c",
parser->keep_init_variable_name, parser->data_type);
}
if (parser->val.init) {
char initValue[256];
memset(initValue, 0, sizeof(char) * 256);
switch (parser->data_type) {
case 'I':
sprintf(initValue, " = %d", (int)parser->val.v.f);
break;
case 'F':
sprintf(initValue, " = %f", parser->val.v.f);
break;
case 'Z':
sprintf(initValue, " = %s", parser->val.v.f ? "true" : "false");
break;
case 'S':
sprintf(initValue, " = %s", parser->val.v.s);
break;
}
strcat(temp, initValue);
parser->val.init = false;
}
result_push_back(temp);
}
}
int local_variable_load() {
if (parser->scope_level > 0 && strlen(parser->decl_name) > 0) {
indexed_symbol_node *target = search_symbol(parser->scope_level, parser->decl_name);
if (!target) {
char msg[1024];
sprintf(msg, "Undeclared variable %s", parser->decl_name);
yyerror(msg);
memset(parser->decl_name, 0, sizeof(char) * 256);
return 0;
}
char temp[1024] = {0};
memset(temp, 0, sizeof(char) * 1024);
if (target->content->scope_level > 0) {
switch (target->content->data_type) {
case 'I':
sprintf(temp, "iload %d", target->index);
break;
case 'F':
sprintf(temp, "fload %d", target->index);
break;
case 'S':
sprintf(temp, "aload %d", target->index);
break;
}
} else {
switch (target->content->data_type) {
case 'I':
sprintf(temp, "getstatic compiler_hw3/%s %c", target->content->name, target->content->data_type);
break;
case 'F':
sprintf(temp, "getstatic compiler_hw3/%s %c", target->content->name, target->content->data_type);
break;
case 'S':
sprintf(temp, "getstatic compiler_hw3/%s %s", target->content->name, "Ljava/lang/String;");
break;
}
}
result_push_back(temp);
if (target->content->data_type == 'I' && parser->assigning) {
result_push_back("i2f");
}
memset(parser->decl_name, 0, sizeof(char) * 256);
return 1;
}
memset(parser->decl_name, 0, sizeof(char) * 256);
return 0;
}
void load_lhs_variable() {
if (parser->scope_level > 0 && parser->assigning && strlen(parser->keep_init_variable_name) > 0) {
indexed_symbol_node *target = search_symbol(parser->scope_level, parser->keep_init_variable_name);
char temp[1024] = {0};
memset(temp, 0, sizeof(char) * 1024);
if (target->content->scope_level > 0) {
switch (target->content->data_type) {
case 'I':
sprintf(temp, "iload %d", target->index);
break;
case 'F':
sprintf(temp, "fload %d", target->index);
break;
case 'S':
sprintf(temp, "aload %d", target->index);
break;
}
} else {
switch (target->content->data_type) {
case 'I':
sprintf(temp, "getstatic compiler_hw3/%s %c", target->content->name, target->content->data_type);
break;
case 'F':
sprintf(temp, "getstatic compiler_hw3/%s %c", target->content->name, target->content->data_type);
break;
case 'S':
sprintf(temp, "getstatic compiler_hw3/%s %s", target->content->name, "Ljava/lang/String;");
break;
}
}
result_push_back(temp);
if (target->content->data_type == 'I') {
result_push_back("i2f");
}
}
}
void relation_parse_output() {
if (!parser->while_stat) {
parser->else_exist = false;
parser->if_num[parser->scope_level]++;
} else {
parser->while_num[parser->scope_level]++;
}
char temp[1024] = {0};
memset(temp, 0, sizeof(char) * 1024);
result_push_back("fsub");
result_push_back("f2i");
switch (parser->relation_type) {
case 0:
if (!parser->while_stat) {
sprintf(temp, "ifne Label_%d_%d", parser->scope_level, parser->if_num[parser->scope_level]);
} else {
sprintf(temp, "ifne WLabel_%d_%d", parser->scope_level, parser->while_num[parser->scope_level]);
}
break;
case 1:
if (!parser->while_stat) {
sprintf(temp, "ifeq Label_%d_%d", parser->scope_level, parser->if_num[parser->scope_level]);
} else {
sprintf(temp, "ifeq WLabel_%d_%d", parser->scope_level, parser->while_num[parser->scope_level]);
}
break;
case 2:
if (!parser->while_stat) {
sprintf(temp, "ifge Label_%d_%d", parser->scope_level, parser->if_num[parser->scope_level]);
} else {
sprintf(temp, "ifge WLabel_%d_%d", parser->scope_level, parser->while_num[parser->scope_level]);
}
break;
case 3:
if (!parser->while_stat) {
sprintf(temp, "ifle Label_%d_%d", parser->scope_level, parser->if_num[parser->scope_level]);
} else {
sprintf(temp, "ifle WLabel_%d_%d", parser->scope_level, parser->while_num[parser->scope_level]);
}
break;
case 4:
if (!parser->while_stat) {
sprintf(temp, "ifgt Label_%d_%d", parser->scope_level, parser->if_num[parser->scope_level]);
} else {
sprintf(temp, "ifgt WLabel_%d_%d", parser->scope_level, parser->while_num[parser->scope_level]);
}
break;
case 5:
if (!parser->while_stat) {
sprintf(temp, "iflt Label_%d_%d", parser->scope_level, parser->if_num[parser->scope_level]);
} else {
sprintf(temp, "iflt WLabel_%d_%d", parser->scope_level, parser->while_num[parser->scope_level]);
}
break;
}
result_push_back(temp);
}