From 33372512fc32c26913c8385637d20f6d98c8376c Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Oct 2021 10:34:25 +0200 Subject: Add constants operation --- src/bytecode/debug.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/bytecode/debug.h') diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h index 3d08d8f..e07b9a2 100644 --- a/src/bytecode/debug.h +++ b/src/bytecode/debug.h @@ -1,27 +1,36 @@ #ifndef BDL_DEBUG_H #define BDL_DEBUG_H -void disassemble_chunk(u8 *chunk, const char *name); -size_t disassemble_instruction(u8 *chunk, size_t offset); +#include "chunk.h" + +void disassemble_chunk(Chunk chunk, const char *name); +size_t disassemble_instruction(Chunk chunk, size_t offset); void -disassemble_chunk(u8 *chunk, const char *name) { +disassemble_chunk(Chunk chunk, const char *name) { printf("== %s ==\n", name); size_t offset = 0; - while (offset < array_size(chunk)) { + while (offset < array_size(chunk.code)) { offset = disassemble_instruction(chunk, offset); } } size_t -disassemble_instruction(u8 *chunk, size_t offset) { +disassemble_instruction(Chunk chunk, size_t offset) { printf("%04ld ", offset); - u8 instruction = chunk[offset]; + u8 instruction = chunk.code[offset]; switch (instruction) { case OP_RETURN: { printf("OP_RETURN\n"); return offset + 1; } break; + case OP_CONSTANT: { + u8 constant = chunk.code[offset + 1]; + printf("%-16s %4d (", "OP_CONSTANT", constant); + display(chunk.constants[constant]); + printf(")\n"); + return offset + 2; + } break; default: { printf("Unknown OP: %d\n", instruction); return offset + 1; -- cgit v1.2.1