diff --git a/makefile b/makefile index 8e2680c..5ef3986 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ #compiler settings reference #CC=gcc -#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2 +#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2 #LIBS+=-lm #LDFLAGS+= diff --git a/repl/main.c b/repl/main.c index 2280249..2746ad9 100644 --- a/repl/main.c +++ b/repl/main.c @@ -42,7 +42,7 @@ unsigned char* readFile(char* path, int* size) { } //read the file - if (fread(buffer, sizeof(unsigned char), *size, file) < *size) { + if (fread(buffer, sizeof(unsigned char), *size, file) < (unsigned int)(*size)) { fclose(file); *size = -2; //singal a read error return NULL; @@ -130,10 +130,12 @@ static void assertFailureAndContinueCallback(const char* msg) { static void noOpCallback(const char* msg) { //NO-OP + (void)msg; } static void silentExitCallback(const char* msg) { //NO-OP + (void)msg; exit(-1); } @@ -151,6 +153,7 @@ typedef struct CmdLine { } CmdLine; void usageCmdLine(int argc, const char* argv[]) { + (void)argc; printf("Usage: %s [ -h | -v | -f source.toy ]\n\n", argv[0]); } @@ -169,6 +172,8 @@ void helpCmdLine(int argc, const char* argv[]) { } void versionCmdLine(int argc, const char* argv[]) { + (void)argc; + (void)argv; printf("The Toy Programming Language, Version %d.%d.%d %s\n\n", TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD); //copy/pasted from the license file - there's a way to include it directly, but it's too finnicky to bother @@ -344,72 +349,37 @@ int repl(const char* filepath) { static void debugStackPrint(Toy_Stack* stack) { //DEBUG: if there's anything on the stack, print it if (stack->count > 0) { + Toy_Bucket* stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); + printf("Stack Dump\n-------------------------\ntype\tvalue\n"); - for (int i = 0; i < stack->count; i++) { - Toy_Value v = ((Toy_Value*)(stack + 1))[i]; + for (unsigned int i = 0; i < stack->count; i++) { + Toy_Value v = ((Toy_Value*)(stack + 1))[i]; //'stack + 1' is a naughty trick + //print type printf("%s\t", Toy_private_getValueTypeAsCString(v.type)); - v = Toy_unwrapValue(v); - - switch(v.type) { - case TOY_VALUE_NULL: - printf("null"); - break; - - case TOY_VALUE_BOOLEAN: - printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false"); - break; - - case TOY_VALUE_INTEGER: - printf("%d", TOY_VALUE_AS_INTEGER(v)); - break; - - case TOY_VALUE_FLOAT: - printf("%f", TOY_VALUE_AS_FLOAT(v)); - break; - - case TOY_VALUE_STRING: { - Toy_String* str = TOY_VALUE_AS_STRING(v); - - //print based on type - if (str->type == TOY_STRING_NODE) { - char* buffer = Toy_getStringRawBuffer(str); - printf("%s", buffer); - free(buffer); - } - else if (str->type == TOY_STRING_LEAF) { - printf("%s", str->as.leaf.data); - } - else if (str->type == TOY_STRING_NAME) { - printf("%s", str->as.name.data); - } - break; - } - - case TOY_VALUE_ARRAY: - case TOY_VALUE_TABLE: - case TOY_VALUE_FUNCTION: - case TOY_VALUE_OPAQUE: - case TOY_VALUE_TYPE: - case TOY_VALUE_ANY: - case TOY_VALUE_REFERENCE: - case TOY_VALUE_UNKNOWN: - printf("???"); - break; - } + //print value + Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v)); + char* buffer = Toy_getStringRawBuffer(string); + printf("%s", buffer); + free(buffer); + Toy_freeString(string); printf("\n"); } + + Toy_freeBucket(&stringBucket); } } static void debugScopePrint(Toy_Scope* scope, int depth) { //DEBUG: if there's anything in the scope, print it if (scope->table->count > 0) { + Toy_Bucket* stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); + printf("Scope %d Dump\n-------------------------\ntype\tname\tvalue\n", depth); - for (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) == false) { + 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) { continue; } @@ -418,58 +388,17 @@ static void debugScopePrint(Toy_Scope* scope, int depth) { printf("%s\t%s\t", Toy_private_getValueTypeAsCString(v.type), TOY_VALUE_AS_STRING(k)->as.name.data); - k = Toy_unwrapValue(k); - v = Toy_unwrapValue(v); - - switch(v.type) { - case TOY_VALUE_NULL: - printf("null"); - break; - - case TOY_VALUE_BOOLEAN: - printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false"); - break; - - case TOY_VALUE_INTEGER: - printf("%d", TOY_VALUE_AS_INTEGER(v)); - break; - - case TOY_VALUE_FLOAT: - printf("%f", TOY_VALUE_AS_FLOAT(v)); - break; - - case TOY_VALUE_STRING: { - Toy_String* str = TOY_VALUE_AS_STRING(v); - - //print based on type - if (str->type == TOY_STRING_NODE) { - char* buffer = Toy_getStringRawBuffer(str); - printf("%s", buffer); - free(buffer); - } - else if (str->type == TOY_STRING_LEAF) { - printf("%s", str->as.leaf.data); - } - else if (str->type == TOY_STRING_NAME) { - printf("%s\nWarning: The above value is a name string", str->as.name.data); - } - break; - } - - case TOY_VALUE_ARRAY: - case TOY_VALUE_TABLE: - case TOY_VALUE_FUNCTION: - case TOY_VALUE_OPAQUE: - case TOY_VALUE_TYPE: - case TOY_VALUE_ANY: - case TOY_VALUE_REFERENCE: - case TOY_VALUE_UNKNOWN: - printf("???"); - break; - } + //print value + Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v)); + char* buffer = Toy_getStringRawBuffer(string); + printf("%s", buffer); + free(buffer); + Toy_freeString(string); printf("\n"); } + + Toy_freeBucket(&stringBucket); } if (scope->next != NULL) { diff --git a/repl/makefile b/repl/makefile index 7084b0f..f0ddc98 100644 --- a/repl/makefile +++ b/repl/makefile @@ -1,6 +1,6 @@ #compiler settings CC=gcc -#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2 +CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2 LIBS+=-lm -lToy LDFLAGS+=-Wl,-rpath,'$$ORIGIN' diff --git a/source/makefile b/source/makefile index 8a06c4d..3a5eda5 100644 --- a/source/makefile +++ b/source/makefile @@ -1,6 +1,6 @@ #compiler settings CC=gcc -CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2 +CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2 LIBS+=-lm LDFLAGS+= diff --git a/source/toy_routine.c b/source/toy_routine.c index 7d06933..297f6de 100644 --- a/source/toy_routine.c +++ b/source/toy_routine.c @@ -10,7 +10,7 @@ #include //utils -static void expand(void** handle, unsigned int* capacity, unsigned int* count, unsigned int amount) { +static void expand(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned int amount) { if ((*count) + amount > (*capacity)) { while ((*count) + amount > (*capacity)) { (*capacity) = (*capacity) < 8 ? 8 : (*capacity) * 2; @@ -24,12 +24,12 @@ static void expand(void** handle, unsigned int* capacity, unsigned int* count, u } } -static void emitByte(void** handle, unsigned int* capacity, unsigned int* count, unsigned char byte) { +static void emitByte(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned char byte) { expand(handle, capacity, count, 1); ((unsigned char*)(*handle))[(*count)++] = byte; } -static void emitInt(void** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) { +static void emitInt(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) { char* ptr = (char*)&bytes; emitByte(handle, capacity, count, *(ptr++)); emitByte(handle, capacity, count, *(ptr++)); @@ -37,7 +37,7 @@ static void emitInt(void** handle, unsigned int* capacity, unsigned int* count, emitByte(handle, capacity, count, *(ptr++)); } -static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count, float bytes) { +static void emitFloat(unsigned char** handle, unsigned int* capacity, unsigned int* count, float bytes) { char* ptr = (char*)&bytes; emitByte(handle, capacity, count, *(ptr++)); emitByte(handle, capacity, count, *(ptr++)); @@ -47,11 +47,11 @@ static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count //write instructions based on the AST types #define EMIT_BYTE(rt, part, byte) \ - emitByte((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), byte) + emitByte((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), byte) #define EMIT_INT(rt, part, bytes) \ - emitInt((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes) + emitInt((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes) #define EMIT_FLOAT(rt, part, bytes) \ - emitFloat((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes) + emitFloat((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes) //skip bytes, but return the address #define SKIP_BYTE(rt, part) (EMIT_BYTE(rt, part, 0), ((*rt)->part##Count - 1)) @@ -59,7 +59,7 @@ static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count //overwrite a pre-existing position #define OVERWRITE_INT(rt, part, addr, bytes) \ - emitInt((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &(addr), bytes); + emitInt((&((*rt)->part)), &((*rt)->part##Capacity), &(addr), bytes); //simply get the address (always an integer) #define CURRENT_ADDRESS(rt, part) ((*rt)->part##Count) @@ -80,7 +80,7 @@ static unsigned int emitString(Toy_Routine** rt, Toy_String* str) { unsigned int startAddr = (*rt)->dataCount; //move the string into the data section - expand((void**)(&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length); + expand((&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length); if (str->type == TOY_STRING_NODE) { char* buffer = Toy_getStringRawBuffer(str); @@ -750,7 +750,7 @@ static void* writeRoutine(Toy_Routine* rt, Toy_Ast* ast) { } //write the header and combine the parts - void* buffer = NULL; + unsigned char* buffer = NULL; unsigned int capacity = 0, count = 0; // int paramAddr = 0, subsAddr = 0; int codeAddr = 0; @@ -766,23 +766,23 @@ static void* writeRoutine(Toy_Routine* rt, Toy_Ast* ast) { //generate blank spaces, cache their positions in the *Addr variables (for storing the start positions) if (rt->paramCount > 0) { // paramAddr = count; - emitInt((void**)&buffer, &capacity, &count, 0); //params + emitInt(&buffer, &capacity, &count, 0); //params } if (rt->codeCount > 0) { codeAddr = count; - emitInt((void**)&buffer, &capacity, &count, 0); //code + emitInt(&buffer, &capacity, &count, 0); //code } if (rt->jumpsCount > 0) { jumpsAddr = count; - emitInt((void**)&buffer, &capacity, &count, 0); //jumps + emitInt(&buffer, &capacity, &count, 0); //jumps } if (rt->dataCount > 0) { dataAddr = count; - emitInt((void**)&buffer, &capacity, &count, 0); //data + emitInt(&buffer, &capacity, &count, 0); //data } if (rt->subsCount > 0) { // subsAddr = count; - emitInt((void**)&buffer, &capacity, &count, 0); //subs + emitInt(&buffer, &capacity, &count, 0); //subs } //append various parts to the buffer diff --git a/source/toy_routine.h b/source/toy_routine.h index 36addb6..ca3ff0b 100644 --- a/source/toy_routine.h +++ b/source/toy_routine.h @@ -13,7 +13,7 @@ typedef struct Toy_Routine { unsigned int codeCapacity; unsigned int codeCount; - unsigned int* jumps; //each 'jump' is the starting address of an element within 'data' + unsigned char* jumps; //each 'jump' is the starting address of an element within 'data' unsigned int jumpsCapacity; unsigned int jumpsCount; diff --git a/tests/benchmarks/makefile b/tests/benchmarks/makefile index 0e3706a..ee6d360 100644 --- a/tests/benchmarks/makefile +++ b/tests/benchmarks/makefile @@ -1,6 +1,6 @@ #compiler settings CC=gcc -CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2 +CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2 LIBS+=-lm LDFLAGS+= diff --git a/tests/cases/makefile b/tests/cases/makefile index 220466a..7540f70 100644 --- a/tests/cases/makefile +++ b/tests/cases/makefile @@ -1,6 +1,6 @@ #compiler settings CC=gcc -CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2 +CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2 LIBS+=-lm LDFLAGS+= diff --git a/tests/cases/test_routine.c b/tests/cases/test_routine.c index 7c848f2..1535e80 100644 --- a/tests/cases/test_routine.c +++ b/tests/cases/test_routine.c @@ -18,7 +18,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { Toy_private_emitAstPass(bucketHandle, &ast); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -66,7 +66,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -114,7 +114,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -166,7 +166,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -218,7 +218,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -271,7 +271,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -324,7 +324,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* header = (int*)buffer; @@ -350,7 +350,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { return -1; } - void* code = buffer + 32; //8 values in the header, each 4 bytes + unsigned char* code = buffer + 32; //8 values in the header, each 4 bytes //check code if ( @@ -374,7 +374,7 @@ int test_routine_expressions(Toy_Bucket** bucketHandle) { return -1; } - void* jumps = code + 12; + unsigned char* jumps = code + 12; //check jumps if ( @@ -430,7 +430,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -495,7 +495,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -560,7 +560,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -625,7 +625,7 @@ int test_routine_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -720,7 +720,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -778,7 +778,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -841,7 +841,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -931,7 +931,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -1045,7 +1045,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* ptr = (int*)buffer; @@ -1102,7 +1102,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* header = (int*)buffer; @@ -1128,7 +1128,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { return -1; } - void* code = buffer + 32; //8 values in the header, each 4 bytes + unsigned char* code = buffer + 32; //8 values in the header, each 4 bytes //check code if ( @@ -1161,7 +1161,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { return -1; } - void* jumps = code + 20; + unsigned char* jumps = code + 20; //check jumps if ( @@ -1209,7 +1209,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - void* buffer = Toy_compileRoutine(ast); + unsigned char* buffer = Toy_compileRoutine(ast); //check header int* header = (int*)buffer; @@ -1235,7 +1235,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { return -1; } - void* code = buffer + 32; //8 values in the header, each 4 bytes + unsigned char* code = buffer + 32; //8 values in the header, each 4 bytes //check code if ( @@ -1268,7 +1268,7 @@ int test_routine_keywords(Toy_Bucket** bucketHandle) { return -1; } - void* jumps = code + 20; + unsigned char* jumps = code + 20; //check jumps if ( diff --git a/tests/integrations/makefile b/tests/integrations/makefile index 7d6cbb5..0fe22fb 100644 --- a/tests/integrations/makefile +++ b/tests/integrations/makefile @@ -1,6 +1,6 @@ #compiler settings CC=gcc -CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2 +CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2 LIBS+=-lm LDFLAGS+= diff --git a/tests/standalone/makefile b/tests/standalone/makefile index ed09b83..5cb614c 100644 --- a/tests/standalone/makefile +++ b/tests/standalone/makefile @@ -1,6 +1,6 @@ #compiler settings CC=gcc -CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wformat=2 +CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2 LIBS+=-lm LDFLAGS+=