Initial commit
This commit is contained in:
commit
b2edca2cb4
25 changed files with 3590 additions and 0 deletions
43
chunk.c
Normal file
43
chunk.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include "chunk.h"
|
||||
#include "memory.h"
|
||||
#include "vm.h"
|
||||
|
||||
void initChunk(Chunk* chunk)
|
||||
{
|
||||
chunk->count = 0;
|
||||
chunk->capacity = 0;
|
||||
chunk->code = nullptr;
|
||||
chunk->lines = nullptr;
|
||||
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)
|
||||
{
|
||||
push(value);
|
||||
writeValueArray(&chunk->constants, value);
|
||||
pop();
|
||||
return chunk->constants.count - 1;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue