Reworked Toy_String as a union, enabled -Wpedantic

Toy now fits into the C spec.

Fixed #158

Addendum: MacOS test caught an error:

error: a function declaration without a prototype is deprecated in all versions of C

That took 3 attempts to fix correctly.

Addendum: 'No new line at the end of file' are you shitting me?
This commit is contained in:
2024-12-15 15:34:57 +11:00
parent 93fce94e9c
commit a28053d4e9
41 changed files with 322 additions and 309 deletions

View File

@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpedantic -Wformat=2
LIBS+=-lm
LDFLAGS+=

View File

@@ -3,6 +3,6 @@
//defined separately, as compilation can take several seconds, invalidating the comparisons of the given macros
static const char* build = __DATE__ " " __TIME__ ", incomplete Toy v2.x";
const char* Toy_private_version_build() {
const char* Toy_private_version_build(void) {
return build;
}

View File

@@ -54,5 +54,5 @@
//defined as a function, for technical reasons
#define TOY_VERSION_BUILD Toy_private_version_build()
TOY_API const char* Toy_private_version_build();
TOY_API const char* Toy_private_version_build(void);

View File

@@ -42,14 +42,14 @@ void Toy_setAssertFailureCallback(Toy_callbackType cb) {
assertCallback = cb;
}
void Toy_resetPrintCallback() {
void Toy_resetPrintCallback(void) {
printCallback = outDefault;
}
void Toy_resetErrorCallback() {
void Toy_resetErrorCallback(void) {
errorCallback = errDefault;
}
void Toy_resetAssertFailureCallback() {
void Toy_resetAssertFailureCallback(void) {
assertCallback = assertDefault;
}

View File

@@ -13,7 +13,7 @@ TOY_API void Toy_setPrintCallback(Toy_callbackType cb);
TOY_API void Toy_setErrorCallback(Toy_callbackType cb);
TOY_API void Toy_setAssertFailureCallback(Toy_callbackType cb);
TOY_API void Toy_resetPrintCallback();
TOY_API void Toy_resetErrorCallback();
TOY_API void Toy_resetAssertFailureCallback();
TOY_API void Toy_resetPrintCallback(void);
TOY_API void Toy_resetErrorCallback(void);
TOY_API void Toy_resetAssertFailureCallback(void);

View File

@@ -71,7 +71,7 @@ static void emitToJumpTable(Toy_Routine** rt, unsigned int startAddr) {
static unsigned int emitString(Toy_Routine** rt, Toy_String* str) {
//4-byte alignment
unsigned int length = str->length + 1;
unsigned int length = str->info.length + 1;
if (length % 4 != 0) {
length += 4 - (length % 4); //ceil
}
@@ -82,16 +82,16 @@ static unsigned int emitString(Toy_Routine** rt, Toy_String* str) {
//move the string into the data section
expand((&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length);
if (str->type == TOY_STRING_NODE) {
if (str->info.type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
memcpy((*rt)->data + (*rt)->dataCount, buffer, str->length + 1);
memcpy((*rt)->data + (*rt)->dataCount, buffer, str->info.length + 1);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
memcpy((*rt)->data + (*rt)->dataCount, str->as.leaf.data, str->length + 1);
else if (str->info.type == TOY_STRING_LEAF) {
memcpy((*rt)->data + (*rt)->dataCount, str->leaf.data, str->info.length + 1);
}
else if (str->type == TOY_STRING_NAME) {
memcpy((*rt)->data + (*rt)->dataCount, str->as.name.data, str->length + 1);
else if (str->info.type == TOY_STRING_NAME) {
memcpy((*rt)->data + (*rt)->dataCount, str->name.data, str->info.length + 1);
}
(*rt)->dataCount += length;
@@ -443,9 +443,9 @@ static unsigned int writeInstructionVarDeclare(Toy_Routine** rt, Toy_AstVarDecla
//delcare with the given name string
EMIT_BYTE(rt, code, TOY_OPCODE_DECLARE);
EMIT_BYTE(rt, code, Toy_getNameStringType(ast.name));
EMIT_BYTE(rt, code, ast.name->length); //quick optimisation to skip a 'strlen()' call
EMIT_BYTE(rt, code, Toy_getNameStringConstant(ast.name) ? 1 : 0); //check for constness
EMIT_BYTE(rt, code, Toy_getNameStringVarType(ast.name));
EMIT_BYTE(rt, code, ast.name->info.length); //quick optimisation to skip a 'strlen()' call
EMIT_BYTE(rt, code, Toy_getNameStringVarConstant(ast.name) ? 1 : 0); //check for constness
emitString(rt, ast.name);
@@ -472,7 +472,7 @@ static unsigned int writeInstructionAssign(Toy_Routine** rt, Toy_AstVarAssign as
}
//target is a name string
if (ast.target->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.target->value.value) && TOY_VALUE_AS_STRING(ast.target->value.value)->type == TOY_STRING_NAME) {
if (ast.target->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.target->value.value) && TOY_VALUE_AS_STRING(ast.target->value.value)->info.type == TOY_STRING_NAME) {
//name string
Toy_String* target = TOY_VALUE_AS_STRING(ast.target->value.value);
@@ -480,7 +480,7 @@ static unsigned int writeInstructionAssign(Toy_Routine** rt, Toy_AstVarAssign as
EMIT_BYTE(rt, code, TOY_OPCODE_READ);
EMIT_BYTE(rt, code, TOY_VALUE_STRING);
EMIT_BYTE(rt, code, TOY_STRING_NAME);
EMIT_BYTE(rt, code, target->length); //store the length (max 255)
EMIT_BYTE(rt, code, target->info.length); //store the length (max 255)
emitString(rt, target);
}
@@ -590,7 +590,7 @@ static unsigned int writeInstructionAssign(Toy_Routine** rt, Toy_AstVarAssign as
}
static unsigned int writeInstructionAccess(Toy_Routine** rt, Toy_AstVarAccess ast) {
if (!(ast.child->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.child->value.value) && TOY_VALUE_AS_STRING(ast.child->value.value)->type == TOY_STRING_NAME)) {
if (!(ast.child->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.child->value.value) && TOY_VALUE_AS_STRING(ast.child->value.value)->info.type == TOY_STRING_NAME)) {
fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Found a non-name-string in a value node when trying to write access\n" TOY_CC_RESET);
exit(-1);
}
@@ -601,7 +601,7 @@ static unsigned int writeInstructionAccess(Toy_Routine** rt, Toy_AstVarAccess as
EMIT_BYTE(rt, code, TOY_OPCODE_READ);
EMIT_BYTE(rt, code, TOY_VALUE_STRING);
EMIT_BYTE(rt, code, TOY_STRING_NAME);
EMIT_BYTE(rt, code, name->length); //store the length (max 255)
EMIT_BYTE(rt, code, name->info.length); //store the length (max 255)
emitString(rt, name);

View File

@@ -92,7 +92,7 @@ Toy_Scope* Toy_deepCopyScope(Toy_Bucket** bucketHandle, Toy_Scope* scope) {
}
void Toy_declareScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}
@@ -100,25 +100,25 @@ void Toy_declareScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
Toy_TableEntry* entryPtr = lookupScope(scope, key, Toy_hashString(key), false);
if (entryPtr != NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Can't redefine a variable: %s", key->as.name.data);
char buffer[key->info.length + 256];
sprintf(buffer, "Can't redefine a variable: %s", key->name.data);
Toy_error(buffer);
return;
}
//type check
Toy_ValueType kt = Toy_getNameStringType(key);
Toy_ValueType kt = Toy_getNameStringVarType(key);
if (kt != TOY_VALUE_ANY && value.type != TOY_VALUE_NULL && kt != value.type && value.type != TOY_VALUE_REFERENCE) {
char buffer[key->length + 256];
sprintf(buffer, "Incorrect value type in declaration of '%s' (expected %s, got %s)", key->as.name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
char buffer[key->info.length + 256];
sprintf(buffer, "Incorrect value type in declaration of '%s' (expected %s, got %s)", key->name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
Toy_error(buffer);
return;
}
//constness check
if (Toy_getNameStringConstant(key) && value.type == TOY_VALUE_NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Can't declare %s as const with value 'null'", key->as.name.data);
if (Toy_getNameStringVarConstant(key) && value.type == TOY_VALUE_NULL) {
char buffer[key->info.length + 256];
sprintf(buffer, "Can't declare %s as const with value 'null'", key->name.data);
Toy_error(buffer);
return;
}
@@ -129,7 +129,7 @@ void Toy_declareScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
//TODO: check for clearign old values
void Toy_assignScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}
@@ -137,25 +137,25 @@ void Toy_assignScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
Toy_TableEntry* entryPtr = lookupScope(scope, key, Toy_hashString(key), true);
if (entryPtr == NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->as.name.data);
char buffer[key->info.length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->name.data);
Toy_error(buffer);
return;
}
//type check
Toy_ValueType kt = Toy_getNameStringType( TOY_VALUE_AS_STRING(entryPtr->key) );
Toy_ValueType kt = Toy_getNameStringVarType( TOY_VALUE_AS_STRING(entryPtr->key) );
if (kt != TOY_VALUE_ANY && value.type != TOY_VALUE_NULL && kt != value.type && value.type != TOY_VALUE_REFERENCE) {
char buffer[key->length + 256];
sprintf(buffer, "Incorrect value type in assignment of '%s' (expected %s, got %s)", key->as.name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
char buffer[key->info.length + 256];
sprintf(buffer, "Incorrect value type in assignment of '%s' (expected %s, got %s)", key->name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
Toy_error(buffer);
return;
}
//constness check
if (Toy_getNameStringConstant( TOY_VALUE_AS_STRING(entryPtr->key) )) {
char buffer[key->length + 256];
sprintf(buffer, "Can't assign to const %s", key->as.name.data);
if (Toy_getNameStringVarConstant( TOY_VALUE_AS_STRING(entryPtr->key) )) {
char buffer[key->info.length + 256];
sprintf(buffer, "Can't assign to const %s", key->name.data);
Toy_error(buffer);
return;
}
@@ -164,7 +164,7 @@ void Toy_assignScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
}
Toy_Value* Toy_accessScopeAsPointer(Toy_Scope* scope, Toy_String* key) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}
@@ -172,8 +172,8 @@ Toy_Value* Toy_accessScopeAsPointer(Toy_Scope* scope, Toy_String* key) {
Toy_TableEntry* entryPtr = lookupScope(scope, key, Toy_hashString(key), true);
if (entryPtr == NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->as.name.data);
char buffer[key->info.length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->name.data);
Toy_error(buffer);
NULL;
}
@@ -182,7 +182,7 @@ Toy_Value* Toy_accessScopeAsPointer(Toy_Scope* scope, Toy_String* key) {
}
bool Toy_isDeclaredScope(Toy_Scope* scope, Toy_String* key) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}

View File

@@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
Toy_Stack* Toy_allocateStack() {
Toy_Stack* Toy_allocateStack(void) {
Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack));
if (stack == NULL) {

View File

@@ -9,7 +9,7 @@ typedef struct Toy_Stack { //32 | 64 BITNESS
Toy_Value data[]; //- | -
} Toy_Stack; //8 | 8
TOY_API Toy_Stack* Toy_allocateStack();
TOY_API Toy_Stack* Toy_allocateStack(void);
TOY_API void Toy_freeStack(Toy_Stack* stack);
TOY_API void Toy_pushStack(Toy_Stack** stackHandle, Toy_Value value);

View File

@@ -10,29 +10,29 @@
static void deepCopyUtil(char* dest, Toy_String* str) {
//sometimes, "clever" can be a bad thing...
if (str->type == TOY_STRING_NODE) {
deepCopyUtil(dest, str->as.node.left);
deepCopyUtil(dest + str->as.node.left->length, str->as.node.right);
if (str->info.type == TOY_STRING_NODE) {
deepCopyUtil(dest, str->node.left);
deepCopyUtil(dest + str->node.left->info.length, str->node.right);
}
else {
memcpy(dest, str->as.leaf.data, str->length);
memcpy(dest, str->leaf.data, str->info.length);
}
}
static void incrementRefCount(Toy_String* str) {
str->refCount++;
if (str->type == TOY_STRING_NODE) {
incrementRefCount(str->as.node.left);
incrementRefCount(str->as.node.right);
str->info.refCount++;
if (str->info.type == TOY_STRING_NODE) {
incrementRefCount(str->node.left);
incrementRefCount(str->node.right);
}
}
static void decrementRefCount(Toy_String* str) {
str->refCount--;
if (str->type == TOY_STRING_NODE) {
decrementRefCount(str->as.node.left);
decrementRefCount(str->as.node.right);
str->info.refCount--;
if (str->info.type == TOY_STRING_NODE) {
decrementRefCount(str->node.left);
decrementRefCount(str->node.right);
}
}
@@ -55,12 +55,12 @@ static Toy_String* partitionStringLength(Toy_Bucket** bucketHandle, const char*
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1);
ret->type = TOY_STRING_LEAF;
ret->length = length;
ret->refCount = 1;
ret->cachedHash = 0; //don't calc until needed
memcpy(ret->as.leaf.data, cstring, length + 1);
ret->as.leaf.data[length] = '\0';
ret->info.type = TOY_STRING_LEAF;
ret->info.length = length;
ret->info.refCount = 1;
ret->info.cachedHash = 0; //don't calc until needed
memcpy(ret->leaf.data, cstring, length + 1);
ret->leaf.data[length] = '\0';
return ret;
}
@@ -91,34 +91,34 @@ Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstrin
return result;
}
Toy_String* Toy_createNameStringLength(Toy_Bucket** bucketHandle, const char* cname, unsigned int length, Toy_ValueType type, bool constant) {
Toy_String* Toy_createNameStringLength(Toy_Bucket** bucketHandle, const char* cname, unsigned int length, Toy_ValueType varType, bool constant) {
//name strings can't be broken up
if (sizeof(Toy_String) + length + 1 > (*bucketHandle)->capacity) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't partition enough space for a name string, requested %d length (%d total) but buckets have a capacity of %d\n" TOY_CC_RESET, (int)length, (int)(sizeof(Toy_String) + length + 1), (int)((*bucketHandle)->capacity));
exit(-1);
}
if (type == TOY_VALUE_NULL) {
if (varType == TOY_VALUE_NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't declare a name string with type 'null'\n" TOY_CC_RESET);
exit(-1);
}
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1);
ret->type = TOY_STRING_NAME;
ret->length = length;
ret->refCount = 1;
ret->cachedHash = 0; //don't calc until needed
memcpy(ret->as.name.data, cname, length + 1);
ret->as.name.data[length] = '\0';
ret->as.name.type = type;
ret->as.name.constant = constant;
ret->info.type = TOY_STRING_NAME;
ret->info.length = length;
ret->info.refCount = 1;
ret->info.cachedHash = 0; //don't calc until needed
memcpy(ret->name.data, cname, length + 1);
ret->name.data[length] = '\0';
ret->name.varType = varType;
ret->name.varConstant = constant;
return ret;
}
Toy_String* Toy_copyString(Toy_String* str) {
if (str->refCount == 0) {
if (str->info.refCount == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't copy a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
@@ -127,60 +127,60 @@ Toy_String* Toy_copyString(Toy_String* str) {
}
Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str) {
if (str->refCount == 0) {
if (str->info.refCount == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't deep copy a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
//handle deep copies of strings that are too long for the bucket capacity NOTE: slow, could replace this at some point
if (sizeof(Toy_String) + str->length + 1 > (*bucketHandle)->capacity) {
if (sizeof(Toy_String) + str->info.length + 1 > (*bucketHandle)->capacity) {
char* buffer = Toy_getStringRawBuffer(str);
Toy_String* result = Toy_createStringLength(bucketHandle, buffer, str->length); //handles the fragmenting
Toy_String* result = Toy_createStringLength(bucketHandle, buffer, str->info.length); //handles the fragmenting
free(buffer);
return result;
}
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + str->length + 1);
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + str->info.length + 1);
if (str->type == TOY_STRING_NODE || str->type == TOY_STRING_LEAF) {
ret->type = TOY_STRING_LEAF;
ret->length = str->length;
ret->refCount = 1;
ret->cachedHash = str->cachedHash;
deepCopyUtil(ret->as.leaf.data, str); //copy each leaf into the buffer
ret->as.leaf.data[ret->length] = '\0';
if (str->info.type == TOY_STRING_NODE || str->info.type == TOY_STRING_LEAF) {
ret->info.type = TOY_STRING_LEAF;
ret->info.length = str->info.length;
ret->info.refCount = 1;
ret->info.cachedHash = str->info.cachedHash;
deepCopyUtil(ret->leaf.data, str); //copy each leaf into the buffer
ret->leaf.data[ret->info.length] = '\0';
}
else {
ret->type = TOY_STRING_NAME;
ret->length = str->length;
ret->refCount = 1;
ret->cachedHash = str->cachedHash;
memcpy(ret->as.name.data, str->as.name.data, str->length + 1);
ret->as.name.data[ret->length] = '\0';
ret->info.type = TOY_STRING_NAME;
ret->info.length = str->info.length;
ret->info.refCount = 1;
ret->info.cachedHash = str->info.cachedHash;
memcpy(ret->name.data, str->name.data, str->info.length + 1);
ret->name.data[ret->info.length] = '\0';
}
return ret;
}
Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_String* right) {
if (left->type == TOY_STRING_NAME || right->type == TOY_STRING_NAME) {
if (left->info.type == TOY_STRING_NAME || right->info.type == TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a name string\n" TOY_CC_RESET);
exit(-1);
}
if (left->refCount == 0 || right->refCount == 0) {
if (left->info.refCount == 0 || right->info.refCount == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String));
ret->type = TOY_STRING_NODE;
ret->length = left->length + right->length;
ret->refCount = 1;
ret->cachedHash = 0; //don't calc until needed
ret->as.node.left = left;
ret->as.node.right = right;
ret->info.type = TOY_STRING_NODE;
ret->info.length = left->info.length + right->info.length;
ret->info.refCount = 1;
ret->info.cachedHash = 0; //don't calc until needed
ret->node.left = left;
ret->node.right = right;
incrementRefCount(left);
incrementRefCount(right);
@@ -193,52 +193,52 @@ void Toy_freeString(Toy_String* str) {
}
unsigned int Toy_getStringLength(Toy_String* str) {
return str->length;
return str->info.length;
}
unsigned int Toy_getStringRefCount(Toy_String* str) {
return str->refCount;
return str->info.refCount;
}
Toy_ValueType Toy_getNameStringType(Toy_String* str) {
if (str->type != TOY_STRING_NAME) {
Toy_ValueType Toy_getNameStringVarType(Toy_String* str) {
if (str->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get the variable type of a non-name string\n" TOY_CC_RESET);
exit(-1);
}
return str->as.name.type;
return str->name.varType;
}
Toy_ValueType Toy_getNameStringConstant(Toy_String* str) {
if (str->type != TOY_STRING_NAME) {
Toy_ValueType Toy_getNameStringVarConstant(Toy_String* str) {
if (str->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get the variable constness of a non-name string\n" TOY_CC_RESET);
exit(-1);
}
return str->as.name.constant;
return str->name.varConstant;
}
char* Toy_getStringRawBuffer(Toy_String* str) {
if (str->type == TOY_STRING_NAME) {
if (str->info.type == TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a name string\n" TOY_CC_RESET);
exit(-1);
}
if (str->refCount == 0) {
if (str->info.refCount == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
//BUGFIX: Make sure it's aligned, and there's space for the null
unsigned int len = (str->length + 3) & ~3;
if (len == str->length) {
unsigned int len = (str->info.length + 3) & ~3;
if (len == str->info.length) { //nulls aren't counted
len += 4;
}
char* buffer = malloc(len);
deepCopyUtil(buffer, str);
buffer[str->length] = '\0';
buffer[str->info.length] = '\0';
return buffer;
}
@@ -253,21 +253,21 @@ static int deepCompareUtil(Toy_String* left, Toy_String* right, const char** lef
}
//BUGFIX: if we're not currently iterating through the left leaf (and leftHead is not null), skip out
if (left->type == TOY_STRING_LEAF && (*leftHead) != NULL && (**leftHead) != '\0' && ((*leftHead) < left->as.leaf.data || (*leftHead) > (left->as.leaf.data + left->length)) ) {
if (left->info.type == TOY_STRING_LEAF && (*leftHead) != NULL && (**leftHead) != '\0' && ((*leftHead) < left->leaf.data || (*leftHead) > (left->leaf.data + left->info.length)) ) {
return result;
}
//BUGFIX: if we're not currently iterating through the right leaf (and rightHead is not null), skip out
if (right->type == TOY_STRING_LEAF && (*rightHead) != NULL && (**rightHead) != '\0' && ((*rightHead) < right->as.leaf.data || (*rightHead) > (right->as.leaf.data + right->length)) ) {
if (right->info.type == TOY_STRING_LEAF && (*rightHead) != NULL && (**rightHead) != '\0' && ((*rightHead) < right->leaf.data || (*rightHead) > (right->leaf.data + right->info.length)) ) {
return result;
}
//dig into left
if (left->type == TOY_STRING_NODE) {
if ((result = deepCompareUtil(left->as.node.left, right, leftHead, rightHead)) != 0) {
if (left->info.type == TOY_STRING_NODE) {
if ((result = deepCompareUtil(left->node.left, right, leftHead, rightHead)) != 0) {
return result;
}
if ((result = deepCompareUtil(left->as.node.right, right, leftHead, rightHead)) != 0) {
if ((result = deepCompareUtil(left->node.right, right, leftHead, rightHead)) != 0) {
return result;
}
@@ -276,12 +276,12 @@ static int deepCompareUtil(Toy_String* left, Toy_String* right, const char** lef
}
//dig into right
if (right->type == TOY_STRING_NODE) {
if ((result = deepCompareUtil(left, right->as.node.left, leftHead, rightHead)) != 0) {
if (right->info.type == TOY_STRING_NODE) {
if ((result = deepCompareUtil(left, right->node.left, leftHead, rightHead)) != 0) {
return result;
}
if ((result = deepCompareUtil(left, right->as.node.right, leftHead, rightHead)) != 0) {
if ((result = deepCompareUtil(left, right->node.right, leftHead, rightHead)) != 0) {
return result;
}
@@ -290,14 +290,14 @@ static int deepCompareUtil(Toy_String* left, Toy_String* right, const char** lef
}
//keep comparing the leaves
if (left->type == TOY_STRING_LEAF && right->type == TOY_STRING_LEAF) {
if (left->info.type == TOY_STRING_LEAF && right->info.type == TOY_STRING_LEAF) {
//initial head states can be null, or null characters
if ((*leftHead) == NULL || (**leftHead) == '\0') {
(*leftHead) = left->as.leaf.data;
(*leftHead) = left->leaf.data;
}
if ((*rightHead) == NULL || (**rightHead) == '\0') {
(*rightHead) = right->as.leaf.data;
(*rightHead) = right->leaf.data;
}
//compare and increment
@@ -318,17 +318,17 @@ static int deepCompareUtil(Toy_String* left, Toy_String* right, const char** lef
int Toy_compareStrings(Toy_String* left, Toy_String* right) {
//BUGFIX: since deepCompareUtil() can't handle strings of length zero, insert a check here
if (left->length == 0 || right->length == 0) {
return left->length - right->length;
if (left->info.length == 0 || right->info.length == 0) {
return left->info.length - right->info.length;
}
if (left->type == TOY_STRING_NAME || right->type == TOY_STRING_NAME) {
if (left->type != right->type) {
if (left->info.type == TOY_STRING_NAME || right->info.type == TOY_STRING_NAME) {
if (left->info.type != right->info.type) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't compare a name string to a non-name string\n" TOY_CC_RESET);
exit(-1);
}
return strncmp(left->as.name.data, right->as.name.data, left->length);
return strncmp(left->name.data, right->name.data, left->info.length);
}
//util pointers
@@ -339,21 +339,21 @@ int Toy_compareStrings(Toy_String* left, Toy_String* right) {
}
unsigned int Toy_hashString(Toy_String* str) {
if (str->cachedHash != 0) {
return str->cachedHash;
if (str->info.cachedHash != 0) {
return str->info.cachedHash;
}
else if (str->type == TOY_STRING_NODE) {
else if (str->info.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? - needs to be benchmarked
char* buffer = Toy_getStringRawBuffer(str);
str->cachedHash = hashCString(buffer);
str->info.cachedHash = hashCString(buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
str->cachedHash = hashCString(str->as.leaf.data);
else if (str->info.type == TOY_STRING_LEAF) {
str->info.cachedHash = hashCString(str->leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
str->cachedHash = hashCString(str->as.name.data);
else if (str->info.type == TOY_STRING_NAME) {
str->info.cachedHash = hashCString(str->name.data);
}
return str->cachedHash;
return str->info.cachedHash;
}

View File

@@ -5,41 +5,53 @@
#include "toy_bucket.h"
#include "toy_value.h"
//rope pattern
typedef struct Toy_String { //32 | 64 BITNESS
enum Toy_StringType {
TOY_STRING_NODE,
TOY_STRING_LEAF,
TOY_STRING_NAME,
} type; //4 | 4
//forward declare
union Toy_String_t;
unsigned int length; //4 | 4
unsigned int refCount; //4 | 4
unsigned int cachedHash; //4 | 4
//rope pattern, conforming to the C spec - see #158
typedef enum Toy_StringType {
TOY_STRING_NODE,
TOY_STRING_LEAF,
TOY_STRING_NAME,
} Toy_StringType;
union {
struct {
struct Toy_String* left; //4 | 8
struct Toy_String* right; //4 | 8
} node; //8 | 16
typedef struct Toy_StringInfo {
Toy_StringType type;
unsigned int length;
unsigned int refCount;
unsigned int cachedHash;
} Toy_StringInfo;
struct {
int _dummy; //4 | 4
char data[]; //- | -
} leaf; //4 | 4
typedef struct Toy_StringNode {
Toy_StringInfo _padding;
union Toy_String_t* left;
union Toy_String_t* right;
} Toy_StringNode;
struct {
Toy_ValueType type; //4 | 4
bool constant; //1 | 1
char data[]; //- | -
} name; //8 | 8
} as; //8 | 16
} Toy_String; //24 | 32
typedef struct Toy_StringLeaf {
Toy_StringInfo _padding;
char data[];
} Toy_StringLeaf;
typedef struct Toy_StringName {
Toy_StringInfo _padding;
Toy_ValueType varType;
bool varConstant;
char data[];
} Toy_StringName;
typedef union Toy_String_t {
Toy_StringInfo info;
Toy_StringNode node;
Toy_StringLeaf leaf;
Toy_StringName name;
} Toy_String;
//
TOY_API Toy_String* Toy_createString(Toy_Bucket** bucketHandle, const char* cstring);
TOY_API Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length);
TOY_API Toy_String* Toy_createNameStringLength(Toy_Bucket** bucketHandle, const char* cname, unsigned int length, Toy_ValueType type, bool constant); //for variable names
TOY_API Toy_String* Toy_createNameStringLength(Toy_Bucket** bucketHandle, const char* cname, unsigned int length, Toy_ValueType varType, bool constant); //for variable names
TOY_API Toy_String* Toy_copyString(Toy_String* str);
TOY_API Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str);
@@ -50,8 +62,8 @@ TOY_API void Toy_freeString(Toy_String* str);
TOY_API unsigned int Toy_getStringLength(Toy_String* str);
TOY_API unsigned int Toy_getStringRefCount(Toy_String* str);
TOY_API Toy_ValueType Toy_getNameStringType(Toy_String* str);
TOY_API Toy_ValueType Toy_getNameStringConstant(Toy_String* str);
TOY_API Toy_ValueType Toy_getNameStringVarType(Toy_String* str);
TOY_API Toy_ValueType Toy_getNameStringVarConstant(Toy_String* str);
TOY_API char* Toy_getStringRawBuffer(Toy_String* str); //allocates the buffer on the heap, needs to be freed

View File

@@ -85,7 +85,7 @@ Toy_Table* Toy_private_adjustTableCapacity(Toy_Table* oldTable, unsigned int new
return newTable;
}
Toy_Table* Toy_allocateTable() {
Toy_Table* Toy_allocateTable(void) {
return Toy_private_adjustTableCapacity(NULL, TOY_TABLE_INITIAL_CAPACITY);
}

View File

@@ -19,7 +19,7 @@ typedef struct Toy_Table { //32 | 64 BITNESS
Toy_TableEntry data[]; //- | -
} Toy_Table; //16 | 16
TOY_API Toy_Table* Toy_allocateTable();
TOY_API Toy_Table* Toy_allocateTable(void);
TOY_API void Toy_freeTable(Toy_Table* table);
TOY_API void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value);
TOY_API Toy_Value Toy_lookupTable(Toy_Table** tableHandle, Toy_Value key);

View File

@@ -451,4 +451,5 @@ const char* Toy_private_getValueTypeAsCString(Toy_ValueType type) {
}
return NULL;
}
}

View File

@@ -5,7 +5,7 @@
//forward declarations
struct Toy_Bucket;
struct Toy_String;
union Toy_String_t;
struct Toy_Array;
typedef enum Toy_ValueType {
@@ -32,7 +32,7 @@ typedef struct Toy_Value { //32 | 64 BITNESS
bool boolean; //1 | 1
int integer; //4 | 4
float number; //4 | 4
struct Toy_String* string; //4 | 8
union Toy_String_t* string; //4 | 8
struct Toy_Array* array; //4 | 8
//TODO: more types go here
//TODO: consider 'stack' as a possible addition
@@ -84,7 +84,7 @@ TOY_API bool Toy_checkValuesAreComparable(Toy_Value left, Toy_Value right);
TOY_API int Toy_compareValues(Toy_Value left, Toy_Value right);
//convert the value to a string - values that *are* strings are simply copied
TOY_API struct Toy_String* Toy_stringifyValue(struct Toy_Bucket** bucketHandle, Toy_Value value);
TOY_API union Toy_String_t* Toy_stringifyValue(struct Toy_Bucket** bucketHandle, Toy_Value value);
//for debugging
TOY_API const char* Toy_private_getValueTypeAsCString(Toy_ValueType type);

View File

@@ -190,7 +190,7 @@ static void processAssign(Toy_VM* vm) {
Toy_Value name = Toy_popStack(&vm->stack);
//check name string type
if (!TOY_VALUE_IS_STRING(name) || TOY_VALUE_AS_STRING(name)->type != TOY_STRING_NAME) {
if (!TOY_VALUE_IS_STRING(name) || TOY_VALUE_AS_STRING(name)->info.type != TOY_STRING_NAME) {
Toy_error("Invalid assignment target");
Toy_freeValue(name);
Toy_freeValue(value);
@@ -211,7 +211,7 @@ static void processAssignCompound(Toy_VM* vm) {
Toy_Value target = Toy_popStack(&vm->stack);
//shake out variable names
if (TOY_VALUE_IS_STRING(target) && TOY_VALUE_AS_STRING(target)->type == TOY_STRING_NAME) {
if (TOY_VALUE_IS_STRING(target) && TOY_VALUE_AS_STRING(target)->info.type == TOY_STRING_NAME) {
Toy_Value* valuePtr = Toy_accessScopeAsPointer(vm->scope, TOY_VALUE_AS_STRING(target));
Toy_freeValue(target);
target = TOY_REFERENCE_FROM_POINTER(valuePtr);
@@ -258,7 +258,7 @@ static void processAccess(Toy_VM* vm) {
Toy_Value name = Toy_popStack(&vm->stack);
//check name string type
if (!TOY_VALUE_IS_STRING(name) && TOY_VALUE_AS_STRING(name)->type != TOY_STRING_NAME) {
if (!TOY_VALUE_IS_STRING(name) && TOY_VALUE_AS_STRING(name)->info.type != TOY_STRING_NAME) {
Toy_error("Invalid access target");
return;
}
@@ -671,7 +671,7 @@ static void processIndex(Toy_VM* vm) {
Toy_String* str = TOY_VALUE_AS_STRING(value);
//check indexing is within bounds
if ( (i < 0 || (unsigned int)i >= str->length) || (i+l <= 0 || (unsigned int)(i+l) > str->length)) {
if ( (i < 0 || (unsigned int)i >= str->info.length) || (i+l <= 0 || (unsigned int)(i+l) > str->info.length)) {
Toy_error("String index is out of bounds");
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
@@ -689,11 +689,11 @@ static void processIndex(Toy_VM* vm) {
Toy_String* result = NULL;
//extract cstring, based on type
if (str->type == TOY_STRING_LEAF) {
const char* cstr = str->as.leaf.data;
if (str->info.type == TOY_STRING_LEAF) {
const char* cstr = str->leaf.data;
result = Toy_createStringLength(&vm->stringBucket, cstr + i, l);
}
else if (str->type == TOY_STRING_NODE) {
else if (str->info.type == TOY_STRING_NODE) {
char* cstr = Toy_getStringRawBuffer(str);
result = Toy_createStringLength(&vm->stringBucket, cstr + i, l);
free(cstr);