Finish Chapter 14

Implements code chunks, a couple of basic opcodes, constants, and
basic execution of those opcodes.

Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
2025-08-21 16:14:22 -05:00
commit 44c7626cd7
13 changed files with 286 additions and 0 deletions

37
src/chunk.c Normal file
View File

@@ -0,0 +1,37 @@
#include "chunk.h"
#include "memory.h"
#include <stdlib.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;
}

46
src/debug.c Normal file
View File

@@ -0,0 +1,46 @@
#include "debug.h"
#include <stdio.h>
#include "chunk.h"
#include "value.h"
void disassembleChunk(Chunk *chunk, const char *name) {
printf("== %s ==\n", name);
for (int offset = 0; offset < chunk->count;) {
offset = disassembleInstruction(chunk, offset);
}
}
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;
}
int disassembleInstruction(Chunk *chunk, int offset) {
printf("%04d ", offset);
if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
printf(" | ");
} else {
printf("%4d ", chunk->lines[offset]);
}
uint8_t instruction = chunk->code[offset];
switch (instruction) {
case OP_CONSTANT:
return constantInstruction("OP_CONSTANT", chunk, offset);
case OP_RETURN:
return simpleInstruction("OP_RETURN", offset);
default:
printf("Unknownopcode %d\n", instruction);
return offset + 1;
}
}

16
src/main.c Normal file
View File

@@ -0,0 +1,16 @@
#include "chunk.h"
#include "common.h"
#include "debug.h"
int main(int argc, const char *argv[]) {
Chunk chunk;
initChunk(&chunk);
int constant = addConstant(&chunk, 1.2);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, constant, 123);
writeChunk(&chunk, OP_RETURN, 123);
disassembleChunk(&chunk, "test chunk");
freeChunk(&chunk);
return 0;
}

15
src/memory.c Normal file
View File

@@ -0,0 +1,15 @@
#include "memory.h"
#include <stdlib.h>
void *reallocate(void *pointer, size_t oldSize, size_t newSize) {
if (newSize == 0) {
free(pointer);
return NULL;
}
void *result = realloc(pointer, newSize);
if (result == NULL) {
exit(1);
}
return result;
}

31
src/value.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include "memory.h"
#include "value.h"
void initValueArray(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);
initValueArray(array);
}
void printValue(Value value) {
printf("%g", value);
}