-
Notifications
You must be signed in to change notification settings - Fork 0
/
mid.cpp
590 lines (562 loc) · 13.9 KB
/
mid.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "mid.h"
#include <fstream>
using namespace std;
std::vector<void(mid::*)()>mid::func;
mid::mid() {
func.push_back(&mid::f_0);
func.push_back(&mid::f_1);
func.push_back(&mid::f_2);
func.push_back(&mid::f_3);
func.push_back(&mid::f_4);
func.push_back(&mid::f_5);
func.push_back(&mid::f_6);
func.push_back(&mid::f_7);
func.push_back(&mid::f_8);
func.push_back(&mid::f_9);
func.push_back(&mid::f_10);
func.push_back(&mid::f_11);
func.push_back(&mid::f_12);
func.push_back(&mid::f_13);
func.push_back(&mid::f_14);
func.push_back(&mid::f_15);
func.push_back(&mid::f_16);
func.push_back(&mid::f_17);
func.push_back(&mid::f_18);
func.push_back(&mid::f_19);
func.push_back(&mid::f_20);
func.push_back(&mid::f_21);
func.push_back(&mid::f_22);
func.push_back(&mid::f_23);
func.push_back(&mid::f_24);
func.push_back(&mid::f_25);
func.push_back(&mid::f_26);
func.push_back(&mid::f_27);
func.push_back(&mid::f_28);
func.push_back(&mid::f_29);
func.push_back(&mid::f_30);
func.push_back(&mid::f_31);
func.push_back(&mid::f_32);
func.push_back(&mid::f_33);
func.push_back(&mid::f_34);
func.push_back(&mid::f_35);
func.push_back(&mid::f_36);
func.push_back(&mid::f_37);
func.push_back(&mid::f_38);
func.push_back(&mid::f_39);
func.push_back(&mid::f_40);
func.push_back(&mid::f_41);
func.push_back(&mid::f_42);
func.push_back(&mid::f_43);
func.push_back(&mid::f_44);
func.push_back(&mid::f_45);
func.push_back(&mid::f_46);
func.push_back(&mid::f_47);
func.push_back(&mid::f_48);
func.push_back(&mid::f_49);
func.push_back(&mid::f_50);
func.push_back(&mid::f_51);
func.push_back(&mid::f_52);
func.push_back(&mid::f_53);
}
void mid::insert(string name,string kind,string var,int level,int addr) {
m.push_back(new entry(name, kind, var, level, addr));
}
void mid::gen_quad(string q[4]) {
code.push_back(new quad(q));
}
void mid::f_0() {//&->A
sta.pop_back();
}
void mid::f_1() {//A->B!
sta.pop_back();
sta.pop_back();
sta.push_back({ " "," "," "," "});
}
void mid::f_2() {//B->FIKH
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.pop_back();
sta.pop_back();
sta.pop_back();
sta.push_back({ " ",next," "," " });
if (nowproc != "main") {
string temp[4] = { "j","-","-","0" };
gen_quad(temp);
int nn = lookup(nowproc);
int tt = code.size() - 1;
m[nn]->level = tt;
}
}
void mid::f_3() {//F->C
type = 1;
sta.pop_back();
sta.push_back({ " "," "," "," " });
}
void mid::f_4() {//F->NULL
type = 1;
sta.push_back({ " "," "," "," " });
}
void mid::f_5() {//I->D
type = 2;
sta.pop_back();
sta.push_back({ " "," "," "," " });
}
void mid::f_6() {//I->NULL
type = 2;
sta.push_back({ " "," "," "," " });
}
void mid::f_7() {//K->E
sta.pop_back();
sta.push_back({ " "," "," "," " });
}
void mid::f_8() {//K->NULL
sta.push_back({ " "," "," "," " });
}
void mid::f_9() {//C->aG0;
sta.pop_back();
sta.pop_back();
sta.pop_back();
sta.pop_back();
sta.push_back({ " "," "," "," " });
}
void mid::f_10() {//0->,G0
sta.pop_back();
sta.pop_back();
sta.pop_back();
sta.push_back({ " "," "," "," " });
}
void mid::f_11() {//0->NULL
sta.push_back({ " "," "," "," " });
}
void mid::f_12() {//G->p=o
//insert(p, "constant", o, l, dx);
string o = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
string p = sta[sta.size() - 1].name;
sta.pop_back();
sta.push_back({ " "," "," "," " });
insert(p, "constant", o, l, dx);
dx++;
}
void mid::f_13() {//D->bp1;
sta.pop_back();
sta.pop_back();
string p = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
sta.push_back({ " "," "," "," " });
insert(p, "variable", "0", l, dx);
dx++;
}
void mid::f_14() {//1->,p1
int thenum = stoi(sta[sta.size() - 1].name);
sta.pop_back();
string p = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
sta.push_back({ to_string(thenum+1)," "," "," "});
if(type==1)
insert(p, "variable", "0", l, dx);
else if (type == 2) {
string temp[4] = { "push","-","-",p };
gen_quad(temp);
}
dx++;
}
void mid::f_15() {//1->NULL
sta.push_back({ "0"," "," "," " });
}
void mid::f_16() {//E->LB;J
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.pop_back();
sta.pop_back();
string beg = sta[sta.size() - 1].next;
backpatch(beg, next);
sta.pop_back();
sta.push_back({ " ",next," "," " });
l--;
nowproc = "main";
//cout << beg<<" "<<next << endl;
}
void mid::f_17() {//J->EJ
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_18() {//J->NULL
string next = to_string(code.size());
sta.push_back({ " ",next," "," " });
}
void mid::f_19() {//L->cp;
string beg = to_string(code.size());
string beg1 = to_string(code.size()+1);
sta.pop_back();
string name = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
sta.push_back({ " ",beg," "," "});
l++;
string temp[4] = { "j*","-","-","0" };
gen_quad(temp);
insert(name, "procedure",beg1,/*end*/0,/*ret*/0);
nowproc = name;
}
void mid::f_20() {//M->N
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_21() {//M->O
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_22() {//M->P
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_23() {//M->Q
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_24() {//M->R
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_25() {//M->S
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_26() {//M->T
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.push_back({ " ",next," "," " });
}
void mid::f_27() {//M->NULL
string next = to_string(code.size());
sta.push_back({ " ",next," "," " });
}
//上面的各种语句赋值给语句也不用四元式
void mid::f_28() {//N->pdV
string vv = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
string p = sta[sta.size() - 1].name;
if (lookup(p) != -1) {//
string temp[4] = { "=",vv,"-",p };
gen_quad(temp);
}
sta.pop_back();
string next = to_string(code.size());
sta.push_back({ " ",next," "," " });
}
void mid::f_29() {//T->eM2f
sta.pop_back();
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.pop_back();
sta.pop_back();
sta.push_back({" ",next," "," "});
}
void mid::f_30() {//2->;M2
string next = sta[sta.size() - 1].next;
sta.pop_back();
sta.pop_back();
sta.pop_back();
sta.push_back({" ",next," ", " "});
}
void mid::f_31() {//2->NULL
string next = to_string(code.size());
sta.push_back({" ",next," "," "});
}
void mid::f_32() {//W->VsV
string t = to_string(code.size());
string id2 = sta[sta.size() - 1].name;
sta.pop_back();
string judge = sta[sta.size() - 1].name;
sta.pop_back();
string id1 = sta[sta.size() - 1].name;
sta.pop_back();
string temp[4] = { "j"+judge, id1, id2, "0"};
gen_quad(temp);
string f = to_string(code.size());
string temp1[4] = { "j","-","-","0" };
gen_quad(temp1);
sta.push_back({ " "," ",t,f });
}
void mid::f_33() {//W->gV
string t = to_string(code.size());
string id1 = sta[sta.size() - 1].name;
sta.pop_back();
string judge = sta[sta.size() - 1].name;
sta.pop_back();
string id2 = "1";
string temp[4] = { "j" + judge, id1, id2, "0"};
gen_quad(temp);
string f = to_string(code.size());
string temp1[4] = { "j","-","-","0" };
gen_quad(temp1);
sta.push_back({ " "," ",t,f });
}
void mid::f_34() {//V->3YU
string u = sta[sta.size() - 1].name;
sta.pop_back();
string y = sta[sta.size() - 1].name;
sta.pop_back();
string judge = sta[sta.size() - 1].name;
sta.pop_back();
string tempvar = "n" + to_string(newtemp);
insert(tempvar, "tempvar", "0", l, dx);
dx++;
newtemp++;
string temp[4] = {judge,y,"-",tempvar};
gen_quad(temp);
string tempvar1 = "n" + to_string(newtemp);
insert(tempvar1, "tempvar", "0", l, dx);
dx++;
newtemp++;
string temp1[4] = { "+",u,tempvar,tempvar1 };
gen_quad(temp1);
sta.push_back({tempvar1," "," "," "});
}
void mid::f_35() {//3->q
string q = sta[sta.size() - 1].name;
sta.pop_back();
sta.push_back({q," "," "," "});
}
void mid::f_36() {//3->NULL
sta.push_back({ "+"," "," "," " });
}
void mid::f_37() {//U->qYU
string u = sta[sta.size() - 1].name;
sta.pop_back();
string y = sta[sta.size() - 1].name;
sta.pop_back();
string judge = sta[sta.size() - 1].name;
sta.pop_back();
string tempvar = "n" + to_string(newtemp);
insert(tempvar, "tempvar", "0", l, dx);
dx++;
newtemp++;
string temp[4] = { judge,y,"-",tempvar };
gen_quad(temp);
string tempvar1 = "n" + to_string(newtemp);
insert(tempvar1, "tempvar", "0", l, dx);
dx++;
newtemp++;
string temp1[4] = { "+",tempvar,u,tempvar1 };
gen_quad(temp1);
sta.push_back({ tempvar1," "," "," " });
}
void mid::f_38() {//U->NULL
sta.push_back({ "-"," "," "," " });
}
void mid::f_39() {//Y->ZX
string x = sta[sta.size() - 1].name;
string symbol = sta[sta.size() - 1].next;
sta.pop_back();
string z = sta[sta.size() - 1].name;
sta.pop_back();
string tempvar = "n" + to_string(newtemp);
insert(tempvar, "tempvar", "0", l, dx);
dx++;
newtemp++;
string temp[4] = { symbol,x,z,tempvar };
gen_quad(temp);
sta.push_back({tempvar," "," "," "});
}
void mid::f_40() {//X->rZX
string xr = sta[sta.size() - 1].name;
string xs = sta[sta.size() - 1].next;
sta.pop_back();
string z = sta[sta.size() - 1].name;
sta.pop_back();
string symbol = sta[sta.size() - 1].name;
sta.pop_back();
string tempvar = "n" + to_string(newtemp);
insert(tempvar, "tempvar", "0", l, dx);
dx++;
string temp[4] = { xs,xr,z,tempvar };
gen_quad(temp);
newtemp++;
sta.push_back({tempvar,symbol," "," "});
}
void mid::f_41() {//X->NULL
sta.push_back({"1","*"," "," "});
}
void mid::f_42() {//Z->p
string p = sta[sta.size() - 1].name;
sta.pop_back();
sta.push_back({p," "," "," "});
}
void mid::f_43() {//Z->o
string o = sta[sta.size() - 1].name;
sta.pop_back();
sta.push_back({ o," "," "," " });
}
void mid::f_44() {//Z->(V)
sta.pop_back();
string v = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
sta.push_back({v," "," "," "});
}
void mid::f_45() {//O->hWiM //if then 语句待加
string next = sta[sta.size() - 1].next;
string q = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
string f = sta[sta.size() - 1].f;
string t = sta[sta.size() - 1].t;
backpatch(t, q);
string snext = merge(f, next);
code[stoi(f)]->q[3] = next;
//backpatch
sta.pop_back();
sta.pop_back();
sta.push_back({" ",snext," "," "});
}
void mid::f_46() {//Q->jp //应该加跳转
string p = sta[sta.size() - 1].name;
sta.pop_back();
if (lookup(p)!=-1) {
string tempp[4] = { "j_call","-","-",m[lookup(p)]->var};
gen_quad(tempp);
m[lookup(p)]->addr = code.size();
code[m[lookup(p)]->level]->q[3] =to_string( m[lookup(p)]->addr);
}
string j = sta[sta.size() - 1].name;
sta.pop_back();
/*string tempvar = "n" + to_string(newtemp);
insert(tempvar, "tempvar", "0", l, dx);
dx++;
newtemp++;
string temp[4] = { j,p,"-","-" };
gen_quad(temp);*/
string next = to_string(code.size());
sta.push_back({" ",next," "," "});
}
void mid::f_47() {//P->kWlM //while 语句待加
string next = sta[sta.size() - 1].next;
string nextquad1 = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
string t = sta[sta.size() - 1].t;
string f = sta[sta.size() - 1].f;
string nextquad2 = sta[sta.size() - 1].name;
sta.pop_back();
sta.pop_back();
backpatch(next, nextquad2);
backpatch(t, nextquad1);
code[stoi(f)]->q[3] = to_string(stoi(next)+1);
string snext = f;
string temp[4] = { "j","-","-",nextquad2 };
gen_quad(temp);
sta.push_back({" ",snext," "," "});
}
void mid::f_48() {//R->m(p1)
sta.pop_back();
int thenum = stoi(sta[sta.size() - 1].name);
sta.pop_back();
string p = sta[sta.size() - 1].name;
string thetemp[4] = { "push","-","-",p };
gen_quad(thetemp);
sta.pop_back();
sta.pop_back();
sta.pop_back();
string temp[4] = { "read","-","-",to_string(thenum + 1) };
gen_quad(temp);
string next = to_string(code.size());
sta.push_back({" ",next," "," "});
}
void mid::f_49() {//S->n(p1)
sta.pop_back();
int thenum = stoi(sta[sta.size() - 1].name);
sta.pop_back();
string p = sta[sta.size() - 1].name;
string thetemp[4] = { "push","-","-",p };
gen_quad(thetemp);
sta.pop_back();
sta.pop_back();
sta.pop_back();
string temp[4] = { "write","-","-",to_string(thenum+1)};
gen_quad(temp);
string next = to_string(code.size());
sta.push_back({ " ",next," "," " });
}
void mid::f_50() {//H->4M
string next = sta[sta.size() - 1].next;
sta.pop_back();
string nextquad = sta[sta.size() - 1].name;
sta.pop_back();
sta.push_back({ nextquad,next," "," " });
}
void mid::f_51() {//4->NULL
string nextquad = to_string(code.size());
sta.push_back({nextquad ," "," "," " });
}
void mid::f_52() {//5->6W
string t = sta[sta.size() - 1].t;
string f = sta[sta.size() - 1].f;
sta.pop_back();
string nextquad = sta[sta.size() - 1].name;
sta.pop_back();
sta.push_back({ nextquad, " ", t, f });
}
void mid::f_53() {//6->NULL
string nextquad = to_string(code.size());
sta.push_back({ nextquad, " ", " ", " "});
}
void mid::show() {
for (int i = 0; i < m.size(); i++) {
m[i]->lr_table = lr_table;
m[i]->show();
}
for (int i = 0; i < code.size(); i++) {
code[i]->mid_code = mid_code;
ofstream file(mid_code, ios::app);
file << i << " ";
file.close();
code[i]->show();
}
}
int mid::lookup(string name) {
for (int i = 0; i < m.size(); i++) {
if (m[i]->name == name/* && m[i]->level == l*/) {
return i;
}
}
return -1;
}
void mid::backpatch(string t, string q) {
int n = stoi(t);
if (n >= code.size())return;
int nx;
if (n == 0) {
code[n]->q[3] = q;
//cout << 1 << endl;
}
else {
while (code[n]->q[0][0] == 'j' && n != 0) {
nx = stoi(code[n]->q[3]);
code[n]->q[3] = q;
n = nx;
}
}
}
string mid::merge(string p1, string p2) {
if (stoi(p2) >= code.size()) return p1;
int p = stoi(code[stoi(p2)]->q[3]);
while (code[p]->q[0][0] == 'j' && code[p]->q[3] != "0") {
p = stoi(code[p]->q[3]);
}
code[p]->q[3] = p1;
return p2;
}