diff --git a/repl/repl_main.c b/repl/repl_main.c index 6a7a85e..dc8d002 100644 --- a/repl/repl_main.c +++ b/repl/repl_main.c @@ -75,7 +75,7 @@ void repl(const char* initialInput) { if (!error) { //get the bytecode dump - int size = 0; + size_t size = 0; unsigned char* tb = Toy_collateCompiler(&compiler, &size); //run the bytecode diff --git a/repl/repl_tools.c b/repl/repl_tools.c index 141ba4d..2d13715 100644 --- a/repl/repl_tools.c +++ b/repl/repl_tools.c @@ -94,7 +94,7 @@ const unsigned char* Toy_compileString(const char* source, size_t* size) { } //get the bytecode dump - const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size)); + const unsigned char* tb = Toy_collateCompiler(&compiler, size); //cleanup Toy_freeCompiler(&compiler); diff --git a/source/toy_common.h b/source/toy_common.h index 52afdf1..9eb3d7b 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -43,6 +43,7 @@ typedef struct { bool verbose; } Toy_CommandLine; +//these are intended for the repl only, despite using the api prefix TOY_API Toy_CommandLine Toy_commandLine; TOY_API void Toy_initCommandLine(int argc, const char* argv[]); diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 804ba36..dcdaef3 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -981,6 +981,8 @@ void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node) { if (op != TOY_OP_EOF) {//compensate for indexing & dot notation being screwy compiler->bytecode[compiler->count++] = (unsigned char)op; //1 byte } + + //TODO: could free up AST Nodes } void Toy_freeCompiler(Toy_Compiler* compiler) { @@ -989,6 +991,7 @@ void Toy_freeCompiler(Toy_Compiler* compiler) { compiler->bytecode = NULL; compiler->capacity = 0; compiler->count = 0; + compiler->panic = false; } static void emitByte(unsigned char** collationPtr, int* capacityPtr, int* countPtr, unsigned char byte) { @@ -1036,7 +1039,7 @@ static void emitFloat(unsigned char** collationPtr, int* capacityPtr, int* count } //return the result -static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size, bool embedHeader) { +static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, size_t* size, bool embedHeader) { if (compiler->panic) { fprintf(stderr, TOY_CC_ERROR "[internal] Can't collate a panicked compiler\n" TOY_CC_RESET); return NULL; @@ -1181,14 +1184,14 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size void* fnCompiler = TOY_AS_FUNCTION(fn).inner.bytecode; //store the compiler here for now //collate the function into bytecode (without header) - int size = 0; + size_t size = 0; unsigned char* bytes = collateCompilerHeaderOpt((Toy_Compiler*)fnCompiler, &size, false); //emit how long this section is, +1 for ending mark Toy_emitShort(&fnCollation, &fnCapacity, &fnCount, (unsigned short)size + 1); //write the fn to the fn collation - for (int i = 0; i < size; i++) { + for (size_t i = 0; i < size; i++) { emitByte(&fnCollation, &fnCapacity, &fnCount, bytes[i]); } @@ -1296,6 +1299,6 @@ static unsigned char* collateCompilerHeaderOpt(Toy_Compiler* compiler, int* size } //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, size_t* size) { return collateCompilerHeaderOpt(compiler, size, true); } diff --git a/source/toy_compiler.h b/source/toy_compiler.h index 08b8d44..58f986c 100644 --- a/source/toy_compiler.h +++ b/source/toy_compiler.h @@ -19,4 +19,4 @@ TOY_API void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node); TOY_API void Toy_freeCompiler(Toy_Compiler* compiler); //embed the header, data section, code section, function section, etc. -TOY_API unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, int* size); +TOY_API unsigned char* Toy_collateCompiler(Toy_Compiler* compiler, size_t* size); diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index a4eb387..5f8f47a 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -21,9 +21,7 @@ static void printWrapper(const char* output) { } static void assertWrapper(const char* output) { - fprintf(stderr, TOY_CC_ERROR "Assertion failure: "); - fprintf(stderr, "%s", output); - fprintf(stderr, "\n" TOY_CC_RESET); //default new line + fprintf(stderr, TOY_CC_ERROR "Assertion failure: %s\n" TOY_CC_RESET, output); } static void errorWrapper(const char* output) { @@ -1179,6 +1177,8 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns) { //check for side-loaded native functions if (TOY_IS_FUNCTION_NATIVE(func)) { + //TODO: parse out identifier values, see issue #64 + //call the native function int returnsCount = TOY_AS_FUNCTION_NATIVE(func)(interpreter, arguments); diff --git a/source/toy_lexer.c b/source/toy_lexer.c index 333592a..80dd566 100644 --- a/source/toy_lexer.c +++ b/source/toy_lexer.c @@ -117,7 +117,7 @@ static Toy_Token makeErrorToken(Toy_Lexer* lexer, char* msg) { #ifndef TOY_EXPORT if (Toy_commandLine.verbose) { printf("err:"); - Toy_printToken(&token); + Toy_private_printToken(&token); } #endif @@ -136,7 +136,7 @@ static Toy_Token makeToken(Toy_Lexer* lexer, Toy_TokenType type) { //BUG #10: this shows TOKEN_EOF twice due to the overarching structure of the program - can't be fixed if (Toy_commandLine.verbose) { printf("tok:"); - Toy_printToken(&token); + Toy_private_printToken(&token); } #endif @@ -168,7 +168,7 @@ static Toy_Token makeIntegerOrFloat(Toy_Lexer* lexer) { } else { printf("flt:"); } - Toy_printToken(&token); + Toy_private_printToken(&token); } #endif @@ -221,7 +221,7 @@ static Toy_Token makeString(Toy_Lexer* lexer, char terminator) { #ifndef TOY_EXPORT if (Toy_commandLine.verbose) { printf("str:"); - Toy_printToken(&token); + Toy_private_printToken(&token); } #endif @@ -248,7 +248,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) { #ifndef TOY_EXPORT if (Toy_commandLine.verbose) { printf("kwd:"); - Toy_printToken(&token); + Toy_private_printToken(&token); } #endif @@ -267,7 +267,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) { #ifndef TOY_EXPORT if (Toy_commandLine.verbose) { printf("idf:"); - Toy_printToken(&token); + Toy_private_printToken(&token); } #endif @@ -281,7 +281,7 @@ void Toy_initLexer(Toy_Lexer* lexer, const char* source) { lexer->source = source; } -Toy_Token Toy_scanLexer(Toy_Lexer* lexer) { +Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer) { eatWhitespace(lexer); lexer->start = lexer->current; @@ -352,7 +352,7 @@ static void trim(char** s, int* l) { //all this to remove a newline? } //for debugging -void Toy_printToken(Toy_Token* token) { +void Toy_private_printToken(Toy_Token* token) { if (token->type == TOY_TOKEN_ERROR) { printf(TOY_CC_ERROR "Error\t%d\t%.*s\n" TOY_CC_RESET, token->line, token->length, token->lexeme); return; diff --git a/source/toy_lexer.h b/source/toy_lexer.h index af361b3..c45f237 100644 --- a/source/toy_lexer.h +++ b/source/toy_lexer.h @@ -21,9 +21,9 @@ typedef struct { } Toy_Token; TOY_API void Toy_initLexer(Toy_Lexer* lexer, const char* source); -TOY_API Toy_Token Toy_scanLexer(Toy_Lexer* lexer); +TOY_API Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer); //for debugging -TOY_API void Toy_printToken(Toy_Token* token); +TOY_API void Toy_private_printToken(Toy_Token* token); TOY_API void Toy_private_setComments(Toy_Lexer* lexer, bool enabled); diff --git a/source/toy_parser.c b/source/toy_parser.c index 3e6fb7e..9f5dbd2 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -32,7 +32,7 @@ static void error(Toy_Parser* parser, Toy_Token token, const char* message) { static void advance(Toy_Parser* parser) { parser->previous = parser->current; - parser->current = Toy_scanLexer(parser->lexer); + parser->current = Toy_private_scanLexer(parser->lexer); if (parser->current.type == TOY_TOKEN_ERROR) { error(parser, parser->current, "Toy_Lexer error"); diff --git a/test/test_compiler.c b/test/test_compiler.c index d4b0a9f..c3c3b80 100644 --- a/test/test_compiler.c +++ b/test/test_compiler.c @@ -39,7 +39,7 @@ int main() { Toy_writeCompiler(&compiler, node); //collate - int size = 0; + size_t size = 0; unsigned char* bytecode = Toy_collateCompiler(&compiler, &size); //cleanup @@ -78,7 +78,7 @@ int main() { } //collate - int size = 0; + size_t size = 0; unsigned char* bytecode = Toy_collateCompiler(&compiler, &size); //cleanup diff --git a/test/test_interpreter.c b/test/test_interpreter.c index 3de9107..12c3ab5 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -87,7 +87,7 @@ int main() { Toy_writeCompiler(&compiler, node); //collate - int size = 0; + size_t size = 0; const unsigned char* bytecode = Toy_collateCompiler(&compiler, &size); //NOTE: suppress print output for testing diff --git a/test/test_lexer.c b/test/test_lexer.c index 41bd34c..a6df522 100644 --- a/test/test_lexer.c +++ b/test/test_lexer.c @@ -15,10 +15,10 @@ int main() { Toy_initLexer(&lexer, source); //get each token - Toy_Token print = Toy_scanLexer(&lexer); - Toy_Token null = Toy_scanLexer(&lexer); - Toy_Token semi = Toy_scanLexer(&lexer); - Toy_Token eof = Toy_scanLexer(&lexer); + Toy_Token print = Toy_private_scanLexer(&lexer); + Toy_Token null = Toy_private_scanLexer(&lexer); + Toy_Token semi = Toy_private_scanLexer(&lexer); + Toy_Token eof = Toy_private_scanLexer(&lexer); //test each token is correct if (strncmp(print.lexeme, "print", print.length)) { diff --git a/test/test_mustfail.c b/test/test_mustfail.c index 42997ae..5952bc9 100644 --- a/test/test_mustfail.c +++ b/test/test_mustfail.c @@ -50,7 +50,7 @@ const unsigned char* compileStringCustom(const char* source, size_t* size) { } //get the bytecode dump - const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size)); + const unsigned char* tb = Toy_collateCompiler(&compiler, size); //cleanup Toy_freeCompiler(&compiler);