Skip to content

Commit

Permalink
@@@ wip: hashtable
Browse files Browse the repository at this point in the history
  • Loading branch information
vegerot committed Nov 2, 2024
1 parent 1209b57 commit 10acae9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bytecode-vm-compiler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 42 additions & 2 deletions bytecode-vm-compiler/test/test-table.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
#include "../src/lox_assert.h"
#include "../src/vm.h"
#include <string.h>
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();
Expand All @@ -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);
}

0 comments on commit 10acae9

Please sign in to comment.