ombre: Add `nan?` and `inf?`

ba9821117f79f803f79535d7346c887cf9142235
Alexis Sellier committed ago 1 parent 96f9be70
ombre/src/lisp/evaluator.rs +21 -0
1049 1049
        env.intrinsic(
1050 1050
            "pow",
1051 1051
            "Raise a number to the power of another",
1052 1052
            intrinsic::pow,
1053 1053
        );
1054 +
        env.intrinsic("nan?", "Check if a number is NaN", intrinsic::nan);
1055 +
        env.intrinsic("inf?", "Check if a number is infinity", intrinsic::inf);
1054 1056
        env.intrinsic("eq?", "Check if all arguments are equal", intrinsic::eq);
1055 1057
        env.intrinsic(
1056 1058
            "and",
1057 1059
            "Logical *and* of two or more boolean values",
1058 1060
            intrinsic::and,
1838 1840
    /// Not (!).
1839 1841
    pub fn not(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
1840 1842
        <(bool,)>::try_from_args(args).map(|(b,)| Expr::Bool(!b))
1841 1843
    }
1842 1844
1845 +
    /// Check if a number is Infinity.
1846 +
    pub fn inf(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
1847 +
        let (n,) = <(f64,)>::try_from_args(args)?;
1848 +
        Ok(Expr::Bool(n.is_infinite()))
1849 +
    }
1850 +
1851 +
    /// Check if a number is NaN.
1852 +
    pub fn nan(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
1853 +
        let (n,) = <(f64,)>::try_from_args(args)?;
1854 +
        Ok(Expr::Bool(n.is_nan()))
1855 +
    }
1856 +
1843 1857
    /// And (&&).
1844 1858
    pub fn and(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
1845 1859
        fold(args, true, |a, b| a && b)
1846 1860
    }
1847 1861
2324 2338
    fn test_not() {
2325 2339
        expect(&["(not true)"], Expr::Bool(false));
2326 2340
        expect(&["(not (not false))"], Expr::Bool(false));
2327 2341
    }
2328 2342
2343 +
    #[test]
2344 +
    fn test_nan() {
2345 +
        expect(&["(nan? (/ 0.0 0.0))"], Expr::Bool(true));
2346 +
        expect(&["(nan? 42)"], Expr::Bool(false));
2347 +
        expect(&["(nan? 3.14)"], Expr::Bool(false));
2348 +
    }
2349 +
2329 2350
    #[test]
2330 2351
    fn test_and() {
2331 2352
        expect(&["(and true true true)"], Expr::Bool(true));
2332 2353
        expect(&["(and true false true)"], Expr::Bool(false));
2333 2354
    }