From 10acae93ef8df12b21cb5ff52b8cb68a3cd8ca72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20=F0=9F=91=A8=F0=9F=8F=BD=E2=80=8D=F0=9F=92=BB=20Copl?= =?UTF-8?q?an?= Date: Fri, 1 Nov 2024 17:17:50 -0700 Subject: [PATCH] @@@ wip: hashtable --- bytecode-vm-compiler/Makefile | 2 +- bytecode-vm-compiler/test/test-table.c | 44 ++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/bytecode-vm-compiler/Makefile b/bytecode-vm-compiler/Makefile index c91f532..6c4fdb4 100644 --- a/bytecode-vm-compiler/Makefile +++ b/bytecode-vm-compiler/Makefile @@ -11,7 +11,7 @@ lox: src/main.c $(SRC_FILES) src/*.h all: format lint ci ci: lox test clangd lint-check # do not run format-check because GH A uses an incompatible version of clang-format -test: test/test-stack test/test-vm test/test-scanner test/test-compiler +test: test/test-stack test/test-vm test/test-scanner test/test-compiler test/test-table test/test-vm: test/test-vm.c src/vm.h src/vm.c src/chunk.h src/chunk.c src/value.h src/value.c src/compiler.h src/compiler.c $(CC) -fsanitize=address $(CFLAGS) -Wno-error test/test-vm.c $(SRC_FILES) -o test/test-vm test/test-vm || (mv test/test-vm test/test-vm.failed && false) diff --git a/bytecode-vm-compiler/test/test-table.c b/bytecode-vm-compiler/test/test-table.c index 90b01ff..1e89b01 100644 --- a/bytecode-vm-compiler/test/test-table.c +++ b/bytecode-vm-compiler/test/test-table.c @@ -1,6 +1,41 @@ +#include "../src/lox_assert.h" #include "../src/vm.h" #include -LoxString* allocateString(VM* vm, char const* cString, int length); +LoxString* allocateString(VM* vm, char const* cString, size_t length); + +#include "../src/common.h" +#include "../src/memory.h" +#include "../src/value.h" + +typedef struct { + LoxString* key; + Value value; +} TableEntry; + +typedef struct { + size_t count; + size_t capacity; + TableEntry* entries; +} LoxTable; + +void tableInit(LoxTable* table); +void tableInit(LoxTable* table) { + table->count = 0; + table->capacity = table->count; + table->entries = NULL; +} +void tableFree(LoxTable* table); +void tableFree(LoxTable* table) { + if (table->entries == NULL) { + LOX_ASSERT(false && "trying to free uninitialized table"); + } + FREE_ARRAY(Entry, table->entries, table->capacity); +} +bool tableSet(LoxTable* table, LoxString* key, Value value); +bool tableSet(LoxTable* table, LoxString* key, Value value) {} +Value* tableGet(LoxTable* table, LoxString* key); +Value* tableGet(LoxTable* table, LoxString* key) {} + int main(void) { // basic initVM(); @@ -9,6 +44,11 @@ int main(void) { tableInit(&table); char const* k = "key"; LoxString* key = allocateString(&vm, "key", strlen(k)); - tableSet(&table, key, NUMBER_VAL(42.0)); + Value want = NUMBER_VAL(42.0); + tableSet(&table, key, want); + + Value* res = tableGet(&table, key); + LOX_ASSERT_VALUE_EQUALS(*res, want); + tableFree(&table); }