From 08571b89e55d8e8a2aeadf24e0be94001540f556 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 3 Jun 2019 16:22:10 -0500 Subject: [PATCH] Convert locals array to dynamic array --- compiler.c | 12 ++++++++++-- value.c | 11 ++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/compiler.c b/compiler.c index 1d24ad0..47c4129 100644 --- a/compiler.c +++ b/compiler.c @@ -6,6 +6,7 @@ #include "compiler.h" #include "scanner.h" #include "object.h" +#include "memory.h" #ifdef DEBUG_PRINT_CODE #include "debug.h" @@ -47,8 +48,9 @@ typedef struct { } Local; typedef struct Compiler { - Local locals[MAX_LOCALS]; //TODO: Make this a dynamic array + Local* locals; //TODO: Make this a dynamic array int localCount; + int localCapacity; int scopeDepth; } Compiler; @@ -165,7 +167,9 @@ static void patchJump(int offset) { } static void initCompiler(Compiler* compiler) { + compiler->locals = NULL; compiler->localCount = 0; + compiler->localCapacity = 0; compiler->scopeDepth = 0; current = compiler; } @@ -341,7 +345,11 @@ static void addLocal(Token name) { error("Too many local variables in function."); return; } - + if (current->localCount==current->localCapacity) { + int oldCapacity=current->localCapacity; + current->localCapacity=GROW_CAPACITY(current->localCapacity); + current->locals=GROW_ARRAY(current->locals, Local, oldCapacity, current->localCapacity); + } Local* local = ¤t->locals[current->localCount++]; local->name = name; local->depth = current->scopeDepth; diff --git a/value.c b/value.c index bd05bcc..f762ccd 100644 --- a/value.c +++ b/value.c @@ -112,29 +112,30 @@ uint32_t hashValue(Value value) { ValueArray* valArray = AS_VARRAY(value); unsigned int sum = 0; for (int i = 0; i < valArray->count; i++) { - uint32_t valHash = hashValue(valArray->values[i] * i); + uint32_t valHash = hashValue(valArray->values[i])*i; if ((UINT_MAX - sum) < valHash) { sum = hashUint(sum); } sum += valHash; } - return hashUInt(sum); + return hashUint(sum); break; } - case OBJ_HASH: + case OBJ_HASH: { Table* hash = AS_HASH(value); Entry** entries = getEntries(hash); unsigned int sum = 0; for (int i = 0; entries[i] != NULL; i++) { Entry* entry = entries[i]; - uint32_t valHash = hashValue((hashValue(entry->key)*i)+(hashValue(entry->value)*i)); + uint32_t valHash = hashUint((hashValue(entry->key)*i)+(hashValue(entry->value)*i)); if ((UINT_MAX - sum) < valHash) { sum = hashUint(sum); } sum += valHash; } - return hashUInt(sum); + return hashUint(sum); break; + } default: return 0; }