-
Notifications
You must be signed in to change notification settings - Fork 0
/
Printer.cpp
363 lines (344 loc) · 11.1 KB
/
Printer.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// Created by 黄继辉 on 2019-09-30.
//
#include "LexicalAnalyzer.h"
#include "Printer.h"
#include <fstream>
#include "GrammarAnalyzer.h"
#include "ErrorHandle.h"
#include "MiddleCode.h"
#include "ExpressionChecker.h"
#include "Optimal.h"
using namespace std;
extern Token tokens[MAXTXTLENGTH];
extern int tokenstop;
extern int tokensstack;
extern int symbol;
extern char token[MAXTXTLENGTH];
extern char inputtxt[MAXTXTLENGTH];
extern int inputtxt_top;
extern ofstream outfile;
extern ofstream errorfile;
extern ofstream middlecodefile;
extern ofstream mips;
extern char id2Tag[100][100];
extern int countLine;
extern int mcodeTableTop;
extern Mcode mcodeTable[MAXTXTLENGTH];
extern char preExpressionName[20];
extern int isCorrect;
extern int submit;
extern McodeList *mcodeListHead;
Token tokens[MAXTXTLENGTH];
int tokenstop = 0;
int tokensstack = 0;
void printToken() {
for (;tokensstack < tokenstop;tokensstack++) {
//printf("%s %s\n", num2typename[tokens[tokensstack].symbol],tokens[tokensstack].name);
outfile << num2typename[tokens[tokensstack].symbol] << " " << tokens[tokensstack].name << endl;
}
}
void inputToken() {
if (!(symbol==0 && token[0]=='\0')) {
tokens[tokenstop].symbol = symbol;
tokens[tokenstop].line = countLine;
strcpy(tokens[tokenstop].name, token);
tokenstop++;
}
}
void strRetract() {
while(inputtxt[inputtxt_top] != ')'
&& inputtxt[inputtxt_top] != ',') {
inputtxt_top--;
}
}
void printTokenRetract(int a) {
tokenstop -= a;
//printToken();
tokenstop += a;
}
void printTag(int tag) {
outfile << id2Tag[tag] << endl;
//printf("%s\n",id2Tag[tag]);
}
void printError(int errorType) {
int line = countLine;
/*
if (errorType == ERROR_TYPE_K) {
line -= changeLine;
}
else if (errorType == ERROR_TYPE_H) {
line -= changeLine;
}
*/
isCorrect = 0;
errorfile << tokens[tokenstop - 1].line << " " << char('a' + errorType) << endl;
printf("%d %c\n",tokens[tokenstop - 1].line,'a' + errorType);
}
char *getMcode(int i) {
return mcode2string(mcodeTable + i);
}
void printMcode() {
McodeList *mcodeListptr1 = mcodeListHead;
while (mcodeListptr1 != NULL) {
char middleCodeContent[MAXTXTLENGTH] = {0};
strcat(middleCodeContent, mcode2string(&mcodeListptr1->mcode));
mcodeListptr1 = mcodeListptr1->nextnode;
middlecodefile << middleCodeContent << endl;
}
}
void printMips(char *str) {
if (str[0] != 0) {
mips << str;
printf("%s", str);
}
}
char *mcode2string(Mcode *mcodeptr) {
char *tempContent = (char *)malloc(sizeof(char) * MAXTXTLENGTH);
int isgloble = 0;
switch (mcodeptr->midType) {
case MID_FUNC_INT: {
sprintf(tempContent, "int %s()\n", mcodeptr->nameLeft);
break;
}
case MID_FUNC_CHAR: {
sprintf(tempContent, "char %s()\n", mcodeptr->nameLeft);
break;
}
case MID_FUNC_VOID: {
sprintf(tempContent, "void %s()\n", mcodeptr->nameLeft);
break;
}
case MID_CON_CHAR: {
if (mcodeptr->numRight2) {
isgloble = 1;
}
sprintf(tempContent, "const char %s = \'%c\'\n", mcodeptr->nameLeft, mcodeptr->numRight1);
break;
}
case MID_CON_INT: {
if (mcodeptr->numRight2) {
isgloble = 1;
}
sprintf(tempContent, "const int %s = %d\n", mcodeptr->nameLeft, mcodeptr->numRight1);
break;
}
case MID_VAR_CHAR: {
if (mcodeptr->numRight2) {
isgloble = 1;
}
sprintf(tempContent, "var char %s\n", mcodeptr->nameLeft);
break;
}
case MID_VAR_INT: {
if (mcodeptr->numRight2) {
isgloble = 1;
}
sprintf(tempContent, "var int %s\n", mcodeptr->nameLeft);
break;
}
case MID_VAR_ARRAY_INT: {
if (mcodeptr->numRight2) {
isgloble = 1;
}
sprintf(tempContent, "var int %s[%d]\n", mcodeptr->nameLeft, mcodeptr->numLeft);
break;
}
case MID_VAR_ARRAY_CHAR: {
if (mcodeptr->numRight2) {
isgloble = 1;
}
sprintf(tempContent, "var char %s[%d]\n", mcodeptr->nameLeft, mcodeptr->numLeft);
break;
}
case MID_PARA_CHAR: {
sprintf(tempContent, "para char %s\n", mcodeptr->nameLeft);
break;
}
case MID_PARA_INT: {
sprintf(tempContent, "para int %s\n", mcodeptr->nameLeft);
break;
}
case MID_SCANF_CALL:
case MID_PRINTF_CALL:
case MID_CALL: {
sprintf(tempContent, "call %s\n", mcodeptr->nameLeft);
break;
}
case MID_BNZ: {
sprintf(tempContent, "BNZ Label_%d\n", mcodeptr->numRight1);
break;
}
case MID_BZ: {
sprintf(tempContent, "BZ Label_%d\n", mcodeptr->numRight1);
break;
}
case MID_GOTO: {
sprintf(tempContent, "GOTO Label_%d\n", mcodeptr->numRight1);
break;
}
case MID_LABEL: {
sprintf(tempContent, "Label_%d :\n", mcodeptr->numLeft);
break;
}
case MID_READ_ARRAY: {
sprintf(tempContent, "t%d = %s[t%d]\n", mcodeptr->tempLeft, mcodeptr->nameRight1, mcodeptr->indexRight);
break;
}
case MID_CONST_ASSIGH: {
sprintf(tempContent, "CONST %s: t%d = %d\n", mcodeptr->nameLeft, mcodeptr->tempLeft, mcodeptr->numRight1);
break;
}
case MID_ASSIGH_INT: {
sprintf(tempContent, "t%d = %d\n", mcodeptr->tempLeft, mcodeptr->numRight1);
break;
}
case MID_ASSIGH_CHAR: {
sprintf(tempContent, "t%d = \'%c\'\n", mcodeptr->tempLeft, mcodeptr->numRight1);
break;
}
case MID_DIVI: {
sprintf(tempContent, "t%d = t%d / t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_MULT: {
sprintf(tempContent, "t%d = t%d * t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_ADD: {
sprintf(tempContent, "t%d = t%d + t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_MINUS: {
sprintf(tempContent, "t%d = t%d - t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_ASSIGH_OPPOSE: {
sprintf(tempContent, "t%d = -t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1);
break;
}
case MID_PUSH: {
sprintf(tempContent, "PUSH t%d\n", mcodeptr->tempRight1);
break;
}
case MID_STORE_VAR: {
sprintf(tempContent, "STORE %s = t%d\n"
, mcodeptr->nameLeft, mcodeptr->tempRight1);
break;
}
case MID_ASSIGH_VAR: {
sprintf(tempContent, "t%d = %s\n", mcodeptr->tempLeft, mcodeptr->nameRight1);
break;
}
case MID_STORE_ARRAY: {
sprintf(tempContent, "STORE %s[t%d] = t%d\n", mcodeptr->nameLeft, mcodeptr->indexLeft, mcodeptr->tempRight1);
break;
}
case MID_LESS: {
sprintf(tempContent, "t%d < t%d\n", mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_EQUAL_LESS: {
sprintf(tempContent, "t%d <= t%d\n", mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_GREATER: {
sprintf(tempContent, "t%d > t%d\n", mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_EQUAL_GREATER: {
sprintf(tempContent, "t%d >= t%d\n", mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_EQUAL: {
sprintf(tempContent, "t%d == t%d\n", mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_NOT_EQUAL: {
sprintf(tempContent, "t%d != t%d\n", mcodeptr->tempRight1, mcodeptr->tempRight2);
break;
}
case MID_RET_VALUE: {
sprintf(tempContent, "ret t%d END_FUNCTION%d\n", mcodeptr->tempRight1, mcodeptr->numLeft);
break;
}
case MID_RET_NOVALUE: {
sprintf(tempContent, "ret END_FUNCTION%d\n", mcodeptr->numLeft);
break;
}
case MID_SCANF_PUSH: {
sprintf(tempContent, "POSISTION t%d SCANF_PUSH %s\n", mcodeptr->tempLeft, mcodeptr->nameLeft);
break;
}
case MID_PRINTF_PUSH_VAR: {
sprintf(tempContent, "PRINTF_PUSH_VAR t%d %s\n", mcodeptr->tempRight1,(mcodeptr->numLeft)? "int" : "char");
break;
}
case MID_PRINTF_PUSH_STR: {
sprintf(tempContent, "PRINTF_PUSH_STR %s \"%s\"\n", mcodeptr->nameRight1, mcodeptr->nameLeft);
break;
}
case MID_END_FUNCTION: {
sprintf(tempContent, "END_FUNCTION%d :\n", mcodeptr->numLeft);
break;
}
case MID_INLINE_END: {
sprintf(tempContent, "END_INLINE_FUNCTION%d :\n", mcodeptr->numLeft);
break;
}
case MID_NEWLINE: {
sprintf(tempContent, "NEWLINE\n");
break;
}
case MID_MOVE_V0: {
sprintf(tempContent, "MOVE t%d v0\n", mcodeptr->tempLeft);
break;
}
case MID_INLINE_MOVE: {
sprintf(tempContent, "INLINE MOVE t%d t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1);
break;
}
case MID_INLINE_LOAD: {
sprintf(tempContent, "INLINE LOAD t%d t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1);
break;
}
case MID_START_PUSH: {
sprintf(tempContent, "START PUSH\n");
break;
}
case MID_END_PUSH: {
sprintf(tempContent, "END PUSH\n");
break;
}
case MID_INLINE_CALL: {
sprintf(tempContent, "INLINE call %s\n", mcodeptr->nameLeft);
break;
}
case MID_INLINE_STORE: {
sprintf(tempContent, "INLINE STORE MOVE t%d t%d\n", mcodeptr->tempLeft, mcodeptr->tempRight1);
break;
}
case MID_NEW_PRINTF: {
sprintf(tempContent, "NEW PRINTF\n");
break;
}
case MID_ADDI: {
sprintf(tempContent, "ADDI t%d = t%d + %d\n", mcodeptr->tempLeft, mcodeptr->tempRight1, mcodeptr->numRight1);
break;
}
case MID_START_LOOP: {
sprintf(tempContent, "START LOOP\n");
break;
}
case MID_END_LOOP: {
sprintf(tempContent, "END LOOP\n");
break;
}
}
if (isgloble) {
char temp[MAXTXTLENGTH] = "global ";
strcat(temp, tempContent);
strcpy(tempContent, temp);
}
return tempContent;
}