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

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

View File

@@ -52,7 +52,7 @@ int main() {
{
//source
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
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_initInterpreter(&interpreter);
@@ -42,18 +42,18 @@ void runBinaryCustom(unsigned char* tb, size_t size) {
Toy_freeInterpreter(&interpreter);
}
void runSourceCustom(char* source) {
void runSourceCustom(const char* source) {
size_t size = 0;
unsigned char* tb = Toy_compileString(source, &size);
const unsigned char* tb = Toy_compileString(source, &size);
if (!tb) {
return;
}
runBinaryCustom(tb, size);
}
void runSourceFileCustom(char* fname) {
void runSourceFileCustom(const char* fname) {
size_t size = 0; //not used
char* source = Toy_readFile(fname, &size);
const char* source = Toy_readFile(fname, &size);
runSourceCustom(source);
free((void*)source);
}
@@ -68,7 +68,7 @@ int main() {
{
//source
char* source = "print null;";
const char* source = "print null;";
//test basic compilation & collation
Toy_Lexer lexer;
@@ -88,7 +88,7 @@ int main() {
//collate
int size = 0;
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
const unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
//NOTE: suppress print output for testing
Toy_setInterpreterPrint(&interpreter, noPrintFn);
@@ -106,7 +106,7 @@ int main() {
{
//run each file in tests/scripts/
char* filenames[] = {
const char* filenames[] = {
"arithmetic.toy",
"casting.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);
}
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_initInterpreter(&interpreter);
@@ -53,7 +53,7 @@ void runBinaryWithLibrary(unsigned char* tb, size_t size, char* library, Toy_Hoo
Toy_freeInterpreter(&interpreter);
}
void runBinaryQuietly(unsigned char* tb, size_t size) {
void runBinaryQuietly(const unsigned char* tb, size_t size) {
Toy_Interpreter interpreter;
Toy_initInterpreter(&interpreter);
@@ -111,14 +111,14 @@ int main() {
//compile the source
size_t size = 0;
char* source = Toy_readFile(fname, &size);
const char* source = Toy_readFile(fname, &size);
if (!source) {
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
failedAsserts++;
continue;
}
unsigned char* tb = Toy_compileString(source, &size);
const unsigned char* tb = Toy_compileString(source, &size);
free((void*)source);
if (!tb) {
@@ -146,14 +146,14 @@ int main() {
//compile the source
size_t size = 0;
char* source = Toy_readFile(fname, &size);
const char* source = Toy_readFile(fname, &size);
if (!source) {
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
failedAsserts++;
continue;
}
unsigned char* tb = Toy_compileString(source, &size);
const unsigned char* tb = Toy_compileString(source, &size);
free((void*)source);
if (!tb) {

View File

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

View File

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

View File

@@ -11,7 +11,7 @@
int main() {
{
//source
char* source = "print null;";
const char* source = "print null;";
//test init & quit
Toy_Lexer lexer;
@@ -24,7 +24,7 @@ int main() {
{
//source
char* source = "print null;";
const char* source = "print null;";
//test parsing
Toy_Lexer lexer;
@@ -58,7 +58,7 @@ int main() {
{
//get the source file
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)
Toy_Lexer lexer;
@@ -85,7 +85,7 @@ int main() {
{
//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
Toy_Lexer lexer;
@@ -123,7 +123,7 @@ int main() {
{
//test parsing of underscored numbers
char* source = "print 1_000_000;";
const char* source = "print 1_000_000;";
//test parsing
Toy_Lexer lexer;