Convert locals array to dynamic array

This commit is contained in:
pjht 2019-06-03 16:22:10 -05:00
parent b355295982
commit 08571b89e5
2 changed files with 16 additions and 7 deletions

View File

@ -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 = &current->locals[current->localCount++];
local->name = name;
local->depth = current->scopeDepth;

11
value.c
View File

@ -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;
}