diff options
Diffstat (limited to 'src/bytecode/chunk.h')
-rwxr-xr-x | src/bytecode/chunk.h | 43 |
1 files changed, 2 insertions, 41 deletions
diff --git a/src/bytecode/chunk.h b/src/bytecode/chunk.h index 5fbc000..3e1c005 100755 --- a/src/bytecode/chunk.h +++ b/src/bytecode/chunk.h | |||
@@ -1,9 +1,10 @@ | |||
1 | #ifndef BDL_CHUNK_H | 1 | #ifndef BDL_CHUNK_H |
2 | #define BDL_CHUNK_H | 2 | #define BDL_CHUNK_H |
3 | 3 | ||
4 | #include "objects.h" | ||
5 | #include "darray.h" | 4 | #include "darray.h" |
6 | 5 | ||
6 | typedef struct Object Object; | ||
7 | |||
7 | typedef struct LineInfo { | 8 | typedef struct LineInfo { |
8 | size_t line; | 9 | size_t line; |
9 | size_t col; | 10 | size_t col; |
@@ -20,44 +21,4 @@ void add_code(Chunk *chunk, u8 byte, size_t line, size_t col); | |||
20 | size_t add_constant(Chunk *chunk, Object obj); | 21 | size_t add_constant(Chunk *chunk, Object obj); |
21 | void chunk_free(Chunk *chunk); | 22 | void chunk_free(Chunk *chunk); |
22 | 23 | ||
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 | for (size_t i = 0; i < array_size(chunk->constants); i++) { | ||
36 | Object obj = chunk->constants[i]; | ||
37 | object_free(obj); | ||
38 | } | ||
39 | array_free(chunk->constants); | ||
40 | array_free(chunk->lines); | ||
41 | free(chunk); | ||
42 | } | ||
43 | |||
44 | void | ||
45 | add_code(Chunk *chunk, u8 byte, size_t line, size_t col) { | ||
46 | array_push(chunk->code, byte); | ||
47 | LineInfo info = (LineInfo){line, col}; | ||
48 | array_push(chunk->lines, info); | ||
49 | } | ||
50 | |||
51 | size_t | ||
52 | add_constant(Chunk *chunk, Object obj) { | ||
53 | size_t pos = array_size(chunk->constants); | ||
54 | for (size_t i = 0; i < pos; i++) { | ||
55 | if (object_equal(obj, chunk->constants[i])) { | ||
56 | return i; | ||
57 | } | ||
58 | } | ||
59 | array_push(chunk->constants, obj); | ||
60 | return pos; | ||
61 | } | ||
62 | |||
63 | #endif // BDL_CHUNK_H | 24 | #endif // BDL_CHUNK_H |