diff options
Diffstat (limited to 'src/bytecode/chunk.c')
-rw-r--r-- | src/bytecode/chunk.c | 47 |
1 files changed, 0 insertions, 47 deletions
diff --git a/src/bytecode/chunk.c b/src/bytecode/chunk.c deleted file mode 100644 index af4a3a2..0000000 --- a/src/bytecode/chunk.c +++ /dev/null | |||
@@ -1,47 +0,0 @@ | |||
1 | #include "chunk.h" | ||
2 | #include "objects.h" | ||
3 | |||
4 | Chunk * | ||
5 | chunk_init(StringView name) { | ||
6 | Chunk *chunk = malloc(sizeof(Chunk)); | ||
7 | array_init(chunk->code, 0); | ||
8 | array_init(chunk->constants, 0); | ||
9 | array_init(chunk->lines, 0); | ||
10 | array_init(chunk->name, name.n); | ||
11 | array_insert(chunk->name, name.start, name.n); | ||
12 | chunk->n_params = 0; | ||
13 | chunk->n_locals = 0; | ||
14 | return chunk; | ||
15 | } | ||
16 | |||
17 | void | ||
18 | chunk_free(Chunk *chunk) { | ||
19 | array_free(chunk->code); | ||
20 | for (size_t i = 0; i < array_size(chunk->constants); i++) { | ||
21 | Object obj = chunk->constants[i]; | ||
22 | object_free(&obj); | ||
23 | } | ||
24 | array_free(chunk->constants); | ||
25 | array_free(chunk->lines); | ||
26 | array_free(chunk->name); | ||
27 | free(chunk); | ||
28 | } | ||
29 | |||
30 | void | ||
31 | add_code(Chunk *chunk, u8 byte, size_t line, size_t col) { | ||
32 | array_push(chunk->code, byte); | ||
33 | LineInfo info = (LineInfo){line, col}; | ||
34 | array_push(chunk->lines, info); | ||
35 | } | ||
36 | |||
37 | size_t | ||
38 | add_constant(Chunk *chunk, Object obj) { | ||
39 | size_t pos = array_size(chunk->constants); | ||
40 | for (size_t i = 0; i < pos; i++) { | ||
41 | if (object_equal(obj, chunk->constants[i])) { | ||
42 | return i; | ||
43 | } | ||
44 | } | ||
45 | array_push(chunk->constants, obj); | ||
46 | return pos; | ||
47 | } | ||