aboutsummaryrefslogtreecommitdiff
path: root/compiler.d
diff options
context:
space:
mode:
authormryouse2023-05-25 21:15:04 +0000
committermryouse2023-05-25 21:15:04 +0000
commit9fe6496202bd95d252ed2323a88fd24781780b64 (patch)
tree907f85973ab3e5b22b076a27a3933b88855e07ba /compiler.d
parentd95e4f6f988b16bbd95a9c78c0355d190390f003 (diff)
lists as sequences, first and rest
Diffstat (limited to 'compiler.d')
-rw-r--r--compiler.d27
1 files changed, 25 insertions, 2 deletions
diff --git a/compiler.d b/compiler.d
index 6a1424d..6f5f0fd 100644
--- a/compiler.d
+++ b/compiler.d
@@ -345,9 +345,12 @@ class Compiler {
*/
int resolveLocal(Symbol sym) {
+ writefln("resolving local: %s", sym.name);
for (int i = this.localCount - 1; i >= 0; i--) {
Local local = this.locals[i];
+ writefln(" > [%d] %s (%s)", i, local.sym.name, local.depth);
if (local.sym.name == sym.name) {
+ writeln("got it!");
return i;
}
}
@@ -524,8 +527,8 @@ class Compiler {
writeln("COMPILE ERROR: 'first' expects exactly one argument");
return;
}
- ValueType vt = this.resolve(args[0], ValueType.OBJ); // TODO need a new type
- this.func.chunk.writeOp(OpCode.OP_TYPE_CHECK_LIST, args[0].line);
+ ValueType vt = this.resolve(args[0], ValueType.SEQ); // TODO need a new type
+ this.func.chunk.writeOp(OpCode.OP_TYPE_CHECK_SEQ, args[0].line);
// pop the value off the stack
// get the address of the first item in the list
@@ -535,6 +538,23 @@ class Compiler {
this.func.chunk.writeOp(OpCode.OP_FIRST, args[0].line);
}
+ void compileRest(Form[] args) {
+ // TODO how do we identify/propagate errors?
+ if (args.length != 1) {
+ writeln("COMPILE ERROR: 'first' expects exactly one argument");
+ return;
+ }
+ ValueType vt = this.resolve(args[0], ValueType.OBJ); // TODO need a new type
+ this.func.chunk.writeOp(OpCode.OP_TYPE_CHECK_SEQ, args[0].line);
+
+ this.func.chunk.writeOp(OpCode.OP_REST, args[0].line);
+
+ // there's probably a nicer way to copy
+ //List lst = new List();
+
+ // create a copy of the list
+ }
+
void compileFunc(Form form) {
Func f = cast(Func)form;
@@ -629,6 +649,9 @@ class Compiler {
//return compileList(cons.tail);
this.compileList(cons.tail);
break;
+ case "rest":
+ this.compileRest(cons.tail);
+ break;
default:
/*
writefln("unsure how to compile %s", sym.name);