diff options
-rw-r--r-- | src/bytecode/chunk.h | 35 | ||||
-rw-r--r-- | src/bytecode/debug.h | 26 | ||||
-rw-r--r-- | src/bytecode/main.c | 39 | ||||
-rw-r--r-- | src/bytecode/vm.h | 27 |
4 files changed, 85 insertions, 42 deletions
diff --git a/src/bytecode/chunk.h b/src/bytecode/chunk.h index 6157057..e3c7383 100644 --- a/src/bytecode/chunk.h +++ b/src/bytecode/chunk.h | |||
@@ -15,20 +15,39 @@ typedef struct Chunk { | |||
15 | LineInfo *lines; | 15 | LineInfo *lines; |
16 | } Chunk; | 16 | } Chunk; |
17 | 17 | ||
18 | void add_code(Chunk chunk, u8 byte, size_t line, size_t col); | 18 | Chunk * chunk_init(void); |
19 | size_t add_constant(Chunk chunk, Object obj); | 19 | void add_code(Chunk *chunk, u8 byte, size_t line, size_t col); |
20 | size_t add_constant(Chunk *chunk, Object obj); | ||
21 | void chunk_free(Chunk *chunk); | ||
22 | |||
23 | Chunk * | ||
24 | chunk_init(void) { | ||
25 | Chunk *chunk = malloc(sizeof(Chunk)); | ||
26 | array_init(chunk->code, 0); | ||
27 | array_init(chunk->constants, 0); | ||
28 | array_init(chunk->lines, 0); | ||
29 | return chunk; | ||
30 | } | ||
31 | |||
32 | void | ||
33 | chunk_free(Chunk *chunk) { | ||
34 | array_free(chunk->code); | ||
35 | array_free(chunk->constants); | ||
36 | array_free(chunk->lines); | ||
37 | free(chunk); | ||
38 | } | ||
20 | 39 | ||
21 | void | 40 | void |
22 | add_code(Chunk chunk, u8 byte, size_t line, size_t col) { | 41 | add_code(Chunk *chunk, u8 byte, size_t line, size_t col) { |
23 | array_push(chunk.code, byte); | 42 | array_push(chunk->code, byte); |
24 | LineInfo info = (LineInfo){line, col}; | 43 | LineInfo info = (LineInfo){line, col}; |
25 | array_push(chunk.lines, info); | 44 | array_push(chunk->lines, info); |
26 | } | 45 | } |
27 | 46 | ||
28 | size_t | 47 | size_t |
29 | add_constant(Chunk chunk, Object obj) { | 48 | add_constant(Chunk *chunk, Object obj) { |
30 | size_t pos = array_size(chunk.constants); | 49 | size_t pos = array_size(chunk->constants); |
31 | array_push(chunk.constants, obj); | 50 | array_push(chunk->constants, obj); |
32 | return pos; | 51 | return pos; |
33 | } | 52 | } |
34 | 53 | ||
diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h index ceedfbf..c891b77 100644 --- a/src/bytecode/debug.h +++ b/src/bytecode/debug.h | |||
@@ -3,47 +3,47 @@ | |||
3 | 3 | ||
4 | #include "chunk.h" | 4 | #include "chunk.h" |
5 | 5 | ||
6 | void disassemble_chunk(Chunk chunk, const char *name); | 6 | void disassemble_chunk(Chunk *chunk, const char *name); |
7 | size_t disassemble_instruction(Chunk chunk, size_t offset); | 7 | size_t disassemble_instruction(Chunk *chunk, size_t offset); |
8 | 8 | ||
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 | printf("code:\n"); |
13 | size_t offset = 0; | 13 | size_t offset = 0; |
14 | while (offset < array_size(chunk.code)) { | 14 | while (offset < array_size(chunk->code)) { |
15 | offset = disassemble_instruction(chunk, offset); | 15 | offset = disassemble_instruction(chunk, offset); |
16 | } | 16 | } |
17 | printf("\nconstants:\n"); | 17 | printf("\nconstants:\n"); |
18 | offset = 0; | 18 | offset = 0; |
19 | while (offset < array_size(chunk.constants)) { | 19 | while (offset < array_size(chunk->constants)) { |
20 | printf("\t%04ld -> ", offset); | 20 | printf("\t%04ld -> ", offset); |
21 | display(chunk.constants[offset]); | 21 | display(chunk->constants[offset]); |
22 | printf("\n"); | 22 | printf("\n"); |
23 | offset++; | 23 | offset++; |
24 | } | 24 | } |
25 | } | 25 | } |
26 | 26 | ||
27 | size_t | 27 | size_t |
28 | disassemble_instruction(Chunk chunk, size_t offset) { | 28 | disassemble_instruction(Chunk *chunk, size_t offset) { |
29 | printf("\t%04ld ", offset); | 29 | printf("\t%04ld ", offset); |
30 | if (offset > 0 | 30 | if (offset > 0 |
31 | && chunk.lines[offset].line == chunk.lines[offset - 1].line | 31 | && chunk->lines[offset].line == chunk->lines[offset - 1].line |
32 | && chunk.lines[offset].col == chunk.lines[offset - 1].col) { | 32 | && chunk->lines[offset].col == chunk->lines[offset - 1].col) { |
33 | printf("%4s|%-4s ", " ", " "); | 33 | printf("%4s|%-4s ", " ", " "); |
34 | } else { | 34 | } else { |
35 | printf("%4ld:%-4ld ", chunk.lines[offset].line, chunk.lines[offset].col); | 35 | printf("%4ld:%-4ld ", chunk->lines[offset].line, chunk->lines[offset].col); |
36 | } | 36 | } |
37 | u8 instruction = chunk.code[offset]; | 37 | u8 instruction = chunk->code[offset]; |
38 | switch (instruction) { | 38 | switch (instruction) { |
39 | case OP_RETURN: { | 39 | case OP_RETURN: { |
40 | printf("OP_RETURN\n"); | 40 | printf("OP_RETURN\n"); |
41 | return offset + 1; | 41 | return offset + 1; |
42 | } break; | 42 | } break; |
43 | case OP_CONSTANT: { | 43 | case OP_CONSTANT: { |
44 | u8 constant = chunk.code[offset + 1]; | 44 | u8 constant = chunk->code[offset + 1]; |
45 | printf("%-16s %4d (", "OP_CONSTANT", constant); | 45 | printf("%-16s %4d (", "OP_CONSTANT", constant); |
46 | display(chunk.constants[constant]); | 46 | display(chunk->constants[constant]); |
47 | printf(")\n"); | 47 | printf(")\n"); |
48 | return offset + 2; | 48 | return offset + 2; |
49 | } break; | 49 | } break; |
diff --git a/src/bytecode/main.c b/src/bytecode/main.c index c994e08..ca1f441 100644 --- a/src/bytecode/main.c +++ b/src/bytecode/main.c | |||
@@ -4,9 +4,8 @@ | |||
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | #include <string.h> | 5 | #include <string.h> |
6 | 6 | ||
7 | #include "types.h" | 7 | #include "vm.h" |
8 | #include "chunk.h" | 8 | |
9 | #include "darray.h" | ||
10 | #include "ops.h" | 9 | #include "ops.h" |
11 | #include "debug.h" | 10 | #include "debug.h" |
12 | #include "errors.c" | 11 | #include "errors.c" |
@@ -14,9 +13,16 @@ | |||
14 | #include "read_line.c" | 13 | #include "read_line.c" |
15 | #include "string_view.c" | 14 | #include "string_view.c" |
16 | 15 | ||
16 | static VM vm; | ||
17 | |||
17 | void | 18 | void |
18 | init(void) { | 19 | init(void) { |
19 | // STUB | 20 | vm = vm_init(); |
21 | } | ||
22 | |||
23 | void | ||
24 | halt(void) { | ||
25 | vm_free(vm); | ||
20 | } | 26 | } |
21 | 27 | ||
22 | void | 28 | void |
@@ -27,26 +33,14 @@ process_source(const StringView *source) { | |||
27 | return; | 33 | return; |
28 | } | 34 | } |
29 | 35 | ||
30 | // Test chunks and debugging utilities. | ||
31 | // Initialize chunk. | ||
32 | Chunk chunk = {0}; | ||
33 | array_init(chunk.code, 0); | ||
34 | array_init(chunk.constants, 0); | ||
35 | array_init(chunk.lines, 0); | ||
36 | |||
37 | // Push some test instructions. | 36 | // Push some test instructions. |
38 | size_t const_idx = add_constant(chunk, 7); | 37 | size_t const_idx = add_constant(vm.chunk, 7); |
39 | add_code(chunk, OP_CONSTANT, 1, 1); | 38 | add_code(vm.chunk, OP_CONSTANT, 1, 1); |
40 | add_code(chunk, const_idx, 1, 1); | 39 | add_code(vm.chunk, const_idx, 1, 1); |
41 | add_code(chunk, OP_RETURN, 1, 1); | 40 | add_code(vm.chunk, OP_RETURN, 1, 1); |
42 | 41 | ||
43 | // Disassemble the chunk. | 42 | // Disassemble the chunk. |
44 | disassemble_chunk(chunk, "test chunk"); | 43 | disassemble_chunk(vm.chunk, "test chunk"); |
45 | |||
46 | // Free chunk. | ||
47 | array_free(chunk.code); | ||
48 | array_free(chunk.constants); | ||
49 | array_free(chunk.lines); | ||
50 | 44 | ||
51 | array_free(tokens); | 45 | array_free(tokens); |
52 | } | 46 | } |
@@ -183,6 +177,7 @@ main(int argc, char *argv[]) { | |||
183 | case 'i': { | 177 | case 'i': { |
184 | // Interactive mode. | 178 | // Interactive mode. |
185 | run_repl(); | 179 | run_repl(); |
180 | halt(); | ||
186 | return EXIT_SUCCESS; | 181 | return EXIT_SUCCESS; |
187 | } break; | 182 | } break; |
188 | default: { | 183 | default: { |
@@ -195,6 +190,7 @@ main(int argc, char *argv[]) { | |||
195 | // Run from stdin. | 190 | // Run from stdin. |
196 | if (optind == argc) { | 191 | if (optind == argc) { |
197 | run_stdin(); | 192 | run_stdin(); |
193 | halt(); | ||
198 | return EXIT_SUCCESS; | 194 | return EXIT_SUCCESS; |
199 | } | 195 | } |
200 | 196 | ||
@@ -205,5 +201,6 @@ main(int argc, char *argv[]) { | |||
205 | optind++; | 201 | optind++; |
206 | } | 202 | } |
207 | 203 | ||
204 | halt(); | ||
208 | return EXIT_SUCCESS; | 205 | return EXIT_SUCCESS; |
209 | } | 206 | } |
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h new file mode 100644 index 0000000..36547cb --- /dev/null +++ b/src/bytecode/vm.h | |||
@@ -0,0 +1,27 @@ | |||
1 | #ifndef BDL_VM_H | ||
2 | #define BDL_VM_H | ||
3 | |||
4 | #include "types.h" | ||
5 | #include "chunk.h" | ||
6 | |||
7 | typedef struct VM { | ||
8 | Chunk *chunk; | ||
9 | } VM; | ||
10 | |||
11 | VM vm_init(void); | ||
12 | void vm_free(VM vm); | ||
13 | |||
14 | VM | ||
15 | vm_init(void) { | ||
16 | VM vm = { | ||
17 | .chunk = chunk_init(), | ||
18 | }; | ||
19 | return vm; | ||
20 | } | ||
21 | |||
22 | void | ||
23 | vm_free(VM vm) { | ||
24 | chunk_free(vm.chunk); | ||
25 | } | ||
26 | |||
27 | #endif // BDL_VM_H | ||