diff --git a/src/chunk.c b/src/chunk.c index e36ddd4..77e73f8 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -6,6 +6,7 @@ void newChunk(Chunk* chunk) { chunk->count = 0; chunk->capacity = 0; chunk->code = NULL; + newValueArray(&chunk->constants); } void writeChunk(Chunk *chunk, uint8_t byte) { @@ -18,5 +19,16 @@ void writeChunk(Chunk *chunk, uint8_t byte) { chunk->code[chunk->count] = byte; chunk->count++; - } + +void freeChunk(Chunk *chunk) { + FREE_ARRAY(uint8_t, chunk->code, chunk->capacity); + freeValueArray(&chunk->constants); + newChunk(chunk); +} + +int addConstant(Chunk* chunk, Value value) { + writeValueArray(&chunk->constants, value); + return chunk->constants.count - 1; +} + diff --git a/src/chunk.h b/src/chunk.h index 6f75384..9df161f 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -3,8 +3,11 @@ #include "common.h" #include "memory.h" +#include "value.h" typedef enum { + OP_NOOP, + OP_CONSTANT, OP_RETURN, } OpCode; @@ -12,10 +15,12 @@ typedef struct Chunk { int count; int capacity; uint8_t *code; + ValueArray constants; } Chunk; void newChunk(Chunk *chunk); void freeChunk(Chunk *chunk); void writeChunk(Chunk *chunk, uint8_t byte); +int addConstant(Chunk *chunk, Value value); #endif diff --git a/src/debug.c b/src/debug.c index 4eb7eba..12f63c4 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,6 +1,7 @@ #include #include "debug.h" +#include "value.h" void disassembleChunk(Chunk* chunk, const char* name) { printf("== %s ==\n", name); @@ -10,6 +11,15 @@ void disassembleChunk(Chunk* chunk, const char* name) { } } +static int constantInstruction(const char* name, Chunk* chunk, + int offset) { + uint8_t constant = chunk->code[offset + 1]; + printf("%-16s %4d '", name, constant); + printValue(chunk->constants.values[constant]); + printf("'\n"); + return offset + 2; +} + static int simpleInstruction(const char* name, int offset) { printf("%s\n", name); return offset + 1; @@ -20,6 +30,10 @@ int disassembleInstruction(Chunk* chunk, int offset) { uint8_t instruction = chunk->code[offset]; switch (instruction) { + case OP_NOOP: + return simpleInstruction("OP_NOOP", offset); + case OP_CONSTANT: + return constantInstruction("OP_CONSTANT", chunk, offset); case OP_RETURN: return simpleInstruction("OP_RETURN", offset); default: diff --git a/src/main.c b/src/main.c index 245eb31..4edaa0a 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,11 @@ int main(int argc, const char** argv) { Chunk chunk; newChunk(&chunk); + + int constant = addConstant(&chunk, 1.2); + writeChunk(&chunk, OP_CONSTANT); + writeChunk(&chunk, constant); + writeChunk(&chunk, OP_RETURN); disassembleChunk(&chunk, "test chunk"); diff --git a/src/memory.c b/src/memory.c index c93a6b3..3a928d4 100644 --- a/src/memory.c +++ b/src/memory.c @@ -14,8 +14,3 @@ void* reallocate(void *pointer, size_t oldSize, size_t newSize) { return result; } -void freeChunk(Chunk *chunk) { - FREE_ARRAY(uint8_t, chunk->code, chunk->capacity); - newChunk(chunk); -} - diff --git a/src/value.c b/src/value.c new file mode 100644 index 0000000..664afdb --- /dev/null +++ b/src/value.c @@ -0,0 +1,31 @@ +#include + +#include "memory.h" +#include "value.h" + +void newValueArray(ValueArray *array) { + array->values = NULL; + array->capacity = 0; + array->count = 0; +} + +void writeValueArray(ValueArray *array, Value value) { + if (array->capacity < array->count + 1) { + int oldCapacity = array->capacity; + array->capacity = GROW_CAPACITY(oldCapacity); + array->values = GROW_ARRAY(Value, array->values, + oldCapacity, array->capacity); + } + + array->values[array->count] = value; + array->count++; +} + +void freeValueArray(ValueArray *array) { + FREE_ARRAY(Value, array->values, array->capacity); + newValueArray(array); +} + +void printValue(Value value) { + printf("%g", value); +} diff --git a/src/value.h b/src/value.h new file mode 100644 index 0000000..6a8c1bd --- /dev/null +++ b/src/value.h @@ -0,0 +1,19 @@ +#ifndef ztl_value_h +#define ztl_value_h + +#include "common.h" + +typedef double Value; + +typedef struct { + int capacity; + int count; + Value *values; +} ValueArray; + +void newValueArray(ValueArray *array); +void writeValueArray(ValueArray *array, Value value); +void freeValueArray(ValueArray *array); +void printValue(Value value); + +#endif