From e6f81b28f95050877061bb8910f66c24798c8d73 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 25 Dec 2017 23:10:16 -0800 Subject: [PATCH] Plus and multiply --- src/rukka_lang/mod.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/rukka_lang/mod.rs b/src/rukka_lang/mod.rs index 1deb66c..30c9e92 100644 --- a/src/rukka_lang/mod.rs +++ b/src/rukka_lang/mod.rs @@ -217,9 +217,28 @@ impl EvaluatorState { use self::Sexp::*; use self::BuiltinFn::*; Ok(match op { - Plus => match operands { - Cons(box NumberAtom(l), box Cons(box NumberAtom(r), box Nil)) => NumberAtom(l + r), - _ => return Err(format!("Bad arguments for +")), + Plus | Mult => { + let mut result = match op { Plus => 0, Mult => 1, _ => unreachable!() }; + let mut operand = &operands; + loop { + match operand { + &Nil => break, + &Cons(ref l, ref r) => { + if let NumberAtom(ref n) = **l { + if let Plus = op { + result += n; + } else if let Mult = op { + result *= n; + } + operand = r as &Sexp; + } else { + return Err(format!("Bad operand")); + } + }, + _ => return Err(format!("Bad operands list")) + } + } + NumberAtom(result) }, _ => return Err(format!("Not implemented")), })