diff --git a/repl/main.c b/repl/main.c index dff7bb7..a44eeda 100644 --- a/repl/main.c +++ b/repl/main.c @@ -174,29 +174,29 @@ int main(int argc, const char* argv[]) { int size; unsigned char* source = readFile(cmd.infile, &size); - free(cmd.infile); - - cmd.infile = NULL; - cmd.infileLength = 0; - //check the file if (source == NULL) { if (size == 0) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Could not parse an empty file, exiting\n" TOY_CC_RESET); + fprintf(stderr, TOY_CC_ERROR "ERROR: Could not parse an empty file '%s', exiting\n" TOY_CC_RESET, cmd.infile); return -1; } else if (size == -1) { - fprintf(stderr, TOY_CC_ERROR "ERROR: File not found, exiting\n" TOY_CC_RESET); + fprintf(stderr, TOY_CC_ERROR "ERROR: File not found '%s', exiting\n" TOY_CC_RESET, cmd.infile); return -1; } else { - fprintf(stderr, TOY_CC_ERROR "ERROR: Unknown error while reading file, exiting\n" TOY_CC_RESET); + fprintf(stderr, TOY_CC_ERROR "ERROR: Unknown error while reading file '%s', exiting\n" TOY_CC_RESET, cmd.infile); return -1; } } + free(cmd.infile); + + cmd.infile = NULL; + cmd.infileLength = 0; + Toy_Lexer lexer; Toy_bindLexer(&lexer, (char*)source); @@ -263,4 +263,4 @@ int main(int argc, const char* argv[]) { return 0; } -//TODO: simple and consistent way to print an AST and Toy_Value \ No newline at end of file +//TODO: simple and consistent way to print an AST and Toy_Value diff --git a/source/toy_array.c b/source/toy_array.c index 4bcaaa0..01ed16d 100644 --- a/source/toy_array.c +++ b/source/toy_array.c @@ -4,7 +4,7 @@ #include #include -Toy_Array* Toy_resizeArray(Toy_Array* paramArray, size_t capacity) { +Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) { if (capacity == 0) { free(paramArray); return NULL; diff --git a/source/toy_array.h b/source/toy_array.h index 3e8105f..f681762 100644 --- a/source/toy_array.h +++ b/source/toy_array.h @@ -4,12 +4,12 @@ //standard generic array typedef struct Toy_Array { //32 | 64 BITNESS - size_t capacity; //4 | 4 - size_t count; //4 | 4 + unsigned int capacity; //4 | 4 + unsigned int count; //4 | 4 char data[]; //- | - } Toy_Array; //8 | 8 -TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, size_t capacity); +TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity); #define TOY_ALLOCATE_ARRAY(type, count) \ Toy_resizeArray(NULL, sizeof(type)*(count)) diff --git a/source/toy_bucket.c b/source/toy_bucket.c index 7d3ca9e..f721c3b 100644 --- a/source/toy_bucket.c +++ b/source/toy_bucket.c @@ -5,7 +5,7 @@ #include //buckets of fun -Toy_Bucket* Toy_allocateBucket(size_t capacity) { +Toy_Bucket* Toy_allocateBucket(unsigned int capacity) { if (capacity == 0) { fprintf(stderr, TOY_CC_ERROR "ERROR: Cannot allocate a 'Toy_Bucket' with zero capacity\n" TOY_CC_RESET); exit(1); @@ -26,7 +26,7 @@ Toy_Bucket* Toy_allocateBucket(size_t capacity) { return bucket; } -void* Toy_partitionBucket(Toy_Bucket** bucketHandle, size_t amount) { +void* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount) { if ((*bucketHandle) == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Expected a 'Toy_Bucket', received NULL\n" TOY_CC_RESET); exit(1); diff --git a/source/toy_bucket.h b/source/toy_bucket.h index 30da803..c0696d4 100644 --- a/source/toy_bucket.h +++ b/source/toy_bucket.h @@ -11,12 +11,12 @@ //a custom allocator typedef struct Toy_Bucket { //32 | 64 BITNESS struct Toy_Bucket* next; //4 | 8 - size_t capacity; //4 | 4 - size_t count; //4 | 4 + unsigned int capacity; //4 | 4 + unsigned int count; //4 | 4 char data[]; //- | - } Toy_Bucket; //12 | 16 -TOY_API Toy_Bucket* Toy_allocateBucket(size_t capacity); -TOY_API void* Toy_partitionBucket(Toy_Bucket** bucketHandle, size_t amount); +TOY_API Toy_Bucket* Toy_allocateBucket(unsigned int capacity); +TOY_API void* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount); TOY_API void Toy_freeBucket(Toy_Bucket** bucketHandle); diff --git a/source/toy_bytecode.c b/source/toy_bytecode.c index 580b418..3324ef0 100644 --- a/source/toy_bytecode.c +++ b/source/toy_bytecode.c @@ -8,7 +8,7 @@ #include //utils -static void expand(Toy_Bytecode* bc, size_t amount) { +static void expand(Toy_Bytecode* bc, unsigned int amount) { if (bc->count + amount > bc->capacity) { while (bc->count + amount > bc->capacity) { //expand as much as needed diff --git a/source/toy_bytecode.h b/source/toy_bytecode.h index 038e8ab..5fd4855 100644 --- a/source/toy_bytecode.h +++ b/source/toy_bytecode.h @@ -5,8 +5,8 @@ typedef struct Toy_Bytecode { unsigned char* ptr; - size_t capacity; - size_t count; + unsigned int capacity; + unsigned int count; } Toy_Bytecode; TOY_API Toy_Bytecode Toy_compileBytecode(Toy_Ast* ast); diff --git a/source/toy_lexer.c b/source/toy_lexer.c index 478b852..aca5ec4 100644 --- a/source/toy_lexer.c +++ b/source/toy_lexer.c @@ -205,7 +205,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) { //scan for a keyword for (int i = 0; Toy_private_keywords[i].keyword; i++) { //WONTFIX: could squeeze miniscule performance gain from this, but ROI isn't worth it - if (strlen(Toy_private_keywords[i].keyword) == (size_t)(lexer->current - lexer->start) && !strncmp(Toy_private_keywords[i].keyword, &lexer->source[lexer->start], lexer->current - lexer->start)) { + if (strlen(Toy_private_keywords[i].keyword) == (lexer->current - lexer->start) && !strncmp(Toy_private_keywords[i].keyword, &lexer->source[lexer->start], lexer->current - lexer->start)) { //make token (keyword) Toy_Token token; @@ -312,7 +312,7 @@ Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer) { } } -static void trim(char** s, size_t* l) { //util +static void trim(char** s, unsigned int* l) { //util while( isspace(( (*((unsigned char**)(s)))[(*l) - 1] )) ) (*l)--; while(**s && isspace( **(unsigned char**)(s)) ) { (*s)++; (*l)--; } } @@ -344,7 +344,7 @@ void Toy_private_printToken(Toy_Token* token) { printf("%s", keyword); } else { char* str = (char*)token->lexeme; //strip const-ness for trimming - size_t length = token->length; + unsigned int length = token->length; trim(&str, &length); printf("%.*s", (int)length, str); } diff --git a/source/toy_lexer.h b/source/toy_lexer.h index 083ba6a..b0b7652 100644 --- a/source/toy_lexer.h +++ b/source/toy_lexer.h @@ -5,17 +5,17 @@ //lexers are bound to a string of code typedef struct { - size_t start; //start of the current token - size_t current; //current position of the lexer - size_t line; //track this for error handling + unsigned int start; //start of the current token + unsigned int current; //current position of the lexer + unsigned int line; //track this for error handling const char* source; } Toy_Lexer; //tokens are intermediaries between lexers and parsers typedef struct { Toy_TokenType type; - size_t length; - size_t line; + unsigned int length; + unsigned int line; const char* lexeme; } Toy_Token; diff --git a/source/toy_parser.c b/source/toy_parser.c index 50068d8..eec3627 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -232,7 +232,7 @@ static Toy_AstFlag atomic(Toy_Bucket** bucket, Toy_Parser* parser, Toy_Ast** roo //filter the '_' character char buffer[parser->previous.length]; - size_t i = 0, o = 0; + unsigned int i = 0, o = 0; do { buffer[i] = parser->previous.lexeme[o]; if (buffer[i] != '_') i++; @@ -249,7 +249,7 @@ static Toy_AstFlag atomic(Toy_Bucket** bucket, Toy_Parser* parser, Toy_Ast** roo //filter the '_' character char buffer[parser->previous.length]; - size_t i = 0, o = 0; + unsigned int i = 0, o = 0; do { buffer[i] = parser->previous.lexeme[o]; if (buffer[i] != '_') i++; diff --git a/source/toy_routine.c b/source/toy_routine.c index a91c490..b73f2e1 100644 --- a/source/toy_routine.c +++ b/source/toy_routine.c @@ -9,7 +9,7 @@ #include //utils -static void expand(void** handle, size_t* capacity, size_t* count, size_t amount) { +static void expand(void** handle, unsigned int* capacity, unsigned int* count, unsigned int amount) { if ((*count) + amount > (*capacity)) { while ((*count) + amount > (*capacity)) { (*capacity) = (*capacity) < 8 ? 8 : (*capacity) * 2; @@ -23,12 +23,12 @@ static void expand(void** handle, size_t* capacity, size_t* count, size_t amount } } -static void emitByte(void** handle, size_t* capacity, size_t* count, unsigned char byte) { +static void emitByte(void** 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, size_t* capacity, size_t* count, size_t bytes) { +static void emitInt(void** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) { char* ptr = (char*)&bytes; emitByte(handle, capacity, count, *(ptr++)); emitByte(handle, capacity, count, *(ptr++)); @@ -36,7 +36,7 @@ static void emitInt(void** handle, size_t* capacity, size_t* count, size_t bytes emitByte(handle, capacity, count, *(ptr++)); } -static void emitFloat(void** handle, size_t* capacity, size_t* count, float bytes) { +static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count, float bytes) { char* ptr = (char*)&bytes; emitByte(handle, capacity, count, *(ptr++)); emitByte(handle, capacity, count, *(ptr++)); @@ -284,7 +284,7 @@ static void* writeRoutine(Toy_Routine* rt, Toy_Ast* ast) { //write the header and combine the parts void* buffer = NULL; - size_t capacity = 0, count = 0; + unsigned int capacity = 0, count = 0; // int paramAddr = 0, codeAddr = 0, jumpsAddr = 0, dataAddr = 0, subsAddr = 0; int codeAddr = 0; diff --git a/source/toy_routine.h b/source/toy_routine.h index 85d36e4..9662fa7 100644 --- a/source/toy_routine.h +++ b/source/toy_routine.h @@ -6,24 +6,24 @@ //internal structure that holds the individual parts of a compiled routine typedef struct Toy_Routine { unsigned char* param; //c-string params in sequence (could be moved below the jump table?) - size_t paramCapacity; - size_t paramCount; + unsigned int paramCapacity; + unsigned int paramCount; unsigned char* code; //the instruction set - size_t codeCapacity; - size_t codeCount; + unsigned int codeCapacity; + unsigned int codeCount; - size_t* jumps; //each 'jump' is the starting address of an element within 'data' - size_t jumpsCapacity; - size_t jumpsCount; + unsigned int* jumps; //each 'jump' is the starting address of an element within 'data' + unsigned int jumpsCapacity; + unsigned int jumpsCount; unsigned char* data; //{type,val} tuples of data - size_t dataCapacity; - size_t dataCount; + unsigned int dataCapacity; + unsigned int dataCount; unsigned char* subs; //subroutines, recursively - size_t subsCapacity; - size_t subsCount; + unsigned int subsCapacity; + unsigned int subsCount; } Toy_Routine; -TOY_API void* Toy_compileRoutine(Toy_Ast* ast); \ No newline at end of file +TOY_API void* Toy_compileRoutine(Toy_Ast* ast); diff --git a/source/toy_stack.c b/source/toy_stack.c index af2a2ad..83104a6 100644 --- a/source/toy_stack.c +++ b/source/toy_stack.c @@ -40,7 +40,7 @@ void Toy_pushStack(Toy_Stack** stack, Toy_Value value) { (*stack)->capacity = (*stack)->capacity < MIN_CAPACITY ? MIN_CAPACITY : (*stack)->capacity * 2; } - size_t newCapacity = (*stack)->capacity; + unsigned int newCapacity = (*stack)->capacity; (*stack) = realloc((*stack), newCapacity * sizeof(Toy_Value) + sizeof(Toy_Stack)); @@ -72,7 +72,7 @@ Toy_Value Toy_popStack(Toy_Stack** stack) { //shrink if possible if ((*stack)->count > MIN_CAPACITY && (*stack)->count < (*stack)->capacity / 4) { (*stack)->capacity /= 2; - size_t newCapacity = (*stack)->capacity; + unsigned int newCapacity = (*stack)->capacity; (*stack) = realloc((*stack), (*stack)->capacity * sizeof(Toy_Value) + sizeof(Toy_Stack)); diff --git a/source/toy_stack.h b/source/toy_stack.h index 634168d..b5ca00d 100644 --- a/source/toy_stack.h +++ b/source/toy_stack.h @@ -4,8 +4,8 @@ #include "toy_value.h" typedef struct Toy_Stack { //32 | 64 BITNESS - size_t capacity; //4 | 4 - size_t count; //4 | 4 + unsigned int capacity; //4 | 4 + unsigned int count; //4 | 4 char data[]; //- | - } Toy_Stack; //8 | 8 diff --git a/source/toy_string.c b/source/toy_string.c index 89b0dcd..c62406b 100644 --- a/source/toy_string.c +++ b/source/toy_string.c @@ -54,8 +54,8 @@ Toy_String* Toy_createStringLength(Toy_Bucket** bucket, const char* cstring, int } Toy_String* Toy_copyString(Toy_Bucket** bucket, Toy_String* str) { - if (str->refCount <= 0) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't copy a string with refcount below or equal to zero\n" TOY_CC_RESET); + if (str->refCount == 0) { + fprintf(stderr, TOY_CC_ERROR "ERROR: Can't copy a string with refcount of zero\n" TOY_CC_RESET); exit(-1); } incrementRefCount(str); @@ -63,8 +63,8 @@ Toy_String* Toy_copyString(Toy_Bucket** bucket, Toy_String* str) { } Toy_String* Toy_deepCopyString(Toy_Bucket** bucket, Toy_String* str) { - if (str->refCount <= 0) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't deep copy a string with refcount below or equal to zero\n" TOY_CC_RESET); + if (str->refCount == 0) { + fprintf(stderr, TOY_CC_ERROR "ERROR: Can't deep copy a string with refcount of zero\n" TOY_CC_RESET); exit(-1); } Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucket, sizeof(Toy_String) + str->length + 1); //TODO: compensate for partitioning more space than bucket capacity @@ -80,8 +80,8 @@ Toy_String* Toy_deepCopyString(Toy_Bucket** bucket, Toy_String* str) { } Toy_String* Toy_concatString(Toy_Bucket** bucket, Toy_String* left, Toy_String* right) { - if (left->refCount <= 0 || right->refCount <= 0) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a string with refcount below or equal to zero\n" TOY_CC_RESET); + if (left->refCount == 0 || right->refCount == 0) { + fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a string with refcount of zero\n" TOY_CC_RESET); exit(-1); } @@ -112,8 +112,8 @@ int Toy_getStringRefCount(Toy_String* str) { } char* Toy_getStringRawBuffer(Toy_String* str) { - if (str->refCount <= 0) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a string with refcount below or equal to zero\n" TOY_CC_RESET); + if (str->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); } diff --git a/source/toy_string.h b/source/toy_string.h index 57bd34b..86de534 100644 --- a/source/toy_string.h +++ b/source/toy_string.h @@ -9,8 +9,8 @@ typedef struct Toy_String { //32 | 64 BITNESS TOY_STRING_LEAF, } type; //4 | 4 - int length; //4 | 4 - int refCount; //4 | 4 + unsigned int length; //4 | 4 + unsigned int refCount; //4 | 4 int _padding; //4 | 4 diff --git a/source/toy_vm.c b/source/toy_vm.c index 5ff1585..60384c3 100644 --- a/source/toy_vm.c +++ b/source/toy_vm.c @@ -12,13 +12,16 @@ #define READ_BYTE(vm) \ vm->routine[vm->routineCounter++] +#define READ_UNSIGNED_INT(vm) \ + *((unsigned int*)(vm->routine + _read_postfix(&(vm->routineCounter), 4))) + #define READ_INT(vm) \ *((int*)(vm->routine + _read_postfix(&(vm->routineCounter), 4))) #define READ_FLOAT(vm) \ *((float*)(vm->routine + _read_postfix(&(vm->routineCounter), 4))) -static inline int _read_postfix(int* ptr, int amount) { +static inline int _read_postfix(unsigned int* ptr, int amount) { int ret = *ptr; *ptr += amount; return ret; @@ -277,7 +280,7 @@ static void process(Toy_VM* vm) { } //exposed functions -void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, int bytecodeSize) { +void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, unsigned int bytecodeSize) { if (bytecode[0] != TOY_VERSION_MAJOR || bytecode[1] > TOY_VERSION_MINOR) { fprintf(stderr, TOY_CC_ERROR "ERROR: Wrong bytecode version found: expected %d.%d.%d found %d.%d.%d, exiting\n" TOY_CC_RESET, TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, bytecode[0], bytecode[1], bytecode[2]); exit(-1); @@ -311,29 +314,29 @@ void Toy_bindVMToRoutine(Toy_VM* vm, unsigned char* routine) { vm->routine = routine; //read the header metadata - vm->routineSize = READ_INT(vm); - vm->paramCount = READ_INT(vm); - vm->jumpsCount = READ_INT(vm); - vm->dataCount = READ_INT(vm); - vm->subsCount = READ_INT(vm); + vm->routineSize = READ_UNSIGNED_INT(vm); + vm->paramCount = READ_UNSIGNED_INT(vm); + vm->jumpsCount = READ_UNSIGNED_INT(vm); + vm->dataCount = READ_UNSIGNED_INT(vm); + vm->subsCount = READ_UNSIGNED_INT(vm); //read the header addresses if (vm->paramCount > 0) { - vm->paramAddr = READ_INT(vm); + vm->paramAddr = READ_UNSIGNED_INT(vm); } - vm->codeAddr = READ_INT(vm); //required + vm->codeAddr = READ_UNSIGNED_INT(vm); //required if (vm->jumpsCount > 0) { - vm->jumpsAddr = READ_INT(vm); + vm->jumpsAddr = READ_UNSIGNED_INT(vm); } if (vm->dataCount > 0) { - vm->dataAddr = READ_INT(vm); + vm->dataAddr = READ_UNSIGNED_INT(vm); } if (vm->subsCount > 0) { - vm->subsAddr = READ_INT(vm); + vm->subsAddr = READ_UNSIGNED_INT(vm); } //preallocate the scope & stack @@ -384,4 +387,4 @@ void Toy_resetVM(Toy_VM* vm) { //init the scope & stack vm->stack = NULL; -} \ No newline at end of file +} diff --git a/source/toy_vm.h b/source/toy_vm.h index 0ac6669..6fec6e3 100644 --- a/source/toy_vm.h +++ b/source/toy_vm.h @@ -7,24 +7,24 @@ typedef struct Toy_VM { //hold the raw bytecode unsigned char* bc; - int bcSize; + unsigned int bcSize; //raw instructions to be executed unsigned char* routine; - int routineSize; + unsigned int routineSize; - int paramCount; - int jumpsCount; - int dataCount; - int subsCount; + unsigned int paramCount; + unsigned int jumpsCount; + unsigned int dataCount; + unsigned int subsCount; - int paramAddr; - int codeAddr; - int jumpsAddr; - int dataAddr; - int subsAddr; + unsigned int paramAddr; + unsigned int codeAddr; + unsigned int jumpsAddr; + unsigned int dataAddr; + unsigned int subsAddr; - int routineCounter; + unsigned int routineCounter; //heap - block-level key/value pairs //TODO: needs string util for identifiers @@ -33,7 +33,7 @@ typedef struct Toy_VM { Toy_Stack* stack; } Toy_VM; -TOY_API void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, int bytecodeSize); //process the version data +TOY_API void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, unsigned int bytecodeSize); //process the version data TOY_API void Toy_bindVMToRoutine(Toy_VM* vm, unsigned char* routine); //process the routine only TOY_API void Toy_runVM(Toy_VM* vm); diff --git a/tests/cases/test_string.c b/tests/cases/test_string.c index 5fcbb6c..c9d40c2 100644 --- a/tests/cases/test_string.c +++ b/tests/cases/test_string.c @@ -309,4 +309,4 @@ int main() { } return total; -} \ No newline at end of file +}