From 457014d577c5f1e323b5053d695191f785bff6d3 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 11 Feb 2023 00:49:21 +0000 Subject: [PATCH] Added MSVC build support, likely broke tests --- .gitignore | 7 +- Repl.vcxproj | 134 +++++++++++++++++++++++++++++++++++ Toy.vcxproj | 147 +++++++++++++++++++++++++++++++++++++++ Toylang.sln | 44 ++++++++++++ repl/lib_compound.c | 16 ++--- repl/lib_runner.c | 10 +-- repl/lib_standard.c | 5 +- repl/lib_timer.c | 126 +++++++++++---------------------- repl/repl_main.c | 9 +-- repl/repl_tools.c | 4 +- source/toy_common.c | 4 -- source/toy_common.h | 24 ++++--- source/toy_interpreter.c | 2 +- source/toy_interpreter.h | 2 +- source/toy_lexer.h | 6 +- source/toy_memory.h | 2 +- source/toy_refstring.h | 22 +++--- source/toy_scope.h | 16 ++--- 18 files changed, 432 insertions(+), 148 deletions(-) create mode 100644 Repl.vcxproj create mode 100644 Toy.vcxproj create mode 100644 Toylang.sln diff --git a/.gitignore b/.gitignore index fff098f..dccf75b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ #Editor generated files -*.sln -*.vcproj *.suo *.ncb *.user @@ -13,7 +11,9 @@ Out/ release/ debug/ out/ +bin/ .cache/ +.vs/ #Project generated files *.db @@ -22,9 +22,10 @@ out/ *.exe *.meta *.log -out +*.out *.stackdump *.tb +*.filters #Shell files *.bat diff --git a/Repl.vcxproj b/Repl.vcxproj new file mode 100644 index 0000000..6c2e7e4 --- /dev/null +++ b/Repl.vcxproj @@ -0,0 +1,134 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76} + Win32Proj + 10.0 + + + + Application + true + v143 + + + Application + false + v143 + + + Application + true + v143 + + + Application + false + v143 + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + $(SolutionDir)bin\$(Configuration) + + + + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDebugDLL + Level3 + ProgramDatabase + Disabled + + + MachineX86 + true + Console + + + + + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + + + MachineX86 + true + Console + true + true + + + + + Toy.lib;%(AdditionalDependencies) + $(SolutionDir)bin\$(Configuration) + + + C:\Users\kayne\Desktop\Toy\source;%(AdditionalIncludeDirectories) + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Toy.vcxproj b/Toy.vcxproj new file mode 100644 index 0000000..677256e --- /dev/null +++ b/Toy.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + {26360002-CC2A-469A-9B28-BA0C1AF41657} + Win32Proj + 10.0 + + + + DynamicLibrary + true + v143 + + + DynamicLibrary + false + v143 + + + DynamicLibrary + true + v143 + + + Application + false + v143 + + + + + + + + + + + + + + + + + + + + + true + + + true + + + $(SolutionDir)bin\$(Configuration) + + + + WIN32;_DEBUG;_WINDOWS;_USRDLL;TOY_EXPORTS;TOY_EXPORT;%(PreprocessorDefinitions) + MultiThreadedDebugDLL + Level3 + ProgramDatabase + Disabled + + + MachineX86 + true + Windows + + + + + WIN32;NDEBUG;_WINDOWS;_USRDLL;TOY_EXPORTS;TOY_EXPORT;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + + + MachineX86 + true + Windows + true + true + + + + + TOY_EXPORT;%(PreprocessorDefinitions) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Toylang.sln b/Toylang.sln new file mode 100644 index 0000000..95e6d86 --- /dev/null +++ b/Toylang.sln @@ -0,0 +1,44 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33213.308 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Toy", "Toy.vcxproj", "{26360002-CC2A-469A-9B28-BA0C1AF41657}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Repl", "Repl.vcxproj", "{97F823E5-3AB8-47EF-B142-C15DD7CADF76}" + ProjectSection(ProjectDependencies) = postProject + {26360002-CC2A-469A-9B28-BA0C1AF41657} = {26360002-CC2A-469A-9B28-BA0C1AF41657} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Debug|x64.ActiveCfg = Debug|x64 + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Debug|x64.Build.0 = Debug|x64 + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Debug|x86.ActiveCfg = Debug|Win32 + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Debug|x86.Build.0 = Debug|Win32 + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Release|x64.ActiveCfg = Release|x64 + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Release|x64.Build.0 = Release|x64 + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Release|x86.ActiveCfg = Release|Win32 + {26360002-CC2A-469A-9B28-BA0C1AF41657}.Release|x86.Build.0 = Release|Win32 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Debug|x64.ActiveCfg = Debug|x64 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Debug|x64.Build.0 = Debug|x64 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Debug|x86.ActiveCfg = Debug|Win32 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Debug|x86.Build.0 = Debug|Win32 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Release|x64.ActiveCfg = Release|x64 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Release|x64.Build.0 = Release|x64 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Release|x86.ActiveCfg = Release|Win32 + {97F823E5-3AB8-47EF-B142-C15DD7CADF76}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7089F1AD-8EC0-4F27-AFD1-5FD43D91AABC} + EndGlobalSection +EndGlobal diff --git a/repl/lib_compound.c b/repl/lib_compound.c index 9ddba6b..895bcba 100644 --- a/repl/lib_compound.c +++ b/repl/lib_compound.c @@ -83,7 +83,7 @@ static int nativeConcat(Toy_Interpreter* interpreter, Toy_LiteralArray* argument } //get the combined length for the new string - int length = TOY_AS_STRING(selfLiteral)->length + TOY_AS_STRING(otherLiteral)->length + 1; + size_t length = TOY_AS_STRING(selfLiteral)->length + TOY_AS_STRING(otherLiteral)->length + 1; if (length > TOY_MAX_STRING_LENGTH) { interpreter->errorOutput("Can't concatenate these strings, result is too long (error found in _concat)\n"); @@ -1136,8 +1136,8 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral); //allocate space for the new string - int bufferBegin = 0; - int bufferEnd = Toy_lengthRefString(selfRefString); + size_t bufferBegin = 0; + size_t bufferEnd = Toy_lengthRefString(selfRefString); //for each character in self, check it against each character in trimChars - on a fail, go to end for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) { @@ -1163,7 +1163,7 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) } //again, from the back - for (int i = Toy_lengthRefString(selfRefString); i >= 0; i--) { + for (int i = (int)Toy_lengthRefString(selfRefString); i >= 0; i--) { int trimIndex = 0; while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) { @@ -1247,8 +1247,8 @@ static int nativeTrimBegin(Toy_Interpreter* interpreter, Toy_LiteralArray* argum Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral); //allocate space for the new string - int bufferBegin = 0; - int bufferEnd = Toy_lengthRefString(selfRefString); + size_t bufferBegin = 0; + size_t bufferEnd = Toy_lengthRefString(selfRefString); //for each character in self, check it against each character in trimChars - on a fail, go to end for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) { @@ -1335,8 +1335,8 @@ static int nativeTrimEnd(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral); //allocate space for the new string - int bufferBegin = 0; - int bufferEnd = Toy_lengthRefString(selfRefString); + size_t bufferBegin = 0; + size_t bufferEnd = Toy_lengthRefString(selfRefString); //again, from the back for (int i = (int)Toy_lengthRefString(selfRefString); i >= 0; i--) { diff --git a/repl/lib_runner.c b/repl/lib_runner.c index 186b6d3..e4c73fe 100644 --- a/repl/lib_runner.c +++ b/repl/lib_runner.c @@ -44,7 +44,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu //use raw types - easier const char* filePath = Toy_toCString(TOY_AS_STRING(filePathLiteral)); - int filePathLength = Toy_lengthRefString(TOY_AS_STRING(filePathLiteral)); + size_t filePathLength = Toy_lengthRefString(TOY_AS_STRING(filePathLiteral)); //load and compile the bytecode size_t fileSize = 0; @@ -138,7 +138,7 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr //get the final real file path (concat) TODO: move this concat to refstring library Toy_RefString* realDrive = Toy_copyRefString(TOY_AS_STRING(realDriveLiteral)); - int realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path); + size_t realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path); char* filePath = TOY_ALLOCATE(char, realLength + 1); //+1 for null snprintf(filePath, realLength, "%s%s", Toy_toCString(realDrive), Toy_toCString(path)); @@ -159,7 +159,7 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr } //check for break-out attempts - for (int i = 0; i < realLength - 1; i++) { + for (size_t i = 0; i < realLength - 1; i++) { if (filePath[i] == '.' && filePath[i + 1] == '.') { interpreter->errorOutput("Parent directory access not allowed\n"); TOY_FREE_ARRAY(char, filePath, realLength); @@ -617,7 +617,7 @@ Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* dr //get the final real file path (concat) TODO: move this concat to refstring library Toy_RefString* realDrive = Toy_copyRefString(TOY_AS_STRING(realDriveLiteral)); - int realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path); + size_t realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path); char* filePath = TOY_ALLOCATE(char, realLength + 1); //+1 for null snprintf(filePath, realLength, "%s%s", Toy_toCString(realDrive), Toy_toCString(path)); @@ -630,7 +630,7 @@ Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* dr Toy_deleteRefString(drivePath); //check for break-out attempts - for (int i = 0; i < realLength - 1; i++) { + for (size_t i = 0; i < realLength - 1; i++) { if (filePath[i] == '.' && filePath[i + 1] == '.') { interpreter->errorOutput("Parent directory access not allowed\n"); TOY_FREE_ARRAY(char, filePath, realLength + 1); diff --git a/repl/lib_standard.c b/repl/lib_standard.c index 33d612f..01601db 100644 --- a/repl/lib_standard.c +++ b/repl/lib_standard.c @@ -3,7 +3,6 @@ #include "toy_memory.h" #include -#include static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { //no arguments @@ -18,7 +17,7 @@ static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments char* timestr = asctime(timeinfo); //push to the stack - int len = strlen(timestr) - 1; //-1 for the newline + size_t len = strlen(timestr) - 1; //-1 for the newline Toy_Literal timeLiteral = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(timestr, len)); //push to the stack @@ -32,7 +31,7 @@ static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments //call the hook typedef struct Natives { - char* name; + const char* name; Toy_NativeFn fn; } Natives; diff --git a/repl/lib_timer.c b/repl/lib_timer.c index d05011e..e79b723 100644 --- a/repl/lib_timer.c +++ b/repl/lib_timer.c @@ -4,45 +4,8 @@ #include #include -#include -//GOD DAMN IT: https://stackoverflow.com/questions/15846762/timeval-subtract-explanation -static int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) { - //normallize - if (x->tv_usec > 999999) { - x->tv_sec += x->tv_usec / 1000000; - x->tv_usec %= 1000000; - } - - if (y->tv_usec > 999999) { - y->tv_sec += y->tv_usec / 1000000; - y->tv_usec %= 1000000; - } - - //calc - result->tv_sec = x->tv_sec - y->tv_sec; - - if ((result->tv_usec = x->tv_usec - y->tv_usec) < 0) { - if (result->tv_sec != 0) { //only works far from 0 - result->tv_usec += 1000000; - result->tv_sec--; // borrow - } - } - - return result->tv_sec < 0 || (result->tv_sec == 0 && result->tv_usec < 0); -} - -//god damn it -static struct timeval* diff(struct timeval* lhs, struct timeval* rhs) { - struct timeval* d = TOY_ALLOCATE(struct timeval, 1); - - //I gave up, copied from SO - timeval_subtract(d, rhs, lhs); - - return d; -} - -//callbacks +//natives static int nativeStartTimer(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { //no arguments if (arguments->count != 0) { @@ -50,15 +13,15 @@ static int nativeStartTimer(Toy_Interpreter* interpreter, Toy_LiteralArray* argu return -1; } - //get the timeinfo from C - struct timeval* timeinfo = TOY_ALLOCATE(struct timeval, 1); - gettimeofday(timeinfo, NULL); + //get the time from C (this is dumb) + clock_t* ptr = TOY_ALLOCATE(clock_t, 1); + *ptr = clock(); //wrap in an opaque literal for Toy - Toy_Literal timeLiteral = TOY_TO_OPAQUE_LITERAL(timeinfo, -1); - Toy_pushLiteralArray(&interpreter->stack, timeLiteral); + Toy_Literal timerLiteral = TOY_TO_OPAQUE_LITERAL(ptr, -1); //TODO: sort out the tags + Toy_pushLiteralArray(&interpreter->stack, timerLiteral); - Toy_freeLiteral(timeLiteral); + Toy_freeLiteral(timerLiteral); return 1; } @@ -70,33 +33,33 @@ static int nativeStopTimer(Toy_Interpreter* interpreter, Toy_LiteralArray* argum return -1; } - //get the timeinfo from C - struct timeval timerStop; - gettimeofday(&timerStop, NULL); + clock_t stop = clock(); //unwrap the opaque literal - Toy_Literal timeLiteral = Toy_popLiteralArray(arguments); + Toy_Literal timerLiteral = Toy_popLiteralArray(arguments); - Toy_Literal timeLiteralIdn = timeLiteral; - if (TOY_IS_IDENTIFIER(timeLiteral) && Toy_parseIdentifierToValue(interpreter, &timeLiteral)) { - Toy_freeLiteral(timeLiteralIdn); + Toy_Literal timerLiteralIdn = timerLiteral; + if (TOY_IS_IDENTIFIER(timerLiteral) && Toy_parseIdentifierToValue(interpreter, &timerLiteral)) { + Toy_freeLiteral(timerLiteralIdn); } - if (!TOY_IS_OPAQUE(timeLiteral)) { + if (!TOY_IS_OPAQUE(timerLiteral)) { interpreter->errorOutput("Incorrect argument type passed to _stopTimer\n"); - Toy_freeLiteral(timeLiteral); + Toy_freeLiteral(timerLiteral); return -1; } - struct timeval* timerStart = TOY_AS_OPAQUE(timeLiteral); + clock_t* ptr = TOY_AS_OPAQUE(timerLiteral); //determine the difference, and wrap it - struct timeval* d = diff(timerStart, &timerStop); - Toy_Literal diffLiteral = TOY_TO_OPAQUE_LITERAL(d, -1); + clock_t* diff = TOY_ALLOCATE(clock_t, 1); + *diff = *ptr - stop; + Toy_Literal diffLiteral = TOY_TO_OPAQUE_LITERAL(diff, -1); + Toy_pushLiteralArray(&interpreter->stack, diffLiteral); //cleanup - Toy_freeLiteral(timeLiteral); + Toy_freeLiteral(timerLiteral); Toy_freeLiteral(diffLiteral); return 1; @@ -137,13 +100,12 @@ static int nativeCreateTimer(Toy_Interpreter* interpreter, Toy_LiteralArray* arg return -1; } - //get the timeinfo from toy - struct timeval* timeinfo = TOY_ALLOCATE(struct timeval, 1); - timeinfo->tv_sec = TOY_AS_INTEGER(secondLiteral); - timeinfo->tv_usec = TOY_AS_INTEGER(microsecondLiteral); + //determine the clocks per whatever + clock_t* timer = TOY_ALLOCATE(clock_t, 1); + *timer = TOY_AS_INTEGER(secondLiteral) * CLOCKS_PER_SEC + TOY_AS_INTEGER(microsecondLiteral); //wrap in an opaque literal for Toy - Toy_Literal timeLiteral = TOY_TO_OPAQUE_LITERAL(timeinfo, -1); + Toy_Literal timeLiteral = TOY_TO_OPAQUE_LITERAL(timer, -1); Toy_pushLiteralArray(&interpreter->stack, timeLiteral); Toy_freeLiteral(timeLiteral); @@ -174,10 +136,10 @@ static int nativeGetTimerSeconds(Toy_Interpreter* interpreter, Toy_LiteralArray* return -1; } - struct timeval* timer = TOY_AS_OPAQUE(timeLiteral); + clock_t* timer = TOY_AS_OPAQUE(timeLiteral); //create the result literal - Toy_Literal result = TOY_TO_INTEGER_LITERAL(timer->tv_sec); + Toy_Literal result = TOY_TO_INTEGER_LITERAL(*timer / CLOCKS_PER_SEC); Toy_pushLiteralArray(&interpreter->stack, result); //cleanup @@ -208,10 +170,10 @@ static int nativeGetTimerMicroseconds(Toy_Interpreter* interpreter, Toy_LiteralA return -1; } - struct timeval* timer = TOY_AS_OPAQUE(timeLiteral); + clock_t* timer = TOY_AS_OPAQUE(timeLiteral); //create the result literal - Toy_Literal result = TOY_TO_INTEGER_LITERAL(timer->tv_usec); + Toy_Literal result = TOY_TO_INTEGER_LITERAL(*timer % CLOCKS_PER_SEC); Toy_pushLiteralArray(&interpreter->stack, result); //cleanup @@ -249,12 +211,14 @@ static int nativeCompareTimer(Toy_Interpreter* interpreter, Toy_LiteralArray* ar return -1; } - struct timeval* lhsTimer = TOY_AS_OPAQUE(lhsLiteral); - struct timeval* rhsTimer = TOY_AS_OPAQUE(rhsLiteral); + clock_t* lhsTimer = TOY_AS_OPAQUE(lhsLiteral); + clock_t* rhsTimer = TOY_AS_OPAQUE(rhsLiteral); //determine the difference, and wrap it - struct timeval* d = diff(lhsTimer, rhsTimer); - Toy_Literal diffLiteral = TOY_TO_OPAQUE_LITERAL(d, -1); + clock_t* diff = TOY_ALLOCATE(clock_t, 1); + *diff = *lhsTimer - *rhsTimer; + + Toy_Literal diffLiteral = TOY_TO_OPAQUE_LITERAL(diff, -1); Toy_pushLiteralArray(&interpreter->stack, diffLiteral); //cleanup @@ -286,20 +250,12 @@ static int nativeTimerToString(Toy_Interpreter* interpreter, Toy_LiteralArray* a return -1; } - struct timeval* timer = TOY_AS_OPAQUE(timeLiteral); + clock_t* timer = TOY_AS_OPAQUE(timeLiteral); //create the string literal - Toy_Literal resultLiteral = TOY_TO_NULL_LITERAL; - if (timer->tv_sec == 0 && timer->tv_usec < 0) { //special case, for when the negative sign is encoded in the usec - char buffer[128]; - snprintf(buffer, 128, "-%ld.%06ld", timer->tv_sec, -timer->tv_usec); - resultLiteral = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(buffer, strlen(buffer))); - } - else { //normal case - char buffer[128]; - snprintf(buffer, 128, "%ld.%06ld", timer->tv_sec, timer->tv_usec); - resultLiteral = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(buffer, strlen(buffer))); - } + char buffer[128]; + snprintf(buffer, 128, "%ld.%06ld", *timer / CLOCKS_PER_SEC, *timer % CLOCKS_PER_SEC); + Toy_Literal resultLiteral = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(buffer, strlen(buffer))); Toy_pushLiteralArray(&interpreter->stack, resultLiteral); @@ -317,7 +273,7 @@ static int nativeDestroyTimer(Toy_Interpreter* interpreter, Toy_LiteralArray* ar return -1; } - //unwrap in an opaque literal + //unwrap in an opaque literal Toy_Literal timeLiteral = Toy_popLiteralArray(arguments); Toy_Literal timeLiteralIdn = timeLiteral; @@ -331,9 +287,9 @@ static int nativeDestroyTimer(Toy_Interpreter* interpreter, Toy_LiteralArray* ar return -1; } - struct timeval* timer = TOY_AS_OPAQUE(timeLiteral); + clock_t* timer = TOY_AS_OPAQUE(timeLiteral); - TOY_FREE(struct timeval, timer); + TOY_FREE(clock_t, timer); Toy_freeLiteral(timeLiteral); diff --git a/repl/repl_main.c b/repl/repl_main.c index 5f21cd0..e0dea6c 100644 --- a/repl/repl_main.c +++ b/repl/repl_main.c @@ -16,13 +16,14 @@ #include #include +#define INPUT_BUFFER_SIZE 2048 + void repl() { //repl does it's own thing for now bool error = false; - const int size = 2048; - char input[size]; - memset(input, 0, size); + char input[INPUT_BUFFER_SIZE]; + memset(input, 0, INPUT_BUFFER_SIZE); Toy_Interpreter interpreter; //persist the interpreter for the scopes Toy_initInterpreter(&interpreter); @@ -38,7 +39,7 @@ void repl() { printf("> "); //handle EOF for exits - if (!fgets(input, size, stdin)) { + if (!fgets(input, INPUT_BUFFER_SIZE, stdin)) { break; } diff --git a/repl/repl_tools.c b/repl/repl_tools.c index 7018e93..543ec18 100644 --- a/repl/repl_tools.c +++ b/repl/repl_tools.c @@ -57,7 +57,7 @@ int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size) { return -1; } - int written = fwrite(bytes, size, 1, file); + size_t written = fwrite(bytes, size, 1, file); if (written != 1) { fprintf(stderr, TOY_CC_ERROR "Could not write file \"%s\"\n" TOY_CC_RESET, path); @@ -118,7 +118,7 @@ void Toy_runBinary(const unsigned char* tb, size_t size) { Toy_injectNativeHook(&interpreter, "timer", Toy_hookTimer); Toy_injectNativeHook(&interpreter, "runner", Toy_hookRunner); - Toy_runInterpreter(&interpreter, tb, size); + Toy_runInterpreter(&interpreter, tb, (int)size); Toy_freeInterpreter(&interpreter); } diff --git a/source/toy_common.c b/source/toy_common.c index 3405553..6f6a07e 100644 --- a/source/toy_common.c +++ b/source/toy_common.c @@ -15,8 +15,6 @@ STATIC_ASSERT(sizeof(unsigned char) == 1); STATIC_ASSERT(sizeof(unsigned short) == 2); STATIC_ASSERT(sizeof(unsigned int) == 4); -#ifndef TOY_EXPORT - //declare the singleton Toy_CommandLine Toy_commandLine; @@ -121,5 +119,3 @@ void Toy_copyrightCommandLine(int argc, const char* argv[]) { printf("2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n\n"); printf("3. This notice may not be removed or altered from any source distribution.\n\n"); } - -#endif diff --git a/source/toy_common.h b/source/toy_common.h index cc5905d..e93df0f 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -11,17 +11,22 @@ //platform-specific specifications #if defined(__linux__) + #define TOY_API extern -#elif defined(_WIN32) || defined(WIN32) -#define TOY_API +#elif defined(_WIN32) || defined(_MSC_VER) + +#ifndef TOY_EXPORT +#define TOY_API __declspec(dllimport) +#else +#define TOY_API __declspec(dllexport) +#endif #else -#define TOY_API +#define TOY_API extern #endif -#ifndef TOY_EXPORT //for processing the command line arguments typedef struct { bool error; @@ -35,11 +40,10 @@ typedef struct { bool verbose; } Toy_CommandLine; -extern Toy_CommandLine Toy_commandLine; +TOY_API Toy_CommandLine Toy_commandLine; -void Toy_initCommandLine(int argc, const char* argv[]); +TOY_API void Toy_initCommandLine(int argc, const char* argv[]); -void Toy_usageCommandLine(int argc, const char* argv[]); -void Toy_helpCommandLine(int argc, const char* argv[]); -void Toy_copyrightCommandLine(int argc, const char* argv[]); -#endif +TOY_API void Toy_usageCommandLine(int argc, const char* argv[]); +TOY_API void Toy_helpCommandLine(int argc, const char* argv[]); +TOY_API void Toy_copyrightCommandLine(int argc, const char* argv[]); diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index 3c21dff..824ac5f 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -2356,7 +2356,7 @@ void Toy_initInterpreter(Toy_Interpreter* interpreter) { Toy_resetInterpreter(interpreter); } -void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, int length) { +void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, size_t length) { //initialize here instead of initInterpreter() Toy_initLiteralArray(&interpreter->literalCache); interpreter->bytecode = NULL; diff --git a/source/toy_interpreter.h b/source/toy_interpreter.h index 53d0f4d..05ae64b 100644 --- a/source/toy_interpreter.h +++ b/source/toy_interpreter.h @@ -48,6 +48,6 @@ TOY_API void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn e //main access TOY_API void Toy_initInterpreter(Toy_Interpreter* interpreter); //start of program -TOY_API void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, int length); //run the code +TOY_API void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, size_t length); //run the code TOY_API void Toy_resetInterpreter(Toy_Interpreter* interpreter); //use this to reset the interpreter's environment between runs TOY_API void Toy_freeInterpreter(Toy_Interpreter* interpreter); //end of program diff --git a/source/toy_lexer.h b/source/toy_lexer.h index 606fd01..af361b3 100644 --- a/source/toy_lexer.h +++ b/source/toy_lexer.h @@ -21,9 +21,9 @@ typedef struct { } Toy_Token; TOY_API void Toy_initLexer(Toy_Lexer* lexer, const char* source); -Toy_Token Toy_scanLexer(Toy_Lexer* lexer); +TOY_API Toy_Token Toy_scanLexer(Toy_Lexer* lexer); //for debugging -void Toy_printToken(Toy_Token* token); +TOY_API void Toy_printToken(Toy_Token* token); -void Toy_private_setComments(Toy_Lexer* lexer, bool enabled); +TOY_API void Toy_private_setComments(Toy_Lexer* lexer, bool enabled); diff --git a/source/toy_memory.h b/source/toy_memory.h index c2f797d..8132b4e 100644 --- a/source/toy_memory.h +++ b/source/toy_memory.h @@ -11,7 +11,7 @@ #define TOY_FREE_ARRAY(type, pointer, oldCount) Toy_reallocate((type*)pointer, sizeof(type) * (oldCount), 0) //implementation details -void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize); +TOY_API void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize); //assign the memory allocator typedef void* (*Toy_MemoryAllocatorFn)(void* pointer, size_t oldSize, size_t newSize); diff --git a/source/toy_refstring.h b/source/toy_refstring.h index 9a4eba7..b0a97b6 100644 --- a/source/toy_refstring.h +++ b/source/toy_refstring.h @@ -3,6 +3,8 @@ #include #include +#include "toy_common.h" + //memory allocation hook typedef void* (*Toy_RefStringAllocatorFn)(void* pointer, size_t oldSize, size_t newSize); void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn); @@ -15,13 +17,13 @@ typedef struct Toy_RefString { } Toy_RefString; //API -Toy_RefString* Toy_createRefString(const char* cstring); -Toy_RefString* Toy_createRefStringLength(const char* cstring, size_t length); -void Toy_deleteRefString(Toy_RefString* refString); -int Toy_countRefString(Toy_RefString* refString); -size_t Toy_lengthRefString(Toy_RefString* refString); -Toy_RefString* Toy_copyRefString(Toy_RefString* refString); -Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString); -const char* Toy_toCString(Toy_RefString* refString); -bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs); -bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring); +TOY_API Toy_RefString* Toy_createRefString(const char* cstring); +TOY_API Toy_RefString* Toy_createRefStringLength(const char* cstring, size_t length); +TOY_API void Toy_deleteRefString(Toy_RefString* refString); +TOY_API int Toy_countRefString(Toy_RefString* refString); +TOY_API size_t Toy_lengthRefString(Toy_RefString* refString); +TOY_API Toy_RefString* Toy_copyRefString(Toy_RefString* refString); +TOY_API Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString); +TOY_API const char* Toy_toCString(Toy_RefString* refString); +TOY_API bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs); +TOY_API bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring); diff --git a/source/toy_scope.h b/source/toy_scope.h index 8407af1..1c0c683 100644 --- a/source/toy_scope.h +++ b/source/toy_scope.h @@ -10,16 +10,16 @@ typedef struct Toy_Scope { int references; //how many scopes point here } Toy_Scope; -Toy_Scope* Toy_pushScope(Toy_Scope* scope); -Toy_Scope* Toy_popScope(Toy_Scope* scope); -Toy_Scope* Toy_copyScope(Toy_Scope* original); +TOY_API Toy_Scope* Toy_pushScope(Toy_Scope* scope); +TOY_API Toy_Scope* Toy_popScope(Toy_Scope* scope); +TOY_API Toy_Scope* Toy_copyScope(Toy_Scope* original); //returns false if error -bool Toy_declareScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal type); -bool Toy_isDelcaredScopeVariable(Toy_Scope* scope, Toy_Literal key); +TOY_API bool Toy_declareScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal type); +TOY_API bool Toy_isDelcaredScopeVariable(Toy_Scope* scope, Toy_Literal key); //return false if undefined -bool Toy_setScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal value, bool constCheck); -bool Toy_getScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal* value); +TOY_API bool Toy_setScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal value, bool constCheck); +TOY_API bool Toy_getScopeVariable(Toy_Scope* scope, Toy_Literal key, Toy_Literal* value); -Toy_Literal Toy_getScopeType(Toy_Scope* scope, Toy_Literal key); +TOY_API Toy_Literal Toy_getScopeType(Toy_Scope* scope, Toy_Literal key);