Stuck on Stage #DE8 - Rust

I’m stuck on Stage #DE8.
GitHub - Gurwi30/interpreter-rust

I have a issue running this lox code, for some reason it says that the hello variable is not defined, but i retry by using a while loop it works fine.

for (var hello = 0; hello < 3; hello = hello + 1) { print hello; }

I’ve tried almost everything i could think of, .

Here are my logs:

Logs from your program will appear here!
(< var IDENTIFIER hello null 3.0) { { var IDENTIFIER hello null } IDENTIFIER hello null = (+ var IDENTIFIER hello null 1.0) }
[Print { expr: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 2 } } }]
[Block { statements: [Print { expr: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 2 } } }] }, Expression { expr: Assign { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 }, value: Binary { left: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 } }, operator: Token { token_type: Plus, lexeme: "+", literal: None, line: 1 }, right: Literal { literal: Float(1.0) } } } }]
[Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 }, initializer: Literal { literal: Float(0.0) } }, While { condition: Binary { left: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 } }, operator: Token { token_type: Less, lexeme: "<", literal: None, line: 1 }, right: Literal { literal: Float(3.0) } }, body: Block { statements: [Block { statements: [Print { expr: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 2 } } }] }, Expression { expr: Assign { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 }, value: Binary { left: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 } }, operator: Token { token_type: Plus, lexeme: "+", literal: None, line: 1 }, right: Literal { literal: Float(1.0) } } } }] } }]
Env
Current environment 0x1b179931450
Dept 1, Lexeme hello
Environment at depth 0: {"hello": Float(0)}
        Pointer { addr: 0x1b179937150, metadata: 5 }    0
  Environment at depth 1: {"clock": Callable(<fn>), "print": Callable(<fn>)}
        Pointer { addr: 0x1b179936f60, metadata: 5 }    <fn builtin clock>
        Pointer { addr: 0x1b179937080, metadata: 5 }    <fn builtin print>
Ancestor
  Environment at depth 1: {"clock": Callable(<fn>), "print": Callable(<fn>)}
        Pointer { addr: 0x1b179936f60, metadata: 5 }    <fn builtin clock>
        Pointer { addr: 0x1b179937080, metadata: 5 }    <fn builtin print>
Search Pointer { addr: 0x1b179937070, metadata: 5 }
[line 1] Error: Undefined variable 'hello'

And here’s a snippet of my code:

fn look_up_var(&self, name: &Token, expr: &Expr) -> Result<Value, ExecError> {
        match self.locals.get(expr) {
            Some(distance) => match Environment::get_at(self.environment.clone(), *distance, name.lexeme.as_str()) {
                Some(val) => Ok(val),
                None => {
                    println!("Env");
                    println!("Current environment {:p}", Rc::as_ptr(&self.environment));
                    println!("Dept {}, Lexeme {}", distance, name.lexeme);
                    self.environment.borrow().debug_print(0);
                    
                    println!("Ancestor");
                    
                    Environment::ancestor(Rc::clone(&self.environment), *distance).borrow().debug_print(*distance);
                    
                    println!("Search {:p}", name.lexeme.as_str());
                    
                    if let Some(v) = Environment::get_at(self.environment.clone(), *distance, "hello") {
                        println!("Found {}", v);
                    } 
                    
                    Err(ExecError::Runtime(
                        RuntimeError::new(name.clone(), format!("Undefined variable '{}'", name.lexeme)), )
                    )
                },
            },
            None => self.globals.borrow().get(name)
        }
    }

but i retry by using a while loop it works fine.

Hey @Gurwi30, would you mind sharing a bit more about the while loop you used?

You can find the source code on github, the loop i used is just a basic for loop but resolver is not resolving it. Because currently the for loop just gets converted in a while loop so it gets converted into this

{
    var hello = 0;
    while (hello < 3) {
        print hello;
        hello = hello + 1;
    }
}

and if i write the while loop like that it works, but for some reason the resolver is unable to resolve the hello var when it’s written as a for lopp

@Gurwi30 I’m no longer able to access your GitHub repo. Let me know if you still need help with this!

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.