Tweaked some APIs, hid some functions I don't want in the API

This commit is contained in:
2023-02-16 13:06:07 +00:00
parent 501ff6fff4
commit 1668dca255
13 changed files with 33 additions and 29 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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