From 1be6991f55448df437c685873b1abf754b3eb012 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 17 Oct 2018 19:39:48 -0700 Subject: [PATCH] Making eval expression method a bit less complex by splitting it into submethods --- schala-lang/language/src/eval.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/schala-lang/language/src/eval.rs b/schala-lang/language/src/eval.rs index fb9deab..59da38a 100644 --- a/schala-lang/language/src/eval.rs +++ b/schala-lang/language/src/eval.rs @@ -181,11 +181,7 @@ impl<'a> State<'a> { obj @ Node::PrimObject { .. } => Ok(obj), Node::Expr(expr) => match expr { literal @ Lit(_) => Ok(Node::Expr(literal)), - Call { box f, args } => match self.expression(Node::Expr(f))? { - Node::Expr(Constructor { type_name, name, tag, arity }) => self.apply_data_constructor(type_name, name, tag, arity, args), - Node::Expr(Func(f)) => self.apply_function(f, args), - other => return Err(format!("Tried to call {:?} which is not a function or data constructor", other)), - }, + Call { box f, args } => self.call_expression(f, args), Val(v) => self.value(v), Constructor { arity, ref name, tag, .. } if arity == 0 => Ok(Node::PrimObject { name: name.clone(), tag, items: vec![] }), constructor @ Constructor { .. } => Ok(Node::Expr(constructor)), @@ -261,6 +257,15 @@ impl<'a> State<'a> { } } + fn call_expression(&mut self, f: Expr, args: Vec) -> EvalResult { + use self::Expr::*; + match self.expression(Node::Expr(f))? { + Node::Expr(Constructor { type_name, name, tag, arity }) => self.apply_data_constructor(type_name, name, tag, arity, args), + Node::Expr(Func(f)) => self.apply_function(f, args), + other => return Err(format!("Tried to call {:?} which is not a function or data constructor", other)), + } + } + fn apply_data_constructor(&mut self, type_name: Rc, name: Rc, tag: usize, arity: usize, args: Vec) -> EvalResult { if arity != args.len() { return Err(format!("Data constructor {} requires {} args", name, arity));