If expressions

This commit is contained in:
greg 2017-12-10 02:58:07 -08:00
parent 3d023a6704
commit e243b99d3b
1 changed files with 19 additions and 1 deletions

View File

@ -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<Chars>) -> Vec<Token> {