aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-22 10:57:23 +0200
committerBad Diode <bd@badd10de.dev>2021-10-22 10:57:23 +0200
commitf4113cbcdc192b23f9b6e5e14b0a3e4afac35272 (patch)
tree8ce6ee2b83256243720465d4fb2e1a89fcee8be7 /src/bytecode
parent33372512fc32c26913c8385637d20f6d98c8376c (diff)
downloadbdl-f4113cbcdc192b23f9b6e5e14b0a3e4afac35272.tar.gz
bdl-f4113cbcdc192b23f9b6e5e14b0a3e4afac35272.zip
Add line/col information for debugging purposes.
Diffstat (limited to 'src/bytecode')
-rw-r--r--src/bytecode/chunk.h15
-rw-r--r--src/bytecode/debug.h18
-rw-r--r--src/bytecode/main.c8
3 files changed, 36 insertions, 5 deletions
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 @@
4#include "objects.h" 4#include "objects.h"
5#include "darray.h" 5#include "darray.h"
6 6
7typedef struct LineInfo {
8 size_t line;
9 size_t col;
10} LineInfo;
11
7typedef struct Chunk { 12typedef struct Chunk {
8 u8 *code; 13 u8 *code;
9 Object *constants; 14 Object *constants;
15 LineInfo *lines;
10} Chunk; 16} Chunk;
11 17
12 18void add_code(Chunk chunk, u8 byte, size_t line, size_t col);
13size_t add_constant(Chunk chunk, Object obj); 19size_t add_constant(Chunk chunk, Object obj);
14 20
21void
22add_code(Chunk chunk, u8 byte, size_t line, size_t col) {
23 array_push(chunk.code, byte);
24 LineInfo info = (LineInfo){line, col};
25 array_push(chunk.lines, info);
26}
27
15size_t 28size_t
16add_constant(Chunk chunk, Object obj) { 29add_constant(Chunk chunk, Object obj) {
17 size_t pos = array_size(chunk.constants); 30 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);
9void 9void
10disassemble_chunk(Chunk chunk, const char *name) { 10disassemble_chunk(Chunk chunk, const char *name) {
11 printf("== %s ==\n", name); 11 printf("== %s ==\n", name);
12 printf("code:\n");
12 size_t offset = 0; 13 size_t offset = 0;
13 while (offset < array_size(chunk.code)) { 14 while (offset < array_size(chunk.code)) {
14 offset = disassemble_instruction(chunk, offset); 15 offset = disassemble_instruction(chunk, offset);
15 } 16 }
17 printf("\nconstants:\n");
18 offset = 0;
19 while (offset < array_size(chunk.constants)) {
20 printf("\t%04ld -> ", offset);
21 display(chunk.constants[offset]);
22 printf("\n");
23 offset++;
24 }
16} 25}
17 26
18size_t 27size_t
19disassemble_instruction(Chunk chunk, size_t offset) { 28disassemble_instruction(Chunk chunk, size_t offset) {
20 printf("%04ld ", offset); 29 printf("\t%04ld ", offset);
30 if (offset > 0
31 && chunk.lines[offset].line == chunk.lines[offset - 1].line
32 && chunk.lines[offset].col == chunk.lines[offset - 1].col) {
33 printf("%4s|%-4s ", " ", " ");
34 } else {
35 printf("%4ld:%-4ld ", chunk.lines[offset].line, chunk.lines[offset].col);
36 }
21 u8 instruction = chunk.code[offset]; 37 u8 instruction = chunk.code[offset];
22 switch (instruction) { 38 switch (instruction) {
23 case OP_RETURN: { 39 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) {
32 Chunk chunk = {0}; 32 Chunk chunk = {0};
33 array_init(chunk.code, 0); 33 array_init(chunk.code, 0);
34 array_init(chunk.constants, 0); 34 array_init(chunk.constants, 0);
35 array_init(chunk.lines, 0);
35 36
36 // Push some test instructions. 37 // Push some test instructions.
37 size_t const_idx = add_constant(chunk, 7); 38 size_t const_idx = add_constant(chunk, 7);
38 array_push(chunk.code, OP_CONSTANT); 39 add_code(chunk, OP_CONSTANT, 1, 1);
39 array_push(chunk.code, const_idx); 40 add_code(chunk, const_idx, 1, 1);
40 array_push(chunk.code, OP_RETURN); 41 add_code(chunk, OP_RETURN, 1, 1);
41 42
42 // Disassemble the chunk. 43 // Disassemble the chunk.
43 disassemble_chunk(chunk, "test chunk"); 44 disassemble_chunk(chunk, "test chunk");
@@ -45,6 +46,7 @@ process_source(const StringView *source) {
45 // Free chunk. 46 // Free chunk.
46 array_free(chunk.code); 47 array_free(chunk.code);
47 array_free(chunk.constants); 48 array_free(chunk.constants);
49 array_free(chunk.lines);
48 50
49 array_free(tokens); 51 array_free(tokens);
50} 52}