From a3b17025af9ad559466763d2c760aa5830fb496d Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 20 Apr 2016 18:16:29 -0400 Subject: rust: start implementing action_apply --- minimal.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/minimal.rs b/minimal.rs index 58e7273..21ef227 100644 --- a/minimal.rs +++ b/minimal.rs @@ -256,7 +256,26 @@ fn lambda_action<'a>(list: &'a Vec, ctx: HashMap<&'a str, SchemeExpr } fn apply_action<'a>(list: &'a Vec, ctx: HashMap<&'a str, SchemeExpr<'a>>) -> Result, &'static str> { - Ok(SchemeExpr::SchemeNull) + if list.len() == 0 { + // TODO: is this correct? + return Ok(SchemeExpr::SchemeNull); + } + let action = &list[0]; + // XXX: shouldn't be an unwrap here, should be a try!() + let args: Vec = list.iter().skip(1).map(|x| scheme_meaning(x, ctx.clone()).unwrap()).collect(); + // XXX: only things that work with more than one arg + match action { + &SchemeExpr::SchemeBuiltin("+") => { + let mut val: f64 = 0.; + for arg in args { + val += match arg { + SchemeExpr::SchemeNum(x) => x, + _ => { return Err("+ builtin only takes nums"); }, + }; + } + Ok(SchemeExpr::SchemeNum(val)) }, + _ => { return Err("unimplemented builtin"); } + } } fn scheme_meaning<'a>(ast: &'a SchemeExpr, ctx: HashMap<&'a str, SchemeExpr<'a>>) -> Result, &'static str> { -- cgit v1.2.3