/
Parser.java
320 lines (289 loc) · 8.27 KB
/
Parser.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
enum CommandType {
A_COMMAND,
C_COMMAND,
L_COMMAND
}
class Parser {
private ArrayList<String> commands;
private int pos;
public Parser(String fileLocation) {
pos = 0;
commands = new ArrayList<>();
try {
Scanner s = new Scanner(new File(fileLocation));
while (s.hasNextLine()){
String val = s.nextLine();
if (!val.equals("") && !val.startsWith("//")) { // Remove whitespace
String finalV = val
.replaceAll("\\s+", "");
int offset = finalV.indexOf("//");
if (offset != -1) {
finalV = finalV.substring(0, offset);
}
if (!finalV.equals("")) {
commands.add(finalV);
}
}
}
s.close();
} catch (FileNotFoundException e) {
System.out.println("Unable to find file.");
}
// for (int i = 0; i < commands.size(); i++) {
// System.out.println(String.format("\"%s\"", commands.get(i)));
// }
// System.out.println(commands.toString());
}
public boolean hasMoreCommands() {
return commands.size() > pos;
}
public void advance() {
// if (!hasMoreCommands()) {
// throw new Error("Can not advance further");
// }
pos++;
// if (pos + 1 < commands.size() && commands.get(pos + 1).startsWith("//")) {
// this.advance();
// }
}
public CommandType commandType() {
String command = commands.get(pos);
if (command.startsWith("@")) {
return CommandType.A_COMMAND;
} else if (command.startsWith("(") && command.endsWith(")")) {
return CommandType.L_COMMAND;
} else {
return CommandType.C_COMMAND;
}
}
public String symbol() {
CommandType commandType = commandType();
if (commandType == CommandType.C_COMMAND) {
throw new Error("Can not get symbol from c command");
}
String command = commands.get(pos);
if (commandType == CommandType.L_COMMAND) {
return command.replace("(", "").replace(")", "");
}
return command.replace("@", "");
}
public String dest() {
String[] split = commands.get(pos).split("=");
if (split.length == 1) { // There is no assignement (no equals sign)
return "";
}
return split[0];
}
public String comp() {
String[] split = commands.get(pos).split("=");
if (split.length == 1) {
String[] postJump = split[0].split(";");
if (postJump.length == 1) {
return "";
}
return postJump[0];
}
return split[1].split(";")[0];
}
public String jump() {
String[] split = commands.get(pos).split(";");
// TODO: what if "A=D;"
if (split.length == 1) { // not jump
return "";
}
return split[1];
}
public static String intToBinary(int number, int padding) {
return String.format("%" + padding +"s", Integer.toBinaryString(number)).replace(' ', '0');
}
public static boolean isNumeric(String str) {
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) return false;
}
return true;
}
public static void main(String[] args) {
SymbolTable symbols = new SymbolTable();
// SYMBOLS
// R0 = 0, R1 = 1, ... R15 = 15
for (int i = 0; i <= 15; i++) {
symbols.addEntry("R"+i, i);
}
symbols.addEntry("SCREEN", 16384);
symbols.addEntry("KBD", 24576);
symbols.addEntry("SP", 0);
symbols.addEntry("LCL", 1);
symbols.addEntry("ARG", 2);
symbols.addEntry("THIS", 3);
symbols.addEntry("THAT", 4);
int n = 16;
Parser parser = new Parser(args[0]);
String output = "";
// First pass to generate label positions
int offset = 0;
while (parser.hasMoreCommands()) {
if (parser.commandType() == CommandType.L_COMMAND) {
System.out.println(parser.symbol() + " pos " + parser.pos);
if (!symbols.contains(parser.symbol())) {
symbols.addEntry(parser.symbol(), parser.pos - offset);
offset++;
}
}
parser.advance();
}
parser.pos = 0;
while (parser.hasMoreCommands()) {
CommandType type = parser.commandType();
String command = "";
if (type == CommandType.L_COMMAND) {
// if (!symbols.contains(parser.symbol())) {
// symbols.addEntry(parser.symbol(), parser.pos + 1);
// }
// TODO: else throw error duplicate labels
} else if (type == CommandType.A_COMMAND) {
String symbol = parser.symbol();
String value = "";
if (!isNumeric(symbol)) {
if (!symbols.contains(symbol)) {
symbols.addEntry(symbol, n++);
}
value = intToBinary(symbols.getAddress(symbol), 15);
} else {
value = intToBinary(Integer.parseInt(symbol), 15);
}
command = "0" + value;
} else {
command = "111" + Code.comp(parser.comp()) + Code.dest(parser.dest()) + Code.jump(parser.jump());
}
if (command != "") {
output += command + "\n";
}
parser.advance();
}
String filename = new File(args[0]).getName();
int extensionPos = filename.indexOf(".");
if (extensionPos > 0) {
filename = filename.substring(0, extensionPos);
}
try {
PrintWriter writer = new PrintWriter(String.format("%s.hack", filename));
writer.print(output);
writer.close();
} catch (FileNotFoundException e) {
System.out.println("Can not write to file.");
}
}
}
class Code {
public static String dest(String dest) {
switch (dest) {
case "M":
return "001";
case "D":
return "010";
case "MD":
return "011";
case "A":
return "100";
case "AM":
return "101";
case "AD":
return "110";
case "AMD":
return "111";
default: // null
return "000";
}
}
public static String comp(String comp) {
String type = comp.contains("M") ? "M" : "A"; // TODO: input validation
String value = "";
// System.out.println("IN COMP");
// System.out.println("type");
// System.out.println(type);
// System.out.println(comp.equals(type));
if (comp.equals("0")) {
value = "101010";
} else if (comp.equals("1")) {
value = "101010";
} else if (comp.equals("-1")) {
value = "111010";
} else if (comp.equals("D")) {
value = "001100";
} else if (comp.equals(type)) {
value = "110000";
} else if (comp.equals("!D")) {
value = "001101";
} else if (comp.equals("!"+type)) {
value = "110001";
} else if (comp.equals("-D")) {
value = "001111";
} else if (comp.equals("-"+type)) {
value = "110011";
} else if (comp.equals("D+1")) {
value = "011111";
} else if (comp.equals(type+"+1")) {
value = "110111";
} else if (comp.equals("D-1")) {
value = "001110";
} else if (comp.equals(type+"-1")) {
value = "110010";
} else if (comp.equals("D+"+type)) {
value = "000010";
} else if (comp.equals("D-"+type)) {
value = "010011";
} else if (comp.equals(type+"-D")) {
value = "000111";
} else if (comp.equals("D&"+type)) {
value = "000000";
} else if (comp.equals("D|"+type)) { // "D|"+type
value = "010101";
} else {
throw new Error(String.format("Invalid comp value \"%s\" found", comp));
}
// aVALUE
return (type == "A" ? "0" : "1") + value;
}
public static String jump(String jump) {
switch (jump) {
case "JGT":
return "001";
case "JEQ":
return "010";
case "JGE":
return "011";
case "JLT":
return "100";
case "JNE":
return "101";
case "JLE":
return "110";
case "JMP":
return "111";
default: // null
return "000";
}
}
}
class SymbolTable {
// I could create my own implementation with an ArrayList
private HashMap<String, Integer> symbolTable;
SymbolTable() {
symbolTable = new HashMap<>();
}
public void addEntry(String symbol, int address) {
symbolTable.put(symbol, address);
}
public boolean contains(String symbol) {
return symbolTable.containsKey(symbol);
}
public int getAddress(String symbol) {
return symbolTable.get(symbol);
}
}