fix: support truthy values

In scheme, everything except false is considered truthy.
This commit is contained in:
Roman Godmaire 2024-02-17 07:07:04 -05:00
parent 5637d256d2
commit 1d16187846
2 changed files with 6 additions and 6 deletions

View file

@ -166,12 +166,12 @@ pub fn core_environment() -> Rc<Environment> {
let (cond, consequence, alternative) = let (cond, consequence, alternative) =
(args[0].clone(), args[1].clone(), args[2].clone()); (args[0].clone(), args[1].clone(), args[2].clone());
// If the value is anything other than true or nil, then we return the alternative // If the value is anything other than false, then it is truthy
if *cond == Expression::Boolean(true) || *cond == Expression::Nil { if *cond == Expression::Boolean(false) {
return Ok(consequence); return Ok(alternative);
}; };
Ok(alternative) Ok(consequence)
}), }),
), ),
// Environment Manipulation // Environment Manipulation

View file

@ -213,8 +213,8 @@ mod test {
#[case("(if true true false)", "true")] #[case("(if true true false)", "true")]
#[case("(if nil true false)", "true")] #[case("(if nil true false)", "true")]
#[case("(if false true false)", "false")] #[case("(if false true false)", "false")]
#[case("(if 4 true false)", "false")] #[case("(if 4 true false)", "true")]
#[case("(if \"blue\" true false)", "false")] #[case("(if \"blue\" true false)", "true")]
// Functions // Functions
#[case("((fn* (a) a) 3)", "3")] #[case("((fn* (a) a) 3)", "3")]
#[case("((fn* (a) (+ a 2)) 1)", "3")] #[case("((fn* (a) (+ a 2)) 1)", "3")]