mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Tweaked some APIs, hid some functions I don't want in the API
This commit is contained in:
@@ -75,7 +75,7 @@ void repl(const char* initialInput) {
|
|||||||
|
|
||||||
if (!error) {
|
if (!error) {
|
||||||
//get the bytecode dump
|
//get the bytecode dump
|
||||||
int size = 0;
|
size_t size = 0;
|
||||||
unsigned char* tb = Toy_collateCompiler(&compiler, &size);
|
unsigned char* tb = Toy_collateCompiler(&compiler, &size);
|
||||||
|
|
||||||
//run the bytecode
|
//run the bytecode
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ const unsigned char* Toy_compileString(const char* source, size_t* size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//get the bytecode dump
|
//get the bytecode dump
|
||||||
const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
|
const unsigned char* tb = Toy_collateCompiler(&compiler, size);
|
||||||
|
|
||||||
//cleanup
|
//cleanup
|
||||||
Toy_freeCompiler(&compiler);
|
Toy_freeCompiler(&compiler);
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ typedef struct {
|
|||||||
bool verbose;
|
bool verbose;
|
||||||
} Toy_CommandLine;
|
} Toy_CommandLine;
|
||||||
|
|
||||||
|
//these are intended for the repl only, despite using the api prefix
|
||||||
TOY_API Toy_CommandLine Toy_commandLine;
|
TOY_API Toy_CommandLine Toy_commandLine;
|
||||||
|
|
||||||
TOY_API void Toy_initCommandLine(int argc, const char* argv[]);
|
TOY_API void Toy_initCommandLine(int argc, const char* argv[]);
|
||||||
|
|||||||
@@ -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
|
if (op != TOY_OP_EOF) {//compensate for indexing & dot notation being screwy
|
||||||
compiler->bytecode[compiler->count++] = (unsigned char)op; //1 byte
|
compiler->bytecode[compiler->count++] = (unsigned char)op; //1 byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: could free up AST Nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
void Toy_freeCompiler(Toy_Compiler* compiler) {
|
void Toy_freeCompiler(Toy_Compiler* compiler) {
|
||||||
@@ -989,6 +991,7 @@ void Toy_freeCompiler(Toy_Compiler* compiler) {
|
|||||||
compiler->bytecode = NULL;
|
compiler->bytecode = NULL;
|
||||||
compiler->capacity = 0;
|
compiler->capacity = 0;
|
||||||
compiler->count = 0;
|
compiler->count = 0;
|
||||||
|
compiler->panic = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void emitByte(unsigned char** collationPtr, int* capacityPtr, int* countPtr, unsigned char byte) {
|
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
|
//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) {
|
if (compiler->panic) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "[internal] Can't collate a panicked compiler\n" TOY_CC_RESET);
|
fprintf(stderr, TOY_CC_ERROR "[internal] Can't collate a panicked compiler\n" TOY_CC_RESET);
|
||||||
return NULL;
|
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
|
void* fnCompiler = TOY_AS_FUNCTION(fn).inner.bytecode; //store the compiler here for now
|
||||||
|
|
||||||
//collate the function into bytecode (without header)
|
//collate the function into bytecode (without header)
|
||||||
int size = 0;
|
size_t size = 0;
|
||||||
unsigned char* bytes = collateCompilerHeaderOpt((Toy_Compiler*)fnCompiler, &size, false);
|
unsigned char* bytes = collateCompilerHeaderOpt((Toy_Compiler*)fnCompiler, &size, false);
|
||||||
|
|
||||||
//emit how long this section is, +1 for ending mark
|
//emit how long this section is, +1 for ending mark
|
||||||
Toy_emitShort(&fnCollation, &fnCapacity, &fnCount, (unsigned short)size + 1);
|
Toy_emitShort(&fnCollation, &fnCapacity, &fnCount, (unsigned short)size + 1);
|
||||||
|
|
||||||
//write the fn to the fn collation
|
//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]);
|
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
|
//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);
|
return collateCompilerHeaderOpt(compiler, size, true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,4 +19,4 @@ TOY_API void Toy_writeCompiler(Toy_Compiler* compiler, Toy_ASTNode* node);
|
|||||||
TOY_API void Toy_freeCompiler(Toy_Compiler* compiler);
|
TOY_API void Toy_freeCompiler(Toy_Compiler* compiler);
|
||||||
|
|
||||||
//embed the header, data section, code section, function section, etc.
|
//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);
|
||||||
|
|||||||
@@ -21,9 +21,7 @@ static void printWrapper(const char* output) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void assertWrapper(const char* output) {
|
static void assertWrapper(const char* output) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "Assertion failure: ");
|
fprintf(stderr, TOY_CC_ERROR "Assertion failure: %s\n" TOY_CC_RESET, output);
|
||||||
fprintf(stderr, "%s", output);
|
|
||||||
fprintf(stderr, "\n" TOY_CC_RESET); //default new line
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void errorWrapper(const char* 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) {
|
bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_LiteralArray* arguments, Toy_LiteralArray* returns) {
|
||||||
//check for side-loaded native functions
|
//check for side-loaded native functions
|
||||||
if (TOY_IS_FUNCTION_NATIVE(func)) {
|
if (TOY_IS_FUNCTION_NATIVE(func)) {
|
||||||
|
//TODO: parse out identifier values, see issue #64
|
||||||
|
|
||||||
//call the native function
|
//call the native function
|
||||||
int returnsCount = TOY_AS_FUNCTION_NATIVE(func)(interpreter, arguments);
|
int returnsCount = TOY_AS_FUNCTION_NATIVE(func)(interpreter, arguments);
|
||||||
|
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ static Toy_Token makeErrorToken(Toy_Lexer* lexer, char* msg) {
|
|||||||
#ifndef TOY_EXPORT
|
#ifndef TOY_EXPORT
|
||||||
if (Toy_commandLine.verbose) {
|
if (Toy_commandLine.verbose) {
|
||||||
printf("err:");
|
printf("err:");
|
||||||
Toy_printToken(&token);
|
Toy_private_printToken(&token);
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
//BUG #10: this shows TOKEN_EOF twice due to the overarching structure of the program - can't be fixed
|
||||||
if (Toy_commandLine.verbose) {
|
if (Toy_commandLine.verbose) {
|
||||||
printf("tok:");
|
printf("tok:");
|
||||||
Toy_printToken(&token);
|
Toy_private_printToken(&token);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -168,7 +168,7 @@ static Toy_Token makeIntegerOrFloat(Toy_Lexer* lexer) {
|
|||||||
} else {
|
} else {
|
||||||
printf("flt:");
|
printf("flt:");
|
||||||
}
|
}
|
||||||
Toy_printToken(&token);
|
Toy_private_printToken(&token);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -221,7 +221,7 @@ static Toy_Token makeString(Toy_Lexer* lexer, char terminator) {
|
|||||||
#ifndef TOY_EXPORT
|
#ifndef TOY_EXPORT
|
||||||
if (Toy_commandLine.verbose) {
|
if (Toy_commandLine.verbose) {
|
||||||
printf("str:");
|
printf("str:");
|
||||||
Toy_printToken(&token);
|
Toy_private_printToken(&token);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -248,7 +248,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) {
|
|||||||
#ifndef TOY_EXPORT
|
#ifndef TOY_EXPORT
|
||||||
if (Toy_commandLine.verbose) {
|
if (Toy_commandLine.verbose) {
|
||||||
printf("kwd:");
|
printf("kwd:");
|
||||||
Toy_printToken(&token);
|
Toy_private_printToken(&token);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -267,7 +267,7 @@ static Toy_Token makeKeywordOrIdentifier(Toy_Lexer* lexer) {
|
|||||||
#ifndef TOY_EXPORT
|
#ifndef TOY_EXPORT
|
||||||
if (Toy_commandLine.verbose) {
|
if (Toy_commandLine.verbose) {
|
||||||
printf("idf:");
|
printf("idf:");
|
||||||
Toy_printToken(&token);
|
Toy_private_printToken(&token);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -281,7 +281,7 @@ void Toy_initLexer(Toy_Lexer* lexer, const char* source) {
|
|||||||
lexer->source = source;
|
lexer->source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Token Toy_scanLexer(Toy_Lexer* lexer) {
|
Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer) {
|
||||||
eatWhitespace(lexer);
|
eatWhitespace(lexer);
|
||||||
|
|
||||||
lexer->start = lexer->current;
|
lexer->start = lexer->current;
|
||||||
@@ -352,7 +352,7 @@ static void trim(char** s, int* l) { //all this to remove a newline?
|
|||||||
}
|
}
|
||||||
|
|
||||||
//for debugging
|
//for debugging
|
||||||
void Toy_printToken(Toy_Token* token) {
|
void Toy_private_printToken(Toy_Token* token) {
|
||||||
if (token->type == TOY_TOKEN_ERROR) {
|
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);
|
printf(TOY_CC_ERROR "Error\t%d\t%.*s\n" TOY_CC_RESET, token->line, token->length, token->lexeme);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ typedef struct {
|
|||||||
} Toy_Token;
|
} Toy_Token;
|
||||||
|
|
||||||
TOY_API void Toy_initLexer(Toy_Lexer* lexer, const char* source);
|
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
|
//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);
|
TOY_API void Toy_private_setComments(Toy_Lexer* lexer, bool enabled);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ static void error(Toy_Parser* parser, Toy_Token token, const char* message) {
|
|||||||
|
|
||||||
static void advance(Toy_Parser* parser) {
|
static void advance(Toy_Parser* parser) {
|
||||||
parser->previous = parser->current;
|
parser->previous = parser->current;
|
||||||
parser->current = Toy_scanLexer(parser->lexer);
|
parser->current = Toy_private_scanLexer(parser->lexer);
|
||||||
|
|
||||||
if (parser->current.type == TOY_TOKEN_ERROR) {
|
if (parser->current.type == TOY_TOKEN_ERROR) {
|
||||||
error(parser, parser->current, "Toy_Lexer error");
|
error(parser, parser->current, "Toy_Lexer error");
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ int main() {
|
|||||||
Toy_writeCompiler(&compiler, node);
|
Toy_writeCompiler(&compiler, node);
|
||||||
|
|
||||||
//collate
|
//collate
|
||||||
int size = 0;
|
size_t size = 0;
|
||||||
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
|
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
|
||||||
|
|
||||||
//cleanup
|
//cleanup
|
||||||
@@ -78,7 +78,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//collate
|
//collate
|
||||||
int size = 0;
|
size_t size = 0;
|
||||||
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
|
unsigned char* bytecode = Toy_collateCompiler(&compiler, &size);
|
||||||
|
|
||||||
//cleanup
|
//cleanup
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ int main() {
|
|||||||
Toy_writeCompiler(&compiler, node);
|
Toy_writeCompiler(&compiler, node);
|
||||||
|
|
||||||
//collate
|
//collate
|
||||||
int size = 0;
|
size_t size = 0;
|
||||||
const 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
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ int main() {
|
|||||||
Toy_initLexer(&lexer, source);
|
Toy_initLexer(&lexer, source);
|
||||||
|
|
||||||
//get each token
|
//get each token
|
||||||
Toy_Token print = Toy_scanLexer(&lexer);
|
Toy_Token print = Toy_private_scanLexer(&lexer);
|
||||||
Toy_Token null = Toy_scanLexer(&lexer);
|
Toy_Token null = Toy_private_scanLexer(&lexer);
|
||||||
Toy_Token semi = Toy_scanLexer(&lexer);
|
Toy_Token semi = Toy_private_scanLexer(&lexer);
|
||||||
Toy_Token eof = Toy_scanLexer(&lexer);
|
Toy_Token eof = Toy_private_scanLexer(&lexer);
|
||||||
|
|
||||||
//test each token is correct
|
//test each token is correct
|
||||||
if (strncmp(print.lexeme, "print", print.length)) {
|
if (strncmp(print.lexeme, "print", print.length)) {
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ const unsigned char* compileStringCustom(const char* source, size_t* size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//get the bytecode dump
|
//get the bytecode dump
|
||||||
const unsigned char* tb = Toy_collateCompiler(&compiler, (int*)(size));
|
const unsigned char* tb = Toy_collateCompiler(&compiler, size);
|
||||||
|
|
||||||
//cleanup
|
//cleanup
|
||||||
Toy_freeCompiler(&compiler);
|
Toy_freeCompiler(&compiler);
|
||||||
|
|||||||
Reference in New Issue
Block a user