diff --git a/src/ast/types.rs b/src/ast/types.rs index ca3a086..990a01b 100644 --- a/src/ast/types.rs +++ b/src/ast/types.rs @@ -1,6 +1,7 @@ //! Ast definition for Ayin. use std::collections::BTreeMap; +use std::rc; /// An expression. #[derive(PartialEq, PartialOrd, Debug, Clone)] @@ -103,10 +104,10 @@ pub struct Label(pub String); pub struct EnvName(pub String); #[derive(PartialEq, PartialOrd, Debug, Clone)] -pub struct Record(pub BTreeMap); +pub struct Record(pub BTreeMap); #[derive(PartialEq, PartialOrd, Debug, Clone)] -pub struct Vector(pub Vec); +pub struct Vector(pub Vec); /// A Program. #[derive(PartialEq, PartialOrd, Debug)] diff --git a/src/interpret/interpret.rs b/src/interpret/interpret.rs index 3f7f81c..b6a7082 100644 --- a/src/interpret/interpret.rs +++ b/src/interpret/interpret.rs @@ -2,7 +2,7 @@ use super::types::*; -use crate::ast::{self, Record}; +use crate::ast; pub fn run( program: ast::Program, @@ -153,6 +153,7 @@ fn eval_expr(expr_env: &Env, state: &mut State, expr: &ast::Expr) -> Result { + let lhs = eval_expr(expr_env, state, lhs)?; let rhs = eval_expr(expr_env, state, rhs)?; if let Some(mut nested_field) = access_nested_field(expr_env, state, expr, field)? @@ -174,19 +175,21 @@ fn eval_expr(expr_env: &Env, state: &mut State, expr: &ast::Expr) -> Result Result { +) -> Result { let lhs = eval_expr(&expr_env, state, expr)?; match lhs { ast::Value::Ref(reference) => { - let mut current: ast::Value = (*state.get_variable(&reference)).clone(); + let mut current_ref = reference.clone(); + let mut current: ast::Value = state.get_variable(&reference); for label in labels { match current { ast::Value::Record(record) => { if let Some(next) = record.0.get(label) { - current = eval_expr(&expr_env, state, &ast::Expr::Value(next.clone()))?; + current_ref = next; + current = state.get_variable(¤t_ref); } else { todo!() } @@ -195,7 +198,7 @@ fn access_nested_field( _ => todo!(), // unexpected } } - Ok(current) + Ok(current_ref) } _ => todo!(), } diff --git a/src/interpret/types.rs b/src/interpret/types.rs index 6a0d2f9..417456a 100644 --- a/src/interpret/types.rs +++ b/src/interpret/types.rs @@ -59,7 +59,7 @@ pub struct State { pub variables: Variables, } -pub struct Variables(pub HashMap>); +pub struct Variables(pub HashMap); impl State { pub fn new(name: String) -> State { @@ -82,8 +82,11 @@ impl State { self.variables.0.insert(name.clone(), Rc::new(value)); name } - pub fn get_variable<'a>(&self, reference: &ast::Ref) -> Rc { - self.variables.0.get(reference).unwrap().clone() + pub fn get_variable<'a>(&'a self, reference: &ast::Ref) -> &'a ast::Value { + self.variables.0.get(reference).unwrap() + } + pub fn set_variable(&mut self, reference: ast::Ref, new_value: ast::Value) { + self.variables.0.insert(reference, new_value) } }