mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
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:
@@ -143,10 +143,10 @@ typedef struct Toy_Thing {
|
||||
int member;
|
||||
} Toy_Thing;
|
||||
|
||||
TOY_API void Toy_useThing();
|
||||
TOY_API void Toy_useThing(void);
|
||||
|
||||
#define TOY_USE_OTHER_THING() Toy_private_useOtherThing()
|
||||
TOY_API void Toy_private_useOtherThing();
|
||||
TOY_API void Toy_private_useOtherThing(void);
|
||||
```
|
||||
|
||||
## Data Type Sizes
|
||||
|
||||
2
makefile
2
makefile
@@ -1,6 +1,6 @@
|
||||
#compiler settings reference
|
||||
#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+=
|
||||
|
||||
|
||||
@@ -379,14 +379,14 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {
|
||||
|
||||
printf("Scope %d Dump\n-------------------------\ntype\tname\tvalue\n", depth);
|
||||
for (unsigned int i = 0; i < scope->table->capacity; i++) {
|
||||
if ( (TOY_VALUE_IS_STRING(scope->table->data[i].key) && TOY_VALUE_AS_STRING(scope->table->data[i].key)->type == TOY_STRING_NAME) != true) {
|
||||
if ( (TOY_VALUE_IS_STRING(scope->table->data[i].key) && TOY_VALUE_AS_STRING(scope->table->data[i].key)->info.type == TOY_STRING_NAME) != true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Toy_Value k = scope->table->data[i].key;
|
||||
Toy_Value v = scope->table->data[i].value;
|
||||
|
||||
printf("%s\t%s\t", Toy_private_getValueTypeAsCString(v.type), TOY_VALUE_AS_STRING(k)->as.name.data);
|
||||
printf("%s\t%s\t", Toy_private_getValueTypeAsCString(v.type), TOY_VALUE_AS_STRING(k)->name.data);
|
||||
|
||||
//print value
|
||||
Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v));
|
||||
|
||||
@@ -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 -lToy
|
||||
LDFLAGS+=-Wl,-rpath,'$$ORIGIN'
|
||||
|
||||
|
||||
@@ -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+=
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -451,4 +451,5 @@ const char* Toy_private_getValueTypeAsCString(Toy_ValueType type) {
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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+=
|
||||
|
||||
|
||||
@@ -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+=
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_array() {
|
||||
int test_array(void) {
|
||||
//test allocation and free
|
||||
{
|
||||
Toy_Array* array = TOY_ARRAY_ALLOCATE();
|
||||
@@ -35,7 +35,7 @@ int test_array() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int test_sizeof_ast_64bit() {
|
||||
int test_sizeof_ast_64bit(void) {
|
||||
//NOTE: This could've covered both bitness sizes as TEST_SIZEOF(type, bit32, bit32)
|
||||
#define TEST_SIZEOF(type, size) \
|
||||
if (sizeof(type) != size) { \
|
||||
@@ -44,7 +44,7 @@ int test_sizeof_ast_64bit() {
|
||||
return -err;
|
||||
}
|
||||
|
||||
int test_sizeof_ast_32bit() {
|
||||
int test_sizeof_ast_32bit(void) {
|
||||
#define TEST_SIZEOF(type, size) \
|
||||
if (sizeof(type) != size) { \
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: sizeof(" #type ") is %d, expected %d\n" TOY_CC_RESET, (int)sizeof(type), size); \
|
||||
@@ -376,8 +376,8 @@ int test_type_emission(Toy_Bucket** bucketHandle) {
|
||||
ast->type != TOY_AST_VAR_DECLARE ||
|
||||
|
||||
ast->varDeclare.name == NULL ||
|
||||
ast->varDeclare.name->type != TOY_STRING_NAME ||
|
||||
strcmp(ast->varDeclare.name->as.name.data, "foobar") != 0 ||
|
||||
ast->varDeclare.name->info.type != TOY_STRING_NAME ||
|
||||
strcmp(ast->varDeclare.name->name.data, "foobar") != 0 ||
|
||||
|
||||
ast->varDeclare.expr != NULL)
|
||||
{
|
||||
@@ -408,10 +408,10 @@ int test_type_emission(Toy_Bucket** bucketHandle) {
|
||||
ast->varAssign.target == NULL ||
|
||||
ast->varAssign.target->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->varAssign.target->value.value) != true ||
|
||||
TOY_VALUE_AS_STRING(ast->varAssign.target->value.value)->type != TOY_STRING_NAME ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->varAssign.target->value.value)->as.name.data, "foobar") != 0 ||
|
||||
TOY_VALUE_AS_STRING(ast->varAssign.target->value.value)->info.type != TOY_STRING_NAME ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->varAssign.target->value.value)->name.data, "foobar") != 0 ||
|
||||
|
||||
TOY_VALUE_AS_STRING(ast->varAssign.target->value.value)->as.name.type != TOY_VALUE_INTEGER ||
|
||||
TOY_VALUE_AS_STRING(ast->varAssign.target->value.value)->name.varType != TOY_VALUE_INTEGER ||
|
||||
|
||||
ast->varAssign.expr->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_AS_INTEGER(ast->varAssign.expr->value.value) != 69)
|
||||
@@ -436,9 +436,9 @@ int test_type_emission(Toy_Bucket** bucketHandle) {
|
||||
ast->varAccess.child == NULL ||
|
||||
ast->varAccess.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->varAccess.child->value.value) != true ||
|
||||
TOY_VALUE_AS_STRING(ast->varAccess.child->value.value)->type != TOY_STRING_NAME ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->varAccess.child->value.value)->as.name.data, "foobar") != 0 ||
|
||||
TOY_VALUE_AS_STRING(ast->varAccess.child->value.value)->as.name.type != TOY_VALUE_INTEGER)
|
||||
TOY_VALUE_AS_STRING(ast->varAccess.child->value.value)->info.type != TOY_STRING_NAME ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->varAccess.child->value.value)->name.data, "foobar") != 0 ||
|
||||
TOY_VALUE_AS_STRING(ast->varAccess.child->value.value)->name.varType != TOY_VALUE_INTEGER)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to emit an access as 'Toy_Ast', state unknown\n" TOY_CC_RESET);
|
||||
return -1;
|
||||
@@ -491,7 +491,7 @@ int test_type_emission(Toy_Bucket** bucketHandle) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_buckets() {
|
||||
int test_buckets(void) {
|
||||
//test initializing and freeing a bucket
|
||||
{
|
||||
//init
|
||||
@@ -72,7 +72,7 @@ int test_buckets() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ int test_bytecode_from_source(Toy_Bucket** bucketHandle) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
{
|
||||
//source code sample to operate on
|
||||
char* source = "print null;";
|
||||
|
||||
@@ -114,8 +114,8 @@ int test_var_declare(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->type != TOY_AST_VAR_DECLARE ||
|
||||
|
||||
ast->block.child->varDeclare.name == NULL ||
|
||||
ast->block.child->varDeclare.name->type != TOY_STRING_NAME ||
|
||||
strcmp(ast->block.child->varDeclare.name->as.name.data, "answer") != 0 ||
|
||||
ast->block.child->varDeclare.name->info.type != TOY_STRING_NAME ||
|
||||
strcmp(ast->block.child->varDeclare.name->name.data, "answer") != 0 ||
|
||||
|
||||
ast->block.child->varDeclare.expr == NULL ||
|
||||
ast->block.child->varDeclare.expr->type != TOY_AST_VALUE ||
|
||||
@@ -141,8 +141,8 @@ int test_var_declare(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->type != TOY_AST_VAR_DECLARE ||
|
||||
|
||||
ast->block.child->varDeclare.name == NULL ||
|
||||
ast->block.child->varDeclare.name->type != TOY_STRING_NAME ||
|
||||
strcmp(ast->block.child->varDeclare.name->as.name.data, "empty") != 0 ||
|
||||
ast->block.child->varDeclare.name->info.type != TOY_STRING_NAME ||
|
||||
strcmp(ast->block.child->varDeclare.name->name.data, "empty") != 0 ||
|
||||
|
||||
ast->block.child->varDeclare.expr == NULL ||
|
||||
ast->block.child->varDeclare.expr->type != TOY_AST_VALUE ||
|
||||
@@ -173,8 +173,8 @@ int test_var_assign(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->varAssign.target == NULL || \
|
||||
ast->block.child->varAssign.target->type != TOY_AST_VALUE || \
|
||||
TOY_VALUE_IS_STRING(ast->block.child->varAssign.target->value.value) != true ||\
|
||||
TOY_VALUE_AS_STRING(ast->block.child->varAssign.target->value.value)->type != TOY_STRING_NAME ||\
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->varAssign.target->value.value)->as.name.data, ARG_NAME) != 0 || \
|
||||
TOY_VALUE_AS_STRING(ast->block.child->varAssign.target->value.value)->info.type != TOY_STRING_NAME ||\
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->varAssign.target->value.value)->name.data, ARG_NAME) != 0 || \
|
||||
ast->block.child->varAssign.expr == NULL || \
|
||||
ast->block.child->varAssign.expr->type != TOY_AST_VALUE || \
|
||||
TOY_VALUE_IS_INTEGER(ast->block.child->varAssign.expr->value.value) == false || \
|
||||
@@ -316,8 +316,8 @@ int test_values(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child == NULL ||
|
||||
ast->block.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->value.value) == false ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->value.value)->type != TOY_STRING_LEAF ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->value.value)->as.leaf.data, "Hello world!") != 0)
|
||||
TOY_VALUE_AS_STRING(ast->block.child->value.value)->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->value.value)->leaf.data, "Hello world!") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to run the parser with string value 'Hello world!'\n" TOY_CC_RESET);
|
||||
return -1;
|
||||
@@ -517,8 +517,8 @@ int test_aggregate(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->aggregate.left->varAccess.child == NULL ||
|
||||
ast->block.child->aggregate.left->varAccess.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value) != true ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->type != TOY_STRING_NAME ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->as.name.data, "name") != 0 ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->info.type != TOY_STRING_NAME ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->name.data, "name") != 0 ||
|
||||
|
||||
ast->block.child->aggregate.right->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_INTEGER(ast->block.child->aggregate.right->value.value) != true ||
|
||||
@@ -551,9 +551,9 @@ int test_aggregate(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->aggregate.left->varAccess.child == NULL ||
|
||||
ast->block.child->aggregate.left->varAccess.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value) != true ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->type != TOY_STRING_NAME ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->info.type != TOY_STRING_NAME ||
|
||||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->as.name.data, "name") != 0 ||
|
||||
strcmp(TOY_VALUE_AS_STRING(ast->block.child->aggregate.left->varAccess.child->value.value)->name.data, "name") != 0 ||
|
||||
|
||||
ast->block.child->aggregate.right->type != TOY_AST_AGGREGATE ||
|
||||
ast->block.child->aggregate.right->aggregate.flag != TOY_AST_FLAG_COLLECTION ||
|
||||
@@ -622,8 +622,8 @@ int test_keywords(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->assert.message == NULL ||
|
||||
ast->block.child->assert.message->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->assert.message->value.value) == false ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->assert.message->value.value)->type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->assert.message->value.value)->as.leaf.data, "foo", 3) != 0)
|
||||
TOY_VALUE_AS_STRING(ast->block.child->assert.message->value.value)->info.type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->assert.message->value.value)->leaf.data, "foo", 3) != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to run the parser, source: %s\n" TOY_CC_RESET, source);
|
||||
return -1;
|
||||
@@ -645,8 +645,8 @@ int test_keywords(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->print.child == NULL ||
|
||||
ast->block.child->print.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->print.child->value.value) == false ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->print.child->value.value)->type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->print.child->value.value)->as.leaf.data, "foo", 3) != 0)
|
||||
TOY_VALUE_AS_STRING(ast->block.child->print.child->value.value)->info.type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->print.child->value.value)->leaf.data, "foo", 3) != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to run the parser, source: %s\n" TOY_CC_RESET, source);
|
||||
return -1;
|
||||
@@ -678,8 +678,8 @@ int test_keywords(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->ifThenElse.thenBranch->block.child->print.child == NULL ||
|
||||
ast->block.child->ifThenElse.thenBranch->block.child->print.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value) == false ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->as.leaf.data, "foo", 3) != 0 ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->info.type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->leaf.data, "foo", 3) != 0 ||
|
||||
|
||||
ast->block.child->ifThenElse.elseBranch != NULL)
|
||||
{
|
||||
@@ -713,8 +713,8 @@ int test_keywords(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->ifThenElse.thenBranch->block.child->print.child == NULL ||
|
||||
ast->block.child->ifThenElse.thenBranch->block.child->print.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value) == false ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->as.leaf.data, "foo", 3) != 0 ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->info.type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.thenBranch->block.child->print.child->value.value)->leaf.data, "foo", 3) != 0 ||
|
||||
|
||||
ast->block.child->ifThenElse.elseBranch == NULL ||
|
||||
ast->block.child->ifThenElse.elseBranch->type != TOY_AST_BLOCK ||
|
||||
@@ -723,8 +723,8 @@ int test_keywords(Toy_Bucket** bucketHandle) {
|
||||
ast->block.child->ifThenElse.elseBranch->block.child->print.child == NULL ||
|
||||
ast->block.child->ifThenElse.elseBranch->block.child->print.child->type != TOY_AST_VALUE ||
|
||||
TOY_VALUE_IS_STRING(ast->block.child->ifThenElse.elseBranch->block.child->print.child->value.value) == false ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.elseBranch->block.child->print.child->value.value)->type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.elseBranch->block.child->print.child->value.value)->as.leaf.data, "bar", 3) != 0 ||
|
||||
TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.elseBranch->block.child->print.child->value.value)->info.type != TOY_STRING_LEAF ||
|
||||
strncmp(TOY_VALUE_AS_STRING(ast->block.child->ifThenElse.elseBranch->block.child->print.child->value.value)->leaf.data, "bar", 3) != 0 ||
|
||||
|
||||
false)
|
||||
{
|
||||
@@ -881,7 +881,7 @@ int test_precedence(Toy_Bucket** bucketHandle) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ void count(const char* msg) {
|
||||
counter++;
|
||||
}
|
||||
|
||||
int test_callbacks() {
|
||||
int test_callbacks(void) {
|
||||
//set a custom print callback, invoke it, and reset
|
||||
{
|
||||
//setup
|
||||
@@ -95,7 +95,7 @@ int test_callbacks() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -1307,7 +1307,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
|
||||
|
||||
int test_scope_allocation() {
|
||||
int test_scope_allocation(void) {
|
||||
//allocate and free a scope
|
||||
{
|
||||
//setup
|
||||
@@ -297,7 +297,7 @@ int test_scope_allocation() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_scope_elements() {
|
||||
int test_scope_elements(void) {
|
||||
//allocate, access and assign an element
|
||||
{
|
||||
//setup
|
||||
@@ -489,7 +489,7 @@ int test_scope_elements() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_stack_basics() {
|
||||
int test_stack_basics(void) {
|
||||
//allocate and free the stack
|
||||
{
|
||||
Toy_Stack* stack = Toy_allocateStack();
|
||||
@@ -82,7 +82,7 @@ int test_stack_basics() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_stack_stress() {
|
||||
int test_stack_stress(void) {
|
||||
//stress the stack's contents
|
||||
{
|
||||
Toy_Stack* stack = Toy_allocateStack();
|
||||
@@ -109,7 +109,7 @@ int test_stack_stress() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int test_sizeof_string_64bit() {
|
||||
int test_sizeof_string_64bit(void) {
|
||||
//test for the correct size
|
||||
{
|
||||
if (sizeof(Toy_String) != 32) {
|
||||
@@ -19,7 +19,7 @@ int test_sizeof_string_64bit() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_sizeof_string_32bit() {
|
||||
int test_sizeof_string_32bit(void) {
|
||||
//test for the correct size
|
||||
{
|
||||
if (sizeof(Toy_String) != 24) {
|
||||
@@ -31,7 +31,7 @@ int test_sizeof_string_32bit() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_string_allocation() {
|
||||
int test_string_allocation(void) {
|
||||
//allocate a single string from a c-string
|
||||
{
|
||||
//setup
|
||||
@@ -41,10 +41,10 @@ int test_string_allocation() {
|
||||
Toy_String* str = Toy_createString(&bucket, cstring);
|
||||
|
||||
//check
|
||||
if (str->type != TOY_STRING_LEAF ||
|
||||
str->length != 11 ||
|
||||
str->refCount != 1 ||
|
||||
strcmp(str->as.leaf.data, "Hello world") != 0)
|
||||
if (str->info.type != TOY_STRING_LEAF ||
|
||||
str->info.length != 11 ||
|
||||
str->info.refCount != 1 ||
|
||||
strcmp(str->leaf.data, "Hello world") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a Toy_String with a private bucket\n" TOY_CC_RESET);
|
||||
Toy_freeBucket(&bucket);
|
||||
@@ -88,9 +88,9 @@ int test_string_allocation() {
|
||||
|
||||
if (str != shallow ||
|
||||
str == deep ||
|
||||
shallow->refCount != 2 ||
|
||||
deep->refCount != 1 ||
|
||||
strcmp(shallow->as.leaf.data, deep->as.leaf.data) != 0)
|
||||
shallow->info.refCount != 2 ||
|
||||
deep->info.refCount != 1 ||
|
||||
strcmp(shallow->leaf.data, deep->leaf.data) != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to copy a string correctly\n" TOY_CC_RESET);
|
||||
Toy_freeBucket(&bucket);
|
||||
@@ -114,9 +114,9 @@ int test_string_allocation() {
|
||||
|
||||
if (str != shallow ||
|
||||
str == deep ||
|
||||
shallow->refCount != 2 ||
|
||||
deep->refCount != 1 ||
|
||||
strcmp(shallow->as.name.data, deep->as.name.data) != 0)
|
||||
shallow->info.refCount != 2 ||
|
||||
deep->info.refCount != 1 ||
|
||||
strcmp(shallow->name.data, deep->name.data) != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to copy a name string correctly\n" TOY_CC_RESET);
|
||||
Toy_freeBucket(&bucket);
|
||||
@@ -135,10 +135,10 @@ int test_string_allocation() {
|
||||
Toy_String* str = Toy_createString(&bucket, cstring);
|
||||
|
||||
//check
|
||||
if (str->type != TOY_STRING_LEAF ||
|
||||
str->length != 0 ||
|
||||
str->refCount != 1 ||
|
||||
strcmp(str->as.leaf.data, "") != 0)
|
||||
if (str->info.type != TOY_STRING_LEAF ||
|
||||
str->info.length != 0 ||
|
||||
str->info.refCount != 1 ||
|
||||
strcmp(str->leaf.data, "") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a Toy_String with zero length\n" TOY_CC_RESET);
|
||||
Toy_freeBucket(&bucket);
|
||||
@@ -154,7 +154,7 @@ int test_string_allocation() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_string_concatenation() {
|
||||
int test_string_concatenation(void) {
|
||||
//one big bucket o' fun
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(1024);
|
||||
|
||||
@@ -168,10 +168,10 @@ int test_string_concatenation() {
|
||||
Toy_String* result = Toy_concatStrings(&bucket, first, second);
|
||||
|
||||
//check the refcounts
|
||||
if (first->refCount != 2 ||
|
||||
second->refCount != 2 ||
|
||||
result->refCount != 1 ||
|
||||
result->length != 11)
|
||||
if (first->info.refCount != 2 ||
|
||||
second->info.refCount != 2 ||
|
||||
result->info.refCount != 1 ||
|
||||
result->info.length != 11)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected state for string refcounts after concatenation\n" TOY_CC_RESET);
|
||||
Toy_freeBucket(&bucket);
|
||||
@@ -183,10 +183,10 @@ int test_string_concatenation() {
|
||||
Toy_freeString(second);
|
||||
|
||||
//check the refcounts again
|
||||
if (first->refCount != 1 ||
|
||||
second->refCount != 1 ||
|
||||
result->refCount != 1 ||
|
||||
result->length != 11)
|
||||
if (first->info.refCount != 1 ||
|
||||
second->info.refCount != 1 ||
|
||||
result->info.refCount != 1 ||
|
||||
result->info.length != 11)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected state for string refcounts after concatenation and free\n" TOY_CC_RESET);
|
||||
Toy_freeBucket(&bucket);
|
||||
@@ -228,7 +228,7 @@ int test_string_concatenation() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_string_with_stressed_bucket() {
|
||||
int test_string_with_stressed_bucket(void) {
|
||||
//how much is that dog in the window?
|
||||
{
|
||||
//test data: 36 characters total, 44 with spaces
|
||||
@@ -256,8 +256,8 @@ int test_string_with_stressed_bucket() {
|
||||
}
|
||||
|
||||
//check
|
||||
if (ptr->refCount != 9 ||
|
||||
str->length != 36)
|
||||
if (ptr->info.refCount != 9 ||
|
||||
str->info.length != 36)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected state of the string after stress test\n" TOY_CC_RESET);
|
||||
Toy_freeBucket(&bucket);
|
||||
@@ -293,7 +293,7 @@ int test_string_with_stressed_bucket() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_string_equality() {
|
||||
int test_string_equality(void) {
|
||||
//simple string equality (no concats)
|
||||
{
|
||||
//setup
|
||||
@@ -700,7 +700,7 @@ int test_string_equality() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_string_diffs() {
|
||||
int test_string_diffs(void) {
|
||||
//simple string diffs (no concats)
|
||||
{
|
||||
//setup
|
||||
@@ -791,7 +791,7 @@ int test_string_diffs() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_string_fragmenting() {
|
||||
int test_string_fragmenting(void) {
|
||||
//allocate a long string
|
||||
{
|
||||
//setup
|
||||
@@ -803,9 +803,9 @@ int test_string_fragmenting() {
|
||||
Toy_String* str = Toy_createString(&bucket, cstring);
|
||||
|
||||
//check
|
||||
if (str->type != TOY_STRING_NODE ||
|
||||
str->length != 445 ||
|
||||
str->refCount != 1)
|
||||
if (str->info.type != TOY_STRING_NODE ||
|
||||
str->info.length != 445 ||
|
||||
str->info.refCount != 1)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to fragment a string within Toy_String\n" TOY_CC_RESET);
|
||||
Toy_freeString(str);
|
||||
@@ -821,7 +821,7 @@ int test_string_fragmenting() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_table_allocation() {
|
||||
int test_table_allocation(void) {
|
||||
//allocate and free a table
|
||||
{
|
||||
//setup
|
||||
@@ -24,7 +24,7 @@ int test_table_allocation() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_table_simple_insert_lookup_and_remove() {
|
||||
int test_table_simple_insert_lookup_and_remove(void) {
|
||||
//simple insert
|
||||
{
|
||||
//setup
|
||||
@@ -86,7 +86,7 @@ int test_table_simple_insert_lookup_and_remove() {
|
||||
TOY_VALUE_AS_INTEGER(table->data[i].value) != v || \
|
||||
table->data[i].psl != p
|
||||
|
||||
int test_table_contents_no_expansion() {
|
||||
int test_table_contents_no_expansion(void) {
|
||||
//single insert
|
||||
{
|
||||
//setup
|
||||
@@ -302,7 +302,7 @@ int test_table_contents_no_expansion() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_table_contents_with_expansions() {
|
||||
int test_table_contents_with_expansions(void) {
|
||||
//simple expansion
|
||||
{
|
||||
//setup
|
||||
@@ -569,7 +569,7 @@ int test_table_contents_with_expansions() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_table_expansions_under_stress() {
|
||||
int test_table_expansions_under_stress(void) {
|
||||
//multiple expansions, find one value
|
||||
{
|
||||
//setup
|
||||
@@ -605,7 +605,7 @@ int test_table_expansions_under_stress() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int test_value_creation() {
|
||||
int test_value_creation(void) {
|
||||
//test for the correct size
|
||||
{
|
||||
#if TOY_BITNESS == 64
|
||||
@@ -52,8 +52,8 @@ int test_value_creation() {
|
||||
Toy_Value greeting = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world!"));
|
||||
|
||||
if (TOY_VALUE_IS_STRING(greeting) == false ||
|
||||
TOY_VALUE_AS_STRING(greeting)->type != TOY_STRING_LEAF ||
|
||||
strcmp(TOY_VALUE_AS_STRING(greeting)->as.leaf.data, "Hello world!") != 0
|
||||
TOY_VALUE_AS_STRING(greeting)->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(TOY_VALUE_AS_STRING(greeting)->leaf.data, "Hello world!") != 0
|
||||
)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: 'string' value failed\n" TOY_CC_RESET);
|
||||
@@ -95,7 +95,7 @@ int test_value_creation() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_value_copying() {
|
||||
int test_value_copying(void) {
|
||||
//test simple integer copy
|
||||
{
|
||||
Toy_Value original = TOY_VALUE_FROM_INTEGER(42);
|
||||
@@ -118,9 +118,9 @@ int test_value_copying() {
|
||||
Toy_Value result = Toy_copyValue(original);
|
||||
|
||||
if (TOY_VALUE_IS_STRING(result) == false ||
|
||||
TOY_VALUE_AS_STRING(result)->type != TOY_STRING_LEAF ||
|
||||
strcmp(TOY_VALUE_AS_STRING(result)->as.leaf.data, "Hello world!") != 0 ||
|
||||
TOY_VALUE_AS_STRING(result)->refCount != 2
|
||||
TOY_VALUE_AS_STRING(result)->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(TOY_VALUE_AS_STRING(result)->leaf.data, "Hello world!") != 0 ||
|
||||
TOY_VALUE_AS_STRING(result)->info.refCount != 2
|
||||
)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: copy a string value failed\n" TOY_CC_RESET);
|
||||
@@ -172,7 +172,7 @@ int test_value_copying() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_value_hashing() {
|
||||
int test_value_hashing(void) {
|
||||
//test value hashing
|
||||
{
|
||||
//setup
|
||||
@@ -197,7 +197,7 @@ int test_value_hashing() {
|
||||
Toy_hashValue(f) != 0 ||
|
||||
Toy_hashValue(i) != 4147366645 ||
|
||||
Toy_hashValue(s) != 994097935 ||
|
||||
TOY_VALUE_AS_STRING(s)->cachedHash == 0 ||
|
||||
TOY_VALUE_AS_STRING(s)->info.cachedHash == 0 ||
|
||||
Toy_hashValue(a) != 2544446955
|
||||
)
|
||||
{
|
||||
@@ -215,7 +215,7 @@ int test_value_hashing() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_value_equality() {
|
||||
int test_value_equality(void) {
|
||||
//test value equality
|
||||
{
|
||||
Toy_Value answer = TOY_VALUE_FROM_INTEGER(42);
|
||||
@@ -290,7 +290,7 @@ int test_value_equality() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_value_comparison() {
|
||||
int test_value_comparison(void) {
|
||||
//test value comparable
|
||||
{
|
||||
Toy_Value answer = TOY_VALUE_FROM_INTEGER(42);
|
||||
@@ -353,7 +353,7 @@ int test_value_comparison() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_value_stringify() {
|
||||
int test_value_stringify(void) {
|
||||
//stringify null
|
||||
{
|
||||
//setup
|
||||
@@ -364,8 +364,8 @@ int test_value_stringify() {
|
||||
Toy_String* string = Toy_stringifyValue(&bucket, value);
|
||||
|
||||
//check
|
||||
if (string->type != TOY_STRING_LEAF ||
|
||||
strcmp(string->as.leaf.data, "null") != 0)
|
||||
if (string->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(string->leaf.data, "null") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify 'null' failed\n" TOY_CC_RESET);
|
||||
Toy_freeString(string);
|
||||
@@ -390,8 +390,8 @@ int test_value_stringify() {
|
||||
Toy_String* string = Toy_stringifyValue(&bucket, value);
|
||||
|
||||
//check
|
||||
if (string->type != TOY_STRING_LEAF ||
|
||||
strcmp(string->as.leaf.data, "true") != 0)
|
||||
if (string->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(string->leaf.data, "true") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify boolean 'true' failed\n" TOY_CC_RESET);
|
||||
Toy_freeString(string);
|
||||
@@ -416,8 +416,8 @@ int test_value_stringify() {
|
||||
Toy_String* string = Toy_stringifyValue(&bucket, value);
|
||||
|
||||
//check
|
||||
if (string->type != TOY_STRING_LEAF ||
|
||||
strcmp(string->as.leaf.data, "false") != 0)
|
||||
if (string->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(string->leaf.data, "false") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify boolean 'false' failed\n" TOY_CC_RESET);
|
||||
Toy_freeString(string);
|
||||
@@ -442,8 +442,8 @@ int test_value_stringify() {
|
||||
Toy_String* string = Toy_stringifyValue(&bucket, value);
|
||||
|
||||
//check
|
||||
if (string->type != TOY_STRING_LEAF ||
|
||||
strcmp(string->as.leaf.data, "42") != 0)
|
||||
if (string->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(string->leaf.data, "42") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify integer '42' failed\n" TOY_CC_RESET);
|
||||
Toy_freeString(string);
|
||||
@@ -468,8 +468,8 @@ int test_value_stringify() {
|
||||
Toy_String* string = Toy_stringifyValue(&bucket, value);
|
||||
|
||||
//check
|
||||
if (string->type != TOY_STRING_LEAF ||
|
||||
strcmp(string->as.leaf.data, "3.1415") != 0)
|
||||
if (string->info.type != TOY_STRING_LEAF ||
|
||||
strcmp(string->leaf.data, "3.1415") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify float '3.1415' failed\n" TOY_CC_RESET);
|
||||
Toy_freeString(string);
|
||||
@@ -539,7 +539,7 @@ int test_value_stringify() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -806,7 +806,7 @@ int test_vm_reuse(Toy_Bucket** bucketHandle) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
|
||||
@@ -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+=
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ static unsigned int hashUInt(unsigned int x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//print the index/hash pairs
|
||||
for (unsigned int i = 0; i < 100; i++) {
|
||||
unsigned int h = hashUInt(i);
|
||||
|
||||
@@ -8,7 +8,7 @@ static unsigned int hashUInt(unsigned int x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//find the first number with a specific hash, then print the c-code
|
||||
for (unsigned int h = 0; h < 20; h++) {
|
||||
for (unsigned int i = 0; i < 100; i++) {
|
||||
|
||||
@@ -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+=
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ int getFileName(char* dest, const char* src) {
|
||||
return len;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
//check the platform
|
||||
printf("Platform: ");
|
||||
#if defined(__linux__)
|
||||
|
||||
Reference in New Issue
Block a user