ombre: Add `assert` intrinsic

3a25ab29e65270ab120caab8c3745ee2f9022c97
Alexis Sellier committed ago 1 parent 8bcb7b6e
ombre/src/lisp/evaluator.rs +57 -0
1154 1154
            "set!",
1155 1155
            "Set the value of a mutable reference",
1156 1156
            intrinsic::set_ref,
1157 1157
        );
1158 1158
        env.intrinsic("tuple", "Create a tuple from arguments", intrinsic::tuple);
1159 +
        env.intrinsic(
1160 +
            "assert",
1161 +
            "Raise an error if the condition is false",
1162 +
            intrinsic::assert,
1163 +
        );
1159 1164
        env.intrinsic(
1160 1165
            "modify!",
1161 1166
            "Modify a mutable reference in-place using a lambda",
1162 1167
            intrinsic::modify,
1163 1168
        );
2040 2045
    /// Create a tuple from arguments.
2041 2046
    pub fn tuple(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
2042 2047
        Ok(Tuple::new(args).into())
2043 2048
    }
2044 2049
2050 +
    /// Assert that a condition is true, raising an error if false.
2051 +
    /// Takes an optional error expression that will be used as the error message.
2052 +
    /// If no error expression is provided, defaults to "assertion failed".
2053 +
    pub fn assert(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
2054 +
        let mut args = args.into_iter();
2055 +
        let Some(cond) = args.next() else {
2056 +
            return Err(Error::WrongArgumentCount {
2057 +
                expected: 1,
2058 +
                actual: 0,
2059 +
            });
2060 +
        };
2061 +
        let Expr::Bool(cond) = cond else {
2062 +
            return Err(Error::UnexpectedValue(cond));
2063 +
        };
2064 +
        if !cond {
2065 +
            let err = args
2066 +
                .next()
2067 +
                .unwrap_or_else(|| Expr::Str("assertion failed".to_owned()));
2068 +
            return Err(Error::User(err));
2069 +
        }
2070 +
        Ok(Expr::nil())
2071 +
    }
2072 +
2045 2073
    fn fold<T: TryFrom<Expr, Error = Expr> + Into<Expr>>(
2046 2074
        args: impl IntoIterator<Item = Expr>,
2047 2075
        init: T,
2048 2076
        op: impl Fn(T, T) -> T,
2049 2077
    ) -> Result<Expr> {
2839 2867
            Expr::Str("The answer to life, the universe and everything.".to_string()),
2840 2868
        );
2841 2869
        expect(&["(doc 'undefined)"], Expr::nil());
2842 2870
    }
2843 2871
2872 +
    #[test]
2873 +
    fn test_assert() {
2874 +
        expect(&["(assert :true)"], Expr::nil());
2875 +
        expect_err(
2876 +
            &["(assert :false)"],
2877 +
            Error::User(Expr::Str("assertion failed".to_owned())),
2878 +
        );
2879 +
        expect_err(
2880 +
            &["(assert :false \"custom message\")"],
2881 +
            Error::User(Expr::Str("custom message".to_owned())),
2882 +
        );
2883 +
        expect_err(&["(assert :false 42)"], Error::User(Expr::Integer(42)));
2884 +
        expect_err(
2885 +
            &["(assert :false '(error details))"],
2886 +
            Error::User(Expr::tuple([
2887 +
                Expr::Symbol("error".into()),
2888 +
                Expr::Symbol("details".into()),
2889 +
            ])),
2890 +
        );
2891 +
        expect_err(&["(assert 42)"], Error::UnexpectedValue(Expr::Integer(42)));
2892 +
        expect_err(
2893 +
            &["(assert)"],
2894 +
            Error::WrongArgumentCount {
2895 +
                expected: 1,
2896 +
                actual: 0,
2897 +
            },
2898 +
        );
2899 +
    }
2900 +
2844 2901
    #[test]
2845 2902
    fn test_vector_field_access() {
2846 2903
        expect(&["(:x (vec2 1 2))"], Expr::Float(1.0));
2847 2904
        expect(&["(:y (vec2 1 2))"], Expr::Float(2.0));
2848 2905