mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
I feel like there's just a few bugs left
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
print "hello world";
|
var a : [any] = [0];
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ bool injectNativeFn(Interpreter* interpreter, char* name, NativeFn func) {
|
|||||||
setLiteralDictionary(&interpreter->scope->types, identifier, type);
|
setLiteralDictionary(&interpreter->scope->types, identifier, type);
|
||||||
|
|
||||||
freeLiteral(identifier);
|
freeLiteral(identifier);
|
||||||
|
freeLiteral(type);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -678,7 +679,11 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
|||||||
Literal identifier = interpreter->literalCache.literals[identifierIndex];
|
Literal identifier = interpreter->literalCache.literals[identifierIndex];
|
||||||
Literal type = interpreter->literalCache.literals[typeIndex];
|
Literal type = interpreter->literalCache.literals[typeIndex];
|
||||||
|
|
||||||
parseIdentifierToValue(interpreter, &type);
|
bool freeType = false;
|
||||||
|
if (IS_IDENTIFIER(type)) {
|
||||||
|
parseIdentifierToValue(interpreter, &type);
|
||||||
|
freeType = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (!declareScopeVariable(interpreter->scope, identifier, type)) {
|
if (!declareScopeVariable(interpreter->scope, identifier, type)) {
|
||||||
printf(ERROR "ERROR: Can't redefine the variable \"");
|
printf(ERROR "ERROR: Can't redefine the variable \"");
|
||||||
@@ -695,7 +700,7 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
|||||||
printLiteral(identifier);
|
printLiteral(identifier);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
|
|
||||||
freeLiteral(identifier);
|
freeLiteral(identifier); //TODO: test this
|
||||||
freeLiteral(type);
|
freeLiteral(type);
|
||||||
freeLiteral(val);
|
freeLiteral(val);
|
||||||
|
|
||||||
@@ -704,6 +709,10 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
|||||||
|
|
||||||
freeLiteral(val);
|
freeLiteral(val);
|
||||||
|
|
||||||
|
if (freeType) {
|
||||||
|
freeLiteral(type);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -711,7 +720,6 @@ static bool execFnDecl(Interpreter* interpreter, bool lng) {
|
|||||||
//read the index in the cache
|
//read the index in the cache
|
||||||
int identifierIndex = 0;
|
int identifierIndex = 0;
|
||||||
int functionIndex = 0;
|
int functionIndex = 0;
|
||||||
Scope* scope = pushScope(interpreter->scope);
|
|
||||||
|
|
||||||
if (lng) {
|
if (lng) {
|
||||||
identifierIndex = (int)readShort(interpreter->bytecode, &interpreter->count);
|
identifierIndex = (int)readShort(interpreter->bytecode, &interpreter->count);
|
||||||
@@ -725,7 +733,7 @@ static bool execFnDecl(Interpreter* interpreter, bool lng) {
|
|||||||
Literal identifier = interpreter->literalCache.literals[identifierIndex];
|
Literal identifier = interpreter->literalCache.literals[identifierIndex];
|
||||||
Literal function = interpreter->literalCache.literals[functionIndex];
|
Literal function = interpreter->literalCache.literals[functionIndex];
|
||||||
|
|
||||||
function.as.function.scope = pushScope(scope); //hacked in
|
function.as.function.scope = pushScope(interpreter->scope); //hacked in
|
||||||
|
|
||||||
Literal type = TO_TYPE_LITERAL(LITERAL_FUNCTION, true);
|
Literal type = TO_TYPE_LITERAL(LITERAL_FUNCTION, true);
|
||||||
|
|
||||||
@@ -733,7 +741,6 @@ static bool execFnDecl(Interpreter* interpreter, bool lng) {
|
|||||||
printf(ERROR "ERROR: Can't redefine the function \"");
|
printf(ERROR "ERROR: Can't redefine the function \"");
|
||||||
printLiteral(identifier);
|
printLiteral(identifier);
|
||||||
printf("\"\n" RESET);
|
printf("\"\n" RESET);
|
||||||
popScope(scope);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -741,7 +748,6 @@ static bool execFnDecl(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);
|
||||||
popScope(scope);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1215,7 +1221,8 @@ static bool execFnCall(Interpreter* interpreter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Literal restType = TO_TYPE_LITERAL(LITERAL_ARRAY, true);
|
Literal restType = TO_TYPE_LITERAL(LITERAL_ARRAY, true);
|
||||||
TYPE_PUSH_SUBTYPE(&restType, TO_TYPE_LITERAL(LITERAL_ANY, false));
|
Literal any = TO_TYPE_LITERAL(LITERAL_ANY, false);
|
||||||
|
TYPE_PUSH_SUBTYPE(&restType, any);
|
||||||
|
|
||||||
//declare & define the rest parameter
|
//declare & define the rest parameter
|
||||||
if (!declareScopeVariable(inner.scope, restParam, restType)) {
|
if (!declareScopeVariable(inner.scope, restParam, restType)) {
|
||||||
@@ -1607,14 +1614,12 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
printf("(array ");
|
printf("(array ");
|
||||||
Literal literal = TO_ARRAY_LITERAL(array);
|
Literal literal = TO_ARRAY_LITERAL(array);
|
||||||
printLiteral(literal);
|
printLiteral(literal);
|
||||||
freeLiteral(literal);
|
|
||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//finally, push the array proper
|
//finally, push the array proper
|
||||||
Literal literal = TO_ARRAY_LITERAL(array);
|
Literal literal = TO_ARRAY_LITERAL(array);
|
||||||
pushLiteralArray(&interpreter->literalCache, literal);
|
pushLiteralArray(&interpreter->literalCache, literal);
|
||||||
freeLiteral(literal);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1635,14 +1640,12 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
printf("(dictionary ");
|
printf("(dictionary ");
|
||||||
Literal literal = TO_DICTIONARY_LITERAL(dictionary);
|
Literal literal = TO_DICTIONARY_LITERAL(dictionary);
|
||||||
printLiteral(literal);
|
printLiteral(literal);
|
||||||
freeLiteral(literal);
|
|
||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//finally, push the dictionary proper
|
//finally, push the dictionary proper
|
||||||
Literal literal = TO_DICTIONARY_LITERAL(dictionary);
|
Literal literal = TO_DICTIONARY_LITERAL(dictionary);
|
||||||
pushLiteralArray(&interpreter->literalCache, literal);
|
pushLiteralArray(&interpreter->literalCache, literal);
|
||||||
freeLiteral(literal);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1694,7 +1697,7 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
freeLiteral(typeLiteral);
|
// freeLiteral(typeLiteral);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1709,7 +1712,7 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
if (AS_TYPE(typeLiteral).typeOf == LITERAL_ARRAY) {
|
if (AS_TYPE(typeLiteral).typeOf == LITERAL_ARRAY) {
|
||||||
unsigned short vt = readShort(interpreter->bytecode, &interpreter->count);
|
unsigned short vt = readShort(interpreter->bytecode, &interpreter->count);
|
||||||
|
|
||||||
TYPE_PUSH_SUBTYPE(&typeLiteral, copyLiteral(interpreter->literalCache.literals[vt]));
|
TYPE_PUSH_SUBTYPE(&typeLiteral, interpreter->literalCache.literals[vt]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (AS_TYPE(typeLiteral).typeOf == LITERAL_DICTIONARY) {
|
if (AS_TYPE(typeLiteral).typeOf == LITERAL_DICTIONARY) {
|
||||||
@@ -1729,7 +1732,7 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// freeLiteral(typeLiteral);
|
// freeLiteral(typeLiteral);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1815,7 +1818,8 @@ void runInterpreter(Interpreter* interpreter, unsigned char* bytecode, int lengt
|
|||||||
|
|
||||||
//BUGFIX: clear the stack (for repl - stack must be balanced)
|
//BUGFIX: clear the stack (for repl - stack must be balanced)
|
||||||
while(interpreter->stack.count > 0) {
|
while(interpreter->stack.count > 0) {
|
||||||
popLiteralArray(&interpreter->stack);
|
Literal lit = popLiteralArray(&interpreter->stack);
|
||||||
|
freeLiteral(lit);
|
||||||
}
|
}
|
||||||
|
|
||||||
//free the bytecode immediately after use
|
//free the bytecode immediately after use
|
||||||
|
|||||||
@@ -48,7 +48,8 @@ void freeLiteral(Literal literal) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (IS_FUNCTION(literal)) {
|
if (IS_FUNCTION(literal)) {
|
||||||
popScope(AS_FUNCTION(literal).scope);
|
AS_FUNCTION(literal).scope = popScope(AS_FUNCTION(literal).scope);
|
||||||
|
FREE_ARRAY(unsigned char, AS_FUNCTION(literal).bytecode, AS_FUNCTION(literal).length);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_IDENTIFIER(literal)) {
|
if (IS_IDENTIFIER(literal)) {
|
||||||
@@ -119,7 +120,7 @@ Literal copyLiteral(Literal original) {
|
|||||||
|
|
||||||
//copy each element
|
//copy each element
|
||||||
for (int i = 0; i < AS_ARRAY(original)->count; i++) {
|
for (int i = 0; i < AS_ARRAY(original)->count; i++) {
|
||||||
pushLiteralArray(array, copyLiteral(AS_ARRAY(original)->literals[i]));
|
pushLiteralArray(array, AS_ARRAY(original)->literals[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TO_ARRAY_LITERAL(array);
|
return TO_ARRAY_LITERAL(array);
|
||||||
@@ -132,7 +133,7 @@ Literal copyLiteral(Literal original) {
|
|||||||
//copy each entry
|
//copy each entry
|
||||||
for (int i = 0; i < AS_DICTIONARY(original)->capacity; i++) {
|
for (int i = 0; i < AS_DICTIONARY(original)->capacity; i++) {
|
||||||
if ( !IS_NULL(AS_DICTIONARY(original)->entries[i].key) ) {
|
if ( !IS_NULL(AS_DICTIONARY(original)->entries[i].key) ) {
|
||||||
setLiteralDictionary(dictionary, copyLiteral(AS_DICTIONARY(original)->entries[i].key), copyLiteral(AS_DICTIONARY(original)->entries[i].value));
|
setLiteralDictionary(dictionary, AS_DICTIONARY(original)->entries[i].key, AS_DICTIONARY(original)->entries[i].value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -213,6 +213,9 @@ bool setScopeVariable(Scope* scope, Literal key, Literal value, bool constCheck)
|
|||||||
|
|
||||||
//actually assign
|
//actually assign
|
||||||
setLiteralDictionary(&scope->variables, key, value);
|
setLiteralDictionary(&scope->variables, key, value);
|
||||||
|
|
||||||
|
freeLiteral(typeLiteral);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user