Skip to content

Commit

Permalink
Complete hash map and string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
dils2k committed Mar 22, 2023
1 parent 60f565e commit 738127e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
11 changes: 11 additions & 0 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "memory.h"
#include "object.h"
#include "table.h"
#include "value.h"
#include "vm.h"

Expand All @@ -22,6 +23,7 @@ static ObjString* allocateString(char* chars, int length, uint32_t hash) {
string->length = length;
string->chars = chars;
string->hash = hash;
tableSet(&vm.strings, string, NIL_VAL);
return string;
}

Expand All @@ -36,6 +38,10 @@ static uint32_t hashString(const char* key, int length) {

ObjString* copyString(const char* chars, int length) {
uint32_t hash = hashString(chars, length);

ObjString* interned = tableFindString(&vm.strings, chars, length, hash);
if (interned != NULL) return interned;

char* heapChars = ALLOCATE(char, length+1);
memcpy(heapChars, chars, length);
heapChars[length] = '\0';
Expand All @@ -44,6 +50,11 @@ ObjString* copyString(const char* chars, int length) {

ObjString* takeString(char* chars, int length) {
uint32_t hash = hashString(chars, length);
ObjString* interned = tableFindString(&vm.strings, chars, length, hash);
if (interned != NULL) {
FREE_ARRAY(char, chars, length+1);
return interned;
}
return allocateString(chars, length, hash);
}

Expand Down
20 changes: 20 additions & 0 deletions table.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ void tableAddAll(Table* from, Table* to) {
}
}

ObjString* tableFindString(Table* table, const char* chars,
int length, uint32_t hash) {
if (table->count == 0) return NULL;

uint32_t index = hash % table->capacity;
for (;;) {
Entry* entry = &table->entries[index];
if (entry->key == NULL) {
// Stop if we find an empty non-tombstone entry.
if (IS_NIL(entry->value)) return NULL;
} else if (entry->key->length == length &&
entry->key->hash == hash &&
memcmp(entry->key->chars, chars, length) == 0) {
// We found it.
return entry->key;
}
index = (index + 1) % table->capacity;
}
}

bool tableGet(Table* table, ObjString* key, Value* value) {
if (table->count == 0) return false;

Expand Down
2 changes: 2 additions & 0 deletions table.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ bool tableGet(Table* table, ObjString* key, Value* value);
bool tableSet(Table* table, ObjString* key, Value value);
bool tableDelete(Table* table, ObjString* key);
void tableAddAll(Table* from, Table* to);
ObjString* tableFindString(Table* table, const char* chars,
int length, uint32_t hash);

#endif
8 changes: 1 addition & 7 deletions value.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ bool valuesEqual(Value a, Value b) {
case VAL_BOOL: return AS_BOOL(a) == AS_BOOL(b);
case VAL_NIL: return true;
case VAL_NUMBER: return AS_NUMBER(a) == AS_NUMBER(b);
case VAL_OBJ: {
ObjString* aString = AS_STRING(a);
ObjString* bString = AS_STRING(b);
return aString->length == bString->length &&
memcmp(aString->chars, bString->chars,
aString->length) == 0;
}
case VAL_OBJ: return AS_OBJ(a) == AS_OBJ(b);
default: return false; // Unreachable.
}
}
2 changes: 2 additions & 0 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ static void runtimeError(const char* format, ...) {
void initVM() {
resetStack();
vm.objects = NULL;
initTable(&vm.strings);
}

void freeVM() {
freeTable(&vm.strings);
freeObjects();
}

Expand Down
2 changes: 2 additions & 0 deletions vm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "chunk.h"
#include "value.h"
#include "table.h"

#ifndef clox_vm_h
#define clox_vm_h
Expand All @@ -14,6 +15,7 @@ typedef struct {
Value stack[STACK_MAX];
Value* stackTop;
Obj* objects;
Table strings;
} VM;

extern VM vm;
Expand Down

0 comments on commit 738127e

Please sign in to comment.