Convert locals array to dynamic array
This commit is contained in:
parent
b355295982
commit
08571b89e5
12
compiler.c
12
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;
|
||||
|
11
value.c
11
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user