-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathFivetran.java
392 lines (338 loc) · 9.87 KB
/
Fivetran.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
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
import java.util.*;
import java.io.*;
public class Fivetran implements MakoConstants {
public static final int REGISTERS = 0;
public static final int PRINT_SUBROUTINE = 42;
public static final int INPUT_SUBROUTINE = 78;
public static final int GRID = 6362;
public static final int SPRITES = 7633;
List<Formula> program = new ArrayList<Formula>();
Map<Integer, Formula> formulas = new HashMap<Integer, Formula>();
Map<String, Variable> variables = new HashMap<String, Variable>();
public static void main(String[] args) {
Fivetran compiler = new Fivetran();
compiler.parseFile(args[0]);
MakoRom rom = compiler.compile();
if (Arrays.asList(args).contains("--run")) {
Mako.exec(rom.toArray(), false, null);
}
}
private void parseFile(String filename) {
// set up variables to shadow Mako registers:
new DimensionFormula("mr(14)");
new DimensionFormula("mg(31, 41)");
new DimensionFormula("ms(256, 4)");
variables.get("mr").address = REGISTERS;
variables.get("mg").address = GRID;
variables.get("ms").address = SPRITES;
try {
Scanner in = new Scanner(new File(filename));
while(in.hasNextLine()) {
String ln = in.nextLine().trim();
// everything after a 'C' is a comment.
if (ln.indexOf('C') >= 0) {
ln = ln.substring(0, ln.indexOf('C'));
}
// ignore blank lines
if (ln.length() < 1) { continue; }
// handle formula numbers:
Cursor cursor = new Cursor(ln);
Integer formulaNumber = null;
if (cursor.isDigit()) {
formulaNumber = cursor.parseNumber();
}
// parse the line:
Formula line = parseFormula(cursor.line);
if (formulaNumber != null) {
line.number = formulaNumber;
formulas.put(line.number, line);
}
program.add(line);
}
in.close();
program.add(new StopFormula(""));
}
catch(IOException e) {
throw new Error("Cannot open file '"+filename+"'");
}
}
private Formula parseFormula(String line) {
if (line.length() == 0) { return new NopFormula(); }
if (line.startsWith("do")) { return new DoFormula( line.substring(2).trim()); }
if (line.startsWith("if")) { return new IfFormula( line.substring(2).trim()); }
if (line.startsWith("go to")) { return new GoToFormula( line.substring(5).trim()); }
if (line.startsWith("stop")) { return new StopFormula( line.substring(4).trim()); }
if (line.startsWith("sync")) { return new SyncFormula( line.substring(4).trim()); }
if (line.startsWith("read")) { return new ReadFormula( line.substring(4).trim()); }
if (line.startsWith("print")) { return new PrintFormula( line.substring(5).trim()); }
if (line.startsWith("dimension")) { return new DimensionFormula(line.substring(9).trim()); }
return new AssignFormula(line);
}
private MakoRom compile() {
// load the 'base' rom with various mandatory
// library functions and memory regions.
MakoRom rom = new MakoRom("base/BaseRom.rom");
rom.label("base rom", 0);
// lay down all referenced variables.
for(Variable v : variables.values()) {
v.emitStorage(rom);
}
// fix the program entrypoint
rom.set(PC, rom.size(), MakoRom.Type.Array);
rom.label("main", rom.size());
// perform a first pass to determine the size
// and effective address of each code segment.
MakoRom romcopy = rom.copy();
for(Formula f : program) {
f.address = romcopy.size();
f.emit(romcopy);
if (f.gotoAfter != null) {
romcopy.addJump(f.gotoAfter);
}
}
// recompile knowing these addresses.
// inefficient, but simple!
for(Formula f : program) {
if (f.number != null) { rom.label("line "+f.number, rom.size()); }
f.emit(rom);
if (f.gotoAfter != null) {
rom.addJump(f.gotoAfter);
}
}
rom.disassemble(System.out);
return rom;
}
class AssignFormula extends Formula {
Variable v;
Expression a;
AssignFormula(String line) {
// variable = expression
String[] parts = line.split("=");
if (parts.length != 2) { fail(); }
v = new Variable( parts[0].trim(), variables);
a = new Expression(parts[1].trim(), variables);
}
void emit(MakoRom rom) {
a.emit(rom);
v.emit(rom);
rom.addStor();
}
}
class DoFormula extends Formula {
Integer lastLine;
Variable var;
Expression first;
Expression last;
Expression step;
DoFormula(String line) {
// do lastLine varname = first, last [, step]
Cursor cursor = new Cursor(line);
lastLine = cursor.parseNumber();
var = new Variable(cursor.parseVar(), variables);
cursor.expect('=');
first = new Expression(cursor.parseExpression(), variables);
cursor.expect(',');
last = new Expression(cursor.parseExpression(), variables);
if (cursor.at(',')) {
cursor.expect(',');
step = new Expression(cursor.parseExpression(), variables);
}
else {
step = new Expression("1", variables);
}
}
void emit(MakoRom rom) {
// i = first
first.emit(rom);
var.emit(rom);
rom.addStor();
// skip initial increment:
int a = rom.addJump(-1);
formulas.get(lastLine).gotoAfter = rom.size();
// i += step
var.emit(rom);
rom.addLoad();
step.emit(rom);
rom.addAdd();
var.emit(rom);
rom.addStor();
rom.set(a, rom.size(), MakoRom.Type.Code);
// if (i <= last) goto loop start
var.emit(rom);
rom.addLoad();
last.emit(rom);
rom.addSgt();
// jump to resume address
rom.addJumpIf(
program.get(program.indexOf(formulas.get(lastLine)) + 1).address
);
}
}
class IfFormula extends Formula {
int trueBranch;
int falseBranch;
int comparison;
Expression a;
Expression b;
IfFormula(String line) {
// if ( expression comparator expression ) truebranch, falsebranch
Cursor cursor = new Cursor(line);
Cursor condCursor = cursor.parseParens();
a = new Expression(condCursor.parseExpression(), variables);
if (condCursor.line.startsWith(">=")) { comparison = 1; condCursor.skip(2); condCursor.trim(); }
else if (condCursor.line.startsWith(">")) { comparison = 0; condCursor.skip(1); condCursor.trim(); }
else if (condCursor.line.startsWith("=")) { comparison = 2; condCursor.skip(1); condCursor.trim(); }
else { condCursor.fail(); }
b = new Expression(condCursor.parseExpression(), variables);
trueBranch = cursor.parseNumber();
cursor.expect(',');
falseBranch = cursor.parseNumber();
}
void emit(MakoRom rom) {
a.emit(rom);
b.emit(rom);
if (comparison == 0) {
// greater-than
rom.addSgt();
rom.addJumpIf(formulas.get(trueBranch).address);
}
else if (comparison == 1) {
// greater-than-or-equal-to
rom.addSlt();
rom.addJumpZ(formulas.get(trueBranch).address);
}
else if (comparison == 2) {
// equal-to
rom.addXor();
rom.addJumpZ(formulas.get(trueBranch).address);
}
rom.addJump(formulas.get(falseBranch).address);
}
}
class GoToFormula extends Formula {
int target;
GoToFormula(String line) {
// go to number
try { target = Integer.parseInt(line); }
catch(NumberFormatException e) { fail(); }
}
void emit(MakoRom rom) {
rom.addJump(formulas.get(target).address);
}
}
class StopFormula extends Formula {
StopFormula(String line) {
// stop
if (line.length() > 0) { fail(); }
}
void emit(MakoRom rom) {
rom.addJump(-1);
}
}
class SyncFormula extends Formula {
SyncFormula(String line) {
// sync
if (line.length() > 0) { fail(); }
}
void emit(MakoRom rom) {
rom.addSync();
}
}
class ReadFormula extends Formula {
List<Variable> args = new ArrayList<Variable>();
ReadFormula(String line) {
// read varname [, and, more, ...]
Cursor cursor = new Cursor(line);
while(true) {
args.add(new Variable(cursor.parseVar(), variables));
if (cursor.done()) { break; }
if (!cursor.at(',')) { break; }
cursor.expect(',');
}
}
void emit(MakoRom rom) {
for(Variable v : args) {
rom.addCall(INPUT_SUBROUTINE);
v.emit(rom);
rom.addStor();
}
}
}
class PrintFormula extends Formula {
List<Expression> args = new ArrayList<Expression>();
PrintFormula(String line) {
// print expression [, and, more, ...]
Cursor cursor = new Cursor(line);
while(true) {
args.add(new Expression(cursor.parseExpression(), variables));
if (cursor.done()) { break; }
if (!cursor.at(',')) { break; }
cursor.expect(',');
}
}
void emit(MakoRom rom) {
for(Expression v : args) {
v.emit(rom);
rom.addCall(PRINT_SUBROUTINE);
}
rom.addConst(10);
rom.addConst(CO);
rom.addStor();
}
}
class NopFormula extends Formula {
void emit(MakoRom rom) {
// do nothing.
}
}
class DimensionFormula extends Formula {
Variable v;
int d1;
Integer d2;
Integer d3;
DimensionFormula(String line) {
// dimension varname(one [, two][, three])
Cursor cursor = new Cursor(line);
String name = cursor.parseVarName();
if (!variables.containsKey(name)) {
v = new Variable(line, variables);
}
Cursor argCursor = cursor.parseParens();
d1 = argCursor.parseNumber();
if (argCursor.at(',')) {
argCursor.expect(',');
d2 = argCursor.parseNumber();
}
if (argCursor.at(',')) {
argCursor.expect(',');
d3 = argCursor.parseNumber();
}
v = variables.get(name);
if (d3 != null) {
if (v.args.size() != 3) { fail(); }
v.value = new int[d1][d2][d3];
}
else if (d2 != null) {
if (v.args.size() != 2) { fail(); }
v.value = new int[d1][d2];
}
else {
if (v.args.size() != 1) { fail(); }
v.value = new int[d1];
}
}
void emit(MakoRom rom) {
// do nothing- this construct only
// has compile-time meaning.
}
}
}
abstract class Formula {
Integer number; // fortran 'formula number'
Integer gotoAfter; // jump to an address after this line?
Integer address = -1; // compiled starting address
static void fail() {
throw new Error("SYNTAX ERROR. WHOOPSIE-DAISY.");
}
abstract void emit(MakoRom rom);
}