From f4113cbcdc192b23f9b6e5e14b0a3e4afac35272 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Oct 2021 10:57:23 +0200 Subject: Add line/col information for debugging purposes. --- src/bytecode/chunk.h | 15 ++++++++++++++- src/bytecode/debug.h | 18 +++++++++++++++++- src/bytecode/main.c | 8 +++++--- 3 files changed, 36 insertions(+), 5 deletions(-) (limited to 'src/bytecode') diff --git a/src/bytecode/chunk.h b/src/bytecode/chunk.h index 29dd99d..6157057 100644 --- a/src/bytecode/chunk.h +++ b/src/bytecode/chunk.h @@ -4,14 +4,27 @@ #include "objects.h" #include "darray.h" +typedef struct LineInfo { + size_t line; + size_t col; +} LineInfo; + typedef struct Chunk { u8 *code; Object *constants; + LineInfo *lines; } Chunk; - +void add_code(Chunk chunk, u8 byte, size_t line, size_t col); size_t add_constant(Chunk chunk, Object obj); +void +add_code(Chunk chunk, u8 byte, size_t line, size_t col) { + array_push(chunk.code, byte); + LineInfo info = (LineInfo){line, col}; + array_push(chunk.lines, info); +} + size_t add_constant(Chunk chunk, Object obj) { size_t pos = array_size(chunk.constants); diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h index e07b9a2..ceedfbf 100644 --- a/src/bytecode/debug.h +++ b/src/bytecode/debug.h @@ -9,15 +9,31 @@ size_t disassemble_instruction(Chunk chunk, size_t offset); void disassemble_chunk(Chunk chunk, const char *name) { printf("== %s ==\n", name); + printf("code:\n"); size_t offset = 0; while (offset < array_size(chunk.code)) { offset = disassemble_instruction(chunk, offset); } + printf("\nconstants:\n"); + offset = 0; + while (offset < array_size(chunk.constants)) { + printf("\t%04ld -> ", offset); + display(chunk.constants[offset]); + printf("\n"); + offset++; + } } size_t disassemble_instruction(Chunk chunk, size_t offset) { - printf("%04ld ", offset); + printf("\t%04ld ", offset); + if (offset > 0 + && chunk.lines[offset].line == chunk.lines[offset - 1].line + && chunk.lines[offset].col == chunk.lines[offset - 1].col) { + printf("%4s|%-4s ", " ", " "); + } else { + printf("%4ld:%-4ld ", chunk.lines[offset].line, chunk.lines[offset].col); + } u8 instruction = chunk.code[offset]; switch (instruction) { case OP_RETURN: { diff --git a/src/bytecode/main.c b/src/bytecode/main.c index ce196c8..c994e08 100644 --- a/src/bytecode/main.c +++ b/src/bytecode/main.c @@ -32,12 +32,13 @@ process_source(const StringView *source) { Chunk chunk = {0}; array_init(chunk.code, 0); array_init(chunk.constants, 0); + array_init(chunk.lines, 0); // Push some test instructions. size_t const_idx = add_constant(chunk, 7); - array_push(chunk.code, OP_CONSTANT); - array_push(chunk.code, const_idx); - array_push(chunk.code, OP_RETURN); + add_code(chunk, OP_CONSTANT, 1, 1); + add_code(chunk, const_idx, 1, 1); + add_code(chunk, OP_RETURN, 1, 1); // Disassemble the chunk. disassemble_chunk(chunk, "test chunk"); @@ -45,6 +46,7 @@ process_source(const StringView *source) { // Free chunk. array_free(chunk.code); array_free(chunk.constants); + array_free(chunk.lines); array_free(tokens); } -- cgit v1.2.1