ombre: Implement logic short-circuiting
d198be57241f1b61142eb38338167f010d4efc5a
1 parent
6d455aba
ombre/src/lang/evaluator.rs
+20 -20
| 1034 | 1034 | intrinsic::pow, |
|
| 1035 | 1035 | ); |
|
| 1036 | 1036 | env.intrinsic("nan?", "Check if a number is NaN", intrinsic::nan); |
|
| 1037 | 1037 | env.intrinsic("inf?", "Check if a number is infinity", intrinsic::inf); |
|
| 1038 | 1038 | env.intrinsic("eq?", "Check if all arguments are equal", intrinsic::eq); |
|
| 1039 | - | env.intrinsic( |
|
| 1040 | - | "and", |
|
| 1041 | - | "Logical *and* of two or more boolean values", |
|
| 1042 | - | intrinsic::and, |
|
| 1043 | - | ); |
|
| 1044 | - | env.intrinsic( |
|
| 1045 | - | "or", |
|
| 1046 | - | "Logical *or* of two or more boolean values", |
|
| 1047 | - | intrinsic::or, |
|
| 1048 | - | ); |
|
| 1049 | 1039 | env.intrinsic("=", "Check if all arguments are equal", intrinsic::eq); |
|
| 1050 | 1040 | env.intrinsic("!=", "Check if any arguments are not equal", intrinsic::neq); |
|
| 1051 | 1041 | env.intrinsic( |
|
| 1052 | 1042 | ">", |
|
| 1053 | 1043 | "Check if the first argument is greater than the second", |
| 1457 | 1447 | Ok(true) => eval(left, env), |
|
| 1458 | 1448 | Ok(false) => eval(right, env), |
|
| 1459 | 1449 | Err(e) => Err(Error::UnexpectedValue(e)), |
|
| 1460 | 1450 | } |
|
| 1461 | 1451 | } |
|
| 1452 | + | "or" => { |
|
| 1453 | + | for expr in items { |
|
| 1454 | + | match eval(expr, env)?.try_into() { |
|
| 1455 | + | Ok(true) => return Ok(Expr::Bool(true)), |
|
| 1456 | + | Ok(false) => continue, |
|
| 1457 | + | Err(e) => return Err(Error::UnexpectedValue(e)), |
|
| 1458 | + | } |
|
| 1459 | + | } |
|
| 1460 | + | Ok(Expr::Bool(false)) |
|
| 1461 | + | } |
|
| 1462 | + | "and" => { |
|
| 1463 | + | for expr in items { |
|
| 1464 | + | match eval(expr, env)?.try_into() { |
|
| 1465 | + | Ok(true) => continue, |
|
| 1466 | + | Ok(false) => return Ok(Expr::Bool(false)), |
|
| 1467 | + | Err(e) => return Err(Error::UnexpectedValue(e)), |
|
| 1468 | + | } |
|
| 1469 | + | } |
|
| 1470 | + | Ok(Expr::Bool(true)) |
|
| 1471 | + | } |
|
| 1462 | 1472 | _ => apply(head, items, env), |
|
| 1463 | 1473 | } |
|
| 1464 | 1474 | } else { |
|
| 1465 | 1475 | apply(head, items, env) |
|
| 1466 | 1476 | } |
| 1859 | 1869 | pub fn nan(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1860 | 1870 | let (n,) = <(f64,)>::try_from_args(args)?; |
|
| 1861 | 1871 | Ok(Expr::Bool(n.is_nan())) |
|
| 1862 | 1872 | } |
|
| 1863 | 1873 | ||
| 1864 | - | /// And (&&). |
|
| 1865 | - | pub fn and(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1866 | - | fold(args, true, |a, b| a && b) |
|
| 1867 | - | } |
|
| 1868 | - | ||
| 1869 | - | /// Or (||). |
|
| 1870 | - | pub fn or(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1871 | - | fold(args, false, |a, b| a || b) |
|
| 1872 | - | } |
|
| 1873 | - | ||
| 1874 | 1874 | /// Equals (==). |
|
| 1875 | 1875 | pub fn eq(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1876 | 1876 | let mut args = args.into_iter(); |
|
| 1877 | 1877 | let Some(head) = args.next() else { |
|
| 1878 | 1878 | return Err(Error::ExpectedValue("expression")); |
ombre/tests/basic.om
+4 -0
| 81 | 81 | (assert (and true true)) |
|
| 82 | 82 | (assert (not (and true false))) |
|
| 83 | 83 | (assert (or false true)) |
|
| 84 | 84 | (assert (not (or false false))) |
|
| 85 | 85 | ||
| 86 | + | ;; Test short-circuiting |
|
| 87 | + | (assert (or true (error "not reached")) "or short-circuiting") |
|
| 88 | + | (assert (not (and false (error "not reached"))) "and short-circuiting") |
|
| 89 | + | ||
| 86 | 90 | ;; Test comparisons |
|
| 87 | 91 | (assert (> 5 3)) |
|
| 88 | 92 | (assert (not (> 3 5))) |
|
| 89 | 93 | (assert (< 3 5)) |
|
| 90 | 94 | (assert (not (< 5 3))) |