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

Clauses do not have access to outer scopes #4

Closed
rbaltrusch opened this issue Nov 8, 2021 · 1 comment
Closed

Clauses do not have access to outer scopes #4

rbaltrusch opened this issue Nov 8, 2021 · 1 comment
Assignees
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rbaltrusch
Copy link
Owner

rbaltrusch commented Nov 8, 2021

Inner scopes do not have access to variables defined in outer scopes. This makes it impossible to access global variables, define closures or even call a function inside another function defined elsewhere, e.g.

define function a as {
    print 1
}

define function b as {
    #this will fail because it doesnt have access to outer scope
    call a
}

To reproduce this example, run the following code, which assigns 1 to a global variable b, then tries to access this variable b to print it inside an inner scope.

set b to 1
set a to {print b} and call it

The example above results in the following exception:

Traceback (most recent call last):

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\main.py", line 25, in <module>
!    inter.interpret(syntax_block)

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\interpreter.py", line 26, in interpret
!    raise e

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\interpreter.py", line 23, in interpret
!    token.run(self)

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\token_.py", line 75, in run
!    function(interpreter)

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\tokens.py", line 188, in _run
!    function.get_value()(interpreter)

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\tokens.py", line 164, in run_tokens
!    token.run(interpreter)

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\token_.py", line 75, in run
!    function(interpreter)

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\tokens.py", line 73, in _run
!    print(value.get_value())

  File "D:\Alle_Dateien\Code\Python\NatScript Interpreter\interpreter\token_.py", line 152, in get_value
!    raise exceptions.UndefinedVariableException(self)

- UndefinedVariableException: Line 2: Tried to access undefined variable b!

Tested with Python 3.8.3, Win10.

@rbaltrusch rbaltrusch added bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed labels Nov 8, 2021
rbaltrusch added a commit that referenced this issue Apr 10, 2022
@rbaltrusch rbaltrusch self-assigned this Apr 10, 2022
@rbaltrusch
Copy link
Owner Author

Fixed with commit 1e4ea23.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant