Started working on Toy_Scope, incomplete

I only worked for a couple hours today.
This commit is contained in:
2024-10-10 22:53:49 +11:00
parent e6fa345fe6
commit 8d1e4d647b
8 changed files with 220 additions and 45 deletions

View File

@@ -70,18 +70,7 @@ bool Toy_private_isEqual(Toy_Value left, Toy_Value right) {
return 0;
}
//hash utils
static unsigned int hashCString(const char* string) {
unsigned int hash = 2166136261u;
for (unsigned int i = 0; string[i]; i++) {
hash *= string[i];
hash ^= 16777619;
}
return hash;
}
//hash util
static unsigned int hashUInt(unsigned int x) {
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
@@ -89,7 +78,6 @@ static unsigned int hashUInt(unsigned int x) {
return x;
}
unsigned int Toy_hashValue(Toy_Value value) {
switch(value.type) {
case TOY_VALUE_NULL:
@@ -104,27 +92,8 @@ unsigned int Toy_hashValue(Toy_Value value) {
case TOY_VALUE_FLOAT:
return hashUInt( *((int*)(&TOY_VALUE_AS_FLOAT(value))) );
case TOY_VALUE_STRING: {
Toy_String* str = TOY_VALUE_AS_STRING(value);
if (str->cachedHash != 0) {
return str->cachedHash;
}
else if (str->type == TOY_STRING_NODE) {
//TODO: I wonder if it would be possible to discretely swap the composite node string with a new leaf string here? Would that speed up other parts of the code by not having to walk the tree in future?
char* buffer = Toy_getStringRawBuffer(str);
str->cachedHash = hashCString(buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
str->cachedHash = hashCString(str->as.leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
str->cachedHash = hashCString(str->as.name.data);
}
return str->cachedHash;
}
case TOY_VALUE_STRING:
return Toy_hashString(TOY_VALUE_AS_STRING(value));
case TOY_VALUE_ARRAY:
case TOY_VALUE_DICTIONARY: