mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Lots of runtime errors
This commit is contained in:
@@ -50,12 +50,14 @@ bool injectNativeFn(Interpreter* interpreter, char* name, NativeFn func) {
|
|||||||
bool parseIdentifierToValue(Interpreter* interpreter, Literal* literalPtr) {
|
bool parseIdentifierToValue(Interpreter* interpreter, Literal* literalPtr) {
|
||||||
//this converts identifiers to values
|
//this converts identifiers to values
|
||||||
if (IS_IDENTIFIER(*literalPtr)) {
|
if (IS_IDENTIFIER(*literalPtr)) {
|
||||||
|
// Literal idn = *literalPtr;
|
||||||
if (!getScopeVariable(interpreter->scope, *literalPtr, literalPtr)) {
|
if (!getScopeVariable(interpreter->scope, *literalPtr, literalPtr)) {
|
||||||
printf(ERROR "Error: Undeclared variable \"");;
|
printf(ERROR "Error: Undeclared variable \"");;
|
||||||
printLiteral(*literalPtr);
|
printLiteral(*literalPtr);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// freeLiteral(idn);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -417,19 +419,26 @@ static bool execAssert(Interpreter* interpreter) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool execPrint(Interpreter* interpreter) {
|
static bool execPrint(Interpreter* interpreter) {
|
||||||
//print what is on top of the stack, then pop it
|
//print what is on top of the stack, then pop it
|
||||||
Literal lit = popLiteralArray(&interpreter->stack);
|
Literal lit = popLiteralArray(&interpreter->stack);
|
||||||
if (!parseIdentifierToValue(interpreter, &lit)) {
|
|
||||||
return false;
|
if (IS_IDENTIFIER(lit)) {
|
||||||
|
Literal idn = lit;
|
||||||
|
if (!parseIdentifierToValue(interpreter, &lit)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printLiteralCustom(lit, interpreter->printOutput);
|
printLiteralCustom(lit, interpreter->printOutput);
|
||||||
|
|
||||||
// freeLiteral(lit); //it's a reference (to the dictionaries), so don't free it
|
freeLiteral(lit);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -460,6 +469,8 @@ static bool rawLiteral(Interpreter* interpreter) {
|
|||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, lit);
|
pushLiteralArray(&interpreter->stack, lit);
|
||||||
|
|
||||||
|
freeLiteral(lit);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -481,10 +492,16 @@ static bool execNegate(Interpreter* interpreter) {
|
|||||||
printf(ERROR "[internal] The interpreter can't negate that literal: ");
|
printf(ERROR "[internal] The interpreter can't negate that literal: ");
|
||||||
printLiteral(lit);
|
printLiteral(lit);
|
||||||
printf("\n" RESET);
|
printf("\n" RESET);
|
||||||
|
|
||||||
|
freeLiteral(lit);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, lit);
|
pushLiteralArray(&interpreter->stack, lit);
|
||||||
|
|
||||||
|
freeLiteral(lit);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -503,10 +520,16 @@ static bool execInvert(Interpreter* interpreter) {
|
|||||||
printf(ERROR "[internal] The interpreter can't invert that literal: ");
|
printf(ERROR "[internal] The interpreter can't invert that literal: ");
|
||||||
printLiteral(lit);
|
printLiteral(lit);
|
||||||
printf("\n" RESET);
|
printf("\n" RESET);
|
||||||
|
|
||||||
|
freeLiteral(lit);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, lit);
|
pushLiteralArray(&interpreter->stack, lit);
|
||||||
|
|
||||||
|
freeLiteral(lit);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,6 +554,8 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
|
|||||||
Literal literal = TO_STRING_LITERAL( copyString(buffer, strlen(buffer)), strlen(buffer) );
|
Literal literal = TO_STRING_LITERAL( copyString(buffer, strlen(buffer)), strlen(buffer) );
|
||||||
pushLiteralArray(&interpreter->stack, literal);
|
pushLiteralArray(&interpreter->stack, literal);
|
||||||
freeLiteral(literal);
|
freeLiteral(literal);
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -630,6 +655,10 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
|
|||||||
printf(" and ");
|
printf(" and ");
|
||||||
printLiteral(rhs);
|
printLiteral(rhs);
|
||||||
printf("\n" RESET);
|
printf("\n" RESET);
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -666,9 +695,17 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
|||||||
printf(ERROR "ERROR: Incorrect type assigned to variable \"");
|
printf(ERROR "ERROR: Incorrect type assigned to variable \"");
|
||||||
printLiteral(identifier);
|
printLiteral(identifier);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
|
|
||||||
|
freeLiteral(identifier);
|
||||||
|
freeLiteral(type);
|
||||||
|
freeLiteral(val);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(type);
|
||||||
|
freeLiteral(val);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -710,6 +747,8 @@ static bool execFnDecl(Interpreter* interpreter, bool lng) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(type);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -740,6 +779,9 @@ static bool execVarAssign(Interpreter* interpreter) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -752,6 +794,9 @@ static bool execVarArithmeticAssign(Interpreter* interpreter) {
|
|||||||
pushLiteralArray(&interpreter->stack, lhs);
|
pushLiteralArray(&interpreter->stack, lhs);
|
||||||
pushLiteralArray(&interpreter->stack, rhs);
|
pushLiteralArray(&interpreter->stack, rhs);
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -767,6 +812,10 @@ static bool execValCast(Interpreter* interpreter) {
|
|||||||
|
|
||||||
if (IS_NULL(value)) {
|
if (IS_NULL(value)) {
|
||||||
printf(ERROR "ERROR: Can't cast a null value\n" RESET);
|
printf(ERROR "ERROR: Can't cast a null value\n" RESET);
|
||||||
|
|
||||||
|
freeLiteral(value);
|
||||||
|
freeLiteral(type);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -835,6 +884,11 @@ static bool execValCast(Interpreter* interpreter) {
|
|||||||
|
|
||||||
//leave the new value on the stack
|
//leave the new value on the stack
|
||||||
pushLiteralArray(&interpreter->stack, result);
|
pushLiteralArray(&interpreter->stack, result);
|
||||||
|
|
||||||
|
freeLiteral(result);
|
||||||
|
freeLiteral(value);
|
||||||
|
freeLiteral(type);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -853,6 +907,9 @@ static bool execCompareEqual(Interpreter* interpreter, bool invert) {
|
|||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(result));
|
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(result));
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -868,6 +925,8 @@ static bool execCompareLess(Interpreter* interpreter, bool invert) {
|
|||||||
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
||||||
printLiteral(lhs);
|
printLiteral(lhs);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -875,6 +934,8 @@ static bool execCompareLess(Interpreter* interpreter, bool invert) {
|
|||||||
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
||||||
printLiteral(rhs);
|
printLiteral(rhs);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -898,6 +959,9 @@ static bool execCompareLess(Interpreter* interpreter, bool invert) {
|
|||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(result));
|
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(result));
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -913,6 +977,8 @@ static bool execCompareLessEqual(Interpreter* interpreter, bool invert) {
|
|||||||
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
||||||
printLiteral(lhs);
|
printLiteral(lhs);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -920,6 +986,8 @@ static bool execCompareLessEqual(Interpreter* interpreter, bool invert) {
|
|||||||
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
printf(ERROR "ERROR: Incorrect type in comparison, value \"");
|
||||||
printLiteral(rhs);
|
printLiteral(rhs);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -943,6 +1011,9 @@ static bool execCompareLessEqual(Interpreter* interpreter, bool invert) {
|
|||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(result));
|
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(result));
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -960,6 +1031,9 @@ static bool execAnd(Interpreter* interpreter) {
|
|||||||
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(false));
|
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -977,6 +1051,9 @@ static bool execOr(Interpreter* interpreter) {
|
|||||||
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(false));
|
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(lhs);
|
||||||
|
freeLiteral(rhs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1018,6 +1095,8 @@ static bool execFalseJump(Interpreter* interpreter) {
|
|||||||
interpreter->count = target + interpreter->codeStart;
|
interpreter->count = target + interpreter->codeStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(lit);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1042,6 +1121,7 @@ static bool execFnCall(Interpreter* interpreter) {
|
|||||||
|
|
||||||
if (!parseIdentifierToValue(interpreter, &func)) {
|
if (!parseIdentifierToValue(interpreter, &func)) {
|
||||||
freeLiteralArray(&arguments);
|
freeLiteralArray(&arguments);
|
||||||
|
freeLiteral(identifier);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1221,6 +1301,7 @@ static bool execFnReturn(Interpreter* interpreter) {
|
|||||||
Literal lit = popLiteralArray(&interpreter->stack);
|
Literal lit = popLiteralArray(&interpreter->stack);
|
||||||
parseIdentifierToValue(interpreter, &lit);
|
parseIdentifierToValue(interpreter, &lit);
|
||||||
pushLiteralArray(&returns, lit); //reverses the order
|
pushLiteralArray(&returns, lit); //reverses the order
|
||||||
|
freeLiteral(lit);
|
||||||
}
|
}
|
||||||
|
|
||||||
//and back again
|
//and back again
|
||||||
@@ -1291,7 +1372,7 @@ static void execInterpreter(Interpreter* interpreter) {
|
|||||||
case OP_VAR_MODULO_ASSIGN:
|
case OP_VAR_MODULO_ASSIGN:
|
||||||
execVarArithmeticAssign(interpreter);
|
execVarArithmeticAssign(interpreter);
|
||||||
if (!execArithmetic(interpreter, opcode)) {
|
if (!execArithmetic(interpreter, opcode)) {
|
||||||
popLiteralArray(&interpreter->stack);
|
freeLiteral(popLiteralArray(&interpreter->stack));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!execVarAssign(interpreter)) {
|
if (!execVarAssign(interpreter)) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "literal_array.h"
|
#include "literal_array.h"
|
||||||
#include "literal_dictionary.h"
|
#include "literal_dictionary.h"
|
||||||
|
#include "scope.h"
|
||||||
|
|
||||||
#include "console_colors.h"
|
#include "console_colors.h"
|
||||||
|
|
||||||
@@ -46,6 +47,10 @@ void freeLiteral(Literal literal) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IS_FUNCTION(literal)) {
|
||||||
|
popScope(AS_FUNCTION(literal).scope);
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_IDENTIFIER(literal)) {
|
if (IS_IDENTIFIER(literal)) {
|
||||||
FREE_ARRAY(char, AS_IDENTIFIER(literal), literal.as.identifier.length);
|
FREE_ARRAY(char, AS_IDENTIFIER(literal), literal.as.identifier.length);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ void freeLiteralDictionary(LiteralDictionary* dictionary) {
|
|||||||
|
|
||||||
void setLiteralDictionary(LiteralDictionary* dictionary, Literal key, Literal value) {
|
void setLiteralDictionary(LiteralDictionary* dictionary, Literal key, Literal value) {
|
||||||
if (IS_NULL(key)) {
|
if (IS_NULL(key)) {
|
||||||
fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys\n" RESET);
|
fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys (get)\n" RESET);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,14 +150,14 @@ void setLiteralDictionary(LiteralDictionary* dictionary, Literal key, Literal va
|
|||||||
|
|
||||||
Literal getLiteralDictionary(LiteralDictionary* dictionary, Literal key) {
|
Literal getLiteralDictionary(LiteralDictionary* dictionary, Literal key) {
|
||||||
if (IS_NULL(key)) {
|
if (IS_NULL(key)) {
|
||||||
fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys\n" RESET);
|
fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys (set)\n" RESET);
|
||||||
return TO_NULL_LITERAL;
|
return TO_NULL_LITERAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, hashLiteral(key), true);
|
_entry* entry = getEntryArray(dictionary->entries, dictionary->capacity, key, hashLiteral(key), true);
|
||||||
|
|
||||||
if (entry != NULL) {
|
if (entry != NULL) {
|
||||||
return entry->value;
|
return copyLiteral(entry->value);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return TO_NULL_LITERAL;
|
return TO_NULL_LITERAL;
|
||||||
@@ -166,7 +166,7 @@ Literal getLiteralDictionary(LiteralDictionary* dictionary, Literal key) {
|
|||||||
|
|
||||||
void removeLiteralDictionary(LiteralDictionary* dictionary, Literal key) {
|
void removeLiteralDictionary(LiteralDictionary* dictionary, Literal key) {
|
||||||
if (IS_NULL(key)) {
|
if (IS_NULL(key)) {
|
||||||
fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys\n" RESET);
|
fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys (remove)\n" RESET);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -142,11 +142,15 @@ Scope* copyScope(Scope* original) {
|
|||||||
|
|
||||||
//copy the contents of the dictionaries
|
//copy the contents of the dictionaries
|
||||||
for (int i = 0; i < original->variables.capacity; i++) {
|
for (int i = 0; i < original->variables.capacity; i++) {
|
||||||
setLiteralDictionary(&scope->variables, original->variables.entries[i].key, original->variables.entries[i].value);
|
if (!IS_NULL(original->variables.entries[i].key)) {
|
||||||
|
setLiteralDictionary(&scope->variables, original->variables.entries[i].key, original->variables.entries[i].value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < original->variables.capacity; i++) {
|
for (int i = 0; i < original->types.capacity; i++) {
|
||||||
setLiteralDictionary(&scope->types, original->types.entries[i].key, original->types.entries[i].value);
|
if (!IS_NULL(original->types.entries[i].key)) {
|
||||||
|
setLiteralDictionary(&scope->types, original->types.entries[i].key, original->types.entries[i].value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return scope;
|
return scope;
|
||||||
|
|||||||
@@ -45,6 +45,67 @@ char* readFile(char* path, size_t* fileSize) {
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned char* compileString(char* source, size_t* size) {
|
||||||
|
Lexer lexer;
|
||||||
|
Parser parser;
|
||||||
|
Compiler compiler;
|
||||||
|
|
||||||
|
initLexer(&lexer, source);
|
||||||
|
initParser(&parser, &lexer);
|
||||||
|
initCompiler(&compiler);
|
||||||
|
|
||||||
|
//run the parser until the end of the source
|
||||||
|
Node* node = scanParser(&parser);
|
||||||
|
while(node != NULL) {
|
||||||
|
//pack up and leave
|
||||||
|
if (node->type == NODE_ERROR) {
|
||||||
|
printf(ERROR "error node detected\n" RESET);
|
||||||
|
freeNode(node);
|
||||||
|
freeCompiler(&compiler);
|
||||||
|
freeParser(&parser);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCompiler(&compiler, node);
|
||||||
|
freeNode(node);
|
||||||
|
node = scanParser(&parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
//get the bytecode dump
|
||||||
|
unsigned char* tb = collateCompiler(&compiler, (int*)(size));
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
freeCompiler(&compiler);
|
||||||
|
freeParser(&parser);
|
||||||
|
//no lexer to clean up
|
||||||
|
|
||||||
|
//finally
|
||||||
|
return tb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void runBinary(unsigned char* tb, size_t size) {
|
||||||
|
Interpreter interpreter;
|
||||||
|
initInterpreter(&interpreter);
|
||||||
|
runInterpreter(&interpreter, tb, size);
|
||||||
|
freeInterpreter(&interpreter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void runSource(char* source) {
|
||||||
|
size_t size = 0;
|
||||||
|
unsigned char* tb = compileString(source, &size);
|
||||||
|
if (!tb) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
runBinary(tb, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void runSourceFile(char* fname) {
|
||||||
|
size_t size = 0; //not used
|
||||||
|
char* source = readFile(fname, &size);
|
||||||
|
runSource(source);
|
||||||
|
free((void*)source);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
{
|
{
|
||||||
//test init & free
|
//test init & free
|
||||||
@@ -88,39 +149,31 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
//source
|
//run each file in ../scripts/test/
|
||||||
size_t sourceLength = 0;
|
int count = 12;
|
||||||
char* source = readFile("sample_code.toy", &sourceLength);
|
char* filenames[] = {
|
||||||
|
"arithmetic.toy",
|
||||||
|
"casting.toy",
|
||||||
|
"comparisons.toy",
|
||||||
|
"functions.toy",
|
||||||
|
"jumps.toy",
|
||||||
|
"logicals.toy",
|
||||||
|
"long-array.toy",
|
||||||
|
"long-dictionary.toy",
|
||||||
|
"long-literals.toy",
|
||||||
|
"native-functions.toy",
|
||||||
|
"panic-within-functions.toy",
|
||||||
|
"types.toy"
|
||||||
|
};
|
||||||
|
|
||||||
//test basic compilation & collation
|
for (int i = 0; i < count; i++) {
|
||||||
Lexer lexer;
|
printf("Running %s\n", filenames[i]);
|
||||||
Parser parser;
|
|
||||||
Compiler compiler;
|
|
||||||
Interpreter interpreter;
|
|
||||||
|
|
||||||
initLexer(&lexer, source);
|
char buffer[128];
|
||||||
initParser(&parser, &lexer);
|
snprintf(buffer, 128, "../scripts/test/%s", filenames[i]);
|
||||||
initCompiler(&compiler);
|
|
||||||
initInterpreter(&interpreter);
|
|
||||||
|
|
||||||
Node* node = scanParser(&parser);
|
runSourceFile(buffer);
|
||||||
|
}
|
||||||
//write
|
|
||||||
writeCompiler(&compiler, node);
|
|
||||||
|
|
||||||
//collate
|
|
||||||
int size = 0;
|
|
||||||
unsigned char* bytecode = collateCompiler(&compiler, &size);
|
|
||||||
|
|
||||||
//run
|
|
||||||
runInterpreter(&interpreter, bytecode, size);
|
|
||||||
|
|
||||||
//cleanup
|
|
||||||
FREE_ARRAY(char, source, sourceLength);
|
|
||||||
freeNode(node);
|
|
||||||
freeParser(&parser);
|
|
||||||
freeCompiler(&compiler);
|
|
||||||
freeInterpreter(&interpreter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(NOTICE "All good\n" RESET);
|
printf(NOTICE "All good\n" RESET);
|
||||||
|
|||||||
Reference in New Issue
Block a user