From ba9418f3657f65fed96427f34a6f9fee0d56ff2d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 4 Apr 2026 20:35:12 +1100 Subject: [PATCH] Removed unneeded files and console colors are disabled --- makefile | 2 +- source/toy_common.h | 2 +- source/toy_console_colors.h | 2 +- source/toy_module_bundle.c | 168 ------------------------------------ source/toy_module_bundle.h | 22 ----- source/toy_parser.c | 14 +-- source/toy_token_types.h | 36 ++++---- 7 files changed, 28 insertions(+), 218 deletions(-) delete mode 100644 source/toy_module_bundle.c delete mode 100644 source/toy_module_bundle.h diff --git a/makefile b/makefile index ba9045e..441b917 100644 --- a/makefile +++ b/makefile @@ -13,7 +13,7 @@ export TOY_OUTDIR=out export TOY_OBJDIR=obj #targets -#all: +all: source repl .PHONY: source source: diff --git a/source/toy_common.h b/source/toy_common.h index 0f637c3..b757a5c 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -55,7 +55,7 @@ //version specifiers, embedded as the header #define TOY_VERSION_MAJOR 2 -#define TOY_VERSION_MINOR 0 +#define TOY_VERSION_MINOR 1 #define TOY_VERSION_PATCH 0 //defined as a function, for technical reasons diff --git a/source/toy_console_colors.h b/source/toy_console_colors.h index 9b06dde..065f60b 100644 --- a/source/toy_console_colors.h +++ b/source/toy_console_colors.h @@ -13,7 +13,7 @@ NOTE: you need both font AND background for these to work */ //platform/compiler-specific instructions -#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__) +#if defined(TOY_CC_ENABLED) && ( defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__) ) //fonts color #define TOY_CC_FONT_BLACK "\033[30;" diff --git a/source/toy_module_bundle.c b/source/toy_module_bundle.c deleted file mode 100644 index 15febc0..0000000 --- a/source/toy_module_bundle.c +++ /dev/null @@ -1,168 +0,0 @@ -#include "toy_module_bundle.h" -#include "toy_console_colors.h" - -#include "toy_module_compiler.h" - -#include -#include -#include - -//utils -static void expand(Toy_ModuleBundle* bundle, unsigned int amount) { - if (bundle->count + amount > bundle->capacity) { - bundle->capacity = 8; //DON'T bitshift zero - - while (bundle->count + amount > bundle->capacity) { //expand as much as needed - bundle->capacity <<= 2; - } - - bundle->ptr = realloc(bundle->ptr, bundle->capacity); - - if (bundle->ptr == NULL) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_ModuleBundle' of %d capacity\n" TOY_CC_RESET, (int)(bundle->capacity)); - exit(1); - } - } -} - -static void emitByte(Toy_ModuleBundle* bundle, unsigned char byte) { - expand(bundle, 1); - bundle->ptr[bundle->count++] = byte; -} - -static void writeModuleBundleHeader(Toy_ModuleBundle* bundle) { - emitByte(bundle, TOY_VERSION_MAJOR); - emitByte(bundle, TOY_VERSION_MINOR); - emitByte(bundle, TOY_VERSION_PATCH); - emitByte(bundle, 0); //module count - - //get the build string - const char* build = Toy_private_version_build(); - size_t len = strlen(build) + 1; //includes null - - //emit the build string - expand(bundle, len); - strncpy((char*)(bundle->ptr + bundle->count), build, len); - bundle->count += len; - - //align the count - bundle->count = (bundle->count + 3) & ~3; -} - -static int validateModuleBundleHeader(Toy_ModuleBundle* bundle) { - if (bundle->ptr[0] != TOY_VERSION_MAJOR || bundle->ptr[1] > TOY_VERSION_MINOR) { - return -1; - } - - if (bundle->ptr[2] != TOY_VERSION_PATCH) { - return 1; - } - - if (strcmp((char*)(bundle->ptr + 4), TOY_VERSION_BUILD) != 0) { - return 2; - } - - return 0; -} - -//exposed functions -void Toy_initModuleBundle(Toy_ModuleBundle* bundle) { - bundle->ptr = NULL; - bundle->capacity = 0; - bundle->count = 0; -} - -void Toy_appendModuleBundle(Toy_ModuleBundle* bundle, Toy_Ast* ast) { - //probably some inefficincies in memory usage here - if (bundle->capacity == 0) { - writeModuleBundleHeader(bundle); //TODO: update the header? - } - - //increment the module count - if (bundle->ptr[3] < 255) { - bundle->ptr[3]++; - } - else { - fprintf(stderr, TOY_CC_ERROR "ERROR: Too many modules in a bundle\n" TOY_CC_RESET); - exit(-1); - } - - unsigned char* module = Toy_compileModule(ast); - - //don't try writing an empty module - if (module == NULL) { - return; - } - - //write the module to the bundle - size_t len = (size_t)(((int*)module)[0]); - - expand(bundle, len); - memcpy(bundle->ptr + bundle->count, module, len); - bundle->count += len; - - free(module); -} - -void Toy_freeModuleBundle(Toy_ModuleBundle* bundle) { - free(bundle->ptr); - Toy_initModuleBundle(bundle); -} - -void Toy_bindModuleBundle(Toy_ModuleBundle* bundle, unsigned char* ptr, unsigned int size) { - if (bundle == NULL) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't bind a NULL bundle\n" TOY_CC_RESET); - exit(-1); - } - - if (bundle->ptr != NULL || bundle->capacity != 0 || bundle->count != 0) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't bind a bundle with pre-existing contents\n" TOY_CC_RESET); - exit(-1); - } - - //copy - expand(bundle, size); - - memcpy(bundle->ptr, ptr, size); - bundle->count = size; - - //TODO: test this - int valid = validateModuleBundleHeader(bundle); - - if (valid < 0) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Wrong version info found in module header: expected %d.%d.%d.%s found %d.%d.%d.%s, exiting\n" TOY_CC_RESET, TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD, bundle->ptr[0], bundle->ptr[1], bundle->ptr[2], (char*)(bundle->ptr + 4)); - exit(valid); - } - - if (valid > 0) { - fprintf(stderr, TOY_CC_WARN "WARNING: Wrong version info found in module header: expected %d.%d.%d.%s found %d.%d.%d.%s, continuing\n" TOY_CC_RESET, TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD, bundle->ptr[0], bundle->ptr[1], bundle->ptr[2], (char*)(bundle->ptr + 4)); - } -} - -Toy_Module Toy_extractModuleFromBundle(Toy_ModuleBundle* bundle, unsigned char index) { - if (bundle == NULL) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't extract from a NULL bundle\n" TOY_CC_RESET); - return (Toy_Module){ 0 }; - } - - if (bundle->ptr == NULL) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Can't extract from an empty bundle\n" TOY_CC_RESET); - return (Toy_Module){ 0 }; - } - - //yes, it's a bit awkward - char* buildPtr = (char*)(bundle->ptr + 4); - int buildLen = strlen(buildPtr); - buildLen = (buildLen + 3) & ~3; - - //first module's start position - unsigned char* moduleHead = bundle->ptr + 4 + buildLen; - - for (unsigned char i = 0; i < index; i++) { - unsigned int size = *((int*)(moduleHead)); - moduleHead += size; - } - - //read in the module - return Toy_parseModule(moduleHead); -} diff --git a/source/toy_module_bundle.h b/source/toy_module_bundle.h deleted file mode 100644 index cc362df..0000000 --- a/source/toy_module_bundle.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "toy_common.h" -#include "toy_ast.h" -#include "toy_module.h" - -typedef struct Toy_ModuleBundle { - unsigned char* ptr; - unsigned int capacity; - unsigned int count; -} Toy_ModuleBundle; - -//create a bundle -TOY_API void Toy_initModuleBundle(Toy_ModuleBundle* bundle); -TOY_API void Toy_appendModuleBundle(Toy_ModuleBundle* bundle, Toy_Ast* ast); //TODO: raw bytes -TOY_API void Toy_freeModuleBundle(Toy_ModuleBundle* bundle); - -//load module bundle with external data (makes an internal copy) -TOY_API void Toy_bindModuleBundle(Toy_ModuleBundle* bundle, unsigned char* ptr, unsigned int size); -TOY_API Toy_Module Toy_extractModuleFromBundle(Toy_ModuleBundle* bundle, unsigned char index); - -//NOTE: 'Toy_ModuleBundle' isn't used anywhere? \ No newline at end of file diff --git a/source/toy_parser.c b/source/toy_parser.c index f6ba6b4..1179008 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -209,16 +209,16 @@ static ParsingTuple parsingRulesetTable[] = { {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_QUESTION, {PREC_GROUP,compound,aggregate},// TOY_TOKEN_OPERATOR_COLON, - {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_SEMICOLON, // ; - {PREC_GROUP,NULL,aggregate},// TOY_TOKEN_OPERATOR_COMMA, // , + {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_SEMICOLON, + {PREC_GROUP,NULL,aggregate},// TOY_TOKEN_OPERATOR_COMMA, - {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_DOT, // . - {PREC_UNARY,NULL,binary},// TOY_TOKEN_OPERATOR_CONCAT, // .. - {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_REST, // ... + {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_DOT, + {PREC_UNARY,NULL,binary},// TOY_TOKEN_OPERATOR_CONCAT, + {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_REST, //unused operators - {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_AMPERSAND, // & - {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_PIPE, // | + {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_AMPERSAND, + {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_PIPE, //meta tokens {PREC_NONE,NULL,NULL},// TOY_TOKEN_ERROR, diff --git a/source/toy_token_types.h b/source/toy_token_types.h index 325346f..f514f3e 100644 --- a/source/toy_token_types.h +++ b/source/toy_token_types.h @@ -66,12 +66,12 @@ typedef enum Toy_TokenType { TOY_TOKEN_OPERATOR_ASSIGN, //comparator operators - TOY_TOKEN_OPERATOR_COMPARE_EQUAL, - TOY_TOKEN_OPERATOR_COMPARE_NOT, - TOY_TOKEN_OPERATOR_COMPARE_LESS, - TOY_TOKEN_OPERATOR_COMPARE_LESS_EQUAL, - TOY_TOKEN_OPERATOR_COMPARE_GREATER, - TOY_TOKEN_OPERATOR_COMPARE_GREATER_EQUAL, + TOY_TOKEN_OPERATOR_COMPARE_EQUAL, // == + TOY_TOKEN_OPERATOR_COMPARE_NOT, // != + TOY_TOKEN_OPERATOR_COMPARE_LESS, // < + TOY_TOKEN_OPERATOR_COMPARE_LESS_EQUAL, // <= + TOY_TOKEN_OPERATOR_COMPARE_GREATER, // > + TOY_TOKEN_OPERATOR_COMPARE_GREATER_EQUAL, // >= //structural operators TOY_TOKEN_OPERATOR_PAREN_LEFT, @@ -82,22 +82,22 @@ typedef enum Toy_TokenType { TOY_TOKEN_OPERATOR_BRACE_RIGHT, //other operators - TOY_TOKEN_OPERATOR_AND, // && - TOY_TOKEN_OPERATOR_OR, // || - TOY_TOKEN_OPERATOR_NEGATE, // ! - TOY_TOKEN_OPERATOR_QUESTION, // ? - TOY_TOKEN_OPERATOR_COLON, // : + TOY_TOKEN_OPERATOR_AND, // && + TOY_TOKEN_OPERATOR_OR, // || + TOY_TOKEN_OPERATOR_NEGATE, // ! (prefix) + TOY_TOKEN_OPERATOR_QUESTION, // ? + TOY_TOKEN_OPERATOR_COLON, // : - TOY_TOKEN_OPERATOR_SEMICOLON, // ; - TOY_TOKEN_OPERATOR_COMMA, // , + TOY_TOKEN_OPERATOR_SEMICOLON, // ; + TOY_TOKEN_OPERATOR_COMMA, // , - TOY_TOKEN_OPERATOR_DOT, // . - TOY_TOKEN_OPERATOR_CONCAT, // .. - TOY_TOKEN_OPERATOR_REST, // ... + TOY_TOKEN_OPERATOR_DOT, // . + TOY_TOKEN_OPERATOR_CONCAT, // .. + TOY_TOKEN_OPERATOR_REST, // ... //unused operators - TOY_TOKEN_OPERATOR_AMPERSAND, // & - TOY_TOKEN_OPERATOR_PIPE, // | + TOY_TOKEN_OPERATOR_AMPERSAND, // & + TOY_TOKEN_OPERATOR_PIPE, // | //meta tokens TOY_TOKEN_ERROR,