diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bytecode/chunk.h | 15 | ||||
-rw-r--r-- | src/bytecode/debug.h | 18 | ||||
-rw-r--r-- | src/bytecode/main.c | 8 |
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 | ||
7 | typedef struct LineInfo { | ||
8 | size_t line; | ||
9 | size_t col; | ||
10 | } LineInfo; | ||
11 | |||
7 | typedef struct Chunk { | 12 | typedef struct Chunk { |
8 | u8 *code; | 13 | u8 *code; |
9 | Object *constants; | 14 | Object *constants; |
15 | LineInfo *lines; | ||
10 | } Chunk; | 16 | } Chunk; |
11 | 17 | ||
12 | 18 | void add_code(Chunk chunk, u8 byte, size_t line, size_t col); | |
13 | size_t add_constant(Chunk chunk, Object obj); | 19 | size_t add_constant(Chunk chunk, Object obj); |
14 | 20 | ||
21 | void | ||
22 | add_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 | |||
15 | size_t | 28 | size_t |
16 | add_constant(Chunk chunk, Object obj) { | 29 | add_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); | |||
9 | void | 9 | void |
10 | disassemble_chunk(Chunk chunk, const char *name) { | 10 | disassemble_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 | ||
18 | size_t | 27 | size_t |
19 | disassemble_instruction(Chunk chunk, size_t offset) { | 28 | disassemble_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 | } |