Skip to content

global variables don't work during chapter 11 #1176

@Xythium

Description

@Xythium

is this intended behavior?

global variables get skipped in the resolver because there is no scope on the stack (in declare() and define())

when accessing a global variable in a new scope (function or loop for example) in visitVariableExpr(), there is a scope on the stack. so it will check if if the variable is defined, which it is not. scopes.peek().get(expr.name.lexeme) == Boolean.FALSE
it will then report a parse error Lox.error(expr.name, "Can't read local variable in its own initializer.");

is this intended during (and/or after) chapter 11? this for example does not work

var a = 0;

for (var i = 0; i < 10; i = i + 1) {
    print a;
}

and im getting these parse errors in my implementation for this example

[line 4] Error at 'a': Can't read local variable in its own initializer.
[line 3] Error at 'i': Can't read local variable in its own initializer.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Xythium

        Issue actions

          global variables don't work during chapter 11 · Issue #1176 · munificent/craftinginterpreters