From ba6ab6759e4257e543ca5da4caf2705711e5b3ed Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 12 Jan 2022 22:58:10 -0800 Subject: Chapter 23.2 --- clox/src/debug.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'clox/src/debug.c') diff --git a/clox/src/debug.c b/clox/src/debug.c index 5a3cedc..b3f756a 100644 --- a/clox/src/debug.c +++ b/clox/src/debug.c @@ -30,6 +30,14 @@ static int byteInstruction(const char *name, Chunk *chunk, int offset) { return offset + 2; } +static int jumpInstruction(const char *name, int sign, Chunk *chunk, + int offset) { + uint16_t jump = (uint16_t)(chunk->code[offset + 1] << 8); + jump |= chunk->code[offset + 2]; + printf("%-16s %4d -> %d\n", name, offset, offset + 3 + sign * jump); + return offset + 3; +} + int disassembleInstruction(Chunk *chunk, int offset) { printf("%04d ", offset); if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) { @@ -80,6 +88,10 @@ int disassembleInstruction(Chunk *chunk, int offset) { return simpleInstruction("OP_NEGATE", offset); case OP_PRINT: return simpleInstruction("OP_PRINT", offset); + case OP_JUMP: + return jumpInstruction("OP_JUMP", 1, chunk, offset); + case OP_JUMP_IF_FALSE: + return jumpInstruction("OP_JUMP_IF_FALSE", 1, chunk, offset); case OP_RETURN: return simpleInstruction("OP_RETURN", offset); default: -- cgit v1.2.3-54-g00ecf