assign
This commit is contained in:
parent
be65935def
commit
a16571acf2
3 changed files with 39 additions and 14 deletions
|
|
@ -39,7 +39,6 @@ pub enum Expr {
|
||||||
#[derive(PartialEq, PartialOrd, Debug, Clone)]
|
#[derive(PartialEq, PartialOrd, Debug, Clone)]
|
||||||
pub enum Op {
|
pub enum Op {
|
||||||
Assign,
|
Assign,
|
||||||
Dot,
|
|
||||||
// Equals,
|
// Equals,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,7 @@ fn eval_expr(expr_env: &Env, state: &mut State, expr: &ast::Expr) -> Result<ast:
|
||||||
|
|
||||||
for (arg, e) in func.args.into_iter().zip(args.iter()) {
|
for (arg, e) in func.args.into_iter().zip(args.iter()) {
|
||||||
let evalled = eval_expr(&closure_env, state, e)?;
|
let evalled = eval_expr(&closure_env, state, e)?;
|
||||||
closure_env.0.insert(arg.name, evalled);
|
closure_env.insert(arg.name, evalled);
|
||||||
}
|
}
|
||||||
eval_expr(&closure_env, state, &func.body)
|
eval_expr(&closure_env, state, &func.body)
|
||||||
}
|
}
|
||||||
|
|
@ -131,7 +131,26 @@ fn eval_expr(expr_env: &Env, state: &mut State, expr: &ast::Expr) -> Result<ast:
|
||||||
}
|
}
|
||||||
_ => Ok(v.clone()),
|
_ => Ok(v.clone()),
|
||||||
},
|
},
|
||||||
ast::Expr::Op { .. } => todo!("Implement bin op"),
|
ast::Expr::Op { lhs, rhs, op } => match op {
|
||||||
|
ast::Op::Assign => match &**lhs {
|
||||||
|
ast::Expr::Var(name) => {
|
||||||
|
let rhs = eval_expr(expr_env, state, rhs)?;
|
||||||
|
state
|
||||||
|
.envs
|
||||||
|
.get_mut(&expr_env.name)
|
||||||
|
.unwrap()
|
||||||
|
.insert(name.clone(), rhs);
|
||||||
|
eval_expr(expr_env, state, &ast::UNIT)
|
||||||
|
}
|
||||||
|
// access via labels
|
||||||
|
_ => todo!(),
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
let _lhs = eval_expr(expr_env, state, lhs)?;
|
||||||
|
let _rhs = eval_expr(expr_env, state, rhs)?;
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,7 +159,7 @@ fn defs_to_env(
|
||||||
env_name: &ast::EnvName,
|
env_name: &ast::EnvName,
|
||||||
envs: &mut Envs,
|
envs: &mut Envs,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let mut env = Env::new();
|
let mut env = Env::new(env_name.clone());
|
||||||
|
|
||||||
for def in defs {
|
for def in defs {
|
||||||
let (name, closure) = match def {
|
let (name, closure) = match def {
|
||||||
|
|
|
||||||
|
|
@ -71,24 +71,26 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Clone)]
|
#[derive(PartialEq, Debug, Clone)]
|
||||||
pub struct Env(pub BTreeMap<ast::Name, ast::Value>);
|
pub struct Env {
|
||||||
impl Default for Env {
|
env: BTreeMap<ast::Name, ast::Value>,
|
||||||
fn default() -> Env {
|
pub name: ast::EnvName,
|
||||||
Env::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Env {
|
impl Env {
|
||||||
pub fn new() -> Env {
|
pub fn new(name: ast::EnvName) -> Env {
|
||||||
Env(BTreeMap::new())
|
Env {
|
||||||
|
env: BTreeMap::new(),
|
||||||
|
name,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn get(&self, name: &ast::Name) -> Result<&ast::Value, Error> {
|
pub fn get(&self, name: &ast::Name) -> Result<&ast::Value, Error> {
|
||||||
self.0.get(name).ok_or(Error::NameNotFound(name.clone()))
|
self.env.get(name).ok_or(Error::NameNotFound(name.clone()))
|
||||||
}
|
}
|
||||||
pub fn insert(&mut self, name: ast::Name, value: ast::Value) {
|
pub fn insert(&mut self, name: ast::Name, value: ast::Value) {
|
||||||
self.0.insert(name.clone(), value);
|
self.env.insert(name.clone(), value);
|
||||||
}
|
}
|
||||||
pub fn insert_nodup(&mut self, name: &ast::Name, value: ast::Value) -> Result<(), Error> {
|
pub fn insert_nodup(&mut self, name: &ast::Name, value: ast::Value) -> Result<(), Error> {
|
||||||
if self.0.insert(name.clone(), value).is_some() {
|
if self.env.insert(name.clone(), value).is_some() {
|
||||||
Err(Error::DuplicateNames(name.clone()))
|
Err(Error::DuplicateNames(name.clone()))
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -105,6 +107,11 @@ impl Envs {
|
||||||
.get(env_name)
|
.get(env_name)
|
||||||
.ok_or(Error::EnvNotFound(env_name.clone()))
|
.ok_or(Error::EnvNotFound(env_name.clone()))
|
||||||
}
|
}
|
||||||
|
pub fn get_mut(&mut self, env_name: &ast::EnvName) -> Result<&mut Env, Error> {
|
||||||
|
self.0
|
||||||
|
.get_mut(env_name)
|
||||||
|
.ok_or(Error::EnvNotFound(env_name.clone()))
|
||||||
|
}
|
||||||
pub fn insert(&mut self, name: &ast::EnvName, env: Env) -> Result<(), Error> {
|
pub fn insert(&mut self, name: &ast::EnvName, env: Env) -> Result<(), Error> {
|
||||||
if self.0.insert(name.clone(), env).is_some() {
|
if self.0.insert(name.clone(), env).is_some() {
|
||||||
Err(Error::DuplicateEnvNames(name.clone()))
|
Err(Error::DuplicateEnvNames(name.clone()))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue