From 618de4c70d8916f64781997f3ae538e3e6109d00 Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Sun, 21 May 2023 16:20:41 -0400 Subject: if/else, 'and' control flow --- dbg.d | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'dbg.d') diff --git a/dbg.d b/dbg.d index 89ae8c1..e14e9fe 100644 --- a/dbg.d +++ b/dbg.d @@ -1,5 +1,6 @@ import std.stdio; import std.string; +import std.conv; import chunk; import parser; @@ -83,6 +84,13 @@ string atomAsString(Atom a) { return printableValue(a.value); } +int jumpInstruction(string message, int sign, Chunk chunk, int offset) { + uint jump = to!uint(chunk.code[offset + 1] << 8); + jump |= chunk.code[offset + 2]; + writefln("%-16s %4d -> %d", message, offset, offset + 3 + sign * jump); + return offset + 3; +} + int byteInstruction(string message, Chunk chunk, int offset) { ubyte slot = chunk.code[offset + 1]; writefln("%-16s %4d", message, slot); @@ -135,6 +143,11 @@ int disassemble(Chunk chunk, int offset) { return simpleInstruction("OP_RETURN", offset); case OpCode.OP_NIL: return simpleInstruction("OP_NIL", offset); + case OpCode.OP_JUMP: + return jumpInstruction("OP_JUMP", 1, chunk, offset); + case OpCode.OP_JUMP_IF_FALSE: + return jumpInstruction("OP_JUMP_IF_FALSE", 1, chunk, offset); + default: writeln("unknown opcode?"); return offset + 1; -- cgit v1.2.3