aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormryouse2023-06-01 19:15:32 -0400
committermryouse2023-06-01 19:24:35 -0400
commitfb4a026dc1a5b6469ff1da013874bc570fe41439 (patch)
tree3f7eee5d62c0aaacc7cd8c9178df3dc3f5a6da58
parent070ced8e6970b136c63677ee29767fe3e1bd4c47 (diff)
rely on the main compile loop to advance
-rw-r--r--compiler.d52
1 files changed, 13 insertions, 39 deletions
diff --git a/compiler.d b/compiler.d
index 6b21473..28785a6 100644
--- a/compiler.d
+++ b/compiler.d
@@ -63,7 +63,6 @@ class Compiler {
void compileNil(Form form) {
form.compile(this.func);
- this.advance();
}
TC typeCheck(ValueType actual, ValueType expecting) {
@@ -122,8 +121,6 @@ class Compiler {
String str = new String(ls.value);
int idx = this.func.chunk.addConstant(makeSeqValue(str));
this.func.chunk.writeOp(to!ubyte(idx), ls.line);
-
- this.advance();
}
void compileAtom(Form form, const ValueType expecting) {
@@ -162,8 +159,6 @@ class Compiler {
}
*/
- this.advance();
-
@@ -208,7 +203,6 @@ class Compiler {
void compileIsAny(Form[] args) {
if (args.length != 1) {
this.error(format("'any?': expecting [1] argument, received %d", args.length), -1);
- this.advance();
return;
}
this.compileAtom(new Atom(true, args[0].line), ValueType.ANY);
@@ -217,7 +211,6 @@ class Compiler {
void compileIsNil(Form[] args) {
if (args.length != 1) {
this.error(format("'nil?': expecting [1] argument, received %d", args.length), -1);
- this.advance();
return;
}
ValueType vt = this.resolve(args[0], ValueType.ANY);
@@ -262,7 +255,6 @@ class Compiler {
void compileMultiply(Form[] args) {
if (args.length < 2) {
this.error(format("'*': expected [2+] arguments, received %d", args.length), -1);
- this.advance();
return;
}
ValueType vt = this.resolve(args[0], ValueType.NUMBER);
@@ -277,7 +269,6 @@ class Compiler {
void compileDivide(Form[] args) {
if (args.length != 2) {
this.error(format("'/': expected [2] arguments, received %d", args.length), -1);
- this.advance();
return;
}
ValueType vt = this.resolve(args[0], ValueType.NUMBER);
@@ -290,7 +281,6 @@ class Compiler {
ValueType compileLess(Form[] args, ValueType expected) {
if (args.length != 2) {
this.error(format("'<': expected [2] arguments, received %d", to!int(args.length)), -1);
- this.advance();
return ValueType.NIL;
}
ValueType vt1 = this.resolve(args[0], ValueType.NUMBER);
@@ -308,7 +298,6 @@ class Compiler {
ValueType compileGreater(Form[] args) {
if (args.length != 2) {
this.error(format("'>': expected [2] arguments, received %d", to!int(args.length)), -1);
- this.advance();
return ValueType.NIL;
}
ValueType vt1 = this.resolve(args[0], ValueType.NUMBER);
@@ -326,7 +315,6 @@ class Compiler {
ValueType compileEq(Form[] args) {
if (args.length != 2) {
this.error(format("'eq?': expected [1] argument, received %d", to!int(args.length)), -1);
- this.advance();
return ValueType.NIL;
}
ValueType vt1 = this.resolve(args[0], ValueType.ANY);
@@ -352,8 +340,8 @@ class Compiler {
if (this.scopeDepth > 0) {
return;
}
- this.func.chunk.writeOp(OpCode.OP_DEF_GLOBAL, this.current.line);
- this.func.chunk.writeOp(to!ubyte(addr), this.current.line);
+ this.func.chunk.writeOp(OpCode.OP_DEF_GLOBAL, -1);
+ this.func.chunk.writeOp(to!ubyte(addr), -1);
}
void declareVariable(Symbol sym) {
@@ -425,14 +413,12 @@ class Compiler {
// get the variable
this.func.chunk.writeOp(to!ubyte(arg), sym.line);
-
- this.advance();
}
int jump(OpCode type) {
- this.func.chunk.writeOp(to!ubyte(type), this.current.line);
- this.func.chunk.writeOp(0xff, this.current.line);
- this.func.chunk.writeOp(0xff, this.current.line);
+ this.func.chunk.writeOp(to!ubyte(type), -1);
+ this.func.chunk.writeOp(0xff, -1);
+ this.func.chunk.writeOp(0xff, -1);
return to!int(this.func.chunk.code.length) - 2;
}
@@ -448,12 +434,12 @@ class Compiler {
void jumpBackTo(OpCode type, int dest) {
- this.func.chunk.writeOp(to!ubyte(type), this.current.line);
+ this.func.chunk.writeOp(to!ubyte(type), -1);
int lower = (dest >> 8) & 0xff;
int higher = dest & 0xff;
- this.func.chunk.writeOp(to!ubyte(lower), this.current.line);
- this.func.chunk.writeOp(to!ubyte(higher), this.current.line);
+ this.func.chunk.writeOp(to!ubyte(lower), -1);
+ this.func.chunk.writeOp(to!ubyte(higher), -1);
}
void compileIf(Form form) {
@@ -580,12 +566,12 @@ class Compiler {
if (args.length == 0) {
this.func.chunk.writeOp(OpCode.OP_LIST, -1);
this.func.chunk.writeOp(to!ubyte(0), -1);
- this.advance();
+ //this.advance();
return;
}
int length = to!int(args.length);
foreach (Form arg ; args) {
- resolve(arg, ValueType.ANY); // resolve everything onto the stack
+ this.resolve(arg, ValueType.ANY); // resolve everything onto the stack
}
this.func.chunk.writeOp(OpCode.OP_LIST, args[0].line);
this.func.chunk.writeOp(to!ubyte(length), args[0].line);
@@ -595,7 +581,6 @@ class Compiler {
// TODO how do we identify/propagate errors?
if (args.length != 1) {
this.error("'length': expected [1] argument, received 0", -1);
- this.advance();
return;
}
ValueType vt = this.resolve(args[0], ValueType.SEQ); // TODO need a new type
@@ -607,7 +592,6 @@ class Compiler {
void compileAppend(Form[] args) {
if (args.length != 2) {
this.error(format("'append': expected [2] arguments, received %d", args.length), -1);
- this.advance();
return;
}
@@ -624,7 +608,6 @@ class Compiler {
// TODO how do we identify/propagate errors?
if (args.length != 2) {
this.error(format("'in?': expected [2] arguments, received %d", args.length), -1);
- this.advance();
return;
}
ValueType vt1 = this.resolve(args[0], ValueType.ANY);
@@ -638,7 +621,6 @@ class Compiler {
// TODO how do we identify/propagate errors?
if (args.length != 2) {
this.error(format("'map': expected [2] arguments, received %d", args.length), -1);
- this.advance();
return;
}
ValueType vt = this.resolve(args[0], ValueType.ANY); // resolves the function
@@ -706,7 +688,6 @@ class Compiler {
// TODO how do we identify/propagate errors?
if (args.length != 1) {
this.error("'first': expected [1] argument, received 0", -1);
- this.advance();
return;
}
ValueType vt = this.resolve(args[0], ValueType.SEQ); // TODO need a new type
@@ -724,7 +705,6 @@ class Compiler {
// TODO how do we identify/propagate errors?
if (args.length != 1) {
this.error("'rest': expected [1] argument, received ?", -1);
- this.advance();
return;
}
ValueType vt = this.resolve(args[0], ValueType.OBJ); // TODO need a new type
@@ -750,8 +730,6 @@ class Compiler {
}
}
- advance(); // ??
-
Block b = new Block(lamb.line);
b.blockBody = lamb.lambdaBody;
compiler.compileBlock(b);
@@ -789,14 +767,13 @@ class Compiler {
}
}
- advance(); // ??
-
Block b = new Block(f.line);
b.blockBody = f.funcBody;
compiler.compileBlock(b);
// write the function as a Value
Function outFunc = compiler.finish();
+
this.func.chunk.writeOp(OpCode.OP_CONSTANT, f.line);
int funcAddr = this.func.chunk.addConstant(makeObjValue(outFunc));
this.func.chunk.writeOp(to!ubyte(funcAddr), f.line);
@@ -817,13 +794,11 @@ class Compiler {
if (head.type == FormType.LAMBDA) {
this.resolve(head);
this.call(cons.tail);
- this.advance();
return ValueType.NIL;
}
if (head.type != FormType.SYMBOL) {
this.error("can't evaluate without a symbol or lambda", head.line);
- this.advance();
return ValueType.NIL;
}
@@ -906,7 +881,6 @@ class Compiler {
default:
this.resolve(head);
this.call(cons.tail);
- this.advance();
break;
}
return ValueType.NIL;
@@ -955,7 +929,6 @@ class Compiler {
default:
write("not sure how to resolve: ");
printForm(form);
- this.advance();
break;
}
return ValueType.NIL;
@@ -965,13 +938,14 @@ class Compiler {
this.advance();
while(current.type != FormType.EOF) {
this.resolve(current);
+ this.advance();
}
return this.finish();
}
Function finish() {
- this.func.chunk.writeOp(OpCode.OP_RETURN, this.current.line);
+ this.func.chunk.writeOp(OpCode.OP_RETURN, -1);
if (DEBUG) {
disassembleChunk(this.func.chunk, to!string(func));
}