kuroko/chunk.c

71 lines
2.0 KiB
C
Raw Normal View History

#include "chunk.h"
#include "memory.h"
#include "vm.h"
void krk_initChunk(KrkChunk * chunk) {
chunk->count = 0;
chunk->capacity = 0;
chunk->code = NULL;
chunk->linesCount = 0;
chunk->linesCapacity = 0;
chunk->lines = NULL;
2020-12-28 04:54:25 +03:00
chunk->filename = NULL;
krk_initValueArray(&chunk->constants);
}
static void addLine(KrkChunk * chunk, size_t line) {
if (chunk->linesCount && chunk->lines[chunk->linesCount-1].line == line) return;
if (chunk->linesCapacity < chunk->linesCount + 1) {
int old = chunk->linesCapacity;
chunk->linesCapacity = GROW_CAPACITY(old);
chunk->lines = GROW_ARRAY(KrkLineMap, chunk->lines, old, chunk->linesCapacity);
}
chunk->lines[chunk->linesCount] = (KrkLineMap){chunk->count, line};
chunk->linesCount++;
}
void krk_writeChunk(KrkChunk * chunk, uint8_t byte, size_t line) {
if (chunk->capacity < chunk->count + 1) {
int old = chunk->capacity;
chunk->capacity = GROW_CAPACITY(old);
chunk->code = GROW_ARRAY(uint8_t, chunk->code, old, chunk->capacity);
}
chunk->code[chunk->count] = byte;
addLine(chunk, line);
chunk->count++;
}
void krk_freeChunk(KrkChunk * chunk) {
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
FREE_ARRAY(size_t, chunk->lines, chunk->capacity);
krk_freeValueArray(&chunk->constants);
krk_initChunk(chunk);
}
size_t krk_addConstant(KrkChunk * chunk, KrkValue value) {
krk_push(value);
krk_writeValueArray(&chunk->constants, value);
krk_pop();
return chunk->constants.count - 1;
}
void krk_emitConstant(KrkChunk * chunk, size_t ind, size_t line) {
if (ind >= 256) {
krk_writeChunk(chunk, OP_CONSTANT_LONG, line);
krk_writeChunk(chunk, 0xFF & (ind >> 16), line);
krk_writeChunk(chunk, 0xFF & (ind >> 8), line);
krk_writeChunk(chunk, 0xFF & (ind >> 0), line);
} else {
krk_writeChunk(chunk, OP_CONSTANT, line);
krk_writeChunk(chunk, ind, line);
}
2020-12-26 10:53:15 +03:00
}
size_t krk_writeConstant(KrkChunk * chunk, KrkValue value, size_t line) {
size_t ind = krk_addConstant(chunk, value);
krk_emitConstant(chunk, ind, line);
return ind;
}