diff options
Diffstat (limited to 'dbg.d')
| -rw-r--r-- | dbg.d | 64 |
1 files changed, 64 insertions, 0 deletions
@@ -0,0 +1,64 @@ +import std.stdio; +import std.string; + +import chunk; +import parser : Value, Atom, ValueType; +//import parser; +//import dbg; +//import value; + +string printableValue(Value val) { + switch (val.type) { + case ValueType.STRING: + return val.as.str; + case ValueType.NUMBER: + return format("%g", val.as.number); + case ValueType.BOOLEAN: + if (val.as.boolean) { + return "true"; + } else { + return "false"; + } + default: + return "! unknown value type !"; + } +} + +string atomAsString(Atom a) { + return printableValue(a.value); +} + +int constantInstruction(string message, Chunk chunk, int offset) { + ubyte idx = chunk.code[offset + 1]; + //writeln("dunno how to write a constant"); + writefln("%-16s %4d '%s'", message, idx, printableValue(chunk.constants[idx])); + //writefln("%-16s %4d '%s'", message, idx, atomAsString(chunk.constants[idx])); + return offset + 2; +} + +int simpleInstruction(string message, int offset) { + writeln(message); + return offset + 1; +} + +int disassemble(Chunk chunk, int offset) { + writef("%04d %4d ", offset, chunk.lines[offset]); + + ubyte inst = chunk.code[offset]; + switch (inst) { + case OpCode.OP_ADD: + return simpleInstruction("OP_ADD", offset); + case OpCode.OP_CONSTANT: + return constantInstruction("OP_CONSTANT", chunk, offset); + case OpCode.OP_NEGATE: + return simpleInstruction("OP_NEGATE", offset); + case OpCode.OP_POP: + return simpleInstruction("OP_POP", offset); + case OpCode.OP_RETURN: + return simpleInstruction("OP_RETURN", offset); + default: + writeln("unknown opcode?"); + return offset + 1; + } + return 0; +} |
