Strengthened constness for cstrings and bytecode

This commit is contained in:
2023-02-10 08:52:38 +00:00
parent 76a0290290
commit ee226ea426
24 changed files with 138 additions and 143 deletions

View File

@@ -36,7 +36,6 @@ Run `make install-tools` to install a number of tools, including:
``` ```
import standard; //for a bunch of utility functions import standard; //for a bunch of utility functions
print "Hello world"; //"print" is a keyword print "Hello world"; //"print" is a keyword
var msg = "foobar"; //declare a variable like this var msg = "foobar"; //declare a variable like this

View File

@@ -506,13 +506,13 @@ static int nativeToLower(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
} }
Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral); Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral);
char* self = Toy_toCString(selfRefString); const char* self = Toy_toCString(selfRefString);
//allocate buffer space for the result //allocate buffer space for the result
char* result = TOY_ALLOCATE(char, Toy_lengthRefString(selfRefString) + 1); char* result = TOY_ALLOCATE(char, Toy_lengthRefString(selfRefString) + 1);
//set each new character //set each new character
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) { for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
result[i] = tolower(self[i]); result[i] = tolower(self[i]);
} }
result[Toy_lengthRefString(selfRefString)] = '\0'; //end the string result[Toy_lengthRefString(selfRefString)] = '\0'; //end the string
@@ -533,7 +533,7 @@ static int nativeToLower(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
static char* toStringUtilObject = NULL; static char* toStringUtilObject = NULL;
static void toStringUtil(const char* input) { static void toStringUtil(const char* input) {
int len = strlen(input) + 1; size_t len = strlen(input) + 1;
if (len > TOY_MAX_STRING_LENGTH) { if (len > TOY_MAX_STRING_LENGTH) {
len = TOY_MAX_STRING_LENGTH; //TODO: don't truncate len = TOY_MAX_STRING_LENGTH; //TODO: don't truncate
@@ -606,13 +606,13 @@ static int nativeToUpper(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
} }
Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral); Toy_RefString* selfRefString = TOY_AS_STRING(selfLiteral);
char* self = Toy_toCString(selfRefString); const char* self = Toy_toCString(selfRefString);
//allocate buffer space for the result //allocate buffer space for the result
char* result = TOY_ALLOCATE(char, Toy_lengthRefString(selfRefString) + 1); char* result = TOY_ALLOCATE(char, Toy_lengthRefString(selfRefString) + 1);
//set each new character //set each new character
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) { for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
result[i] = toupper(self[i]); result[i] = toupper(self[i]);
} }
result[Toy_lengthRefString(selfRefString)] = '\0'; //end the string result[Toy_lengthRefString(selfRefString)] = '\0'; //end the string
@@ -675,10 +675,10 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
int bufferEnd = Toy_lengthRefString(selfRefString); int bufferEnd = Toy_lengthRefString(selfRefString);
//for each character in self, check it against each character in trimChars - on a fail, go to end //for each character in self, check it against each character in trimChars - on a fail, go to end
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) { for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
int trimIndex = 0; int trimIndex = 0;
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) { while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
//there is a match - DON'T increment anymore //there is a match - DON'T increment anymore
if (Toy_toCString(selfRefString)[i] == Toy_toCString(trimCharsRefString)[trimIndex]) { if (Toy_toCString(selfRefString)[i] == Toy_toCString(trimCharsRefString)[trimIndex]) {
break; break;
@@ -688,7 +688,7 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
} }
//if the match is found, increment buffer begin //if the match is found, increment buffer begin
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) { if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
bufferBegin++; bufferBegin++;
continue; continue;
} }
@@ -701,7 +701,7 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
for (int i = Toy_lengthRefString(selfRefString); i >= 0; i--) { for (int i = Toy_lengthRefString(selfRefString); i >= 0; i--) {
int trimIndex = 0; int trimIndex = 0;
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) { while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
//there is a match - DON'T increment anymore //there is a match - DON'T increment anymore
if (Toy_toCString(selfRefString)[i-1] == Toy_toCString(trimCharsRefString)[trimIndex]) { if (Toy_toCString(selfRefString)[i-1] == Toy_toCString(trimCharsRefString)[trimIndex]) {
break; break;
@@ -711,7 +711,7 @@ static int nativeTrim(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
} }
//if the match is found, increment buffer begin //if the match is found, increment buffer begin
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) { if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
bufferEnd--; bufferEnd--;
continue; continue;
} }
@@ -786,10 +786,10 @@ static int nativeTrimBegin(Toy_Interpreter* interpreter, Toy_LiteralArray* argum
int bufferEnd = Toy_lengthRefString(selfRefString); int bufferEnd = Toy_lengthRefString(selfRefString);
//for each character in self, check it against each character in trimChars - on a fail, go to end //for each character in self, check it against each character in trimChars - on a fail, go to end
for (int i = 0; i < Toy_lengthRefString(selfRefString); i++) { for (int i = 0; i < (int)Toy_lengthRefString(selfRefString); i++) {
int trimIndex = 0; int trimIndex = 0;
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) { while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
//there is a match - DON'T increment anymore //there is a match - DON'T increment anymore
if (Toy_toCString(selfRefString)[i] == Toy_toCString(trimCharsRefString)[trimIndex]) { if (Toy_toCString(selfRefString)[i] == Toy_toCString(trimCharsRefString)[trimIndex]) {
break; break;
@@ -799,7 +799,7 @@ static int nativeTrimBegin(Toy_Interpreter* interpreter, Toy_LiteralArray* argum
} }
//if the match is found, increment buffer begin //if the match is found, increment buffer begin
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) { if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
bufferBegin++; bufferBegin++;
continue; continue;
} }
@@ -874,10 +874,10 @@ static int nativeTrimEnd(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
int bufferEnd = Toy_lengthRefString(selfRefString); int bufferEnd = Toy_lengthRefString(selfRefString);
//again, from the back //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; int trimIndex = 0;
while (trimIndex < Toy_lengthRefString(trimCharsRefString)) { while (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
//there is a match - DON'T increment anymore //there is a match - DON'T increment anymore
if (Toy_toCString(selfRefString)[i-1] == Toy_toCString(trimCharsRefString)[trimIndex]) { if (Toy_toCString(selfRefString)[i-1] == Toy_toCString(trimCharsRefString)[trimIndex]) {
break; break;
@@ -887,7 +887,7 @@ static int nativeTrimEnd(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
} }
//if the match is found, increment buffer begin //if the match is found, increment buffer begin
if (trimIndex < Toy_lengthRefString(trimCharsRefString)) { if (trimIndex < (int)Toy_lengthRefString(trimCharsRefString)) {
bufferEnd--; bufferEnd--;
continue; continue;
} }

View File

@@ -10,7 +10,7 @@
typedef struct Toy_Runner { typedef struct Toy_Runner {
Toy_Interpreter interpreter; Toy_Interpreter interpreter;
unsigned char* bytecode; const unsigned char* bytecode;
size_t size; size_t size;
bool dirty; bool dirty;
@@ -43,12 +43,12 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
Toy_freeLiteral(drivePathLiteral); Toy_freeLiteral(drivePathLiteral);
//use raw types - easier //use raw types - easier
char* filePath = Toy_toCString(TOY_AS_STRING(filePathLiteral)); const char* filePath = Toy_toCString(TOY_AS_STRING(filePathLiteral));
int filePathLength = Toy_lengthRefString(TOY_AS_STRING(filePathLiteral)); int filePathLength = Toy_lengthRefString(TOY_AS_STRING(filePathLiteral));
//load and compile the bytecode //load and compile the bytecode
size_t fileSize = 0; size_t fileSize = 0;
char* source = Toy_readFile(filePath, &fileSize); const char* source = Toy_readFile(filePath, &fileSize);
if (!source) { if (!source) {
interpreter->errorOutput("Failed to load source file\n"); interpreter->errorOutput("Failed to load source file\n");
@@ -56,7 +56,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
return -1; return -1;
} }
unsigned char* bytecode = Toy_compileString(source, &fileSize); const unsigned char* bytecode = Toy_compileString(source, &fileSize);
free((void*)source); free((void*)source);
if (!bytecode) { if (!bytecode) {
@@ -105,7 +105,7 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(drivePathLiteral)); Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(drivePathLiteral));
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library //get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
int driveLength = 0; size_t driveLength = 0;
while (Toy_toCString(drivePath)[driveLength] != ':') { while (Toy_toCString(drivePath)[driveLength] != ':') {
if (driveLength >= Toy_lengthRefString(drivePath)) { if (driveLength >= Toy_lengthRefString(drivePath)) {
interpreter->errorOutput("Incorrect drive path format given to loadScriptBytecode\n"); interpreter->errorOutput("Incorrect drive path format given to loadScriptBytecode\n");
@@ -492,7 +492,7 @@ static int nativeCheckScriptDirty(Toy_Interpreter* interpreter, Toy_LiteralArray
//call the hook //call the hook
typedef struct Natives { typedef struct Natives {
char* name; const char* name;
Toy_NativeFn fn; Toy_NativeFn fn;
} Natives; } Natives;
@@ -585,7 +585,7 @@ Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* dr
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(*drivePathLiteral)); Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(*drivePathLiteral));
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library //get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
int driveLength = 0; size_t driveLength = 0;
while (Toy_toCString(drivePath)[driveLength] != ':') { while (Toy_toCString(drivePath)[driveLength] != ':') {
if (driveLength >= Toy_lengthRefString(drivePath)) { if (driveLength >= Toy_lengthRefString(drivePath)) {
interpreter->errorOutput("Incorrect drive path format given to Toy_getFilePathLiteral\n"); interpreter->errorOutput("Incorrect drive path format given to Toy_getFilePathLiteral\n");

View File

@@ -151,11 +151,11 @@ int main(int argc, const char* argv[]) {
//compile source file //compile source file
if (Toy_commandLine.compilefile && Toy_commandLine.outfile) { if (Toy_commandLine.compilefile && Toy_commandLine.outfile) {
size_t size = 0; size_t size = 0;
char* source = Toy_readFile(Toy_commandLine.compilefile, &size); const char* source = Toy_readFile(Toy_commandLine.compilefile, &size);
if (!source) { if (!source) {
return 1; return 1;
} }
unsigned char* tb = Toy_compileString(source, &size); const unsigned char* tb = Toy_compileString(source, &size);
if (!tb) { if (!tb) {
return 1; return 1;
} }

View File

@@ -16,7 +16,7 @@
#include <stdlib.h> #include <stdlib.h>
//IO functions //IO functions
char* Toy_readFile(char* path, size_t* fileSize) { const char* Toy_readFile(const char* path, size_t* fileSize) {
FILE* file = fopen(path, "rb"); FILE* file = fopen(path, "rb");
if (file == NULL) { if (file == NULL) {
@@ -49,7 +49,7 @@ char* Toy_readFile(char* path, size_t* fileSize) {
return buffer; return buffer;
} }
int Toy_writeFile(char* path, unsigned char* bytes, size_t size) { int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size) {
FILE* file = fopen(path, "wb"); FILE* file = fopen(path, "wb");
if (file == NULL) { if (file == NULL) {
@@ -70,7 +70,7 @@ int Toy_writeFile(char* path, unsigned char* bytes, size_t size) {
} }
//repl functions //repl functions
unsigned char* Toy_compileString(char* source, size_t* size) { const unsigned char* Toy_compileString(const char* source, size_t* size) {
Toy_Lexer lexer; Toy_Lexer lexer;
Toy_Parser parser; Toy_Parser parser;
Toy_Compiler compiler; Toy_Compiler compiler;
@@ -96,7 +96,7 @@ unsigned char* Toy_compileString(char* source, size_t* size) {
} }
//get the bytecode dump //get the bytecode dump
unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size)); const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
//cleanup //cleanup
Toy_freeCompiler(&compiler); Toy_freeCompiler(&compiler);
@@ -107,7 +107,7 @@ unsigned char* Toy_compileString(char* source, size_t* size) {
return tb; return tb;
} }
void Toy_runBinary(unsigned char* tb, size_t size) { void Toy_runBinary(const unsigned char* tb, size_t size) {
Toy_Interpreter interpreter; Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter); Toy_initInterpreter(&interpreter);
@@ -122,9 +122,9 @@ void Toy_runBinary(unsigned char* tb, size_t size) {
Toy_freeInterpreter(&interpreter); Toy_freeInterpreter(&interpreter);
} }
void Toy_runBinaryFile(char* fname) { void Toy_runBinaryFile(const char* fname) {
size_t size = 0; //not used size_t size = 0; //not used
unsigned char* tb = (unsigned char*)Toy_readFile(fname, &size); const unsigned char* tb = (const unsigned char*)Toy_readFile(fname, &size);
if (!tb) { if (!tb) {
return; return;
} }
@@ -132,9 +132,9 @@ void Toy_runBinaryFile(char* fname) {
//interpreter takes ownership of the binary data //interpreter takes ownership of the binary data
} }
void Toy_runSource(char* source) { void Toy_runSource(const char* source) {
size_t size = 0; size_t size = 0;
unsigned char* tb = Toy_compileString(source, &size); const unsigned char* tb = Toy_compileString(source, &size);
if (!tb) { if (!tb) {
return; return;
} }
@@ -142,9 +142,9 @@ void Toy_runSource(char* source) {
Toy_runBinary(tb, size); Toy_runBinary(tb, size);
} }
void Toy_runSourceFile(char* fname) { void Toy_runSourceFile(const char* fname) {
size_t size = 0; //not used size_t size = 0; //not used
char* source = Toy_readFile(fname, &size); const char* source = Toy_readFile(fname, &size);
if (!source) { if (!source) {
return; return;
} }

View File

@@ -2,13 +2,13 @@
#include "toy_common.h" #include "toy_common.h"
char* Toy_readFile(char* path, size_t* fileSize); const char* Toy_readFile(const char* path, size_t* fileSize);
int Toy_writeFile(char* path, unsigned char* bytes, size_t size); int Toy_writeFile(const char* path, const unsigned char* bytes, size_t size);
unsigned char* Toy_compileString(char* source, size_t* size); const unsigned char* Toy_compileString(const char* source, size_t* size);
void Toy_runBinary(unsigned char* tb, size_t size); void Toy_runBinary(const unsigned char* tb, size_t size);
void Toy_runBinaryFile(char* fname); void Toy_runBinaryFile(const char* fname);
void Toy_runSource(char* source); void Toy_runSource(const char* source);
void Toy_runSourceFile(char* fname); void Toy_runSourceFile(const char* fname);

View File

@@ -1,6 +1,5 @@
import standard; //for a bunch of utility functions import standard; //for a bunch of utility functions
print "Hello world"; //"print" is a keyword print "Hello world"; //"print" is a keyword
var msg = "foobar"; //declare a variable like this var msg = "foobar"; //declare a variable like this

View File

@@ -793,7 +793,7 @@ int Toy_private_index(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
} }
//handle each error case //handle each error case
if (!TOY_IS_INTEGER(first) || TOY_AS_INTEGER(first) < 0 || TOY_AS_INTEGER(first) >= TOY_AS_STRING(compound)->length) { if (!TOY_IS_INTEGER(first) || TOY_AS_INTEGER(first) < 0 || TOY_AS_INTEGER(first) >= (int)Toy_lengthRefString(TOY_AS_STRING(compound))) {
interpreter->errorOutput("Bad first indexing in string\n"); interpreter->errorOutput("Bad first indexing in string\n");
//something is weird - skip out //something is weird - skip out
@@ -807,7 +807,7 @@ int Toy_private_index(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
return -1; return -1;
} }
if ((!TOY_IS_NULL(second) && !TOY_IS_INTEGER(second)) || TOY_AS_INTEGER(second) < 0 || TOY_AS_INTEGER(second) >= TOY_AS_STRING(compound)->length) { if ((!TOY_IS_NULL(second) && !TOY_IS_INTEGER(second)) || TOY_AS_INTEGER(second) < 0 || TOY_AS_INTEGER(second) >= (int)Toy_lengthRefString(TOY_AS_STRING(compound))) {
interpreter->errorOutput("Bad second indexing in string\n"); interpreter->errorOutput("Bad second indexing in string\n");
//something is weird - skip out //something is weird - skip out
@@ -838,7 +838,7 @@ int Toy_private_index(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
//simple indexing if second is null //simple indexing if second is null
if (TOY_IS_NULL(second)) { if (TOY_IS_NULL(second)) {
char* cstr = Toy_toCString(TOY_AS_STRING(compound)); const char* cstr = Toy_toCString(TOY_AS_STRING(compound));
char buf[16]; char buf[16];
snprintf(buf, 16, "%s", &(cstr[ TOY_AS_INTEGER(first) ]) ); snprintf(buf, 16, "%s", &(cstr[ TOY_AS_INTEGER(first) ]) );
@@ -937,7 +937,7 @@ int Toy_private_index(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
} }
//handle each error case //handle each error case
if (!TOY_IS_INTEGER(first) || TOY_AS_INTEGER(first) < 0 || TOY_AS_INTEGER(first) >= TOY_AS_STRING(compound)->length) { if (!TOY_IS_INTEGER(first) || TOY_AS_INTEGER(first) < 0 || TOY_AS_INTEGER(first) >= (int)Toy_lengthRefString(TOY_AS_STRING(compound))) {
interpreter->errorOutput("Bad first indexing in string assignment\n"); interpreter->errorOutput("Bad first indexing in string assignment\n");
//something is weird - skip out //something is weird - skip out
@@ -951,7 +951,7 @@ int Toy_private_index(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
return -1; return -1;
} }
if ((!TOY_IS_NULL(second) && !TOY_IS_INTEGER(second)) || TOY_AS_INTEGER(second) < 0 || TOY_AS_INTEGER(second) >= TOY_AS_STRING(compound)->length) { if ((!TOY_IS_NULL(second) && !TOY_IS_INTEGER(second)) || TOY_AS_INTEGER(second) < 0 || TOY_AS_INTEGER(second) >= (int)Toy_lengthRefString(TOY_AS_STRING(compound))) {
interpreter->errorOutput("Bad second indexing in string assignment\n"); interpreter->errorOutput("Bad second indexing in string assignment\n");
//something is weird - skip out //something is weird - skip out

View File

@@ -1096,7 +1096,7 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size
Toy_Literal str = compiler->literalCache.literals[i]; Toy_Literal str = compiler->literalCache.literals[i];
for (int c = 0; c < TOY_AS_STRING(str)->length; c++) { for (int c = 0; c < (int)Toy_lengthRefString(TOY_AS_STRING(str)); c++) {
emitByte(&collation, &capacity, &count, Toy_toCString(TOY_AS_STRING(str))[c]); emitByte(&collation, &capacity, &count, Toy_toCString(TOY_AS_STRING(str))[c]);
} }
@@ -1198,7 +1198,7 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size
Toy_Literal identifier = compiler->literalCache.literals[i]; Toy_Literal identifier = compiler->literalCache.literals[i];
for (int c = 0; c < TOY_AS_IDENTIFIER(identifier)->length; c++) { for (int c = 0; c < (int)Toy_lengthRefString(TOY_AS_IDENTIFIER(identifier)); c++) {
emitByte(&collation, &capacity, &count, Toy_toCString(TOY_AS_IDENTIFIER(identifier))[c]); emitByte(&collation, &capacity, &count, Toy_toCString(TOY_AS_IDENTIFIER(identifier))[c]);
} }
@@ -1284,6 +1284,7 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size
return collation; return collation;
} }
//the whole point of the compiler is to alter bytecode, so leave it as non-const
unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, int* size) { unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, int* size) {
return collateCompilerHeaderOpt(compiler, size, true); return collateCompilerHeaderOpt(compiler, size, true);
} }

View File

@@ -26,7 +26,7 @@ static void errorWrapper(const char* output) {
fprintf(stderr, TOY_CC_ERROR "%s" TOY_CC_RESET, output); //no newline fprintf(stderr, TOY_CC_ERROR "%s" TOY_CC_RESET, output); //no newline
} }
bool Toy_injectNativeFn(Toy_Interpreter* interpreter, char* name, Toy_NativeFn func) { bool Toy_injectNativeFn(Toy_Interpreter* interpreter, const char* name, Toy_NativeFn func) {
//reject reserved words //reject reserved words
if (Toy_findTypeByKeyword(name) != TOY_TOKEN_EOF) { if (Toy_findTypeByKeyword(name) != TOY_TOKEN_EOF) {
interpreter->errorOutput("Can't override an existing keyword\n"); interpreter->errorOutput("Can't override an existing keyword\n");
@@ -54,7 +54,7 @@ bool Toy_injectNativeFn(Toy_Interpreter* interpreter, char* name, Toy_NativeFn f
return true; return true;
} }
bool Toy_injectNativeHook(Toy_Interpreter* interpreter, char* name, Toy_HookFn hook) { bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_HookFn hook) {
//reject reserved words //reject reserved words
if (Toy_findTypeByKeyword(name) != TOY_TOKEN_EOF) { if (Toy_findTypeByKeyword(name) != TOY_TOKEN_EOF) {
interpreter->errorOutput("Can't inject a hook on an existing keyword\n"); interpreter->errorOutput("Can't inject a hook on an existing keyword\n");
@@ -169,40 +169,40 @@ void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn errorOutp
} }
//utils //utils
static unsigned char readByte(unsigned char* tb, int* count) { static unsigned char readByte(const unsigned char* tb, int* count) {
unsigned char ret = *(unsigned char*)(tb + *count); unsigned char ret = *(unsigned char*)(tb + *count);
*count += 1; *count += 1;
return ret; return ret;
} }
static unsigned short readShort(unsigned char* tb, int* count) { static unsigned short readShort(const unsigned char* tb, int* count) {
unsigned short ret = 0; unsigned short ret = 0;
memcpy(&ret, tb + *count, 2); memcpy(&ret, tb + *count, 2);
*count += 2; *count += 2;
return ret; return ret;
} }
static int readInt(unsigned char* tb, int* count) { static int readInt(const unsigned char* tb, int* count) {
int ret = 0; int ret = 0;
memcpy(&ret, tb + *count, 4); memcpy(&ret, tb + *count, 4);
*count += 4; *count += 4;
return ret; return ret;
} }
static float readFloat(unsigned char* tb, int* count) { static float readFloat(const unsigned char* tb, int* count) {
float ret = 0; float ret = 0;
memcpy(&ret, tb + *count, 4); memcpy(&ret, tb + *count, 4);
*count += 4; *count += 4;
return ret; return ret;
} }
static char* readString(unsigned char* tb, int* count) { static const char* readString(const unsigned char* tb, int* count) {
unsigned char* ret = tb + *count; const unsigned char* ret = tb + *count;
*count += strlen((char*)ret) + 1; //+1 for null character *count += strlen((char*)ret) + 1; //+1 for null character
return (char*)ret; return (const char*)ret;
} }
static void consumeByte(Toy_Interpreter* interpreter, unsigned char byte, unsigned char* tb, int* count) { static void consumeByte(Toy_Interpreter* interpreter, unsigned char byte, const unsigned char* tb, int* count) {
if (byte != tb[*count]) { if (byte != tb[*count]) {
char buffer[512]; char buffer[512];
snprintf(buffer, 512, "[internal] Failed to consume the correct byte (expected %u, found %u)\n", byte, tb[*count]); snprintf(buffer, 512, "[internal] Failed to consume the correct byte (expected %u, found %u)\n", byte, tb[*count]);
@@ -211,7 +211,7 @@ static void consumeByte(Toy_Interpreter* interpreter, unsigned char byte, unsign
*count += 1; *count += 1;
} }
static void consumeShort(Toy_Interpreter* interpreter, unsigned short bytes, unsigned char* tb, int* count) { static void consumeShort(Toy_Interpreter* interpreter, unsigned short bytes, const unsigned char* tb, int* count) {
if (bytes != *(unsigned short*)(tb + *count)) { if (bytes != *(unsigned short*)(tb + *count)) {
char buffer[512]; char buffer[512];
snprintf(buffer, 512, "[internal] Failed to consume the correct bytes (expected %u, found %u)\n", bytes, *(unsigned short*)(tb + *count)); snprintf(buffer, 512, "[internal] Failed to consume the correct bytes (expected %u, found %u)\n", bytes, *(unsigned short*)(tb + *count));
@@ -1446,7 +1446,7 @@ bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_Liter
return true; return true;
} }
bool Toy_callFn(Toy_Interpreter* interpreter, char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns) { bool Toy_callFn(Toy_Interpreter* interpreter, const char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns) {
Toy_Literal key = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefStringLength(name, strlen(name))); Toy_Literal key = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefStringLength(name, strlen(name)));
Toy_Literal val = TOY_TO_NULL_LITERAL; Toy_Literal val = TOY_TO_NULL_LITERAL;
@@ -2125,7 +2125,7 @@ static void readInterpreterSections(Toy_Interpreter* interpreter) {
break; break;
case TOY_LITERAL_STRING: { case TOY_LITERAL_STRING: {
char* s = readString(interpreter->bytecode, &interpreter->count); const char* s = readString(interpreter->bytecode, &interpreter->count);
int length = strlen(s); int length = strlen(s);
Toy_Literal literal = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(s, length)); Toy_Literal literal = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(s, length));
Toy_pushLiteralArray(&interpreter->literalCache, literal); Toy_pushLiteralArray(&interpreter->literalCache, literal);
@@ -2222,7 +2222,7 @@ static void readInterpreterSections(Toy_Interpreter* interpreter) {
break; break;
case TOY_LITERAL_IDENTIFIER: { case TOY_LITERAL_IDENTIFIER: {
char* str = readString(interpreter->bytecode, &interpreter->count); const char* str = readString(interpreter->bytecode, &interpreter->count);
int length = strlen(str); int length = strlen(str);
Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefStringLength(str, length)); Toy_Literal identifier = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefStringLength(str, length));
@@ -2356,7 +2356,7 @@ void Toy_initInterpreter(Toy_Interpreter* interpreter) {
Toy_resetInterpreter(interpreter); Toy_resetInterpreter(interpreter);
} }
void Toy_runInterpreter(Toy_Interpreter* interpreter, unsigned char* bytecode, int length) { void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, int length) {
//initialize here instead of initInterpreter() //initialize here instead of initInterpreter()
Toy_initLiteralArray(&interpreter->literalCache); Toy_initLiteralArray(&interpreter->literalCache);
interpreter->bytecode = NULL; interpreter->bytecode = NULL;

View File

@@ -11,7 +11,7 @@ typedef void (*Toy_PrintFn)(const char*);
//the interpreter acts depending on the bytecode instructions //the interpreter acts depending on the bytecode instructions
typedef struct Toy_Interpreter { typedef struct Toy_Interpreter {
//input //input
unsigned char* bytecode; const unsigned char* bytecode;
int length; int length;
int count; int count;
int codeStart; //BUGFIX: for jumps, must be initialized to -1 int codeStart; //BUGFIX: for jumps, must be initialized to -1
@@ -34,11 +34,11 @@ typedef struct Toy_Interpreter {
} Toy_Interpreter; } Toy_Interpreter;
//native API //native API
TOY_API bool Toy_injectNativeFn(Toy_Interpreter* interpreter, char* name, Toy_NativeFn func); TOY_API bool Toy_injectNativeFn(Toy_Interpreter* interpreter, const char* name, Toy_NativeFn func);
TOY_API bool Toy_injectNativeHook(Toy_Interpreter* interpreter, char* name, Toy_HookFn hook); TOY_API bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_HookFn hook);
TOY_API bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns); TOY_API bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns);
TOY_API bool Toy_callFn(Toy_Interpreter* interpreter, char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns); TOY_API bool Toy_callFn(Toy_Interpreter* interpreter, const char* name, Toy_LiteralArray* arguments, Toy_LiteralArray* returns);
//utilities for the host program //utilities for the host program
TOY_API bool Toy_parseIdentifierToValue(Toy_Interpreter* interpreter, Toy_Literal* literalPtr); TOY_API bool Toy_parseIdentifierToValue(Toy_Interpreter* interpreter, Toy_Literal* literalPtr);
@@ -48,6 +48,6 @@ TOY_API void Toy_setInterpreterError(Toy_Interpreter* interpreter, Toy_PrintFn e
//main access //main access
TOY_API void Toy_initInterpreter(Toy_Interpreter* interpreter); //start of program TOY_API void Toy_initInterpreter(Toy_Interpreter* interpreter); //start of program
TOY_API void Toy_runInterpreter(Toy_Interpreter* interpreter, unsigned char* bytecode, int length); //run the code TOY_API void Toy_runInterpreter(Toy_Interpreter* interpreter, const unsigned char* bytecode, int 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_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 TOY_API void Toy_freeInterpreter(Toy_Interpreter* interpreter); //end of program

View File

@@ -270,7 +270,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) {
} }
//exposed functions //exposed functions
void Toy_initLexer(Toy_Lexer* lexer, char* source) { void Toy_initLexer(Toy_Lexer* lexer, const char* source) {
cleanLexer(lexer); cleanLexer(lexer);
lexer->source = source; lexer->source = source;
@@ -363,7 +363,7 @@ void Toy_printToken(Toy_Token* token) {
if (keyword != NULL) { if (keyword != NULL) {
printf("%s", keyword); printf("%s", keyword);
} else { } else {
char* str = token->lexeme; char* str = (char*)token->lexeme; //strip const-ness for trimming
int length = token->length; int length = token->length;
trim(&str, &length); trim(&str, &length);
printf("%.*s", length, str); printf("%.*s", length, str);

View File

@@ -5,7 +5,7 @@
//lexers are bound to a string of code, and return a single token every time scan is called //lexers are bound to a string of code, and return a single token every time scan is called
typedef struct { typedef struct {
char* source; const char* source;
int start; //start of the token int start; //start of the token
int current; //current position of the lexer int current; //current position of the lexer
int line; //track this for error handling int line; //track this for error handling
@@ -14,12 +14,12 @@ typedef struct {
//tokens are intermediaries between lexers and parsers //tokens are intermediaries between lexers and parsers
typedef struct { typedef struct {
Toy_TokenType type; Toy_TokenType type;
char* lexeme; const char* lexeme;
int length; int length;
int line; int line;
} Toy_Token; } Toy_Token;
TOY_API void Toy_initLexer(Toy_Lexer* lexer, char* source); TOY_API void Toy_initLexer(Toy_Lexer* lexer, const char* source);
Toy_Token Toy_scanLexer(Toy_Lexer* lexer); Toy_Token Toy_scanLexer(Toy_Lexer* lexer);
//for debugging //for debugging

View File

@@ -487,10 +487,10 @@ void Toy_printLiteralCustom(Toy_Literal literal, void (printFn)(const char*)) {
case TOY_LITERAL_STRING: { case TOY_LITERAL_STRING: {
char buffer[TOY_MAX_STRING_LENGTH]; char buffer[TOY_MAX_STRING_LENGTH];
if (!quotes) { if (!quotes) {
snprintf(buffer, TOY_MAX_STRING_LENGTH, "%.*s", Toy_lengthRefString(TOY_AS_STRING(literal)), Toy_toCString(TOY_AS_STRING(literal))); snprintf(buffer, TOY_MAX_STRING_LENGTH, "%.*s", (int)Toy_lengthRefString(TOY_AS_STRING(literal)), Toy_toCString(TOY_AS_STRING(literal)));
} }
else { else {
snprintf(buffer, TOY_MAX_STRING_LENGTH, "%c%.*s%c", quotes, Toy_lengthRefString(TOY_AS_STRING(literal)), Toy_toCString(TOY_AS_STRING(literal)), quotes); snprintf(buffer, TOY_MAX_STRING_LENGTH, "%c%.*s%c", quotes, (int)Toy_lengthRefString(TOY_AS_STRING(literal)), Toy_toCString(TOY_AS_STRING(literal)), quotes);
} }
printFn(buffer); printFn(buffer);
} }
@@ -596,7 +596,7 @@ void Toy_printLiteralCustom(Toy_Literal literal, void (printFn)(const char*)) {
case TOY_LITERAL_IDENTIFIER: { case TOY_LITERAL_IDENTIFIER: {
char buffer[256]; char buffer[256];
snprintf(buffer, 256, "%.*s", Toy_lengthRefString(TOY_AS_IDENTIFIER(literal)), Toy_toCString(TOY_AS_IDENTIFIER(literal))); snprintf(buffer, 256, "%.*s", (int)Toy_lengthRefString(TOY_AS_IDENTIFIER(literal)), Toy_toCString(TOY_AS_IDENTIFIER(literal)));
printFn(buffer); printFn(buffer);
} }
break; break;

View File

@@ -508,7 +508,7 @@ static Toy_Opcode unary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
return TOY_OP_EOF; return TOY_OP_EOF;
} }
static char* removeChar(char* lexeme, int length, char c) { static char* removeChar(const char* lexeme, int length, char c) {
int resPos = 0; int resPos = 0;
char* result = TOY_ALLOCATE(char, length + 1); char* result = TOY_ALLOCATE(char, length + 1);
@@ -540,7 +540,7 @@ static Toy_Opcode atomic(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
case TOY_TOKEN_LITERAL_INTEGER: { case TOY_TOKEN_LITERAL_INTEGER: {
int value = 0; int value = 0;
char* lexeme = removeChar(parser->previous.lexeme, parser->previous.length, '_'); const char* lexeme = removeChar(parser->previous.lexeme, parser->previous.length, '_');
sscanf(lexeme, "%d", &value); sscanf(lexeme, "%d", &value);
TOY_FREE_ARRAY(char, lexeme, parser->previous.length + 1); TOY_FREE_ARRAY(char, lexeme, parser->previous.length + 1);
Toy_emitASTNodeLiteral(nodeHandle, TOY_TO_INTEGER_LITERAL(value)); Toy_emitASTNodeLiteral(nodeHandle, TOY_TO_INTEGER_LITERAL(value));
@@ -549,7 +549,7 @@ static Toy_Opcode atomic(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
case TOY_TOKEN_LITERAL_FLOAT: { case TOY_TOKEN_LITERAL_FLOAT: {
float value = 0; float value = 0;
char* lexeme = removeChar(parser->previous.lexeme, parser->previous.length, '_'); const char* lexeme = removeChar(parser->previous.lexeme, parser->previous.length, '_');
sscanf(lexeme, "%f", &value); sscanf(lexeme, "%f", &value);
TOY_FREE_ARRAY(char, lexeme, parser->previous.length + 1); TOY_FREE_ARRAY(char, lexeme, parser->previous.length + 1);
Toy_emitASTNodeLiteral(nodeHandle, TOY_TO_FLOAT_LITERAL(value)); Toy_emitASTNodeLiteral(nodeHandle, TOY_TO_FLOAT_LITERAL(value));

View File

@@ -1,14 +1,6 @@
#include "toy_refstring.h" #include "toy_refstring.h"
#include <string.h> #include <string.h>
#include <assert.h>
//test variable sizes based on platform (safety)
#define STATIC_ASSERT(test_for_true) static_assert((test_for_true), "(" #test_for_true ") failed")
STATIC_ASSERT(sizeof(Toy_RefString) == 12);
STATIC_ASSERT(sizeof(int) == 4);
STATIC_ASSERT(sizeof(char) == 1);
//memory allocation //memory allocation
extern void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize); extern void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize);
@@ -19,18 +11,22 @@ void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn allocator) {
} }
//API //API
Toy_RefString* Toy_createRefString(char* cstring) { Toy_RefString* Toy_createRefString(const char* cstring) {
int length = strlen(cstring); size_t length = strlen(cstring);
return Toy_createRefStringLength(cstring, length); return Toy_createRefStringLength(cstring, length);
} }
Toy_RefString* Toy_createRefStringLength(char* cstring, int length) { Toy_RefString* Toy_createRefStringLength(const char* cstring, size_t length) {
//allocate the memory area (including metadata space) //allocate the memory area (including metadata space)
Toy_RefString* refString = (Toy_RefString*)allocate(NULL, 0, sizeof(int) * 2 + sizeof(char) * length + 1); Toy_RefString* refString = allocate(NULL, 0, sizeof(size_t) + sizeof(int) + sizeof(char) * (length + 1));
if (refString == NULL) {
return NULL;
}
//set the data //set the data
refString->refcount = 1; refString->refCount = 1;
refString->length = length; refString->length = length;
strncpy(refString->data, cstring, refString->length); strncpy(refString->data, cstring, refString->length);
@@ -41,32 +37,32 @@ Toy_RefString* Toy_createRefStringLength(char* cstring, int length) {
void Toy_deleteRefString(Toy_RefString* refString) { void Toy_deleteRefString(Toy_RefString* refString) {
//decrement, then check //decrement, then check
refString->refcount--; refString->refCount--;
if (refString->refcount <= 0) { if (refString->refCount <= 0) {
allocate(refString, sizeof(int) * 2 + sizeof(char) * refString->length + 1, 0); allocate(refString, sizeof(size_t) + sizeof(int) + sizeof(char) * (refString->length + 1), 0);
} }
} }
int Toy_countRefString(Toy_RefString* refString) { int Toy_countRefString(Toy_RefString* refString) {
return refString->refcount; return refString->refCount;
} }
int Toy_lengthRefString(Toy_RefString* refString) { size_t Toy_lengthRefString(Toy_RefString* refString) {
return refString->length; return refString->length;
} }
Toy_RefString* Toy_copyRefString(Toy_RefString* refString) { Toy_RefString* Toy_copyRefString(Toy_RefString* refString) {
//Cheaty McCheater Face //Cheaty McCheater Face
refString->refcount++; refString->refCount++;
return refString; return refString;
} }
Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString) { Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString) {
//create a new string, with a new refcount //create a new string, with a new refCount
return Toy_createRefStringLength(refString->data, refString->length); return Toy_createRefStringLength(refString->data, refString->length);
} }
char* Toy_toCString(Toy_RefString* refString) { const char* Toy_toCString(Toy_RefString* refString) {
return refString->data; return refString->data;
} }
@@ -87,7 +83,7 @@ bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs) {
bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring) { bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring) {
//get the rhs length //get the rhs length
int length = strlen(cstring); size_t length = strlen(cstring);
//different length //different length
if (lhs->length != length) { if (lhs->length != length) {

View File

@@ -9,19 +9,19 @@ void Toy_setRefStringAllocatorFn(Toy_RefStringAllocatorFn);
//the RefString structure //the RefString structure
typedef struct Toy_RefString { typedef struct Toy_RefString {
int refcount; size_t length;
int length; int refCount;
char data[1]; char data[];
} Toy_RefString; } Toy_RefString;
//API //API
Toy_RefString* Toy_createRefString(char* cstring); Toy_RefString* Toy_createRefString(const char* cstring);
Toy_RefString* Toy_createRefStringLength(char* cstring, int length); Toy_RefString* Toy_createRefStringLength(const char* cstring, size_t length);
void Toy_deleteRefString(Toy_RefString* refString); void Toy_deleteRefString(Toy_RefString* refString);
int Toy_countRefString(Toy_RefString* refString); int Toy_countRefString(Toy_RefString* refString);
int Toy_lengthRefString(Toy_RefString* refString); size_t Toy_lengthRefString(Toy_RefString* refString);
Toy_RefString* Toy_copyRefString(Toy_RefString* refString); Toy_RefString* Toy_copyRefString(Toy_RefString* refString);
Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString); Toy_RefString* Toy_deepCopyRefString(Toy_RefString* refString);
char* Toy_toCString(Toy_RefString* refString); const char* Toy_toCString(Toy_RefString* refString);
bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs); bool Toy_equalsRefString(Toy_RefString* lhs, Toy_RefString* rhs);
bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring); bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring);

View File

@@ -27,8 +27,8 @@ void error(char* msg) {
int main() { int main() {
{ {
size_t size = 0; size_t size = 0;
char* source = Toy_readFile("scripts/call-from-host.toy", &size); const char* source = Toy_readFile("scripts/call-from-host.toy", &size);
unsigned char* tb = Toy_compileString(source, &size); const unsigned char* tb = Toy_compileString(source, &size);
free((void*)source); free((void*)source);
if (!tb) { if (!tb) {

View File

@@ -52,7 +52,7 @@ int main() {
{ {
//source //source
size_t sourceLength = 0; size_t sourceLength = 0;
char* source = Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength); const char* source = Toy_readFile("scripts/compiler_sample_code.toy", &sourceLength);
//test basic compilation & collation //test basic compilation & collation
Toy_Lexer lexer; Toy_Lexer lexer;

View File

@@ -30,7 +30,7 @@ static void noAssertFn(const char* output) {
} }
} }
void runBinaryCustom(unsigned char* tb, size_t size) { void runBinaryCustom(const unsigned char* tb, size_t size) {
Toy_Interpreter interpreter; Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter); Toy_initInterpreter(&interpreter);
@@ -42,18 +42,18 @@ void runBinaryCustom(unsigned char* tb, size_t size) {
Toy_freeInterpreter(&interpreter); Toy_freeInterpreter(&interpreter);
} }
void runSourceCustom(char* source) { void runSourceCustom(const char* source) {
size_t size = 0; size_t size = 0;
unsigned char* tb = Toy_compileString(source, &size); const unsigned char* tb = Toy_compileString(source, &size);
if (!tb) { if (!tb) {
return; return;
} }
runBinaryCustom(tb, size); runBinaryCustom(tb, size);
} }
void runSourceFileCustom(char* fname) { void runSourceFileCustom(const char* fname) {
size_t size = 0; //not used size_t size = 0; //not used
char* source = Toy_readFile(fname, &size); const char* source = Toy_readFile(fname, &size);
runSourceCustom(source); runSourceCustom(source);
free((void*)source); free((void*)source);
} }
@@ -68,7 +68,7 @@ int main() {
{ {
//source //source
char* source = "print null;"; const char* source = "print null;";
//test basic compilation & collation //test basic compilation & collation
Toy_Lexer lexer; Toy_Lexer lexer;
@@ -88,7 +88,7 @@ int main() {
//collate //collate
int size = 0; int size = 0;
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size); const unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
//NOTE: suppress print output for testing //NOTE: suppress print output for testing
Toy_setInterpreterPrint(&interpreter, noPrintFn); Toy_setInterpreterPrint(&interpreter, noPrintFn);
@@ -106,7 +106,7 @@ int main() {
{ {
//run each file in tests/scripts/ //run each file in tests/scripts/
char* filenames[] = { const char* filenames[] = {
"arithmetic.toy", "arithmetic.toy",
"casting.toy", "casting.toy",
"coercions.toy", "coercions.toy",

View File

@@ -37,7 +37,7 @@ static void errorWrapper(const char* output) {
fprintf(stderr, TOY_CC_ERROR "%s" TOY_CC_RESET, output); fprintf(stderr, TOY_CC_ERROR "%s" TOY_CC_RESET, output);
} }
void runBinaryWithLibrary(unsigned char* tb, size_t size, char* library, Toy_HookFn hook) { void runBinaryWithLibrary(const unsigned char* tb, size_t size, const char* library, Toy_HookFn hook) {
Toy_Interpreter interpreter; Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter); Toy_initInterpreter(&interpreter);
@@ -53,7 +53,7 @@ void runBinaryWithLibrary(unsigned char* tb, size_t size, char* library, Toy_Hoo
Toy_freeInterpreter(&interpreter); Toy_freeInterpreter(&interpreter);
} }
void runBinaryQuietly(unsigned char* tb, size_t size) { void runBinaryQuietly(const unsigned char* tb, size_t size) {
Toy_Interpreter interpreter; Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter); Toy_initInterpreter(&interpreter);
@@ -111,14 +111,14 @@ int main() {
//compile the source //compile the source
size_t size = 0; size_t size = 0;
char* source = Toy_readFile(fname, &size); const char* source = Toy_readFile(fname, &size);
if (!source) { if (!source) {
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname); printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
failedAsserts++; failedAsserts++;
continue; continue;
} }
unsigned char* tb = Toy_compileString(source, &size); const unsigned char* tb = Toy_compileString(source, &size);
free((void*)source); free((void*)source);
if (!tb) { if (!tb) {
@@ -146,14 +146,14 @@ int main() {
//compile the source //compile the source
size_t size = 0; size_t size = 0;
char* source = Toy_readFile(fname, &size); const char* source = Toy_readFile(fname, &size);
if (!source) { if (!source) {
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname); printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
failedAsserts++; failedAsserts++;
continue; continue;
} }
unsigned char* tb = Toy_compileString(source, &size); const unsigned char* tb = Toy_compileString(source, &size);
free((void*)source); free((void*)source);
if (!tb) { if (!tb) {

View File

@@ -23,7 +23,7 @@ static void noErrorFn(const char* output) {
errorsTriggered++; errorsTriggered++;
} }
unsigned char* compileStringCustom(char* source, size_t* size) { const unsigned char* compileStringCustom(const char* source, size_t* size) {
Toy_Lexer lexer; Toy_Lexer lexer;
Toy_Parser parser; Toy_Parser parser;
Toy_Compiler compiler; Toy_Compiler compiler;
@@ -50,7 +50,7 @@ unsigned char* compileStringCustom(char* source, size_t* size) {
} }
//get the bytecode dump //get the bytecode dump
unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size)); const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
//cleanup //cleanup
Toy_freeCompiler(&compiler); Toy_freeCompiler(&compiler);
@@ -61,7 +61,7 @@ unsigned char* compileStringCustom(char* source, size_t* size) {
return tb; return tb;
} }
void runBinaryCustom(unsigned char* tb, size_t size) { void runBinaryCustom(const unsigned char* tb, size_t size) {
Toy_Interpreter interpreter; Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter); Toy_initInterpreter(&interpreter);
@@ -73,18 +73,18 @@ void runBinaryCustom(unsigned char* tb, size_t size) {
Toy_freeInterpreter(&interpreter); Toy_freeInterpreter(&interpreter);
} }
void runSourceCustom(char* source) { void runSourceCustom(const char* source) {
size_t size = 0; size_t size = 0;
unsigned char* tb = compileStringCustom(source, &size); const unsigned char* tb = compileStringCustom(source, &size);
if (!tb) { if (!tb) {
return; return;
} }
runBinaryCustom(tb, size); runBinaryCustom(tb, size);
} }
void runSourceFileCustom(char* fname) { void runSourceFileCustom(const char* fname) {
size_t size = 0; //not used size_t size = 0; //not used
char* source = Toy_readFile(fname, &size); const char* source = Toy_readFile(fname, &size);
runSourceCustom(source); runSourceCustom(source);
free((void*)source); free((void*)source);
} }

View File

@@ -68,8 +68,8 @@ static int consume(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
int main() { int main() {
{ {
size_t size = 0; size_t size = 0;
char* source = Toy_readFile("scripts/opaque-data-type.toy", &size); const char* source = Toy_readFile("scripts/opaque-data-type.toy", &size);
unsigned char* tb = Toy_compileString(source, &size); const unsigned char* tb = Toy_compileString(source, &size);
free((void*)source); free((void*)source);
if (!tb) { if (!tb) {

View File

@@ -11,7 +11,7 @@
int main() { int main() {
{ {
//source //source
char* source = "print null;"; const char* source = "print null;";
//test init & quit //test init & quit
Toy_Lexer lexer; Toy_Lexer lexer;
@@ -24,7 +24,7 @@ int main() {
{ {
//source //source
char* source = "print null;"; const char* source = "print null;";
//test parsing //test parsing
Toy_Lexer lexer; Toy_Lexer lexer;
@@ -58,7 +58,7 @@ int main() {
{ {
//get the source file //get the source file
size_t size = 0; size_t size = 0;
char* source = Toy_readFile("scripts/parser_sample_code.toy", &size); const char* source = Toy_readFile("scripts/parser_sample_code.toy", &size);
//test parsing a chunk of junk (valgrind will find leaks) //test parsing a chunk of junk (valgrind will find leaks)
Toy_Lexer lexer; Toy_Lexer lexer;
@@ -85,7 +85,7 @@ int main() {
{ {
//test parsing of escaped characters //test parsing of escaped characters
char* source = "print \"\\\"\";"; //NOTE: this string goes through two layers of escaping const char* source = "print \"\\\"\";"; //NOTE: this string goes through two layers of escaping
//test parsing //test parsing
Toy_Lexer lexer; Toy_Lexer lexer;
@@ -123,7 +123,7 @@ int main() {
{ {
//test parsing of underscored numbers //test parsing of underscored numbers
char* source = "print 1_000_000;"; const char* source = "print 1_000_000;";
//test parsing //test parsing
Toy_Lexer lexer; Toy_Lexer lexer;