-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmidCode.cpp
111 lines (109 loc) · 2.86 KB
/
midCode.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
#include <iostream>
#include <fstream>
#include <vector>
#include "midCode.h"
using namespace std;
extern vector<midCode> midCodeTable;
extern ofstream midCodefile;
void outputMidCode() {
for (int i = 0; i < midCodeTable.size(); i++) {
midCode mc = midCodeTable[i];
switch (mc.op) {
case PLUSOP:
midCodefile << mc.z << " = " << mc.x << " + " << mc.y << "\n";
break;
case MINUOP:
midCodefile << mc.z << " = " << mc.x << " - " << mc.y << "\n";
break;
case MULTOP:
midCodefile << mc.z << " = " << mc.x << " * " << mc.y << "\n";
break;
case DIVOP:
midCodefile << mc.z << " = " << mc.x << " / " << mc.y << "\n";
break;
case LSSOP:
midCodefile << mc.z << " = (" << mc.x << " < " << mc.y << ")\n";
break;
case LEQOP:
midCodefile << mc.z << " = (" << mc.x << " <= " << mc.y << ")\n";
break;
case GREOP:
midCodefile << mc.z << " = (" << mc.x << " > " << mc.y << ")\n";
break;
case GEQOP:
midCodefile << mc.z << " = (" << mc.x << " >= " << mc.y << ")\n";
break;
case EQLOP:
midCodefile << mc.z << " = (" << mc.x << " == " << mc.y << ")\n";
break;
case NEQOP:
midCodefile << mc.z << " = (" << mc.x << " != " << mc.y << ")\n";
break;
case ASSIGNOP:
midCodefile << mc.z << " = " << mc.x << "\n";
break;
case GOTO:
midCodefile << "GOTO " << mc.z << "\n";
break;
case BZ:
midCodefile << "BZ " << mc.z << "(" << mc.x << "=0)" << "\n";
break;
case BNZ:
midCodefile << "BNZ " << mc.z << "(" << mc.x << "=1)" << "\n";
break;
case PUSH:
midCodefile << "PUSH " << mc.z << "(" << mc.y << ")" << "\n";
break;
case CALL:
midCodefile << "CALL " << mc.z << "\n";
break;
case RET:
midCodefile << "RET " << mc.z << "\n";
break;
case INLINERET:
midCodefile << "INLINERET " << mc.z << "\n";
break;
case RETVALUE:
midCodefile << "RETVALUE " << mc.z << " = " << mc.x << "\n";
break;
case SCAN:
midCodefile << "SCAN " << mc.z << "\n";
break;
case PRINT:
midCodefile << "PRINT " << mc.z << " " << mc.x << "\n";
break;
case LABEL:
midCodefile << mc.z << ": \n";
break;
case CONST:
midCodefile << "CONST " << mc.z << " " << mc.x << " = " << mc.y << endl;
break;
case ARRAY:
midCodefile << "ARRAY " << mc.z << " " << mc.x << "[" << mc.y << "]" << endl;
break;
case VAR:
midCodefile << "VAR " << mc.z << " " << mc.x << endl;
break;
case FUNC:
midCodefile << "FUNC " << mc.z << " " << mc.x << "()" << endl;
break;
case PARAM:
midCodefile << "PARA " << mc.z << " " << mc.x << endl;
break;
case GETARRAY:
midCodefile << mc.z << " = " << mc.x << "[" << mc.y << "]\n";
break;
case PUTARRAY:
midCodefile << mc.z << "[" << mc.x << "]" << " = " << mc.y << "\n";
break;
case EXIT:
midCodefile << "EXIT\n";
break;
case INLINEEND:
midCodefile << "INLINEEND " << mc.z << " " << mc.x << "\n";
break;
default:
break;
}
}
}