5 #include "opcode_enum.h"
12 chunk->linesCount = 0;
13 chunk->linesCapacity = 0;
15 chunk->filename = NULL;
20 static void addLine(
KrkChunk * chunk,
size_t line) {
21 if (chunk->linesCount && chunk->lines[chunk->linesCount-1].line == line)
return;
22 if (chunk->linesCapacity < chunk->linesCount + 1) {
23 int old = chunk->linesCapacity;
24 chunk->linesCapacity = KRK_GROW_CAPACITY(old);
25 chunk->lines = KRK_GROW_ARRAY(
KrkLineMap, chunk->lines, old, chunk->linesCapacity);
27 chunk->lines[chunk->linesCount] = (
KrkLineMap){chunk->count, line};
32 if (chunk->capacity < chunk->count + 1) {
33 int old = chunk->capacity;
34 chunk->capacity = KRK_GROW_CAPACITY(old);
35 chunk->code = KRK_GROW_ARRAY(uint8_t, chunk->code, old, chunk->capacity);
38 chunk->code[chunk->count] = byte;
44 KRK_FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
45 KRK_FREE_ARRAY(
KrkLineMap, chunk->lines, chunk->linesCapacity);
54 return chunk->constants.
count - 1;
78 size_t hi = chunk->linesCount;
83 for (
size_t i = lo; i < hi; ++i) {
84 if (chunk->lines[i].startOffset > offset)
break;
85 line = chunk->lines[i].line;
90 size_t mp = lo + (hi - lo) / 2;
92 if (chunk->lines[mp].startOffset > offset) {
Structures and enums for bytecode chunks.
Functions for dealing with garbage collection and memory allocation.
Opcode chunk of a code object.
void krk_initChunk(KrkChunk *chunk)
Initialize an opcode chunk.
void krk_freeChunk(KrkChunk *chunk)
Release the resources allocated to an opcode chunk.
void krk_emitConstant(KrkChunk *chunk, size_t ind, size_t line)
Write an OP_CONSTANT(_LONG) instruction.
size_t krk_writeConstant(KrkChunk *chunk, KrkValue value, size_t line)
Add a new constant and write an instruction for it.
size_t krk_lineNumber(KrkChunk *chunk, size_t offset)
Obtain the line number for a byte offset into a bytecode chunk.
size_t krk_addConstant(KrkChunk *chunk, KrkValue value)
Add a new constant value to an opcode chunk.
void krk_writeChunk(KrkChunk *chunk, uint8_t byte, size_t line)
Append a byte to an opcode chunk.
Map entry of instruction offsets to line numbers.
void krk_initValueArray(KrkValueArray *array)
Initialize a value array.
void krk_freeValueArray(KrkValueArray *array)
Release relesources used by a value array.
void krk_writeValueArray(KrkValueArray *array, KrkValue value)
Add a value to a value array.
Stack reference or primative value.
Core API for the bytecode virtual machine.
KrkValue krk_pop(void)
Pop the top of the stack.
void krk_push(KrkValue value)
Push a stack value.