Interpreter - Renamed resolved variables field
This commit is contained in:
parent
363cdab86f
commit
95ddd34f9c
1 changed files with 6 additions and 6 deletions
|
@ -23,17 +23,17 @@ pub fn evaluate(ast: &ast::ProgramNode, vars: ResolvedVariables) -> SloxResult<V
|
||||||
pub struct InterpreterState<'a> {
|
pub struct InterpreterState<'a> {
|
||||||
pub(super) globals: EnvironmentRef,
|
pub(super) globals: EnvironmentRef,
|
||||||
pub(super) environment: EnvironmentRef,
|
pub(super) environment: EnvironmentRef,
|
||||||
pub(super) variables: &'a ResolvedVariables,
|
pub(super) locals: &'a ResolvedVariables,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> InterpreterState<'a> {
|
impl<'a> InterpreterState<'a> {
|
||||||
/// Initialize the interpreter state from the resolved variables map.
|
/// Initialize the interpreter state from the resolved variables map.
|
||||||
fn new(vars: &'a ResolvedVariables) -> Self {
|
fn new(locals: &'a ResolvedVariables) -> Self {
|
||||||
let env = Rc::new(RefCell::new(Environment::default()));
|
let env = Rc::new(RefCell::new(Environment::default()));
|
||||||
Self {
|
Self {
|
||||||
environment: env.clone(),
|
environment: env.clone(),
|
||||||
globals: env,
|
globals: env,
|
||||||
variables: &vars,
|
locals: &locals,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,19 +45,19 @@ impl<'a> InterpreterState<'a> {
|
||||||
InterpreterState {
|
InterpreterState {
|
||||||
environment: Environment::create_child(&parent.environment),
|
environment: Environment::create_child(&parent.environment),
|
||||||
globals: parent.globals.clone(),
|
globals: parent.globals.clone(),
|
||||||
variables: parent.variables,
|
locals: parent.locals,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lookup_var(&self, name: &Token, expr_id: &usize) -> SloxResult<Value> {
|
fn lookup_var(&self, name: &Token, expr_id: &usize) -> SloxResult<Value> {
|
||||||
match self.variables.get(expr_id) {
|
match self.locals.get(expr_id) {
|
||||||
Some(distance) => self.environment.borrow().get_at(*distance, name),
|
Some(distance) => self.environment.borrow().get_at(*distance, name),
|
||||||
None => self.globals.borrow().get(name),
|
None => self.globals.borrow().get(name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assign_var(&self, name: &Token, expr_id: &usize, value: Value) -> SloxResult<()> {
|
fn assign_var(&self, name: &Token, expr_id: &usize, value: Value) -> SloxResult<()> {
|
||||||
match self.variables.get(expr_id) {
|
match self.locals.get(expr_id) {
|
||||||
Some(distance) => self
|
Some(distance) => self
|
||||||
.environment
|
.environment
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
|
|
Loading…
Reference in a new issue