If expressions

This commit is contained in:
greg 2017-12-10 02:58:07 -08:00
parent 3d023a6704
commit e243b99d3b

View File

@ -111,7 +111,17 @@ impl EvaluatorState {
_ => return Err(format!("Bad assignment")), _ => return Err(format!("Bad assignment")),
} }
"lambda" => unimplemented!(), "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!(), _ => unimplemented!(),
}, },
other => {println!("OTHER? {:?}", other); unimplemented!() } other => {println!("OTHER? {:?}", other); unimplemented!() }
@ -167,6 +177,14 @@ impl Sexp {
&Nil => format!("()"), &Nil => format!("()"),
} }
} }
fn truthy(&self) -> bool {
use self::Sexp::*;
match self {
&False => false,
_ => true
}
}
} }
fn tokenize(input: &mut Peekable<Chars>) -> Vec<Token> { fn tokenize(input: &mut Peekable<Chars>) -> Vec<Token> {