ombre: Add `while` special form

5e6e6bd4e991c97d2d4c4574d67f1220e1287aba
Alexis Sellier committed ago 1 parent c5bc36bc
ombre/src/lang/evaluator.rs +42 -6
1348 1348
    .into())
1349 1349
}
1350 1350
1351 1351
/// Evaluate an iterator and return the last value.
1352 1352
fn eval_iter(iter: impl IntoIterator<Item = Expr>, env: &mut Env<'_>) -> Result<Expr> {
1353 -
    Ok(iter
1354 -
        .into_iter()
1355 -
        .map(|e| eval(e, env))
1356 -
        .collect::<Result<Vec<_>>>()?
1357 -
        .pop()
1358 -
        .unwrap_or(Expr::Nil))
1353 +
    let mut last = Expr::Nil;
1354 +
    for expr in iter {
1355 +
        last = eval(expr, env)?;
1356 +
    }
1357 +
    Ok(last)
1359 1358
}
1360 1359
1361 1360
/// Evaluate a [`Tuple`].
1362 1361
fn eval_tuple(mut tuple: Tuple, env: &mut Env<'_>) -> Result<Expr> {
1363 1362
    // Literal tuples are not evaluated as function calls.
1468 1467
                        Err(e) => return Err(Error::UnexpectedValue(e)),
1469 1468
                    }
1470 1469
                }
1471 1470
                Ok(Expr::Bool(true))
1472 1471
            }
1472 +
            "while" => {
1473 +
                let cond = items.next().ok_or(Error::ExpectedValue("condition"))?;
1474 +
                let body: Vec<_> = items.collect();
1475 +
                if body.is_empty() {
1476 +
                    return Err(Error::ExpectedValue("body"));
1477 +
                }
1478 +
                let mut result = Expr::Nil;
1479 +
                while eval(cond.clone(), env)?.try_into().map_err(Error::UnexpectedValue)? {
1480 +
                    result = eval_iter(body.clone(), env)?;
1481 +
                }
1482 +
                Ok(result)
1483 +
            }
1473 1484
            _ => apply(head, items, env),
1474 1485
        }
1475 1486
    } else {
1476 1487
        apply(head, items, env)
1477 1488
    }
2787 2798
        expect(&["(nil? 42)"], Expr::Bool(false));
2788 2799
        expect(&["(nil? [])"], Expr::Bool(false));
2789 2800
        expect(&["(nil? (first []))"], Expr::Bool(true));
2790 2801
    }
2791 2802
2803 +
    #[test]
2804 +
    fn test_while() {
2805 +
        expect(
2806 +
            &[
2807 +
                "(def counter (ref 0))",
2808 +
                "(while (< (deref counter) 3)",
2809 +
                "  (modify! counter (fn [x] (+ x 1))))",
2810 +
                "(deref counter)",
2811 +
            ],
2812 +
            Expr::Integer(3),
2813 +
        );
2814 +
        expect(
2815 +
            &[
2816 +
                "(def sum (ref 0))",
2817 +
                "(def i (ref 1))",
2818 +
                "(while (<= (deref i) 5)",
2819 +
                "  (modify! sum (fn [x] (+ x (deref i))))",
2820 +
                "  (modify! i (fn [x] (+ x 1))))",
2821 +
                "(deref sum)",
2822 +
            ],
2823 +
            Expr::Integer(15),
2824 +
        );
2825 +
        expect(&["(while false 42)"], Expr::Nil);
2826 +
    }
2827 +
2792 2828
    #[test]
2793 2829
    fn test_contains() {
2794 2830
        expect(&["(contains? 2 '(1 2 3))"], Expr::Bool(true));
2795 2831
        expect(&["(contains? 4 '(1 2 3))"], Expr::Bool(false));
2796 2832
        expect(&["(contains? 2 [1 2 3])"], Expr::Bool(true));