From 470836a390cade5cacc506b34640925d84688d20 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 8 Feb 2025 00:23:19 +1100 Subject: [PATCH] Renamed Toy_ModuleBuilder to Toy_ModuleCompiler I also noticed that Toy_ModuleBundle isn't being used right now. --- repl/main.c | 6 +- source/toy_module_bundle.c | 4 +- source/toy_module_bundle.h | 2 + ...module_builder.c => toy_module_compiler.c} | 179 +++++++++--------- ...module_builder.h => toy_module_compiler.h} | 6 +- source/toy_string.c | 2 +- ...odule_builder.c => test_module_compiler.c} | 136 ++++++------- tests/cases/test_vm.c | 34 ++-- 8 files changed, 186 insertions(+), 183 deletions(-) rename source/{toy_module_builder.c => toy_module_compiler.c} (85%) rename source/{toy_module_builder.h => toy_module_compiler.h} (93%) rename tests/cases/{test_module_builder.c => test_module_compiler.c} (88%) diff --git a/repl/main.c b/repl/main.c index 98ba962..66d6719 100644 --- a/repl/main.c +++ b/repl/main.c @@ -2,7 +2,7 @@ #include "toy_lexer.h" #include "toy_parser.h" -#include "toy_module_builder.h" +#include "toy_module_compiler.h" #include "toy_vm.h" #include @@ -332,7 +332,7 @@ int repl(const char* filepath) { continue; } - void* buffer = Toy_compileModuleBuilder(ast); + void* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); Toy_bindVM(&vm, &module, runCount++ > 0); @@ -481,7 +481,7 @@ int main(int argc, const char* argv[]) { Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Ast* ast = Toy_scanParser(&bucket, &parser); - void* buffer = Toy_compileModuleBuilder(ast); + void* buffer = Toy_compileModule(ast); Toy_freeBucket(&bucket); free(source); diff --git a/source/toy_module_bundle.c b/source/toy_module_bundle.c index 58ed043..9486839 100644 --- a/source/toy_module_bundle.c +++ b/source/toy_module_bundle.c @@ -1,7 +1,7 @@ #include "toy_module_bundle.h" #include "toy_console_colors.h" -#include "toy_module_builder.h" +#include "toy_module_compiler.h" #include #include @@ -87,7 +87,7 @@ void Toy_appendModuleBundle(Toy_ModuleBundle* bundle, Toy_Ast* ast) { exit(-1); } - void* module = Toy_compileModuleBuilder(ast); + void* module = Toy_compileModule(ast); //don't try writing an empty module if (module == NULL) { diff --git a/source/toy_module_bundle.h b/source/toy_module_bundle.h index c00d209..cc362df 100644 --- a/source/toy_module_bundle.h +++ b/source/toy_module_bundle.h @@ -18,3 +18,5 @@ TOY_API void Toy_freeModuleBundle(Toy_ModuleBundle* bundle); //load module bundle with external data (makes an internal copy) TOY_API void Toy_bindModuleBundle(Toy_ModuleBundle* bundle, unsigned char* ptr, unsigned int size); TOY_API Toy_Module Toy_extractModuleFromBundle(Toy_ModuleBundle* bundle, unsigned char index); + +//NOTE: 'Toy_ModuleBundle' isn't used anywhere? \ No newline at end of file diff --git a/source/toy_module_builder.c b/source/toy_module_compiler.c similarity index 85% rename from source/toy_module_builder.c rename to source/toy_module_compiler.c index ac856d0..f2522f0 100644 --- a/source/toy_module_builder.c +++ b/source/toy_module_compiler.c @@ -1,4 +1,4 @@ -#include "toy_module_builder.h" +#include "toy_module_compiler.h" #include "toy_console_colors.h" #include "toy_opcodes.h" @@ -43,7 +43,7 @@ void* Toy_private_resizeEscapeArray(Toy_private_EscapeArray* ptr, unsigned int c ptr = (Toy_private_EscapeArray*)realloc(ptr, capacity * sizeof(Toy_private_EscapeEntry_t) + sizeof(Toy_private_EscapeArray)); if (ptr == NULL) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to resize an escape array within 'Toy_ModuleBuilder' from %d to %d capacity\n" TOY_CC_RESET, (int)originalCapacity, (int)capacity); + fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to resize an escape array within 'Toy_ModuleCompiler' from %d to %d capacity\n" TOY_CC_RESET, (int)originalCapacity, (int)capacity); exit(-1); } @@ -62,7 +62,7 @@ static void expand(unsigned char** handle, unsigned int* capacity, unsigned int* (*handle) = realloc((*handle), (*capacity)); if ((*handle) == NULL) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate %d space for a part of 'Toy_ModuleBuilder'\n" TOY_CC_RESET, (int)(*capacity)); + fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate %d space for a part of 'Toy_ModuleCompiler'\n" TOY_CC_RESET, (int)(*capacity)); exit(1); } } @@ -108,12 +108,12 @@ static void emitFloat(unsigned char** handle, unsigned int* capacity, unsigned i //simply get the address (always an integer) #define CURRENT_ADDRESS(mb, part) ((*mb)->part##Count) -static void emitToJumpTable(Toy_ModuleBuilder** mb, unsigned int startAddr) { +static void emitToJumpTable(Toy_ModuleCompiler** mb, unsigned int startAddr) { EMIT_INT(mb, code, (*mb)->jumpsCount); //mark the jump index in the code EMIT_INT(mb, jumps, startAddr); //save address at the jump index } -static unsigned int emitString(Toy_ModuleBuilder** mb, Toy_String* str) { +static unsigned int emitString(Toy_ModuleCompiler** mb, Toy_String* str) { //4-byte alignment unsigned int length = str->info.length + 1; if (length % 4 != 0) { @@ -146,10 +146,10 @@ static unsigned int emitString(Toy_ModuleBuilder** mb, Toy_String* str) { return 1; } -static unsigned int writeModuleBuilderCode(Toy_ModuleBuilder** mb, Toy_Ast* ast); //forward declare for recursion -static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAssign ast, bool chainedAssignment); //forward declare for chaining of var declarations +static unsigned int writeModuleCompilerCode(Toy_ModuleCompiler** mb, Toy_Ast* ast); //forward declare for recursion +static unsigned int writeInstructionAssign(Toy_ModuleCompiler** mb, Toy_AstVarAssign ast, bool chainedAssignment); //forward declare for chaining of var declarations -static unsigned int writeInstructionValue(Toy_ModuleBuilder** mb, Toy_AstValue ast) { +static unsigned int writeInstructionValue(Toy_ModuleCompiler** mb, Toy_AstValue ast) { EMIT_BYTE(mb, code, TOY_OPCODE_READ); EMIT_BYTE(mb, code, ast.value.type); @@ -196,11 +196,11 @@ static unsigned int writeInstructionValue(Toy_ModuleBuilder** mb, Toy_AstValue a return 1; } -static unsigned int writeInstructionUnary(Toy_ModuleBuilder** mb, Toy_AstUnary ast) { +static unsigned int writeInstructionUnary(Toy_ModuleCompiler** mb, Toy_AstUnary ast) { unsigned int result = 0; if (ast.flag == TOY_AST_FLAG_NEGATE) { - result = writeModuleBuilderCode(mb, ast.child); + result = writeModuleCompilerCode(mb, ast.child); EMIT_BYTE(mb, code, TOY_OPCODE_NEGATE); @@ -304,10 +304,10 @@ static unsigned int writeInstructionUnary(Toy_ModuleBuilder** mb, Toy_AstUnary a return result; } -static unsigned int writeInstructionBinary(Toy_ModuleBuilder** mb, Toy_AstBinary ast) { +static unsigned int writeInstructionBinary(Toy_ModuleCompiler** mb, Toy_AstBinary ast) { //left, then right, then the binary's operation - writeModuleBuilderCode(mb, ast.left); - writeModuleBuilderCode(mb, ast.right); + writeModuleCompilerCode(mb, ast.left); + writeModuleCompilerCode(mb, ast.right); if (ast.flag == TOY_AST_FLAG_ADD) { EMIT_BYTE(mb, code,TOY_OPCODE_ADD); @@ -341,9 +341,9 @@ static unsigned int writeInstructionBinary(Toy_ModuleBuilder** mb, Toy_AstBinary return 1; //leaves only 1 value on the stack } -static unsigned int writeInstructionBinaryShortCircuit(Toy_ModuleBuilder** mb, Toy_AstBinaryShortCircuit ast) { +static unsigned int writeInstructionBinaryShortCircuit(Toy_ModuleCompiler** mb, Toy_AstBinaryShortCircuit ast) { //lhs - writeModuleBuilderCode(mb, ast.left); + writeModuleCompilerCode(mb, ast.left); //duplicate the top (so the lhs can be 'returned' by this expression, if needed) EMIT_BYTE(mb, code,TOY_OPCODE_DUPLICATE); @@ -382,7 +382,7 @@ static unsigned int writeInstructionBinaryShortCircuit(Toy_ModuleBuilder** mb, T EMIT_BYTE(mb, code, 0); //rhs - writeModuleBuilderCode(mb, ast.right); + writeModuleCompilerCode(mb, ast.right); //set the parameter OVERWRITE_INT(mb, code, paramAddr, CURRENT_ADDRESS(mb, code) - (paramAddr + 4)); @@ -390,10 +390,10 @@ static unsigned int writeInstructionBinaryShortCircuit(Toy_ModuleBuilder** mb, T return 1; //leaves only 1 value on the stack } -static unsigned int writeInstructionCompare(Toy_ModuleBuilder** mb, Toy_AstCompare ast) { +static unsigned int writeInstructionCompare(Toy_ModuleCompiler** mb, Toy_AstCompare ast) { //left, then right, then the compare's operation - writeModuleBuilderCode(mb, ast.left); - writeModuleBuilderCode(mb, ast.right); + writeModuleCompilerCode(mb, ast.left); + writeModuleCompilerCode(mb, ast.right); if (ast.flag == TOY_AST_FLAG_COMPARE_EQUAL) { EMIT_BYTE(mb, code,TOY_OPCODE_COMPARE_EQUAL); @@ -432,13 +432,13 @@ static unsigned int writeInstructionCompare(Toy_ModuleBuilder** mb, Toy_AstCompa return 1; //leaves only 1 value on the stack } -static unsigned int writeInstructionGroup(Toy_ModuleBuilder** mb, Toy_AstGroup ast) { +static unsigned int writeInstructionGroup(Toy_ModuleCompiler** mb, Toy_AstGroup ast) { //not certain what this leaves - return writeModuleBuilderCode(mb, ast.child); + return writeModuleCompilerCode(mb, ast.child); } -static unsigned int writeInstructionCompound(Toy_ModuleBuilder** mb, Toy_AstCompound ast) { - unsigned int result = writeModuleBuilderCode(mb, ast.child); +static unsigned int writeInstructionCompound(Toy_ModuleCompiler** mb, Toy_AstCompound ast) { + unsigned int result = writeModuleCompilerCode(mb, ast.child); if (ast.flag == TOY_AST_FLAG_COMPOUND_ARRAY) { //signal how many values to read in as array elements @@ -475,12 +475,12 @@ static unsigned int writeInstructionCompound(Toy_ModuleBuilder** mb, Toy_AstComp } } -static unsigned int writeInstructionAggregate(Toy_ModuleBuilder** mb, Toy_AstAggregate ast) { +static unsigned int writeInstructionAggregate(Toy_ModuleCompiler** mb, Toy_AstAggregate ast) { unsigned int result = 0; //left, then right - result += writeModuleBuilderCode(mb, ast.left); - result += writeModuleBuilderCode(mb, ast.right); + result += writeModuleCompilerCode(mb, ast.left); + result += writeModuleCompilerCode(mb, ast.right); if (ast.flag == TOY_AST_FLAG_COLLECTION) { //collections are handled above @@ -508,10 +508,10 @@ static unsigned int writeInstructionAggregate(Toy_ModuleBuilder** mb, Toy_AstAgg } } -static unsigned int writeInstructionAssert(Toy_ModuleBuilder** mb, Toy_AstAssert ast) { +static unsigned int writeInstructionAssert(Toy_ModuleCompiler** mb, Toy_AstAssert ast) { //the thing to print - writeModuleBuilderCode(mb, ast.child); - writeModuleBuilderCode(mb, ast.message); + writeModuleCompilerCode(mb, ast.child); + writeModuleCompilerCode(mb, ast.message); //output the print opcode EMIT_BYTE(mb, code, TOY_OPCODE_ASSERT); @@ -524,9 +524,9 @@ static unsigned int writeInstructionAssert(Toy_ModuleBuilder** mb, Toy_AstAssert return 0; } -static unsigned int writeInstructionIfThenElse(Toy_ModuleBuilder** mb, Toy_AstIfThenElse ast) { +static unsigned int writeInstructionIfThenElse(Toy_ModuleCompiler** mb, Toy_AstIfThenElse ast) { //cond-branch - writeModuleBuilderCode(mb, ast.condBranch); + writeModuleCompilerCode(mb, ast.condBranch); //emit the jump word (opcode, type, condition, padding) EMIT_BYTE(mb, code, TOY_OPCODE_JUMP); @@ -537,7 +537,7 @@ static unsigned int writeInstructionIfThenElse(Toy_ModuleBuilder** mb, Toy_AstIf unsigned int thenParamAddr = SKIP_INT(mb, code); //parameter to be written later //emit then-branch - writeModuleBuilderCode(mb, ast.thenBranch); + writeModuleCompilerCode(mb, ast.thenBranch); if (ast.elseBranch != NULL) { //emit the jump-to-end (opcode, type, condition, padding) @@ -552,7 +552,7 @@ static unsigned int writeInstructionIfThenElse(Toy_ModuleBuilder** mb, Toy_AstIf OVERWRITE_INT(mb, code, thenParamAddr, CURRENT_ADDRESS(mb, code) - (thenParamAddr + 4)); //emit the else branch - writeModuleBuilderCode(mb, ast.elseBranch); + writeModuleCompilerCode(mb, ast.elseBranch); //specify the ending position for the else branch OVERWRITE_INT(mb, code, elseParamAddr, CURRENT_ADDRESS(mb, code) - (elseParamAddr + 4)); @@ -566,12 +566,12 @@ static unsigned int writeInstructionIfThenElse(Toy_ModuleBuilder** mb, Toy_AstIf return 0; } -static unsigned int writeInstructionWhileThen(Toy_ModuleBuilder** mb, Toy_AstWhileThen ast) { +static unsigned int writeInstructionWhileThen(Toy_ModuleCompiler** mb, Toy_AstWhileThen ast) { //begin unsigned int beginAddr = CURRENT_ADDRESS(mb, code); //cond-branch - writeModuleBuilderCode(mb, ast.condBranch); + writeModuleCompilerCode(mb, ast.condBranch); //emit the jump word (opcode, type, condition, padding) EMIT_BYTE(mb, code, TOY_OPCODE_JUMP); @@ -582,7 +582,7 @@ static unsigned int writeInstructionWhileThen(Toy_ModuleBuilder** mb, Toy_AstWhi unsigned int paramAddr = SKIP_INT(mb, code); //parameter to be written later //emit then-branch - writeModuleBuilderCode(mb, ast.thenBranch); + writeModuleCompilerCode(mb, ast.thenBranch); //jump to begin to repeat the conditional test EMIT_BYTE(mb, code, TOY_OPCODE_JUMP); @@ -627,7 +627,7 @@ static unsigned int writeInstructionWhileThen(Toy_ModuleBuilder** mb, Toy_AstWhi return 0; } -static unsigned int writeInstructionBreak(Toy_ModuleBuilder** mb, Toy_AstBreak ast) { +static unsigned int writeInstructionBreak(Toy_ModuleCompiler** mb, Toy_AstBreak ast) { //unused (void)ast; @@ -651,7 +651,7 @@ static unsigned int writeInstructionBreak(Toy_ModuleBuilder** mb, Toy_AstBreak a return 0; } -static unsigned int writeInstructionContinue(Toy_ModuleBuilder** mb, Toy_AstContinue ast) { +static unsigned int writeInstructionContinue(Toy_ModuleCompiler** mb, Toy_AstContinue ast) { //unused (void)ast; @@ -675,9 +675,9 @@ static unsigned int writeInstructionContinue(Toy_ModuleBuilder** mb, Toy_AstCont return 0; } -static unsigned int writeInstructionPrint(Toy_ModuleBuilder** mb, Toy_AstPrint ast) { +static unsigned int writeInstructionPrint(Toy_ModuleCompiler** mb, Toy_AstPrint ast) { //the thing to print - writeModuleBuilderCode(mb, ast.child); + writeModuleCompilerCode(mb, ast.child); //output the print opcode EMIT_BYTE(mb, code,TOY_OPCODE_PRINT); @@ -690,13 +690,13 @@ static unsigned int writeInstructionPrint(Toy_ModuleBuilder** mb, Toy_AstPrint a return 0; } -static unsigned int writeInstructionVarDeclare(Toy_ModuleBuilder** mb, Toy_AstVarDeclare ast) { +static unsigned int writeInstructionVarDeclare(Toy_ModuleCompiler** mb, Toy_AstVarDeclare ast) { //if we're dealing with chained assignments, hijack the next assignment with 'chainedAssignment' set to true if (checkForChaining(ast.expr)) { writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - writeModuleBuilderCode(mb, ast.expr); //default value + writeModuleCompilerCode(mb, ast.expr); //default value } //delcare with the given name string @@ -710,7 +710,7 @@ static unsigned int writeInstructionVarDeclare(Toy_ModuleBuilder** mb, Toy_AstVa return 0; } -static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAssign ast, bool chainedAssignment) { +static unsigned int writeInstructionAssign(Toy_ModuleCompiler** mb, Toy_AstVarAssign ast, bool chainedAssignment) { unsigned int result = 0; //target is a name string @@ -729,15 +729,15 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss //target is an indexing of some compound value else if (ast.target->type == TOY_AST_AGGREGATE && ast.target->aggregate.flag == TOY_AST_FLAG_INDEX) { - writeModuleBuilderCode(mb, ast.target->aggregate.left); //any deeper indexing will just work, using reference values - writeModuleBuilderCode(mb, ast.target->aggregate.right); //key + writeModuleCompilerCode(mb, ast.target->aggregate.left); //any deeper indexing will just work, using reference values + writeModuleCompilerCode(mb, ast.target->aggregate.right); //key //if we're dealing with chained assignments, hijack the next assignment with 'chainedAssignment' set to true if (checkForChaining(ast.expr)) { result += writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - result += writeModuleBuilderCode(mb, ast.expr); //default value + result += writeModuleCompilerCode(mb, ast.expr); //default value } EMIT_BYTE(mb, code, TOY_OPCODE_ASSIGN_COMPOUND); //uses the top three values on the stack @@ -762,7 +762,7 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss result += writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - result += writeModuleBuilderCode(mb, ast.expr); //default value + result += writeModuleCompilerCode(mb, ast.expr); //default value } EMIT_BYTE(mb, code, TOY_OPCODE_ASSIGN); @@ -781,7 +781,7 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss result += writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - result += writeModuleBuilderCode(mb, ast.expr); //default value + result += writeModuleCompilerCode(mb, ast.expr); //default value } EMIT_BYTE(mb, code,TOY_OPCODE_ADD); @@ -800,7 +800,7 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss result += writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - result += writeModuleBuilderCode(mb, ast.expr); //default value + result += writeModuleCompilerCode(mb, ast.expr); //default value } EMIT_BYTE(mb, code,TOY_OPCODE_SUBTRACT); @@ -819,7 +819,7 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss result += writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - result += writeModuleBuilderCode(mb, ast.expr); //default value + result += writeModuleCompilerCode(mb, ast.expr); //default value } EMIT_BYTE(mb, code,TOY_OPCODE_MULTIPLY); @@ -838,7 +838,7 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss result += writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - result += writeModuleBuilderCode(mb, ast.expr); //default value + result += writeModuleCompilerCode(mb, ast.expr); //default value } EMIT_BYTE(mb, code,TOY_OPCODE_DIVIDE); @@ -857,7 +857,7 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss result += writeInstructionAssign(mb, ast.expr->varAssign, true); } else { - result += writeModuleBuilderCode(mb, ast.expr); //default value + result += writeModuleCompilerCode(mb, ast.expr); //default value } EMIT_BYTE(mb, code,TOY_OPCODE_MODULO); @@ -874,7 +874,7 @@ static unsigned int writeInstructionAssign(Toy_ModuleBuilder** mb, Toy_AstVarAss return result + (chainedAssignment ? 1 : 0); } -static unsigned int writeInstructionAccess(Toy_ModuleBuilder** mb, Toy_AstVarAccess ast) { +static unsigned int writeInstructionAccess(Toy_ModuleCompiler** mb, Toy_AstVarAccess ast) { if (!(ast.child->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.child->value.value) && TOY_VALUE_AS_STRING(ast.child->value.value)->info.type == TOY_STRING_NAME)) { fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Found a non-name-string in a value node when trying to write access\n" TOY_CC_RESET); exit(-1); @@ -899,13 +899,14 @@ static unsigned int writeInstructionAccess(Toy_ModuleBuilder** mb, Toy_AstVarAcc return 1; } -static unsigned int writeInstructionFnDeclare(Toy_ModuleBuilder** mb, Toy_AstFnDeclare ast) { +static unsigned int writeInstructionFnDeclare(Toy_ModuleCompiler** mb, Toy_AstFnDeclare ast) { + //URGENT: currently a no-op (void)mb; (void)ast; return 0; } -static unsigned int writeModuleBuilderCode(Toy_ModuleBuilder** mb, Toy_Ast* ast) { +static unsigned int writeModuleCompilerCode(Toy_ModuleCompiler** mb, Toy_Ast* ast) { if (ast == NULL) { return 0; } @@ -930,8 +931,8 @@ static unsigned int writeModuleBuilderCode(Toy_ModuleBuilder** mb, Toy_Ast* ast) (*mb)->currentScopeDepth++; } - result += writeModuleBuilderCode(mb, ast->block.child); - result += writeModuleBuilderCode(mb, ast->block.next); + result += writeModuleCompilerCode(mb, ast->block.child); + result += writeModuleCompilerCode(mb, ast->block.next); if (ast->block.innerScope) { EMIT_BYTE(mb, code, TOY_OPCODE_SCOPE_POP); @@ -1034,9 +1035,9 @@ static unsigned int writeModuleBuilderCode(Toy_ModuleBuilder** mb, Toy_Ast* ast) return result; } -static void* writeModuleBuilder(Toy_ModuleBuilder* mb, Toy_Ast* ast) { +static void* writeModuleCompiler(Toy_ModuleCompiler* mb, Toy_Ast* ast) { //code - writeModuleBuilderCode(&mb, ast); + writeModuleCompilerCode(&mb, ast); EMIT_BYTE(&mb, code, TOY_OPCODE_RETURN); //end terminator EMIT_BYTE(&mb, code, 0); //4-byte alignment @@ -1121,48 +1122,48 @@ static void* writeModuleBuilder(Toy_ModuleBuilder* mb, Toy_Ast* ast) { } //exposed functions -void* Toy_compileModuleBuilder(Toy_Ast* ast) { +void* Toy_compileModule(Toy_Ast* ast) { //setup - Toy_ModuleBuilder builder; + Toy_ModuleCompiler compiler; - builder.code = NULL; - builder.codeCapacity = 0; - builder.codeCount = 0; + compiler.code = NULL; + compiler.codeCapacity = 0; + compiler.codeCount = 0; - builder.jumps = NULL; - builder.jumpsCapacity = 0; - builder.jumpsCount = 0; + compiler.jumps = NULL; + compiler.jumpsCapacity = 0; + compiler.jumpsCount = 0; - builder.param = NULL; - builder.paramCapacity = 0; - builder.paramCount = 0; + compiler.param = NULL; + compiler.paramCapacity = 0; + compiler.paramCount = 0; - builder.data = NULL; - builder.dataCapacity = 0; - builder.dataCount = 0; + compiler.data = NULL; + compiler.dataCapacity = 0; + compiler.dataCount = 0; - builder.subs = NULL; - builder.subsCapacity = 0; - builder.subsCount = 0; + compiler.subs = NULL; + compiler.subsCapacity = 0; + compiler.subsCount = 0; - builder.currentScopeDepth = 0; - builder.breakEscapes = Toy_private_resizeEscapeArray(NULL, TOY_ESCAPE_INITIAL_CAPACITY); - builder.continueEscapes = Toy_private_resizeEscapeArray(NULL, TOY_ESCAPE_INITIAL_CAPACITY); + compiler.currentScopeDepth = 0; + compiler.breakEscapes = Toy_private_resizeEscapeArray(NULL, TOY_ESCAPE_INITIAL_CAPACITY); + compiler.continueEscapes = Toy_private_resizeEscapeArray(NULL, TOY_ESCAPE_INITIAL_CAPACITY); - builder.panic = false; + compiler.panic = false; - //build - void * buffer = writeModuleBuilder(&builder, ast); + //compile the ast to memory + void * buffer = writeModuleCompiler(&compiler, ast); //cleanup - Toy_private_resizeEscapeArray(builder.breakEscapes, 0); - Toy_private_resizeEscapeArray(builder.continueEscapes, 0); + Toy_private_resizeEscapeArray(compiler.breakEscapes, 0); + Toy_private_resizeEscapeArray(compiler.continueEscapes, 0); - free(builder.param); - free(builder.code); - free(builder.jumps); - free(builder.data); - free(builder.subs); + free(compiler.param); + free(compiler.code); + free(compiler.jumps); + free(compiler.data); + free(compiler.subs); return buffer; } diff --git a/source/toy_module_builder.h b/source/toy_module_compiler.h similarity index 93% rename from source/toy_module_builder.h rename to source/toy_module_compiler.h index 6c1c486..66443f5 100644 --- a/source/toy_module_builder.h +++ b/source/toy_module_compiler.h @@ -27,7 +27,7 @@ typedef struct Toy_private_EscapeArray { TOY_API void* Toy_private_resizeEscapeArray(Toy_private_EscapeArray* ptr, unsigned int capacity); //structure for holding the module as it is built -typedef struct Toy_ModuleBuilder { +typedef struct Toy_ModuleCompiler { unsigned char* code; //the instruction set unsigned int codeCapacity; unsigned int codeCount; @@ -57,6 +57,6 @@ typedef struct Toy_ModuleBuilder { //compilation errors bool panic; -} Toy_ModuleBuilder; +} Toy_ModuleCompiler; -TOY_API void* Toy_compileModuleBuilder(Toy_Ast* ast); +TOY_API void* Toy_compileModule(Toy_Ast* ast); diff --git a/source/toy_string.c b/source/toy_string.c index ef5cee7..e67f96a 100644 --- a/source/toy_string.c +++ b/source/toy_string.c @@ -206,7 +206,7 @@ char* Toy_getStringRawBuffer(Toy_String* str) { } static int deepCompareUtil(Toy_String* left, Toy_String* right, const char** leftHead, const char** rightHead) { - //WARNING: this function can't handle strings of zero length + //NOTE: this function can't handle strings of zero length int result = 0; //if it's the same object, of course they match diff --git a/tests/cases/test_module_builder.c b/tests/cases/test_module_compiler.c similarity index 88% rename from tests/cases/test_module_builder.c rename to tests/cases/test_module_compiler.c index 4c98f4d..c84a11f 100644 --- a/tests/cases/test_module_builder.c +++ b/tests/cases/test_module_compiler.c @@ -1,4 +1,4 @@ -#include "toy_module_builder.h" +#include "toy_module_compiler.h" #include "toy_console_colors.h" #include "toy_opcodes.h" @@ -12,7 +12,7 @@ //NOTE: This set of tests also covers 'Toy_Module' //tests -int test_builder_expressions(Toy_Bucket** bucketHandle) { +int test_compiler_expressions(Toy_Bucket** bucketHandle) { //simple test to ensure the header looks right with an empty ast { //setup @@ -20,7 +20,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { Toy_private_emitAstPass(bucketHandle, &ast); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -31,7 +31,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, ast: PASS\n" TOY_CC_RESET); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, ast: PASS\n" TOY_CC_RESET); //cleanup and return free(buffer); @@ -45,7 +45,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 27)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, ast: PASS\n" TOY_CC_RESET); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, ast: PASS\n" TOY_CC_RESET); //cleanup and return free(buffer); @@ -68,7 +68,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -79,7 +79,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -93,7 +93,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 27)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -116,7 +116,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -127,7 +127,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -145,7 +145,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 31)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -168,7 +168,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -179,7 +179,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -197,7 +197,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 31)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -220,7 +220,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -231,7 +231,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -250,7 +250,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 35)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -273,7 +273,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -284,7 +284,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -303,7 +303,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 35)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -326,7 +326,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* header = (int*)buffer; @@ -345,7 +345,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -369,7 +369,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -385,7 +385,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder jumps, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module jumps, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -401,7 +401,7 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder data, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module data, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -415,11 +415,11 @@ int test_builder_expressions(Toy_Bucket** bucketHandle) { return 0; } -// int test_builder_unary(Toy_Bucket** bucketHandle) { +// int test_compiler_unary(Toy_Bucket** bucketHandle) { // //Nothing produces a unary instruction yet // } -int test_builder_binary(Toy_Bucket** bucketHandle) { +int test_compiler_binary(Toy_Bucket** bucketHandle) { //produce a simple algorithm { //setup @@ -432,7 +432,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -443,7 +443,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -474,7 +474,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 47)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -497,7 +497,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -508,7 +508,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -539,7 +539,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 47)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -562,7 +562,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -573,7 +573,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -604,7 +604,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 47)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -627,7 +627,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -638,7 +638,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -695,7 +695,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 71)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -709,7 +709,7 @@ int test_builder_binary(Toy_Bucket** bucketHandle) { return 0; } -int test_builder_keywords(Toy_Bucket** bucketHandle) { +int test_compiler_keywords(Toy_Bucket** bucketHandle) { //assert { //setup @@ -722,7 +722,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -733,7 +733,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -757,7 +757,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 35)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -780,7 +780,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -791,7 +791,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -820,7 +820,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 39)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -843,7 +843,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -862,7 +862,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -910,7 +910,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -933,7 +933,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -952,7 +952,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1024,7 +1024,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1047,7 +1047,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* ptr = (int*)buffer; @@ -1058,7 +1058,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { (ptr++)[0] != 0 || //data count (ptr++)[0] != 0) //subs count { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1081,7 +1081,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { *((unsigned char*)(buffer + 39)) != 0 ) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1104,7 +1104,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* header = (int*)buffer; @@ -1123,7 +1123,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1156,7 +1156,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1172,7 +1172,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder jumps, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module jumps, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1188,7 +1188,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder data, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module data, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1211,7 +1211,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); //run - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); //check header int* header = (int*)buffer; @@ -1230,7 +1230,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder header, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module header, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1263,7 +1263,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder code, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module code, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1279,7 +1279,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder jumps, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module jumps, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1295,7 +1295,7 @@ int test_builder_keywords(Toy_Bucket** bucketHandle) { false) { - fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module builder data, source: %s\n" TOY_CC_RESET, source); + fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected module data, source: %s\n" TOY_CC_RESET, source); //cleanup and return free(buffer); @@ -1315,7 +1315,7 @@ int main(void) { { Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); - res = test_builder_expressions(&bucket); + res = test_compiler_expressions(&bucket); Toy_freeBucket(&bucket); if (res == 0) { printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET); @@ -1325,7 +1325,7 @@ int main(void) { { Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); - res = test_builder_binary(&bucket); + res = test_compiler_binary(&bucket); Toy_freeBucket(&bucket); if (res == 0) { printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET); @@ -1335,7 +1335,7 @@ int main(void) { { Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); - res = test_builder_keywords(&bucket); + res = test_compiler_keywords(&bucket); Toy_freeBucket(&bucket); if (res == 0) { printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET); diff --git a/tests/cases/test_vm.c b/tests/cases/test_vm.c index 1fb6eee..c609810 100644 --- a/tests/cases/test_vm.c +++ b/tests/cases/test_vm.c @@ -3,7 +3,7 @@ #include "toy_lexer.h" #include "toy_parser.h" -#include "toy_module_builder.h" +#include "toy_module_compiler.h" #include "toy_print.h" #include @@ -19,7 +19,7 @@ unsigned char* makeCodeFromSource(Toy_Bucket** bucketHandle, const char* source) Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - return Toy_compileModuleBuilder(ast); + return Toy_compileModule(ast); } //tests @@ -36,7 +36,7 @@ int test_setup_and_teardown(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -82,7 +82,7 @@ int test_simple_execution(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -129,7 +129,7 @@ int test_opcode_not_equal(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -186,7 +186,7 @@ int test_keyword_assert(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -231,7 +231,7 @@ int test_keyword_assert(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -277,7 +277,7 @@ int test_keyword_assert(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -327,7 +327,7 @@ int test_keyword_print(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -373,7 +373,7 @@ int test_keyword_print(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -419,7 +419,7 @@ int test_keyword_print(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -469,7 +469,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -515,7 +515,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -560,7 +560,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -606,7 +606,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -655,7 +655,7 @@ int test_scope(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup @@ -704,7 +704,7 @@ int test_scope(Toy_Bucket** bucketHandle) { Toy_bindParser(&parser, &lexer); Toy_Ast* ast = Toy_scanParser(bucketHandle, &parser); - unsigned char* buffer = Toy_compileModuleBuilder(ast); + unsigned char* buffer = Toy_compileModule(ast); Toy_Module module = Toy_parseModule(buffer); //run the setup