mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
To help with storing strings within tables, I've replaced the unused '_padding' member of 'Toy_String' with 'cachedHash', which is set to zero on string allocation. The hash of a string isn't generated and stored until it's actually needed, as the rope pattern means not every string needs a hash - hopefully this will save unnecessarily wasted time. When a hash of a string is needed, the hashing function first checks to see if that string already has one, and if so, returns it. Again, less time wasted. When generating a new string hash, the hashing function takes the string's type into account, as node-based strings first need their contents assembled into a simple char buffer. Other changes include: * Changed 'TOY_VALUE_TO_*' to 'TOY_VALUE_FROM_*' * Changed 'TOY_VALUE_IS_EQUAL' to 'TOY_VALUES_ARE_EQUAL' * Added a missing '#pragma once' to 'toy_print.h'
139 lines
3.4 KiB
C
139 lines
3.4 KiB
C
#include "toy_value.h"
|
|
#include "toy_console_colors.h"
|
|
|
|
#include "toy_print.h"
|
|
#include "toy_string.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
bool Toy_private_isTruthy(Toy_Value value) {
|
|
//null is an error
|
|
if (TOY_VALUE_IS_NULL(value)) {
|
|
Toy_error(TOY_CC_ERROR "ERROR: 'null' is neither true nor false\n" TOY_CC_RESET);
|
|
}
|
|
|
|
//only 'false' is falsy
|
|
if (TOY_VALUE_IS_BOOLEAN(value)) {
|
|
return TOY_VALUE_AS_BOOLEAN(value);
|
|
}
|
|
|
|
//anything else is truthy
|
|
return true;
|
|
}
|
|
|
|
bool Toy_private_isEqual(Toy_Value left, Toy_Value right) {
|
|
//temp check
|
|
if (right.type > TOY_VALUE_STRING) {
|
|
Toy_error(TOY_CC_ERROR "ERROR: Unknown types in value equality comparison\n" TOY_CC_RESET); //TODO: varargs
|
|
}
|
|
|
|
switch(left.type) {
|
|
case TOY_VALUE_NULL:
|
|
return TOY_VALUE_IS_NULL(right);
|
|
|
|
case TOY_VALUE_BOOLEAN:
|
|
return TOY_VALUE_IS_BOOLEAN(right) && TOY_VALUE_AS_BOOLEAN(left) == TOY_VALUE_AS_BOOLEAN(right);
|
|
|
|
case TOY_VALUE_INTEGER:
|
|
if (TOY_VALUE_AS_INTEGER(right)) {
|
|
return TOY_VALUE_AS_INTEGER(left) == TOY_VALUE_AS_INTEGER(right);
|
|
}
|
|
if (TOY_VALUE_AS_FLOAT(right)) {
|
|
return TOY_VALUE_AS_INTEGER(left) == TOY_VALUE_AS_FLOAT(right);
|
|
}
|
|
return false;
|
|
|
|
case TOY_VALUE_FLOAT:
|
|
if (TOY_VALUE_AS_FLOAT(right)) {
|
|
return TOY_VALUE_AS_FLOAT(left) == TOY_VALUE_AS_FLOAT(right);
|
|
}
|
|
if (TOY_VALUE_AS_INTEGER(right)) {
|
|
return TOY_VALUE_AS_FLOAT(left) == TOY_VALUE_AS_INTEGER(right);
|
|
}
|
|
return false;
|
|
|
|
case TOY_VALUE_STRING:
|
|
if (TOY_VALUE_IS_STRING(right)) {
|
|
return Toy_compareStrings(TOY_VALUE_AS_STRING(left), TOY_VALUE_AS_STRING(right)) == 0;
|
|
}
|
|
return false;
|
|
|
|
case TOY_VALUE_ARRAY:
|
|
case TOY_VALUE_DICTIONARY:
|
|
case TOY_VALUE_FUNCTION:
|
|
case TOY_VALUE_OPAQUE:
|
|
default:
|
|
Toy_error(TOY_CC_ERROR "ERROR: Unknown types in value equality comparison\n" TOY_CC_RESET); //TODO: varargs
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static unsigned int hashUInt(unsigned int x) {
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
x = (x >> 16) ^ x;
|
|
return x;
|
|
}
|
|
|
|
|
|
unsigned int Toy_hashValue(Toy_Value value) {
|
|
switch(value.type) {
|
|
case TOY_VALUE_NULL:
|
|
return 0;
|
|
|
|
case TOY_VALUE_BOOLEAN:
|
|
return TOY_VALUE_AS_BOOLEAN(value) ? 1 : 0;
|
|
|
|
case TOY_VALUE_INTEGER:
|
|
return hashUInt(TOY_VALUE_AS_INTEGER(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 it 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?
|
|
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_ARRAY:
|
|
case TOY_VALUE_DICTIONARY:
|
|
case TOY_VALUE_FUNCTION:
|
|
case TOY_VALUE_OPAQUE:
|
|
default:
|
|
Toy_error(TOY_CC_ERROR "ERROR: Can't hash an unknown type\n" TOY_CC_RESET);
|
|
}
|
|
|
|
return 0;
|
|
}
|