aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/chunk.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-24 18:26:42 +0200
committerBad Diode <bd@badd10de.dev>2021-10-24 18:26:42 +0200
commite2c284b57641b5feec9a8d04313b0cd6d556e860 (patch)
tree3edf7d008f0d68b57727c234a22a0123fe91e383 /src/bytecode/chunk.c
parent35f93683d56d8b7f57c3f27fa7085847e2ad4598 (diff)
downloadbdl-e2c284b57641b5feec9a8d04313b0cd6d556e860.tar.gz
bdl-e2c284b57641b5feec9a8d04313b0cd6d556e860.zip
Add lambda type and minor file cleanup
Diffstat (limited to 'src/bytecode/chunk.c')
-rw-r--r--src/bytecode/chunk.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/bytecode/chunk.c b/src/bytecode/chunk.c
new file mode 100644
index 0000000..3dc2421
--- /dev/null
+++ b/src/bytecode/chunk.c
@@ -0,0 +1,43 @@
1#include "chunk.h"
2#include "objects.h"
3
4Chunk *
5chunk_init(void) {
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 return chunk;
11}
12
13void
14chunk_free(Chunk *chunk) {
15 array_free(chunk->code);
16 for (size_t i = 0; i < array_size(chunk->constants); i++) {
17 Object obj = chunk->constants[i];
18 object_free(obj);
19 }
20 array_free(chunk->constants);
21 array_free(chunk->lines);
22 free(chunk);
23}
24
25void
26add_code(Chunk *chunk, u8 byte, size_t line, size_t col) {
27 array_push(chunk->code, byte);
28 LineInfo info = (LineInfo){line, col};
29 array_push(chunk->lines, info);
30}
31
32size_t
33add_constant(Chunk *chunk, Object obj) {
34 size_t pos = array_size(chunk->constants);
35 for (size_t i = 0; i < pos; i++) {
36 if (object_equal(obj, chunk->constants[i])) {
37 return i;
38 }
39 }
40 array_push(chunk->constants, obj);
41 return pos;
42}
43