rusche 0.2.4

A lightweight Scheme interpreter embeddable in Rust applications
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use std::any::Any;
use std::rc::Rc;

use crate::eval::{eval, EvalContext, EvalError};
use crate::expr::Expr;
use crate::list::List;

/// Get exactly one argument from a list.
///
/// Check if `args` contains extactly one argument. If so, return a reference
/// to the argument. Otherwise, return an error message.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `args` - List of arguments.
///
/// # Example
///
/// ```
/// use rusche::{
///     expr::Expr,
///     utils::get_exact_1_arg,
///     list
/// };
///
/// let args = list!(1);
/// let result = get_exact_1_arg("add", &args);
/// assert_eq!(result, Ok(&Expr::from(1)));
///
/// let args = list!(1, 2);
/// let result = get_exact_1_arg("add", &args);
/// assert!(result.is_err());
/// ```
pub fn get_exact_1_arg<'a>(proc_name: &str, args: &'a List) -> Result<&'a Expr, EvalError> {
    let mut iter = args.iter();
    let Some(arg) = iter.next() else {
        return Err(EvalError::from(format!("{proc_name} needs an argument.")));
    };
    if iter.next().is_none() {
        Ok(arg)
    } else {
        Err(EvalError::from(format!(
            "{proc_name} expects only 1 argument."
        )))
    }
}

/// Get exactly two arguments from a list.
///
/// Check if `args` contains extactly two arguments. If so, return a tuple that contains
/// references to the two arguments. Otherwise, return an error message.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `args` - List of arguments.
///
/// # Example
///
/// ```
/// use rusche::{
///     expr::Expr,
///     utils::get_exact_2_args,
///     list
/// };
///
/// let args = list!(1, 2);
/// let result = get_exact_2_args("add", &args);
/// assert_eq!(result, Ok((&Expr::from(1), &Expr::from(2))));
///
/// let args = list!(1);
/// let result = get_exact_2_args("add", &args);
/// assert!(result.is_err());
/// ```
pub fn get_exact_2_args<'a>(
    proc_name: &str,
    args: &'a List,
) -> Result<(&'a Expr, &'a Expr), EvalError> {
    let mut iter = args.iter();

    let arg1 = iter.next();
    let arg2 = iter.next();
    let arg3 = iter.next();

    match (arg1, arg2, arg3) {
        (Some(arg1), Some(arg2), None) => Ok((arg1, arg2)),
        (Some(_), Some(_), Some(_)) => Err(EvalError::from(format!(
            "{proc_name}: takes only two arguments"
        ))),
        _ => Err(EvalError::from(format!(
            "{proc_name}: requres two arguments"
        ))),
    }
}

/// Get exactly three arguments from a list.
///
/// Check if `args` contains extactly three arguments. If so, return a tuple that contains
/// references to the three arguments. Otherwise, return an error message.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `args` - List of arguments.
///
/// # Example
///
/// ```
/// use rusche::{
///     expr::Expr,
///     utils::get_exact_3_args,
///     list
/// };
///
/// let args = list!(1, 2, 3);
/// let result = get_exact_3_args("add", &args);
/// assert_eq!(result, Ok((&Expr::from(1), &Expr::from(2), &Expr::from(3))));
///
/// let args = list!(1, 2, 3, 4);
/// let result = get_exact_3_args("add", &args);
/// assert!(result.is_err());
/// ```
pub fn get_exact_3_args<'a>(
    proc_name: &str,
    args: &'a List,
) -> Result<(&'a Expr, &'a Expr, &'a Expr), EvalError> {
    let mut iter = args.iter();

    let arg1 = iter.next();
    let arg2 = iter.next();
    let arg3 = iter.next();
    let arg4 = iter.next();

    match (arg1, arg2, arg3, arg4) {
        (Some(arg1), Some(arg2), Some(arg3), None) => Ok((arg1, arg2, arg3)),
        (Some(_), Some(_), Some(_), Some(_)) => Err(EvalError::from(format!(
            "{proc_name}: takes only two arguments"
        ))),
        _ => Err(EvalError::from(format!(
            "{proc_name}: requres two arguments"
        ))),
    }
}

/// Get two or three arguments from a list.
///
/// Check if `args` contains two or three arguments. If so, return a tuple that contains
/// references to the two arguments and optional 3rd argument. Otherwise, return an error message.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `args` - List of arguments.
///
/// # Example
///
/// ```
/// use rusche::{
///     expr::Expr,
///     utils::get_2_or_3_args,
///     list
/// };
///
/// let args = list!(1, 2);
/// let result = get_2_or_3_args("add", &args);
/// assert_eq!(result, Ok((&Expr::from(1), &Expr::from(2), None)));
///
/// let args = list!(1, 2, 3);
/// let result = get_2_or_3_args("add", &args);
/// assert_eq!(result, Ok((&Expr::from(1), &Expr::from(2), Some(&Expr::from(3)))));
/// ```
pub fn get_2_or_3_args<'a>(
    proc_name: &str,
    args: &'a List,
) -> Result<(&'a Expr, &'a Expr, Option<&'a Expr>), EvalError> {
    let mut iter = args.iter();

    let arg1 = iter.next();
    let arg2 = iter.next();
    let arg3 = iter.next();
    let arg4 = iter.next();

    match (arg1, arg2, arg3, arg4) {
        (Some(arg1), Some(arg2), arg3, None) => Ok((arg1, arg2, arg3)),
        (Some(_), Some(_), Some(_), Some(_)) => Err(EvalError::from(format!(
            "{proc_name}: takes only up to 3 arguments"
        ))),
        _ => Err(EvalError::from(format!(
            "{proc_name}: requres at least 2 arguments"
        ))),
    }
}

/// Make a vector of symbol names from a list of arguments.
///
/// Check if `list` contains only symbols. If so, return a vector of the symbols.
/// Otherwise, return an error message. This function can be used to extract formal
/// arguments when implementing a function-like special form such as `lambda` or `defmacro`.
pub fn make_formal_args(list: &List) -> Result<Vec<String>, EvalError> {
    let mut formal_args = Vec::new();
    for item in list.iter() {
        let Expr::Sym(formal_arg, _) = item else {
            return Err(EvalError {
                message: format!("{item} is not a symbol."),
                span: item.span(),
            });
        };
        formal_args.push(formal_arg.clone());
    }

    Ok(formal_args)
}

/// Evaluate an expression into a string.
///
/// Check if `expr` evaluates to a string. If so, return the string. Otherwise, return an error message.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `expr` - Expression to evaluate.
/// * `context` - Evaluation context.
///
/// # Example
///
/// ```
/// use rusche::{
///     eval::Evaluator,
///     expr::Expr,
///     utils::eval_into_str,
/// };
///
/// let evaluator = Evaluator::default();
/// let expr = Expr::from("hello");
/// let result = eval_into_str("test", &expr, evaluator.context());
/// assert_eq!(result, Ok("hello".to_string()));
/// ```
pub fn eval_into_str(
    proc_name: &str,
    expr: &Expr,
    context: &EvalContext,
) -> Result<String, EvalError> {
    match eval(expr, context)? {
        Expr::Str(text, _) => Ok(text),
        _ => Err(EvalError {
            message: format!("{proc_name}: `{expr}` does not evaluate to a string."),
            span: expr.span(),
        }),
    }
}

/// Evaluate an expression into a number (`f64``).
///
/// Check if `expr` evaluates to a number. If so, return the number. Otherwise, return an error message.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `expr` - Expression to evaluate.
/// * `context` - Evaluation context.
///
/// # Example
///
/// ```
/// use rusche::{
///     eval::Evaluator,
///     expr::Expr,
///     utils::eval_into_num,
/// };
///
/// let evaluator = Evaluator::default();
/// let expr = Expr::from(12e-3);
/// let result = eval_into_num("test", &expr, evaluator.context());
/// assert_eq!(result, Ok(12e-3));
/// ```
pub fn eval_into_num(
    proc_name: &str,
    expr: &Expr,
    context: &EvalContext,
) -> Result<f64, EvalError> {
    match eval(expr, context)? {
        Expr::Num(value, _) => Ok(value),
        _ => Err(EvalError {
            message: format!("{proc_name}: `{expr}` does not evaluate to a number."),
            span: expr.span(),
        }),
    }
}

/// Evaluate an expression into an integer (`i32`).
///
/// Check if `expr` evaluates to `f64`` with `fract() == 0``. If so, return the number
/// as i32. Otherwise, return an error message.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `arg_name` - Name of the argument that we want to evaluate to an integer.
/// * `expr` - Expression to evaluate.
/// * `context` - Evaluation context.
///
/// # Example
///
/// ```
/// use rusche::{
///     eval::Evaluator,
///     expr::Expr,
///     utils::eval_into_int,
/// };
///
/// let evaluator = Evaluator::default();
///
/// let expr = Expr::from(12);
/// let result = eval_into_int("test", "index", &expr, evaluator.context());
/// assert_eq!(result, Ok(12));
///
/// let expr = Expr::from(12.0);
/// let result = eval_into_int("test", "index", &expr, evaluator.context());
/// assert_eq!(result, Ok(12));
///
/// let expr = Expr::from(12.5);
/// let result = eval_into_int("test", "index", &expr, evaluator.context());
/// assert!(result.is_err());
/// ```
pub fn eval_into_int(
    proc_name: &str,
    arg_name: &str,
    expr: &Expr,
    context: &EvalContext,
) -> Result<i32, EvalError> {
    let num = eval_into_num(proc_name, expr, context)?;

    if num.fract() == 0.0 {
        Ok(num as i32)
    } else {
        Err(EvalError {
            message: format!(
                "{}: {} must be an integer, but got {}.",
                proc_name, arg_name, num
            ),
            span: expr.span(),
        })
    }
}

/// Evaluate an expression into a foreign object.
///
/// Check if `expr` evaluates to a foreign object (`Expr::Foreign`). If so, return
/// the object (`Rc<dyn Any>`). Otherwise, return an error message.
/// The caller of this function can downcase the object to the expected type.
///
/// # Arguments
///
/// * `proc_name` - Name of the procedure who is calling this function.
/// * `expr` - Expression to evaluate.
/// * `context` - Evaluation context.
///
/// # Example
///
/// ```
/// use std::{any::Any, rc::Rc};
/// use rusche::{
///     eval::Evaluator,
///     expr::Expr,
///     utils::eval_into_foreign,
/// };
///
/// let evaluator = Evaluator::default();
/// let context = evaluator.context();
/// let expr = Expr::Foreign(Rc::new(Vec::<i32>::new()));
/// let object = eval_into_foreign("test", &expr, context).unwrap();
/// assert!(object.downcast::<Vec<i32>>().is_ok());
/// ```
pub fn eval_into_foreign(
    proc_name: &str,
    expr: &Expr,
    context: &EvalContext,
) -> Result<Rc<dyn Any>, EvalError> {
    match eval(expr, context)? {
        Expr::Foreign(object) => Ok(object),
        _ => Err(EvalError {
            message: format!("{proc_name}: `{expr}` does not evaluate to a foreign object."),
            span: expr.span(),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::eval::Evaluator;
    use crate::expr::intern;
    use crate::expr::test_utils::num;
    use crate::macros::list;

    #[test]
    fn test_get_exact_1_arg() {
        let args = list!(1);
        let result = get_exact_1_arg("add", &args);
        assert_eq!(result, Ok(&num(1)));

        let args = list!();
        let result = get_exact_1_arg("add", &args);
        assert!(result.is_err());

        let args = list!(1, 2);
        let result = get_exact_1_arg("add", &args);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_exact_2_args() {
        let args = list!(1, 2);
        let result = get_exact_2_args("add", &args);
        assert_eq!(result, Ok((&num(1), &num(2))));

        let args = list!(1);
        let result = get_exact_2_args("add", &args);
        assert!(result.is_err());

        let args = list!(1, 2, 3);
        let result = get_exact_2_args("add", &args);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_exact_3_args() {
        let args = list!(1, 2, 3);
        let result = get_exact_3_args("add", &args);
        assert_eq!(result, Ok((&num(1), &num(2), &num(3))));

        let args = list!(1, 2);
        let result = get_exact_3_args("add", &args);
        assert!(result.is_err());

        let args = list!(1, 2, 3, 4);
        let result = get_exact_3_args("add", &args);
        assert!(result.is_err());
    }

    #[test]
    fn test_eval_into_str() {
        let evaluator = Evaluator::new();
        let context = evaluator.context();

        let result = eval_into_str("test", &Expr::from("hello"), context);
        assert_eq!(result, Ok("hello".to_string()));

        let result = eval_into_str("test", &Expr::from(1), context);
        assert!(result.is_err());
    }

    #[test]
    fn test_eval_into_num() {
        let evaluator = Evaluator::new();
        let context = evaluator.context();

        let result = eval_into_num("test", &Expr::from(1), context);
        assert_eq!(result, Ok(1_f64));

        let result = eval_into_num("test", &Expr::from("1"), context);
        assert!(result.is_err());
    }

    #[test]
    fn test_eval_into_int() {
        let evaluator = Evaluator::new();
        let context = evaluator.context();

        let result = eval_into_int("test", "index", &Expr::from(1), context);
        assert_eq!(result, Ok(1));

        let result = eval_into_int("test", "index", &Expr::from(1.1), context);
        assert!(result.is_err());

        let result = eval_into_int("test", "index", &Expr::from("1"), context);
        assert!(result.is_err());
    }

    #[test]
    fn test_eval_into_foreign() {
        let evaluator = Evaluator::new();
        let context = evaluator.context();

        let expr = Expr::Foreign(Rc::new(Vec::<i32>::new()));
        let object = eval_into_foreign("test", &expr, context).unwrap();
        assert!(object.downcast::<Vec<i32>>().is_ok());

        assert!(eval_into_foreign("test", &Expr::from(1), context).is_err());
        assert!(eval_into_foreign("test", &Expr::from("str"), context).is_err());
        assert!(eval_into_foreign("test", &intern("sym"), context).is_err());
    }
}