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 "compiler.h"
|
||||||
#include "scanner.h"
|
#include "scanner.h"
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
#ifdef DEBUG_PRINT_CODE
|
#ifdef DEBUG_PRINT_CODE
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
@ -47,8 +48,9 @@ typedef struct {
|
|||||||
} Local;
|
} Local;
|
||||||
|
|
||||||
typedef struct Compiler {
|
typedef struct Compiler {
|
||||||
Local locals[MAX_LOCALS]; //TODO: Make this a dynamic array
|
Local* locals; //TODO: Make this a dynamic array
|
||||||
int localCount;
|
int localCount;
|
||||||
|
int localCapacity;
|
||||||
int scopeDepth;
|
int scopeDepth;
|
||||||
} Compiler;
|
} Compiler;
|
||||||
|
|
||||||
@ -165,7 +167,9 @@ static void patchJump(int offset) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void initCompiler(Compiler* compiler) {
|
static void initCompiler(Compiler* compiler) {
|
||||||
|
compiler->locals = NULL;
|
||||||
compiler->localCount = 0;
|
compiler->localCount = 0;
|
||||||
|
compiler->localCapacity = 0;
|
||||||
compiler->scopeDepth = 0;
|
compiler->scopeDepth = 0;
|
||||||
current = compiler;
|
current = compiler;
|
||||||
}
|
}
|
||||||
@ -341,7 +345,11 @@ static void addLocal(Token name) {
|
|||||||
error("Too many local variables in function.");
|
error("Too many local variables in function.");
|
||||||
return;
|
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* local = ¤t->locals[current->localCount++];
|
||||||
local->name = name;
|
local->name = name;
|
||||||
local->depth = current->scopeDepth;
|
local->depth = current->scopeDepth;
|
||||||
|
11
value.c
11
value.c
@ -112,29 +112,30 @@ uint32_t hashValue(Value value) {
|
|||||||
ValueArray* valArray = AS_VARRAY(value);
|
ValueArray* valArray = AS_VARRAY(value);
|
||||||
unsigned int sum = 0;
|
unsigned int sum = 0;
|
||||||
for (int i = 0; i < valArray->count; i++) {
|
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) {
|
if ((UINT_MAX - sum) < valHash) {
|
||||||
sum = hashUint(sum);
|
sum = hashUint(sum);
|
||||||
}
|
}
|
||||||
sum += valHash;
|
sum += valHash;
|
||||||
}
|
}
|
||||||
return hashUInt(sum);
|
return hashUint(sum);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OBJ_HASH:
|
case OBJ_HASH: {
|
||||||
Table* hash = AS_HASH(value);
|
Table* hash = AS_HASH(value);
|
||||||
Entry** entries = getEntries(hash);
|
Entry** entries = getEntries(hash);
|
||||||
unsigned int sum = 0;
|
unsigned int sum = 0;
|
||||||
for (int i = 0; entries[i] != NULL; i++) {
|
for (int i = 0; entries[i] != NULL; i++) {
|
||||||
Entry* entry = entries[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) {
|
if ((UINT_MAX - sum) < valHash) {
|
||||||
sum = hashUint(sum);
|
sum = hashUint(sum);
|
||||||
}
|
}
|
||||||
sum += valHash;
|
sum += valHash;
|
||||||
}
|
}
|
||||||
return hashUInt(sum);
|
return hashUint(sum);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user