mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
tired
This commit is contained in:
@@ -118,6 +118,10 @@ int _set(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(obj);
|
||||||
|
freeLiteral(key);
|
||||||
|
freeLiteral(val);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,6 +152,10 @@ int _set(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(obj);
|
||||||
|
freeLiteral(key);
|
||||||
|
freeLiteral(val);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,15 +191,20 @@ int _get(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, AS_ARRAY(obj)->literals[AS_INTEGER(key)]);
|
pushLiteralArray(&interpreter->stack, AS_ARRAY(obj)->literals[AS_INTEGER(key)]);
|
||||||
|
freeLiteral(obj);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LITERAL_DICTIONARY:
|
case LITERAL_DICTIONARY: {
|
||||||
pushLiteralArray(&interpreter->stack, getLiteralDictionary(AS_DICTIONARY(obj), key));
|
Literal dict = getLiteralDictionary(AS_DICTIONARY(obj), key);
|
||||||
|
pushLiteralArray(&interpreter->stack, dict);
|
||||||
|
freeLiteral(dict);
|
||||||
|
freeLiteral(obj);
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
(interpreter->printOutput)("Incorrect compound type in _get");
|
(interpreter->printOutput)("Incorrect compound type in _get ");
|
||||||
printLiteral(obj);
|
printLiteral(obj);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -238,11 +251,13 @@ int _push(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(obj);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
(interpreter->printOutput)("Incorrect compound type in _push");
|
(interpreter->printOutput)("Incorrect compound type in _push ");
|
||||||
printLiteral(obj);
|
printLiteral(obj);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -278,11 +293,13 @@ int _pop(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(obj);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
(interpreter->printOutput)("Incorrect compound type in _pop");
|
(interpreter->printOutput)("Incorrect compound type in _pop ");
|
||||||
printLiteral(obj);
|
printLiteral(obj);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -326,7 +343,7 @@ int _length(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
(interpreter->printOutput)("Incorrect compound type in _length");
|
(interpreter->printOutput)("Incorrect compound type in _length ");
|
||||||
printLiteral(obj);
|
printLiteral(obj);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -373,7 +390,7 @@ int _clear(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
|
|
||||||
freeLiteral(obj);
|
freeLiteral(obj);
|
||||||
|
|
||||||
return 1;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LITERAL_DICTIONARY: {
|
case LITERAL_DICTIONARY: {
|
||||||
@@ -391,14 +408,17 @@ int _clear(Interpreter* interpreter, LiteralArray* arguments) {
|
|||||||
|
|
||||||
freeLiteral(obj);
|
freeLiteral(obj);
|
||||||
|
|
||||||
return 1;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
(interpreter->printOutput)("Incorrect compound type in _get");
|
(interpreter->printOutput)("Incorrect compound type in _clear ");
|
||||||
printLiteral(obj);
|
printLiteral(obj);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(obj);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void initInterpreter(Interpreter* interpreter) {
|
void initInterpreter(Interpreter* interpreter) {
|
||||||
@@ -1378,7 +1398,9 @@ static bool execFnCall(Interpreter* interpreter) {
|
|||||||
initLiteralArray(&rest);
|
initLiteralArray(&rest);
|
||||||
|
|
||||||
while (arguments.count > 0) {
|
while (arguments.count > 0) {
|
||||||
pushLiteralArray(&rest, popLiteralArray(&arguments));
|
Literal lit = popLiteralArray(&arguments);
|
||||||
|
pushLiteralArray(&rest, lit);
|
||||||
|
freeLiteral(lit);
|
||||||
}
|
}
|
||||||
|
|
||||||
Literal restType = TO_TYPE_LITERAL(LITERAL_ARRAY, true);
|
Literal restType = TO_TYPE_LITERAL(LITERAL_ARRAY, true);
|
||||||
@@ -1407,6 +1429,8 @@ static bool execFnCall(Interpreter* interpreter) {
|
|||||||
freeInterpreter(&inner);
|
freeInterpreter(&inner);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(restType);
|
||||||
}
|
}
|
||||||
|
|
||||||
//execute the interpreter
|
//execute the interpreter
|
||||||
@@ -1426,19 +1450,16 @@ static bool execFnCall(Interpreter* interpreter) {
|
|||||||
freeLiteral(lit);
|
freeLiteral(lit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool returnValue = true;
|
||||||
|
|
||||||
//TODO: remove this when multiple assignment is enabled - note the BUGFIX that balances the stack
|
//TODO: remove this when multiple assignment is enabled - note the BUGFIX that balances the stack
|
||||||
if (returns.count > 1) {
|
if (returns.count > 1) {
|
||||||
printf(ERROR "ERROR: Too many values returned (multiple returns not yet implemented)\n" RESET);
|
printf(ERROR "ERROR: Too many values returned (multiple returns not yet implemented)\n" RESET);
|
||||||
|
|
||||||
//free, and skip out
|
returnValue = false;
|
||||||
freeLiteralArray(&returns);
|
|
||||||
freeLiteralArray(&arguments);
|
|
||||||
freeInterpreter(&inner);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < returns.count; i++) {
|
for (int i = 0; i < returns.count && returnValue; i++) {
|
||||||
Literal ret = popLiteralArray(&returns);
|
Literal ret = popLiteralArray(&returns);
|
||||||
|
|
||||||
//check the return types
|
//check the return types
|
||||||
@@ -1446,12 +1467,8 @@ static bool execFnCall(Interpreter* interpreter) {
|
|||||||
printf(ERROR "ERROR: bad type found in return value\n" RESET);
|
printf(ERROR "ERROR: bad type found in return value\n" RESET);
|
||||||
|
|
||||||
//free, and skip out
|
//free, and skip out
|
||||||
freeLiteral(ret);
|
returnValue = false;
|
||||||
freeLiteralArray(&returns);
|
break;
|
||||||
freeLiteralArray(&arguments);
|
|
||||||
freeInterpreter(&inner);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pushLiteralArray(&interpreter->stack, ret); //NOTE: reverses again
|
pushLiteralArray(&interpreter->stack, ret); //NOTE: reverses again
|
||||||
@@ -1459,6 +1476,19 @@ static bool execFnCall(Interpreter* interpreter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//free
|
//free
|
||||||
|
//BUGFIX: handle scopes of functions, which refer to the parent scope (leaking memory)
|
||||||
|
for (int i = 0; i < inner.scope->variables.capacity; i++) {
|
||||||
|
//handle keys, just in case
|
||||||
|
if (IS_FUNCTION(inner.scope->variables.entries[i].key)) {
|
||||||
|
popScope(AS_FUNCTION(inner.scope->variables.entries[i].key).scope);
|
||||||
|
AS_FUNCTION(inner.scope->variables.entries[i].key).scope = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IS_FUNCTION(inner.scope->variables.entries[i].value)) {
|
||||||
|
popScope(AS_FUNCTION(inner.scope->variables.entries[i].value).scope);
|
||||||
|
AS_FUNCTION(inner.scope->variables.entries[i].value).scope = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
freeLiteralArray(&returns);
|
freeLiteralArray(&returns);
|
||||||
freeLiteralArray(&arguments);
|
freeLiteralArray(&arguments);
|
||||||
freeLiteralArray(&inner.stack);
|
freeLiteralArray(&inner.stack);
|
||||||
|
|||||||
@@ -277,9 +277,12 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//compare the values
|
//compare the values
|
||||||
if (!literalsAreEqual(AS_DICTIONARY(lhs)->entries[i].value, getLiteralDictionary(AS_DICTIONARY(rhs), AS_DICTIONARY(lhs)->entries[i].key) )) {
|
Literal val = getLiteralDictionary(AS_DICTIONARY(rhs), AS_DICTIONARY(lhs)->entries[i].key); //TODO: could be more efficient
|
||||||
|
if (!literalsAreEqual(AS_DICTIONARY(lhs)->entries[i].value, val)) {
|
||||||
|
freeLiteral(val);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
freeLiteral(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ int pushLiteralArray(LiteralArray* array, Literal literal) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Literal popLiteralArray(LiteralArray* array) {
|
Literal popLiteralArray(LiteralArray* array) {
|
||||||
|
if (array->count <= 0) {
|
||||||
|
return TO_NULL_LITERAL;
|
||||||
|
}
|
||||||
|
|
||||||
//get the return
|
//get the return
|
||||||
Literal ret = array->literals[array->count-1];
|
Literal ret = array->literals[array->count-1];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user