ombre: Allow `def` everywhere
819457470b72b771c994ea24db0ee5a89bd055fb
1 parent
668bc332
ombre/src/lisp/evaluator.rs
+37 -50
| 1143 | 1143 | return eval_tuple(tuple, env); |
|
| 1144 | 1144 | }; |
|
| 1145 | 1145 | ||
| 1146 | 1146 | // Handle special top-level forms. |
|
| 1147 | 1147 | match head.deref() { |
|
| 1148 | - | "def" => { |
|
| 1149 | - | let mut iter = tuple.into_iter().skip(1); |
|
| 1150 | - | let sym = iter.next().ok_or(Error::ExpectedValue("symbol"))?; |
|
| 1151 | - | let Expr::Symbol(name) = sym else { |
|
| 1152 | - | return Err(Error::UnexpectedValue(sym)); |
|
| 1153 | - | }; |
|
| 1154 | - | let (docstr, value) = match (iter.next(), iter.next()) { |
|
| 1155 | - | (Some(Expr::Str(docstr)), Some(value)) => (Some(docstr), value), |
|
| 1156 | - | (Some(value), None) => (None, value), |
|
| 1157 | - | ||
| 1158 | - | _ => return Err(Error::ExpectedValue("expression")), |
|
| 1159 | - | }; |
|
| 1160 | - | ||
| 1161 | - | match eval(value, env)? { |
|
| 1162 | - | Expr::Function(Function::Closure(mut closure)) => { |
|
| 1163 | - | if closure.name.is_none() { |
|
| 1164 | - | if let Some(closure) = Rc::get_mut(&mut closure) { |
|
| 1165 | - | closure.name = Some(name.clone()); |
|
| 1166 | - | } |
|
| 1167 | - | } |
|
| 1168 | - | env.binding( |
|
| 1169 | - | name, |
|
| 1170 | - | docstr.as_deref(), |
|
| 1171 | - | Expr::Function(Function::Closure(closure)), |
|
| 1172 | - | ); |
|
| 1173 | - | } |
|
| 1174 | - | other => { |
|
| 1175 | - | env.binding(name, docstr.as_deref(), other); |
|
| 1176 | - | } |
|
| 1177 | - | } |
|
| 1178 | - | Ok(Expr::nil()) |
|
| 1179 | - | } |
|
| 1180 | 1148 | "module" => { |
|
| 1181 | 1149 | let mut items = tuple.into_iter().skip(1).peekable(); |
|
| 1182 | 1150 | let Some(Expr::Symbol(name)) = items.next() else { |
|
| 1183 | 1151 | return Err(Error::ExpectedValue("symbol")); |
|
| 1184 | 1152 | }; |
| 1297 | 1265 | return Ok(Expr::nil()); |
|
| 1298 | 1266 | }; |
|
| 1299 | 1267 | if let Expr::Symbol(ref name) = head { |
|
| 1300 | 1268 | match name.deref() { |
|
| 1301 | 1269 | "do" => eval_iter(items, env), |
|
| 1270 | + | "def" => { |
|
| 1271 | + | let sym = items.next().ok_or(Error::ExpectedValue("symbol"))?; |
|
| 1272 | + | let Expr::Symbol(name) = sym else { |
|
| 1273 | + | return Err(Error::UnexpectedValue(sym)); |
|
| 1274 | + | }; |
|
| 1275 | + | let (docstr, value) = match (items.next(), items.next()) { |
|
| 1276 | + | (Some(Expr::Str(docstr)), Some(value)) => (Some(docstr), value), |
|
| 1277 | + | (Some(value), None) => (None, value), |
|
| 1278 | + | ||
| 1279 | + | _ => return Err(Error::ExpectedValue("expression")), |
|
| 1280 | + | }; |
|
| 1281 | + | ||
| 1282 | + | match eval(value, env)? { |
|
| 1283 | + | Expr::Function(Function::Closure(mut closure)) => { |
|
| 1284 | + | if closure.name.is_none() { |
|
| 1285 | + | if let Some(closure) = Rc::get_mut(&mut closure) { |
|
| 1286 | + | closure.name = Some(name.clone()); |
|
| 1287 | + | } |
|
| 1288 | + | } |
|
| 1289 | + | env.binding( |
|
| 1290 | + | name, |
|
| 1291 | + | docstr.as_deref(), |
|
| 1292 | + | Expr::Function(Function::Closure(closure)), |
|
| 1293 | + | ); |
|
| 1294 | + | } |
|
| 1295 | + | other => { |
|
| 1296 | + | env.binding(name, docstr.as_deref(), other); |
|
| 1297 | + | } |
|
| 1298 | + | } |
|
| 1299 | + | Ok(Expr::nil()) |
|
| 1300 | + | } |
|
| 1302 | 1301 | "eval" => { |
|
| 1303 | 1302 | let Some(expr) = items.next() else { |
|
| 1304 | 1303 | return Err(Error::ExpectedValue("expression")); |
|
| 1305 | 1304 | }; |
|
| 1306 | 1305 | if let Some(e) = items.next() { |
| 1318 | 1317 | if body.is_empty() { |
|
| 1319 | 1318 | return Err(Error::ExpectedValue("expression")); |
|
| 1320 | 1319 | } |
|
| 1321 | 1320 | eval_let(assignments, body, env) |
|
| 1322 | 1321 | } |
|
| 1323 | - | "local" => { |
|
| 1324 | - | let Some(Expr::Symbol(name)) = items.next() else { |
|
| 1325 | - | return Err(Error::ExpectedValue("symbol")); |
|
| 1326 | - | }; |
|
| 1327 | - | let Some(expr) = items.next() else { |
|
| 1328 | - | return Err(Error::ExpectedValue("expression")); |
|
| 1329 | - | }; |
|
| 1330 | - | let expr = eval(expr, env)?; |
|
| 1331 | - | env.binding(name, None, expr); |
|
| 1332 | - | ||
| 1333 | - | Ok(Expr::nil()) |
|
| 1334 | - | } |
|
| 1335 | 1322 | "fn" => { |
|
| 1336 | 1323 | let formals = items.next().ok_or(Error::ExpectedValue("vector"))?; |
|
| 1337 | 1324 | let body = items.collect::<Vec<_>>(); |
|
| 1338 | 1325 | ||
| 1339 | 1326 | let Expr::Vector(formals) = formals else { |
| 2033 | 2020 | } |
|
| 2034 | 2021 | ||
| 2035 | 2022 | #[test] |
|
| 2036 | 2023 | fn test_scoping() { |
|
| 2037 | 2024 | expect( |
|
| 2038 | - | &["(def x 0)", "(def f (fn [] (local x 42) x))", "(f)"], |
|
| 2025 | + | &["(def x 0)", "(def f (fn [] (def x 42) x))", "(f)"], |
|
| 2039 | 2026 | Expr::Integer(42), |
|
| 2040 | 2027 | ); |
|
| 2041 | 2028 | } |
|
| 2042 | 2029 | ||
| 2043 | 2030 | #[test] |
| 2301 | 2288 | ); |
|
| 2302 | 2289 | } |
|
| 2303 | 2290 | ||
| 2304 | 2291 | #[test] |
|
| 2305 | 2292 | fn test_do() { |
|
| 2306 | - | expect(&["(do (local x 1) (local y 2) (+ x y))"], Expr::Integer(3)); |
|
| 2293 | + | expect(&["(do (def x 1) (def y 2) (+ x y))"], Expr::Integer(3)); |
|
| 2307 | 2294 | } |
|
| 2308 | 2295 | ||
| 2309 | 2296 | #[test] |
|
| 2310 | 2297 | fn test_let() { |
|
| 2311 | 2298 | expect(&["(let [x 1 y 2 z 3] (+ x y z))"], Expr::Integer(6)); |
| 2558 | 2545 | // Test mutations to refs are persisted beyond a lambda's scope. |
|
| 2559 | 2546 | expect( |
|
| 2560 | 2547 | &[ |
|
| 2561 | 2548 | "(def inc-ref", |
|
| 2562 | 2549 | " (fn [r]", |
|
| 2563 | - | " (local temp (deref r))", |
|
| 2550 | + | " (def temp (deref r))", |
|
| 2564 | 2551 | " (set! r (+ temp 1))))", |
|
| 2565 | 2552 | "(def a (ref 0))", |
|
| 2566 | 2553 | "(inc-ref a)", |
|
| 2567 | 2554 | "(deref a)", |
|
| 2568 | 2555 | ], |
| 2571 | 2558 | expect( |
|
| 2572 | 2559 | &[ |
|
| 2573 | 2560 | "(def ref-mapper", |
|
| 2574 | 2561 | " (fn [f]", |
|
| 2575 | 2562 | " (fn [r]", |
|
| 2576 | - | " (local temp (deref r))", |
|
| 2563 | + | " (def temp (deref r))", |
|
| 2577 | 2564 | " (set! r (f temp)))))", |
|
| 2578 | 2565 | "(def ref-incer (ref-mapper (fn [x] (+ x 1))))", |
|
| 2579 | 2566 | "(def foo (ref 0))", |
|
| 2580 | 2567 | "(ref-incer foo)", |
|
| 2581 | 2568 | "(deref foo)", |
| 2585 | 2572 | // Test multiple refs mutated in multiple lambdas. |
|
| 2586 | 2573 | expect( |
|
| 2587 | 2574 | &[ |
|
| 2588 | 2575 | "(def make-counter", |
|
| 2589 | 2576 | " (fn []", |
|
| 2590 | - | " (local current (ref 0))", |
|
| 2577 | + | " (def current (ref 0))", |
|
| 2591 | 2578 | " (fn []", |
|
| 2592 | - | " (local temp (deref current))", |
|
| 2579 | + | " (def temp (deref current))", |
|
| 2593 | 2580 | " (set! current (+ temp 1))", |
|
| 2594 | 2581 | " temp)))", |
|
| 2595 | 2582 | "(def c1 (make-counter))", |
|
| 2596 | 2583 | "(c1)", |
|
| 2597 | 2584 | "(c1)", |