Interpreter - Fixed special case for init

This commit is contained in:
Emmanuel BENOîT 2023-01-11 08:03:42 +01:00
parent 3af544a78e
commit c4f0f0f721
2 changed files with 7 additions and 4 deletions

View file

@ -5,7 +5,10 @@ use crate::{
tokens::Token, tokens::Token,
}; };
use super::{Value, native_fn::{self, NativeFunction}}; use super::{
native_fn::{self, NativeFunction},
Value,
};
/// A mutable reference to an environment. /// A mutable reference to an environment.
pub(super) type EnvironmentRef = Rc<RefCell<Environment>>; pub(super) type EnvironmentRef = Rc<RefCell<Environment>>;
@ -127,7 +130,7 @@ impl Environment {
pub fn read(&self, name: &str) -> Value { pub fn read(&self, name: &str) -> Value {
match self.values.get(name) { match self.values.get(name) {
Some(Some(v)) => v.clone(), Some(Some(v)) => v.clone(),
_ => panic!("Symbol {name} does not exist"), _ => panic!("Symbol {name} does not exist ({:?})", self.values.keys()),
} }
} }

View file

@ -77,13 +77,13 @@ impl Callable for Function {
InterpreterFlowControl::Result(_) => (), InterpreterFlowControl::Result(_) => (),
InterpreterFlowControl::Return(v) if !self.is_initializer => return Ok(v), InterpreterFlowControl::Return(v) if !self.is_initializer => return Ok(v),
InterpreterFlowControl::Return(_) => { InterpreterFlowControl::Return(_) => {
return Ok(itpr_state.environment.borrow().read("this")) return Ok(self.env.borrow().read("this"))
} }
_ => panic!("unexpected flow control {:?}", result), _ => panic!("unexpected flow control {:?}", result),
} }
} }
if self.is_initializer { if self.is_initializer {
Ok(itpr_state.environment.borrow().read("this")) Ok(self.env.borrow().read("this"))
} else { } else {
Ok(Value::Nil) Ok(Value::Nil)
} }