diff --git a/scripts/small.toy b/scripts/small.toy index aab6c90..2b56c10 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,52 +1,8 @@ -//assume the args are matrices -fn matrix(first, second) { - //get the matrix size - var l1 = _length(first); //rows - var l2 = _length(_get(first, 0)); //cols - - var l3 = _length(second); //rows - var l4 = _length(_get(second, 0)); //cols - - //pre-allocate the matrix - var row = []; - for (var j = 0; j < l4; j++) { - _push(row, 0); +{ + fn f() { + return i; } - - var result = []; - for (var i = 0; i < l1; i++) { - _push(result, row); - } - - //assign the values - for (var i = 0; i < _length(first); i++) { - //select each element of "first" - var firstElement = _get(first, i); - - //for each element of second - for (var i2 = 0; i2 < _length(second); i2++) { - for (var j2 = 0; j2 < _length(_get(second, 0)); j2++) { - - var val = _get(_get(first, i), i2) * _get(_get(second, i2), j2); - - //TODO: needs better notation than this tmpRow variable - var tmpRow = _get(result, i); - _set(tmpRow, j2, val); - _set(result, i, tmpRow); - - //result[ i ][ j2 ] += first[i][i2] * second[i2][j2] - } - } - } - - return result; } - -//matrix multiply -var c = [[4], [5], [6]]; //this is a 3x1 -var d = [[1, 2, 3]]; //this is a 1x3 - -print matrix(c, d); diff --git a/source/interpreter.c b/source/interpreter.c index a4028bf..00e2b93 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -102,30 +102,21 @@ void initInterpreter(Interpreter* interpreter) { } void freeInterpreter(Interpreter* interpreter) { - //BUGFIX: handle scopes/types in the exports - for (int i = 0; i < interpreter->exports->capacity; i++) { - freeLiteral(interpreter->exports->entries[i].key); - freeLiteral(interpreter->exports->entries[i].value); - freeLiteral(interpreter->exportTypes->entries[i].key); - freeLiteral(interpreter->exportTypes->entries[i].value); + //free the interpreter scope + while(interpreter->scope != NULL) { + interpreter->scope = popScope(interpreter->scope); } - //BUGFIX: handle scopes of functions, which refer to the parent scope (leaking memory) - while(interpreter->scope != NULL) { - for (int i = 0; i < interpreter->scope->variables.capacity; i++) { - //handle keys, just in case - if (IS_FUNCTION(interpreter->scope->variables.entries[i].key)) { - popScope(AS_FUNCTION(interpreter->scope->variables.entries[i].key).scope); - AS_FUNCTION(interpreter->scope->variables.entries[i].key).scope = NULL; - } - - if (IS_FUNCTION(interpreter->scope->variables.entries[i].value)) { - popScope(AS_FUNCTION(interpreter->scope->variables.entries[i].value).scope); - AS_FUNCTION(interpreter->scope->variables.entries[i].value).scope = NULL; - } + //BUGFIX: handle scopes/types in the exports + for (int i = 0; i < interpreter->exports->capacity; i++) { + if (IS_FUNCTION(interpreter->exports->entries[i].key)) { + popScope(AS_FUNCTION(interpreter->exports->entries[i].key).scope); + AS_FUNCTION(interpreter->exports->entries[i].key).scope = NULL; + } + if (IS_FUNCTION(interpreter->exports->entries[i].value)) { + popScope(AS_FUNCTION(interpreter->exports->entries[i].value).scope); + AS_FUNCTION(interpreter->exports->entries[i].value).scope = NULL; } - - interpreter->scope = popScope(interpreter->scope); } freeLiteralDictionary(interpreter->exports); @@ -1850,7 +1841,7 @@ static void readInterpreterSections(Interpreter* interpreter) { //change the type to normal interpreter->literalCache.literals[i] = TO_FUNCTION_LITERAL(bytes, size); - AS_FUNCTION(interpreter->literalCache.literals[i]).scope = pushScope(interpreter->scope); //BUGFIX + AS_FUNCTION(interpreter->literalCache.literals[i]).scope = NULL; } } diff --git a/source/parser.c b/source/parser.c index 54a8e69..d9ed360 100644 --- a/source/parser.c +++ b/source/parser.c @@ -1195,6 +1195,9 @@ static void importStmt(Parser* parser, Node** nodeHandle) { emitNodeImport(nodeHandle, NODE_IMPORT, idn, alias); consume(parser, TOKEN_SEMICOLON, "Expected ';' at end of import statement"); + + freeLiteral(idn); + freeLiteral(alias); } static void exportStmt(Parser* parser, Node** nodeHandle) { @@ -1217,6 +1220,9 @@ static void exportStmt(Parser* parser, Node** nodeHandle) { emitNodeImport(nodeHandle, NODE_EXPORT, idn, alias); consume(parser, TOKEN_SEMICOLON, "Expected ';' at end of export statement"); + + freeLiteral(idn); + freeLiteral(alias); } //precedence functions diff --git a/source/scope.c b/source/scope.c index eef1599..2a7b8cb 100644 --- a/source/scope.c +++ b/source/scope.c @@ -127,6 +127,20 @@ Scope* popScope(Scope* scope) { Scope* ret = scope->ancestor; + //BUGFIX: when freeing a scope, free the function's scopes manually + for (int i = 0; i < scope->variables.capacity; i++) { + //handle keys, just in case + if (IS_FUNCTION(scope->variables.entries[i].key)) { + popScope(AS_FUNCTION(scope->variables.entries[i].key).scope); + AS_FUNCTION(scope->variables.entries[i].key).scope = NULL; + } + + if (IS_FUNCTION(scope->variables.entries[i].value)) { + popScope(AS_FUNCTION(scope->variables.entries[i].value).scope); + AS_FUNCTION(scope->variables.entries[i].value).scope = NULL; + } + } + freeAncestorChain(scope); return ret;