Skip to content

Commit

Permalink
Fixed function scope destruction
Browse files Browse the repository at this point in the history
Copy argument tokens to avoid destroying originals
Only destroy arguments after evaluating builtins
Arguments will be destroyed in child scope with user-defined functions
Copy parameter strings
Scope destruction ignores function wrappers which are destroyed with AST
root
  • Loading branch information
Arc676 committed Jun 22, 2018
1 parent 92df1be commit 30376cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/evaluation.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,20 @@ Token* eval(Token* exp, Scope* scope) {

// evaluate function arguments before passing to function
for (int i = 0; i < argc; i++) {
args[i] = eval(givenArgs[i], scope);
args[i] = copyToken(eval(givenArgs[i], scope));
}

for (int i = 0; i < BUILTIN_COUNT; i++) {
if (!strcmp(fID, builtinFunctions[i])) {
returnValue = builtins[i](argc, args);
hasEvaluated = 1;

// tear down copy of arguments
for (int i = 0; i < argc; i++) {
if (!isLiteralToken(args[i])) {
destroyToken(args[i]);
}
}
}
}

Expand All @@ -320,23 +327,17 @@ Token* eval(Token* exp, Scope* scope) {
// assign given arguments to parameter names, shadowing
// any variables in parent scopes with the same name
for (int i = 0; i < argc; i++) {
defineVariable(fScope, params[i], args[i]);
defineVariable(fScope, copyString(params[i]), args[i]);
}
Token* fBody = ht_find_token(func->tokenData, FUNCTION_BODY);
returnValue = eval(fBody, fScope);

// tear down child scope
// tear down child scope and arguments contained within it
destroyScope(fScope);
hasEvaluated = 1;
}
}

// tear down copy of arguments
for (int i = 0; i < argc; i++) {
if (!isLiteralToken(args[i])) {
destroyToken(args[i]);
}
}
free(args);

if (!hasEvaluated) {
Expand Down
4 changes: 3 additions & 1 deletion src/scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ void destroyScope(Scope* scope) {
char* identifier = scope->storedIdentifiers[i];
if (identifier) {
Token* token = ht_find(scope->variables, identifier);
destroyToken(token);
if (token->type != FUNC_WRAPPER) {
destroyToken(token);
}
free(identifier);
}
}
Expand Down

0 comments on commit 30376cd

Please sign in to comment.