-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.java
More file actions
248 lines (232 loc) · 7 KB
/
CodeGenerator.java
File metadata and controls
248 lines (232 loc) · 7 KB
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
import java.util.ArrayList;
import java.util.Arrays;
public class CodeGenerator {
private static String lineNumber = "0";
private static String mechCode = "";
private static String staticCode = "00 ";
private static String heapCode = "";
private static ArrayList<String> varTable = new ArrayList<String>();
//private static ArrayList<String[]> strTable = new ArrayList<String[]>();
private static ArrayList<String> jumpTable = new ArrayList<String>();
public static void generateCode(ArrayList<Object> ast) {
//need more than just a "T0"...
varTable.add("T0");
for(Object branch : ast) {
if(branch instanceof Object[]) {
generateBranch((Object[])branch);
}
}
mechCode += "00 ";
replaceTemps();
replaceJumps();
String result = mechCode.toUpperCase() + staticCode;
int filler = 255 - (result.length() + heapCode.length()) / 3;
for(int i = 0; i < filler; i++) {
result += "00 ";
}
System.out.println(result + " " + heapCode);
}
private static void generateBranch(Object[] branch) {
String temp = "";
if(branch[0].equals("LINE")) {
lineNumber = (String) branch[1];
}
else if(branch[0].equals("BLOCK")) {
for(int i = 1; i < branch.length; i++) {
generateBranch((Object[])branch[i]);
}
}
else if(branch[0].equals("DECLARE")) {
if(branch[1].equals("INT") || branch[1].equals("BOOL")) {
varTable.add("T" + branch[2]);
staticCode += "00 ";
}
else if(branch[1].equals("STR")) {
varTable.add("T" + branch[2]);
staticCode += "FF ";
//strTable.add(new String[]{("S" + branch[2]), Integer.toHexString(255 - Math.round(heapCode.length()/3)*4)});
}
}
else if(branch[0].equals("SET")) {
if(branch[2] instanceof Object[]) {
temp = generateExpr((Object[])branch[2], branch[1]);
}
else if(branch[2] instanceof String) {
temp = generateExpr((String)branch[2]);
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "]You should never get this message.\n";
}
mechCode += temp + " 8D T" + branch[1] + " 00 ";
}
else if(branch[0].equals("PRINT")) {
if(branch[1] instanceof Object[]) {
temp = generateExpr((Object[])branch[1], "0");
if(((Object[])branch[1])[0].equals("QUOTE")) {
mechCode += temp + " 8D T0 00 AC T0 00 A2 02 FF ";
return;
}
}
else if(branch[1] instanceof String) {
temp = generateExpr((String)branch[1]);
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "]You should never get this message.\n";
}
mechCode += temp + " 8D T0 00 AC T0 00 A2 01 FF ";
}
else if(branch[0].equals("BRANCH") || branch[0].equals("LOOP")) {
boolean isLoop = branch[0].equals("LOOP");
int jStart, jEnd, jPos;
String jump = "00";
jPos = jumpTable.size();
jumpTable.add(jump);
temp = generateExpr((Object[])branch[1], "0");
mechCode += temp + " 8D T0 00 A2 00 EC T0 00 D0 J" + jPos + " ";
jStart = mechCode.replaceAll("J\\d+", "J0").length()/3;
generateBranch((Object[])branch[2]);
jEnd = mechCode.replaceAll("J\\d+", "J0").length()/3;
if(isLoop) {
jEnd += 7;
}
jump = Integer.toHexString(jEnd - jStart);
if(jump.length() == 1)
jump = "0" + jump;
jumpTable.set(jPos, jump);
if(isLoop) {
jump = Integer.toHexString(jEnd - jStart);
if(jump.length() == 1)
jump = "0" + jump;
jumpTable.set(jPos, jump);
jPos = temp.replaceAll("J\\d+", "J0").length()/3 + 17;
jump = Integer.toHexString(256 - jPos);
if(jump.length() == 1)
jump = "0" + jump;
mechCode += "A9 00 EC FF 00 D0 " + jump + " ";
}
}
else if(branch[0].equals("")) {
}
}
private static String generateExpr(Object[] expr, Object id) {
String temp = "";
if(expr[0].equals("PLUS")) {
if(expr[1] instanceof Object[]) {
temp = generateExpr((Object[])expr[1], id);
}
else if (expr[1] instanceof String) {
temp = generateExpr((String)expr[1]);
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "] You should never get this message.\n";
}
temp += " 8D T" + id + " 00 ";
if(expr[2] instanceof Object[]) {
temp += generateExpr((Object[])expr[2], id);
}
else if (expr[2] instanceof String) {
temp += generateExpr((String)expr[2]);
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "] You should never get this message.\n";
}
temp += " 6D T" + id + " 00";
}
else if(expr[0].equals("EQTO") || expr[0].equals("NOTEQ")) {
if(expr[1] instanceof Object[]) {
temp = generateExpr((Object[])expr[1], id);
}
else if (expr[1] instanceof String) {
temp = generateExpr((String)expr[1]);
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "] You should never get this message.\n";
}
temp += " 8D T" + id + " 00 AE T" + id + " 00 ";
if(expr[2] instanceof Object[]) {
temp += generateExpr((Object[])expr[2], id);
}
else if (expr[2] instanceof String) {
temp += generateExpr((String)expr[2]);
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "] You should never get this message.\n";
}
temp += " 8D T" + id + " 00 EC T" + id + " 00";
if(expr[0].equals("EQTO")) {
temp += " A9 01 D0 02 A9 00";
}
else {
temp += " A9 00 D0 02 A9 01";
}
}
else if(expr[0].equals("QUOTE")) {
String quote = "";
for(int i = 1; i < expr.length; i++) {
temp = (String) expr[i];
if(temp.equals("SPACE")) {
quote += "20 ";
}
else if (!temp.equals("EMPTY")){
char[] test = temp.toCharArray();
for(char tst : test) {
quote += Integer.toHexString(tst) + " ";
}
}
}
quote += "00 ";
heapCode = quote + heapCode;
return "A9 " + Integer.toHexString(255 - (heapCode.length()/3));
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "] You should never get this message.\n";
}
return temp;
}
private static String generateExpr(String expr) {
if(expr.matches("\\d")) {
expr = Integer.toHexString(Integer.parseInt(expr));
if(expr.length() < 2) {
expr = "0" + expr;
}
}
else if(expr.equals("TRUE")) {
expr = "01";
}
else if(expr.equals("FALSE")) {
expr = "00";
}
else if(expr.matches("[a-z]")) {
return "AD T" + expr + " 00";
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "] You should never get this message.\n";
}
return "A9 " + expr;
}
private static void replaceTemps() {
String temp = "T0",
sPos = "00";
int sStart = mechCode.replaceAll("J\\d+", "J0").length()/3;
System.out.println("Start Static at " + sStart);
for(int i = 0; i < varTable.size(); i++) {
temp = varTable.get(i);
sPos = Integer.toHexString(sStart + i);
if(sPos.length() < 2) {
sPos = "0" + sPos;
}
System.out.println("Next Static at " + sPos);
mechCode = mechCode.replaceAll(temp, sPos);
}
}
private static void replaceJumps() {
String temp = "J0";
for(int i = 0; i < jumpTable.size(); i++) {
temp = "J" + i;
mechCode = mechCode.replaceAll(temp, jumpTable.get(i));
}
}
}
/*{intxintyintzbooleanabooleanbbooleanc
if((x==y)==((z != 3 + 4)==(a == (b != (c == true))))) {
print(2)}}$*/