From 9fe6496202bd95d252ed2323a88fd24781780b64 Mon Sep 17 00:00:00 2001 From: mryouse Date: Thu, 25 May 2023 21:15:04 +0000 Subject: lists as sequences, first and rest --- compiler.d | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'compiler.d') 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); -- cgit v1.2.3