Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not declared errors in source native variant REPL #1479

Merged
merged 3 commits into from
Sep 3, 2023

Conversation

shenyih0ng
Copy link
Member

@shenyih0ng shenyih0ng commented Sep 3, 2023

Fixes #1477

Description

When using the REPL with the Source Native variant, UndefinedVariable errors will be raised when trying to reference previously declared functions in the REPL.

This happens when a user first runs a program in the editor (via the "play" button) and subsequently tries to access the functions/variables in the REPL.

The Cause

UndefinedVariable Errors

The bug is due to the function and variable identifiers not being properly kept tracked of during the transpilation stage of a Source Native program.

Declared functions and variables in a program are not added to the context (in context.nativeStorage) when executing a program. Hence, upon accessing previously declared functions/variables, an UndefinedVariable error will be raised as those declarations have been lost.

The fix would be add those declarations during the transpilation stage of a Source Native program:

getFunctionDeclarationNamesInProgram(program).forEach(id =>
context.nativeStorage.previousProgramsIdentifiers.add(id)
)
getGloballyDeclaredIdentifiers(program).forEach(id =>
context.nativeStorage.previousProgramsIdentifiers.add(id)
)

Why does f; not result in a UndefinedVariable error?

The bug is due to the use of getIdentifiersInProgram during the pre-evaluation stage of the FullJSRunner:

getIdentifiersInProgram(preEvalProgram).forEach(id =>
context.nativeStorage.previousProgramsIdentifiers.add(id)
)

The snippet above adds all identifiers in the program, which includes identifiers of arguments in a function declaration, into the program context.

It turns out some of the arguments we have in our stdlib functions are named f:

function $map(f, xs, acc) {
return is_null(xs)
? reverse(acc)
: $map(f, tail(xs), pair(f(head(xs)), acc));
}
function map(f, xs) {
return $map(f, xs, null);
}

f works in Source Native 2-4 since the list is introduced in those chapters and not in Source Native 1.

The fix for this is the same for the error above. Instead of using getIdentifiersInProgram we specifically pick out function and variable declarations using getFunctionDeclarationNamesInProgram and getGloballyDeclaredIdentifiers

@shenyih0ng shenyih0ng self-assigned this Sep 3, 2023
@shenyih0ng shenyih0ng changed the title fix: not declared errors in source native variant REPL fix: not declared errors in source native variant REPL Sep 3, 2023
@shenyih0ng shenyih0ng marked this pull request as ready for review September 3, 2023 07:12
@martin-henz
Copy link
Member

Yes, makes sense.

Copy link
Member

@martin-henz martin-henz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@martin-henz martin-henz merged commit 0107752 into master Sep 3, 2023
1 check passed
@martin-henz martin-henz deleted the fix/repl/native-variant-func-not-declared branch September 3, 2023 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REPL of Source §x Native does not recognize (most) function declarations
2 participants