From 9b4499c5ac3d17184fe2d39740398a51a3dc6ebf Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 10 Dec 2017 02:58:07 -0800 Subject: [PATCH] If expressions --- src/rukka_lang/mod.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/rukka_lang/mod.rs b/src/rukka_lang/mod.rs index a4cea06..7f671a0 100644 --- a/src/rukka_lang/mod.rs +++ b/src/rukka_lang/mod.rs @@ -111,7 +111,17 @@ impl EvaluatorState { _ => return Err(format!("Bad assignment")), } "lambda" => unimplemented!(), - "cond" => unimplemented!(), + "if" => match operands { + Cons(box test, box body) => { + let truth_value = test.truthy(); + match (truth_value, body) { + (true, Cons(box consequent, _)) => consequent, + (false, Cons(_, box Cons(box alternative, _))) => alternative, + _ => return Err(format!("Bad if expression")) + } + }, + _ => return Err(format!("Bad if expression")) + }, _ => unimplemented!(), }, other => {println!("OTHER? {:?}", other); unimplemented!() } @@ -167,6 +177,14 @@ impl Sexp { &Nil => format!("()"), } } + + fn truthy(&self) -> bool { + use self::Sexp::*; + match self { + &False => false, + _ => true + } + } } fn tokenize(input: &mut Peekable) -> Vec {