ombre: Add `local` syntax to lisp
0e3fdcfcadfb0a99f5db754119cb0fe6a08158bc
1 parent
794d5aa4
ombre/src/lisp/evaluator.rs
+18 -6
| 772 | 772 | if body.is_empty() { |
|
| 773 | 773 | return Err(Error::ExpectedValue("expression")); |
|
| 774 | 774 | } |
|
| 775 | 775 | eval_let(assignments, body, env) |
|
| 776 | 776 | } |
|
| 777 | + | "local" => { |
|
| 778 | + | let Some(Expr::Symbol(name)) = items.next() else { |
|
| 779 | + | return Err(Error::ExpectedValue("symbol")); |
|
| 780 | + | }; |
|
| 781 | + | let Some(expr) = items.next() else { |
|
| 782 | + | return Err(Error::ExpectedValue("expression")); |
|
| 783 | + | }; |
|
| 784 | + | let expr = eval(expr, env)?; |
|
| 785 | + | env.binding(&name, None, expr); |
|
| 786 | + | ||
| 787 | + | Ok(Expr::nil()) |
|
| 788 | + | } |
|
| 777 | 789 | "fn" => { |
|
| 778 | 790 | let formals = items.next().ok_or(Error::ExpectedValue("vector"))?; |
|
| 779 | 791 | let body = items.collect::<Vec<_>>(); |
|
| 780 | 792 | ||
| 781 | 793 | let Expr::Vector(formals) = formals else { |
| 1525 | 1537 | } |
|
| 1526 | 1538 | ||
| 1527 | 1539 | #[test] |
|
| 1528 | 1540 | fn test_scoping() { |
|
| 1529 | 1541 | expect( |
|
| 1530 | - | &["(def x 0)", "(def f (fn [] (def x 42) x))", "(f)"], |
|
| 1542 | + | &["(def x 0)", "(def f (fn [] (local x 42) x))", "(f)"], |
|
| 1531 | 1543 | Expr::Integer(42), |
|
| 1532 | 1544 | ); |
|
| 1533 | 1545 | } |
|
| 1534 | 1546 | ||
| 1535 | 1547 | #[test] |
| 1784 | 1796 | expect_err(&["(nth [4 3 1] 1.2)"], Error::ExpectedValue("integer")); |
|
| 1785 | 1797 | } |
|
| 1786 | 1798 | ||
| 1787 | 1799 | #[test] |
|
| 1788 | 1800 | fn test_do() { |
|
| 1789 | - | expect(&["(do (def x 1) (def y 2) (+ x y))"], Expr::Integer(3)); |
|
| 1801 | + | expect(&["(do (local x 1) (local y 2) (+ x y))"], Expr::Integer(3)); |
|
| 1790 | 1802 | } |
|
| 1791 | 1803 | ||
| 1792 | 1804 | #[test] |
|
| 1793 | 1805 | fn test_let() { |
|
| 1794 | 1806 | expect(&["(let [x 1 y 2 z 3] (+ x y z))"], Expr::Integer(6)); |
| 2023 | 2035 | // Test mutations to refs are persisted beyond a lambda's scope. |
|
| 2024 | 2036 | expect( |
|
| 2025 | 2037 | &[ |
|
| 2026 | 2038 | "(def inc-ref", |
|
| 2027 | 2039 | " (fn [r]", |
|
| 2028 | - | " (def temp (deref r))", |
|
| 2040 | + | " (local temp (deref r))", |
|
| 2029 | 2041 | " (set! r (+ temp 1))))", |
|
| 2030 | 2042 | "(def a (ref 0))", |
|
| 2031 | 2043 | "(inc-ref a)", |
|
| 2032 | 2044 | "(deref a)", |
|
| 2033 | 2045 | ], |
| 2036 | 2048 | expect( |
|
| 2037 | 2049 | &[ |
|
| 2038 | 2050 | "(def ref-mapper", |
|
| 2039 | 2051 | " (fn [f]", |
|
| 2040 | 2052 | " (fn [r]", |
|
| 2041 | - | " (def temp (deref r))", |
|
| 2053 | + | " (local temp (deref r))", |
|
| 2042 | 2054 | " (set! r (f temp)))))", |
|
| 2043 | 2055 | "(def ref-incer (ref-mapper (fn [x] (+ x 1))))", |
|
| 2044 | 2056 | "(def foo (ref 0))", |
|
| 2045 | 2057 | "(ref-incer foo)", |
|
| 2046 | 2058 | "(deref foo)", |
| 2050 | 2062 | // Test multiple refs mutated in multiple lambdas. |
|
| 2051 | 2063 | expect( |
|
| 2052 | 2064 | &[ |
|
| 2053 | 2065 | "(def make-counter", |
|
| 2054 | 2066 | " (fn []", |
|
| 2055 | - | " (def current (ref 0))", |
|
| 2067 | + | " (local current (ref 0))", |
|
| 2056 | 2068 | " (fn []", |
|
| 2057 | - | " (def temp (deref current))", |
|
| 2069 | + | " (local temp (deref current))", |
|
| 2058 | 2070 | " (set! current (+ temp 1))", |
|
| 2059 | 2071 | " temp)))", |
|
| 2060 | 2072 | "(def c1 (make-counter))", |
|
| 2061 | 2073 | "(c1)", |
|
| 2062 | 2074 | "(c1)", |