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
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm : canFind;
import parser;
import chunk;
import dbg;
struct Local {
Symbol sym;
int depth;
}
class Compiler {
Function func;
Parser* parser;
Local[] locals;
int localCount;
int scopeDepth;
Form current;
Form previous;
Form outer;
int outerIdx;
void advance() {
previous = current;
/*
if (outer.type != FormType.EOF && outerIdx < outer) {
current = parser.parseForm();
} else {
}
*/
current = parser.parseForm();
}
void compileNil(Form form) {
form.compile(func);
advance();
}
void compileAtom(Form form) {
form.compile(func);
advance();
}
void compileAdd(Form[] args) {
resolve(args[0]);
// (+ n) always returns n
if (args.length == 1) {
return;
}
for (int i = 1; i < args.length; i++) {
resolve(args[i]);
func.chunk.writeOp(OpCode.OP_ADD, current.line);
}
}
void compileNegate(Form arg) {
resolve(arg);
func.chunk.writeOp(OpCode.OP_NEGATE, current.line);
}
void compileSubtract(Form[] args) {
resolve(args[0]);
for (int i = 1; i < args.length; i++) {
resolve(args[i]);
func.chunk.writeOp(OpCode.OP_SUBTRACT, current.line);
}
}
void compileLess(Form[] args) {
if (args.length != 2) {
writeln("'<' requires 2 arguments");
return;
}
resolve(args[0]);
resolve(args[1]);
func.chunk.writeOp(OpCode.OP_LESS, current.line);
}
//int parseVariable(Def def) {
int parseVariable(Symbol sym) {
declareVariable(sym);
if (this.scopeDepth > 0) {
return 0;
}
int addr = func.chunk.addConstant(makeStringValue(sym.name));
return addr;
}
void defineVariable(int addr) {
if (this.scopeDepth > 0) {
return;
}
func.chunk.writeOp(OpCode.OP_DEF_GLOBAL, current.line);
func.chunk.writeOp(to!ubyte(addr), current.line);
}
void declareVariable(Symbol sym) {
if (this.scopeDepth == 0) {
return;
}
for (int i = localCount - 1; i >= 0; i--) {
Local local = locals[i];
if (local.depth != -1 && local.depth < this.scopeDepth) {
break;
}
if (sym.name == local.sym.name) {
writefln("ERROR: already a variable named '%s' in this scope", sym.name);
return;
}
}
Local loc = Local(sym, this.scopeDepth);
if (localCount == locals.length) {
locals ~= loc;
} else {
locals[localCount] = loc;
}
localCount++;
}
void compileDef(Form form) {
Def def = cast(Def)form;
// resolve the value
resolve(def.val);
// add the variable name to the chunk (if applicable)
int addr = parseVariable(def.name);
// are we setting a local?
if (this.scopeDepth > 0) {
func.chunk.writeOp(OpCode.OP_DEF_LOCAL, current.line);
}
// define the variable
defineVariable(addr);
}
int resolveLocal(Symbol sym) {
for (int i = localCount - 1; i >= 0; i--) {
Local local = locals[i];
if (local.sym.name == sym.name) {
return i;
}
}
return -1;
}
void compileSymbol(Form form) {
Symbol sym = cast(Symbol)form;
int arg = resolveLocal(sym);
if (arg != -1) {
func.chunk.writeOp(OpCode.OP_GET_LOCAL, sym.line);
} else {
arg = func.chunk.addConstant(makeStringValue(sym.name));
func.chunk.writeOp(OpCode.OP_GET_GLOBAL, sym.line);
}
// get the variable
func.chunk.writeOp(to!ubyte(arg), sym.line);
//advance();
}
int jump(OpCode type) {
func.chunk.writeOp(to!ubyte(type), current.line);
func.chunk.writeOp(0xff, current.line);
func.chunk.writeOp(0xff, current.line);
return to!int(func.chunk.code.length) - 2;
}
void patchJump(int offset) {
// additional -2 to account for the 2 byte jump itself
int jmp = to!int(func.chunk.code.length) - offset - 2;
// TODO check to make sure we didn't jump too far?
this.func.chunk.code[offset] = (jmp >> 8) & 0xff;
this.func.chunk.code[offset + 1] = jmp & 0xff;
}
void compileIf(Form form) {
If if_ = cast(If)form;
resolve(if_.cond);
int thenJump = jump(OpCode.OP_JUMP_IF_FALSE);
this.func.chunk.writeOp(OpCode.OP_POP, if_.line);
resolve(if_.thenForm);
int elseJump = jump(OpCode.OP_JUMP);
patchJump(thenJump);
this.func.chunk.writeOp(OpCode.OP_POP, if_.line);
if (if_.hasElse) {
resolve(if_.elseForm);
}
patchJump(elseJump);
}
void compileAnd(Form form) {
And and_ = cast(And)form;
resolve(and_.clauses[0]);
int[] jumps;
jumps ~= jump(OpCode.OP_JUMP_IF_FALSE);
int count = 1;
while (count < and_.clauses.length) {
this.func.chunk.writeOp(OpCode.OP_POP, and_.line);
resolve(and_.clauses[count]);
jumps ~= jump(OpCode.OP_JUMP_IF_FALSE);
count++;
}
// patch all the jumps
foreach (int jmp; jumps) {
patchJump(jmp);
}
}
void compileOr(Form form) {
Or or_ = cast(Or)form;
resolve(or_.clauses[0]);
int[] jumps;
jumps ~= jump(OpCode.OP_JUMP_IF_TRUE);
int count = 1;
while (count < or_.clauses.length) {
this.func.chunk.writeOp(OpCode.OP_POP, or_.line);
resolve(or_.clauses[count]);
jumps ~= jump(OpCode.OP_JUMP_IF_TRUE);
count++;
}
// patch all the jumps
foreach (int jmp; jumps) {
patchJump(jmp);
}
}
void compileBlock(Form form) {
Block block = cast(Block)form;
beginScope();
foreach (Form inner; block.blockBody) {
resolve(inner);
}
endScope();
}
void beginScope() {
this.scopeDepth++;
}
void endScope() {
this.scopeDepth--;
while (localCount > 0 &&
locals[localCount - 1].depth > this.scopeDepth) {
func.chunk.writeOp(OpCode.OP_POPB, -1);
localCount--;
}
}
void call(Form[] args) {
//ubyte argCount = argumentList();
foreach (Form f ; args) {
resolve(f);
}
func.chunk.writeOp(OpCode.OP_CALL, -1);
//func.chunk.writeOp(argCount, -1);
func.chunk.writeOp(to!ubyte(args.length), -1);
}
void compileFunc(Form form) {
Func f = cast(Func)form;
// name the function
int global = parseVariable(f.name);
Compiler compiler = new Compiler(ObjType.FUNCTION, parser, f.name.name);
compiler.beginScope();
if (f.args.type != FormType.NIL) {
Symbol sym = cast(Symbol)f.args.head;
int constant = compiler.parseVariable(sym);
compiler.defineVariable(constant);
foreach (Form inner ; f.args.tail) {
sym = cast(Symbol)inner;
constant = compiler.parseVariable(sym);
compiler.defineVariable(constant);
}
}
/*
// compile each inner Form
foreach (Form inner; f.funcBody) {
compiler.resolve(inner);
}
*/
advance(); // ??
Block b = new Block(f.line);
b.blockBody = f.funcBody;
compiler.compileBlock(b);
// write the function as a Value
Function outFunc = compiler.finish();
func.chunk.writeOp(OpCode.OP_CONSTANT, f.line);
int funcAddr = func.chunk.addConstant(makeObjValue(outFunc));
func.chunk.writeOp(to!ubyte(funcAddr), f.line);
// define the global variable
defineVariable(global);
}
void compileCons(Form form) {
Cons cons = cast(Cons)form;
Form head = cons.head;
if (head.type != FormType.SYMBOL) {
writeln("cons must start with a symbol");
return;
}
Symbol sym = cast(Symbol)head;
switch (sym.name) {
case "+":
compileAdd(cons.tail);
break;
case "-":
if (cons.tail.length == 1) {
compileNegate(cons.tail[0]);
} else {
compileSubtract(cons.tail);
}
break;
case "<":
compileLess(cons.tail);
break;
default:
/*
writefln("unsure how to compile %s", sym.name);
advance();
*/
resolve(head);
call(cons.tail);
advance();
break;
}
}
void resolve(Form form) {
//printForm(form);
switch(form.type) {
case FormType.ATOM:
this.compileAtom(form);
break;
case FormType.CONS:
this.compileCons(form);
break;
case FormType.NIL:
this.compileNil(form);
break;
case FormType.DEF:
this.compileDef(form);
break;
case FormType.SYMBOL:
this.compileSymbol(form);
break;
case FormType.BLOCK:
this.compileBlock(form);
break;
case FormType.IF:
this.compileIf(form);
break;
case FormType.AND:
this.compileAnd(form);
break;
case FormType.OR:
this.compileOr(form);
break;
case FormType.FUNC:
this.compileFunc(form);
break;
default:
write("not sure how to resolve: ");
printForm(form);
advance();
break;
}
}
Function compile() {
advance();
while(current.type != FormType.EOF) {
resolve(current);
}
return finish();
}
Function finish() {
this.func.chunk.writeOp(OpCode.OP_RETURN, current.line);
//disassembleChunk(func.chunk, to!string(func));
return this.func;
}
this(ObjType type, Parser* parser, string name = "") {
this.parser = parser;
this.func = new Function(type, name);
//localCount = 0;
locals ~= Local(new Symbol("", -1), 0);
localCount++;
}
}
|