Added MSVC build support, likely broke tests

This commit is contained in:
2023-02-11 00:49:21 +00:00
parent be4cbf1ad6
commit 457014d577
18 changed files with 432 additions and 148 deletions

View File

@@ -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--) {

View File

@@ -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);

View File

@@ -3,7 +3,6 @@
#include "toy_memory.h"
#include <time.h>
#include <sys/time.h>
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;

View File

@@ -4,45 +4,8 @@
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
//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);

View File

@@ -16,13 +16,14 @@
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@@ -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);
}