diff options
| author | Ben Winston | 2023-05-20 15:12:30 -0400 |
|---|---|---|
| committer | Ben Winston | 2023-05-20 15:12:30 -0400 |
| commit | 0c70372774297272dd14133d48e40e9a3624420a (patch) | |
| tree | 4e66fb1b97a0fd537a20dca4ddcd26003f83c94d /dbg.d | |
| parent | 6902cc5abe09da9f6f2d86f22d06684d97cfa9f3 (diff) | |
initial commit of compiler/VM with a couple basic instructions
Diffstat (limited to 'dbg.d')
| -rw-r--r-- | dbg.d | 55 |
1 files changed, 54 insertions, 1 deletions
@@ -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 <fn %s>", prefix, func.name.name); + printForm(func.args, format("%s -", prefix)); + writefln("%s with %d body lines", prefix, func.funcBody.length); + writefln("%s <end fn %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; |
