diff options
author | 2021-07-08 02:24:24 -0700 | |
---|---|---|
committer | 2021-07-08 02:25:13 -0700 | |
commit | 2c007a8f94d65dc40f638b284db7e374a58b632f (patch) | |
tree | 260a1460cdb86c913e785171a85447afc03efddf /clox/src/chunk.c | |
parent | a779473cede81f4d3b4eed6f6b4e4184d43ffa87 (diff) | |
download | crafting-interpreters-2c007a8f94d65dc40f638b284db7e374a58b632f.tar.gz crafting-interpreters-2c007a8f94d65dc40f638b284db7e374a58b632f.zip |
Chapter 14.1-6
Diffstat (limited to 'clox/src/chunk.c')
-rw-r--r-- | clox/src/chunk.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/clox/src/chunk.c b/clox/src/chunk.c new file mode 100644 index 0000000..f368dec --- /dev/null +++ b/clox/src/chunk.c @@ -0,0 +1,37 @@ +#include <stdlib.h> + +#include "chunk.h" +#include "memory.h" + +void initChunk(Chunk* chunk) { + chunk->count = 0; + chunk->capacity = 0; + chunk->code = NULL; + chunk->lines = NULL; + initValueArray(&chunk->constants); +} + +void freeChunk(Chunk* chunk) { + FREE_ARRAY(uint8_t, chunk->code, chunk->capacity); + FREE_ARRAY(int, chunk->lines, chunk->capacity); + freeValueArray(&chunk->constants); + initChunk(chunk); +} + +void writeChunk(Chunk* chunk, uint8_t byte, int line) { + if (chunk->capacity < chunk->count + 1) { + int oldCapacity = chunk->capacity; + chunk->capacity = GROW_CAPACITY(oldCapacity); + chunk->code = GROW_ARRAY(uint8_t, chunk->code, oldCapacity, chunk->capacity); + chunk->lines = GROW_ARRAY(int, chunk->lines, oldCapacity, chunk->capacity); + } + + chunk->code[chunk->count] = byte; + chunk->lines[chunk->count] = line; + chunk->count++; +} + +int addConstant(Chunk* chunk, Value value) { + writeValueArray(&chunk->constants, value); + return chunk->constants.count - 1; +} |