aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/debug.c
diff options
context:
space:
mode:
authorGravatar Tom Willemse2022-01-12 22:58:10 -0800
committerGravatar Tom Willemse2022-01-12 22:58:10 -0800
commitba6ab6759e4257e543ca5da4caf2705711e5b3ed (patch)
treec8a2c895d4c07e0979981c816f03d960b789449a /clox/src/debug.c
parent65275bde8a82f54710a65d6e6563394bb850b7ef (diff)
downloadcrafting-interpreters-ba6ab6759e4257e543ca5da4caf2705711e5b3ed.tar.gz
crafting-interpreters-ba6ab6759e4257e543ca5da4caf2705711e5b3ed.zip
Chapter 23.2
Diffstat (limited to 'clox/src/debug.c')
-rw-r--r--clox/src/debug.c12
1 files changed, 12 insertions, 0 deletions
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: