1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use std::sync::Arc;

use crate::{
    context::Context,
    error::Error,
    expr::Expr,
    util::{
        args::{unpack_bool_arg, unpack_float_arg, unpack_int_arg, unpack_stringable_arg},
        module_util::require_module,
    },
};

// #todo support all types!

// #todo add support for eq_array, eq_map

// #todo #temp hackish polymorphism helper!
pub fn eq_polymorphic(args: &[Expr], context: &mut Context) -> Result<Expr, Error> {
    let Some(expr) = args.first() else {
        return Err(Error::invalid_arguments("malformed equality test", None));
    };
    match expr.unpack() {
        Expr::Int(..) => eq_int(args, context),
        Expr::Bool(..) => eq_bool(args, context),
        Expr::Float(..) => eq_float(args, context),
        Expr::String(..) => eq_string(args, context),
        Expr::Symbol(..) | Expr::KeySymbol(..) | Expr::Type(..) => eq_symbol(args, context),
        _ => Err(Error::invalid_arguments("malformed equality test", None)),
    }
}

pub fn eq_int(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // Use macros to monomorphise functions? or can we leverage Rust's generics? per viariant? maybe with cost generics?
    // #todo support overloading,
    // #todo make equality a method of Expr?
    // #todo support non-Int types
    // #todo support multiple arguments.

    // #todo also pass the function name, or at least show the function name upstream.
    let a = unpack_int_arg(args, 0, "a")?;
    let b = unpack_int_arg(args, 1, "b")?;

    Ok(Expr::Bool(a == b))
}

pub fn eq_float(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // Use macros to monomorphise functions? or can we leverage Rust's generics? per viariant? maybe with cost generics?
    // #todo support overloading,
    // #todo make equality a method of Expr?
    // #todo support non-Int types
    // #todo support multiple arguments.

    let a = unpack_float_arg(args, 0, "a")?;
    let b = unpack_float_arg(args, 1, "b")?;

    Ok(Expr::Bool(a == b))
}

pub fn eq_bool(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // #todo check comments in other eq_* functions.

    // #todo also pass the function name, or at least show the function name upstream.
    let a = unpack_bool_arg(args, 0, "a")?;
    let b = unpack_bool_arg(args, 1, "b")?;

    Ok(Expr::Bool(a == b))
}

pub fn eq_string(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // Use macros to monomorphise functions? or can we leverage Rust's generics? per viariant? maybe with cost generics?
    // #todo support overloading,
    // #todo make equality a method of Expr?
    // #todo support non-Int types
    // #todo support multiple arguments.

    let a = unpack_stringable_arg(args, 0, "a")?;
    let b = unpack_stringable_arg(args, 1, "b")?;

    Ok(Expr::Bool(a == b))
}

// #insight handles both (quoted) Symbol and KeySymbol, they are the same thing anyway. Also handles Type.
pub fn eq_symbol(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // Use macros to monomorphise functions? or can we leverage Rust's generics? per viariant? maybe with cost generics?
    // #todo support overloading,
    // #todo make equality a method of Expr?
    // #todo support non-Int types
    // #todo support multiple arguments.
    let [a, b] = args else {
        return Err(Error::invalid_arguments(
            "`=` requires at least two arguments",
            None,
        ));
    };

    let Some(a) = a.as_symbolic() else {
        return Err(Error::invalid_arguments(
            &format!("`{a}` is not a Symbol"),
            a.range(),
        ));
    };

    let Some(b) = b.as_symbolic() else {
        return Err(Error::invalid_arguments(
            &format!("`{b}` is not a Symbol"),
            b.range(),
        ));
    };

    Ok(Expr::Bool(a == b))
}

// #todo implement not_eq_* with Tan? can be automatically generic!

pub fn not_eq_int(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    Ok(Expr::Bool(eq_int(args, _context)?.is_false()))
}

pub fn not_eq_float(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    Ok(Expr::Bool(eq_float(args, _context)?.is_false()))
}

pub fn not_eq_string(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    Ok(Expr::Bool(eq_string(args, _context)?.is_false()))
}

// #insight handles both (quoted) Symbol and KeySymbol, they are the same thing anyway.
pub fn not_eq_symbol(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // Use macros to monomorphise functions? or can we leverage Rust's generics? per viariant? maybe with cost generics?
    // #todo support overloading,
    // #todo make equality a method of Expr?
    // #todo support non-Int types
    // #todo support multiple arguments.
    let [a, b] = args else {
        return Err(Error::invalid_arguments(
            "`!=` requires at least two arguments",
            None,
        ));
    };

    let Some(a) = a.as_symbolic() else {
        return Err(Error::invalid_arguments(
            &format!("`{a}` is not a String"),
            a.range(),
        ));
    };

    let Some(b) = b.as_symbolic() else {
        return Err(Error::invalid_arguments(
            &format!("`{b}` is not a Symbol"),
            b.range(),
        ));
    };

    Ok(Expr::Bool(a != b))
}

pub fn int_gt(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // #todo support multiple arguments.
    let [a, b] = args else {
        return Err(Error::invalid_arguments(
            "`>` requires at least two arguments",
            None,
        ));
    };

    let Some(a) = a.as_int() else {
        return Err(Error::invalid_arguments(
            &format!("`{a}` is not an Int"),
            a.range(),
        ));
    };

    let Some(b) = b.as_int() else {
        return Err(Error::invalid_arguments(
            &format!("`{b}` is not an Int"),
            b.range(),
        ));
    };

    Ok(Expr::Bool(a > b))
}

pub fn float_gt(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // #todo support multiple arguments.
    let [a, b] = args else {
        return Err(Error::invalid_arguments(
            "`>` requires at least two arguments",
            None,
        ));
    };

    let Some(a) = a.as_float() else {
        return Err(Error::invalid_arguments(
            &format!("`{a}` is not a Float"),
            a.range(),
        ));
    };

    let Some(b) = b.as_float() else {
        return Err(Error::invalid_arguments(
            &format!("`{b}` is not a Float"),
            b.range(),
        ));
    };

    Ok(Expr::Bool(a > b))
}

pub fn int_lt(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // #todo support multiple arguments.
    let [a, b] = args else {
        return Err(Error::invalid_arguments(
            "`<` requires at least two arguments",
            None,
        ));
    };

    let Some(a) = a.as_int() else {
        return Err(Error::invalid_arguments(
            &format!("`{a}` is not an Int"),
            a.range(),
        ));
    };

    let Some(b) = b.as_int() else {
        return Err(Error::invalid_arguments(
            &format!("`{b}` is not an Int"),
            b.range(),
        ));
    };

    Ok(Expr::Bool(a < b))
}

pub fn float_lt(args: &[Expr], _context: &mut Context) -> Result<Expr, Error> {
    // #todo support multiple arguments.

    let a = unpack_float_arg(args, 0, "a")?;
    let b = unpack_float_arg(args, 0, "b")?;

    Ok(Expr::Bool(a < b))
}

// #todo should we have an explicit module for these functions?

pub fn setup_lib_eq(context: &mut Context) {
    let module = require_module("prelude", context);

    module.insert("=", Expr::ForeignFunc(Arc::new(eq_int)));
    module.insert("=$$Int$$Int", Expr::ForeignFunc(Arc::new(eq_int)));
    module.insert("=$$Bool$$Bool", Expr::ForeignFunc(Arc::new(eq_bool)));
    module.insert("=$$Float$$Float", Expr::ForeignFunc(Arc::new(eq_float)));
    module.insert("=$$String$$String", Expr::ForeignFunc(Arc::new(eq_string)));
    // module.insert("=$$Symbol$$Symbol", Expr::ForeignFunc(Arc::new(eq_symbol)));
    module.insert(
        "=$$KeySymbol$$KeySymbol",
        Expr::ForeignFunc(Arc::new(eq_symbol)),
    );
    // #todo #hack this is nasty!
    module.insert("=$$Type$$Type", Expr::ForeignFunc(Arc::new(eq_symbol)));
    module.insert("=$$Type$$String", Expr::ForeignFunc(Arc::new(eq_symbol)));
    module.insert("=$$Type$$KeySymbol", Expr::ForeignFunc(Arc::new(eq_symbol)));

    module.insert("!=", Expr::ForeignFunc(Arc::new(not_eq_int)));
    module.insert("!=$$Int$$Int", Expr::ForeignFunc(Arc::new(not_eq_int)));
    module.insert(
        "!=$$Float$$Float",
        Expr::ForeignFunc(Arc::new(not_eq_float)),
    );
    module.insert(
        "!=$$String$$String",
        Expr::ForeignFunc(Arc::new(not_eq_string)),
    );
    module.insert(
        "!=$$Symbol$$Symbol",
        Expr::ForeignFunc(Arc::new(not_eq_symbol)),
    );
    module.insert(
        "!=$$KeySymbol$$KeySymbol",
        Expr::ForeignFunc(Arc::new(not_eq_symbol)),
    );

    module.insert(">", Expr::ForeignFunc(Arc::new(int_gt)));
    module.insert(">$$Int$$Int", Expr::ForeignFunc(Arc::new(int_gt)));
    module.insert(">$$Float$$Float", Expr::ForeignFunc(Arc::new(float_gt)));
    module.insert("<", Expr::ForeignFunc(Arc::new(int_lt)));
    module.insert("<$$Int$$Int", Expr::ForeignFunc(Arc::new(int_lt)));
    module.insert("<$$Float$$Float", Expr::ForeignFunc(Arc::new(float_lt)));
}

#[cfg(test)]
mod tests {
    use assert_matches::assert_matches;

    use crate::{api::eval_string, context::Context, expr::Expr};

    #[test]
    fn not_eq_usage() {
        let mut context = Context::new();

        let input = "(!= 4 5)";
        let expr = eval_string(input, &mut context).unwrap();
        assert_matches!(expr, Expr::Bool(true));

        let input = "(!= 5 5)";
        let expr = eval_string(input, &mut context).unwrap();
        assert_matches!(expr, Expr::Bool(false));

        let input = "(!= 5.2 5.2)";
        let expr = eval_string(input, &mut context).unwrap();
        assert_matches!(expr, Expr::Bool(false));

        let input = r#"(!= "george" "nadia")"#;
        let expr = eval_string(input, &mut context).unwrap();
        assert_matches!(expr, Expr::Bool(true));
    }
}