From 40ccea8c05a00a05a5c0715d8b18929264347d41 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 18 Oct 2018 01:46:30 -0700 Subject: [PATCH] Separate assign_expression method --- schala-lang/language/src/eval.rs | 36 +++++++++++++++++--------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/schala-lang/language/src/eval.rs b/schala-lang/language/src/eval.rs index 59da38a..a4983ac 100644 --- a/schala-lang/language/src/eval.rs +++ b/schala-lang/language/src/eval.rs @@ -191,23 +191,7 @@ impl<'a> State<'a> { Ok(Node::PrimTuple { items: nodes }) }, Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause), - Assign { box val, box expr } => { - let name = match val { - Expr::Val(name) => name, - _ => return Err(format!("Trying to assign to a non-value")), - }; - - let constant = match self.values.lookup(&name) { - None => return Err(format!("{} is undefined", name)), - Some(ValueEntry::Binding { constant, .. }) => constant.clone(), - }; - if constant { - return Err(format!("trying to update {}, a non-mutable binding", name)); - } - let val = self.expression(Node::Expr(expr))?; - self.values.insert(name.clone(), ValueEntry::Binding { constant: false, val }); - Ok(Node::Expr(Expr::Unit)) - }, + Assign { box val, box expr } => self.assign_expression(val, expr), Unit => Ok(Node::Expr(Unit)), CaseMatch { box cond, alternatives } => match self.expression(Node::Expr(cond))? { Node::PrimObject { name, tag, items } => { @@ -391,6 +375,24 @@ impl<'a> State<'a> { }) } + fn assign_expression(&mut self, val: Expr, expr: Expr) -> EvalResult { + let name = match val { + Expr::Val(name) => name, + _ => return Err(format!("Trying to assign to a non-value")), + }; + + let constant = match self.values.lookup(&name) { + None => return Err(format!("Constant {} is undefined", name)), + Some(ValueEntry::Binding { constant, .. }) => constant.clone(), + }; + if constant { + return Err(format!("trying to update {}, a non-mutable binding", name)); + } + let val = self.expression(Node::Expr(expr))?; + self.values.insert(name.clone(), ValueEntry::Binding { constant: false, val }); + Ok(Node::Expr(Expr::Unit)) + } + fn value(&mut self, name: Rc) -> EvalResult { use self::ValueEntry::*; use self::Func::*;