From 6fa224fa7b445ec1e8169a6ebf8afdfa5731b79c Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 18 Feb 2023 16:47:38 +0000 Subject: [PATCH] Hooks can't be dict keys, tweaked Toy_readFile --- repl/lib_runner.c | 2 +- repl/repl_main.c | 4 ++-- repl/repl_tools.c | 10 +++++----- repl/repl_tools.h | 2 +- source/toy_literal_array.h | 2 ++ source/toy_literal_dictionary.c | 6 +++--- test/test_call_from_host.c | 2 +- test/test_compiler.c | 2 +- test/test_interpreter.c | 2 +- test/test_libraries.c | 4 ++-- test/test_mustfail.c | 2 +- test/test_opaque_data_type.c | 2 +- test/test_parser.c | 2 +- 13 files changed, 22 insertions(+), 20 deletions(-) diff --git a/repl/lib_runner.c b/repl/lib_runner.c index 6089781..49f177a 100644 --- a/repl/lib_runner.c +++ b/repl/lib_runner.c @@ -49,7 +49,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu //load and compile the bytecode size_t fileSize = 0; - const char* source = Toy_readFile(filePath, &fileSize); + const char* source = (const char*)Toy_readFile(filePath, &fileSize); if (!source) { interpreter->errorOutput("Failed to load source file\n"); diff --git a/repl/repl_main.c b/repl/repl_main.c index dc8d002..2f601b2 100644 --- a/repl/repl_main.c +++ b/repl/repl_main.c @@ -180,7 +180,7 @@ int main(int argc, const char* argv[]) { //compile and save size_t size = 0; - const char* source = Toy_readFile(Toy_commandLine.compilefile, &size); + const char* source = (const char*)Toy_readFile(Toy_commandLine.compilefile, &size); if (!source) { return 1; } @@ -220,7 +220,7 @@ int main(int argc, const char* argv[]) { } size_t size; - initialSource = Toy_readFile(Toy_commandLine.initialfile, &size); + initialSource = (const char*)Toy_readFile(Toy_commandLine.initialfile, &size); } repl(initialSource); diff --git a/repl/repl_tools.c b/repl/repl_tools.c index 2d13715..0b46080 100644 --- a/repl/repl_tools.c +++ b/repl/repl_tools.c @@ -14,7 +14,7 @@ #include //IO functions -const char* Toy_readFile(const char* path, size_t* fileSize) { +const unsigned char* Toy_readFile(const char* path, size_t* fileSize) { FILE* file = fopen(path, "rb"); if (file == NULL) { @@ -26,14 +26,14 @@ const char* Toy_readFile(const char* path, size_t* fileSize) { *fileSize = ftell(file); rewind(file); - char* buffer = (char*)malloc(*fileSize + 1); + unsigned char* buffer = (unsigned char*)malloc(*fileSize + 1); if (buffer == NULL) { fprintf(stderr, TOY_CC_ERROR "Not enough memory to read \"%s\"\n" TOY_CC_RESET, path); return NULL; } - size_t bytesRead = fread(buffer, sizeof(char), *fileSize, file); + size_t bytesRead = fread(buffer, sizeof(unsigned char), *fileSize, file); buffer[*fileSize] = '\0'; //NOTE: fread doesn't append this @@ -120,7 +120,7 @@ void Toy_runBinary(const unsigned char* tb, size_t size) { void Toy_runBinaryFile(const char* fname) { size_t size = 0; //not used - const unsigned char* tb = (const unsigned char*)Toy_readFile(fname, &size); + const unsigned char* tb = Toy_readFile(fname, &size); if (!tb) { return; } @@ -140,7 +140,7 @@ void Toy_runSource(const char* source) { void Toy_runSourceFile(const char* fname) { size_t size = 0; //not used - const char* source = Toy_readFile(fname, &size); + const char* source = (const char*)Toy_readFile(fname, &size); if (!source) { return; } diff --git a/repl/repl_tools.h b/repl/repl_tools.h index 819f752..235cef8 100644 --- a/repl/repl_tools.h +++ b/repl/repl_tools.h @@ -2,7 +2,7 @@ #include "toy_common.h" -const char* Toy_readFile(const char* path, size_t* fileSize); +const unsigned char* Toy_readFile(const char* path, size_t* fileSize); int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size); const unsigned char* Toy_compileString(const char* source, size_t* size); diff --git a/source/toy_literal_array.h b/source/toy_literal_array.h index 8d98fbd..5e5004e 100644 --- a/source/toy_literal_array.h +++ b/source/toy_literal_array.h @@ -18,3 +18,5 @@ TOY_API bool Toy_setLiteralArray(Toy_LiteralArray* array, Toy_Literal index, Toy TOY_API Toy_Literal Toy_getLiteralArray(Toy_LiteralArray* array, Toy_Literal index); int Toy_findLiteralIndex(Toy_LiteralArray* array, Toy_Literal literal); + +//TODO: add a function to get the capacity & count \ No newline at end of file diff --git a/source/toy_literal_dictionary.c b/source/toy_literal_dictionary.c index 0dd7bdf..46b5de7 100644 --- a/source/toy_literal_dictionary.c +++ b/source/toy_literal_dictionary.c @@ -141,7 +141,7 @@ void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key } //BUGFIX: Can't hash a function - if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) { + if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) { fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (set)\n" TOY_CC_RESET); return; } @@ -166,7 +166,7 @@ Toy_Literal Toy_getLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Lite } //BUGFIX: Can't hash a function - if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) { + if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) { fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (get)\n" TOY_CC_RESET); return TOY_TO_NULL_LITERAL; } @@ -193,7 +193,7 @@ void Toy_removeLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal } //BUGFIX: Can't hash a function - if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key)) { + if (TOY_IS_FUNCTION(key) || TOY_IS_FUNCTION_NATIVE(key) || TOY_IS_FUNCTION_HOOK(key)) { fprintf(stderr, TOY_CC_ERROR "Dictionaries can't have function keys (remove)\n" TOY_CC_RESET); return; } diff --git a/test/test_call_from_host.c b/test/test_call_from_host.c index d693339..ed2da8f 100644 --- a/test/test_call_from_host.c +++ b/test/test_call_from_host.c @@ -27,7 +27,7 @@ void error(char* msg) { int main() { { size_t size = 0; - const char* source = Toy_readFile("scripts/call-from-host.toy", &size); + const char* source = (const char*)Toy_readFile("scripts/call-from-host.toy", &size); const unsigned char* tb = Toy_compileString(source, &size); free((void*)source); diff --git a/test/test_compiler.c b/test/test_compiler.c index c3c3b80..397c542 100644 --- a/test/test_compiler.c +++ b/test/test_compiler.c @@ -52,7 +52,7 @@ int main() { { //source size_t sourceLength = 0; - const char* source = Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength); + const char* source = (const char*)Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength); //test basic compilation & collation Toy_Lexer lexer; diff --git a/test/test_interpreter.c b/test/test_interpreter.c index 12c3ab5..da7a007 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -53,7 +53,7 @@ void runSourceCustom(const char* source) { void runSourceFileCustom(const char* fname) { size_t size = 0; //not used - const char* source = Toy_readFile(fname, &size); + const char* source = (const char*)Toy_readFile(fname, &size); runSourceCustom(source); free((void*)source); } diff --git a/test/test_libraries.c b/test/test_libraries.c index 9b8cbef..d850358 100644 --- a/test/test_libraries.c +++ b/test/test_libraries.c @@ -105,7 +105,7 @@ int main() { //compile the source size_t size = 0; - const char* source = Toy_readFile(fname, &size); + const char* source = (const char*)Toy_readFile(fname, &size); if (!source) { printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname); failedAsserts++; @@ -140,7 +140,7 @@ int main() { //compile the source size_t size = 0; - const char* source = Toy_readFile(fname, &size); + const char* source = (const char*)Toy_readFile(fname, &size); if (!source) { printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname); failedAsserts++; diff --git a/test/test_mustfail.c b/test/test_mustfail.c index 5952bc9..8da55d0 100644 --- a/test/test_mustfail.c +++ b/test/test_mustfail.c @@ -84,7 +84,7 @@ void runSourceCustom(const char* source) { void runSourceFileCustom(const char* fname) { size_t size = 0; //not used - const char* source = Toy_readFile(fname, &size); + const char* source = (const char*)Toy_readFile(fname, &size); runSourceCustom(source); free((void*)source); } diff --git a/test/test_opaque_data_type.c b/test/test_opaque_data_type.c index ab8a299..04c0204 100644 --- a/test/test_opaque_data_type.c +++ b/test/test_opaque_data_type.c @@ -68,7 +68,7 @@ static int consume(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { int main() { { size_t size = 0; - const char* source = Toy_readFile("scripts/opaque-data-type.toy", &size); + const char* source = (const char*)Toy_readFile("scripts/opaque-data-type.toy", &size); const unsigned char* tb = Toy_compileString(source, &size); free((void*)source); diff --git a/test/test_parser.c b/test/test_parser.c index ccaee6d..4dafbd4 100644 --- a/test/test_parser.c +++ b/test/test_parser.c @@ -58,7 +58,7 @@ int main() { { //get the source file size_t size = 0; - const char* source = Toy_readFile("scripts/parser_sample_code.toy", &size); + const char* source = (const char*)Toy_readFile("scripts/parser_sample_code.toy", &size); //test parsing a chunk of junk (valgrind will find leaks) Toy_Lexer lexer;