From 0c70372774297272dd14133d48e40e9a3624420a Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Sat, 20 May 2023 15:12:30 -0400 Subject: initial commit of compiler/VM with a couple basic instructions --- dbg.d | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'dbg.d') diff --git a/dbg.d b/dbg.d index 4af02a6..dba6070 100644 --- a/dbg.d +++ b/dbg.d @@ -2,11 +2,58 @@ import std.stdio; import std.string; import chunk; -import parser : Value, Atom, ValueType; +import parser; //import parser; //import dbg; //import value; +void printForm(Form f, string prefix = "") { + switch (f.type) { + case FormType.EOF: + writeln("eof"); + return; + case FormType.ATOM: + writefln("%s atom: %s", prefix, atomAsString(cast(Atom)f)); + break; + case FormType.CONS: + Cons c = cast(Cons)f; + writef("%s cons <", prefix); + if (c.evaluate) { + writeln("true>"); + } else { + writeln("false>"); + } + + printForm(c.head, format("%s>", prefix)); + foreach (Form i ; c.tail) { + printForm(i, format("%s>", prefix)); + } + break; + case FormType.NIL: + writefln("%s NIL", prefix); + break; + case FormType.PARSE_ERROR: + ParseError pe = cast(ParseError)f; + writefln("ERROR: %s", pe.message); + break; + case FormType.SYMBOL: + Symbol s = cast(Symbol)f; + writefln("%s sym: %s", prefix, s.name); + break; + case FormType.FUNC: + Func func = cast(Func)f; + writefln("%s ", prefix, func.name.name); + printForm(func.args, format("%s -", prefix)); + writefln("%s with %d body lines", prefix, func.funcBody.length); + writefln("%s ", prefix, func.name.name); + break; + default: + writeln("printFormDefault"); + break; + } + +} + string printableValue(Value val) { switch (val.type) { case ValueType.STRING: @@ -48,6 +95,10 @@ int disassemble(Chunk chunk, int offset) { switch (inst) { case OpCode.OP_ADD: return simpleInstruction("OP_ADD", offset); + case OpCode.OP_LESS: + return simpleInstruction("OP_LESS", offset); + case OpCode.OP_SUBTRACT: + return simpleInstruction("OP_SUBTRACT", offset); case OpCode.OP_CONSTANT: return constantInstruction("OP_CONSTANT", chunk, offset); case OpCode.OP_NEGATE: @@ -56,6 +107,8 @@ int disassemble(Chunk chunk, int offset) { return simpleInstruction("OP_POP", offset); case OpCode.OP_RETURN: return simpleInstruction("OP_RETURN", offset); + case OpCode.OP_NIL: + return simpleInstruction("OP_NIL", offset); default: writeln("unknown opcode?"); return offset + 1; -- cgit v1.2.3