ombre: Improve language semantics and syntax
f731fcc347be1656c2c9803e2c90e5492993ce05
Move closer to Janet lisp.
1 parent
c7628188
ombre/src/lisp/evaluator.rs
+53 -40
| 1022 | 1022 | env.intrinsic( |
|
| 1023 | 1023 | "nth", |
|
| 1024 | 1024 | "Get the *nth* element of a list or vector", |
|
| 1025 | 1025 | intrinsic::nth, |
|
| 1026 | 1026 | ); |
|
| 1027 | - | env.intrinsic( |
|
| 1028 | - | "list", |
|
| 1029 | - | "Create a new list from the given arguments", |
|
| 1030 | - | intrinsic::list, |
|
| 1031 | - | ); |
|
| 1032 | 1027 | env.intrinsic( |
|
| 1033 | 1028 | "dict", |
|
| 1034 | 1029 | "Create a new dictionary from the given list", |
|
| 1035 | 1030 | intrinsic::dict, |
|
| 1036 | 1031 | ); |
| 1392 | 1387 | }), |
|
| 1393 | 1388 | Expr::Vector(v) => v |
|
| 1394 | 1389 | .into_iter() |
|
| 1395 | 1390 | .map(|e| eval(e, env)) |
|
| 1396 | 1391 | .collect::<Result<Vec<_>>>() |
|
| 1397 | - | .map(Expr::Vector), |
|
| 1392 | + | .map(Expr::List), |
|
| 1398 | 1393 | Expr::Vector2D(v) => Ok(Expr::Vector2D(v)), |
|
| 1399 | 1394 | Expr::Vector3D(v) => Ok(Expr::Vector3D(v)), |
|
| 1400 | 1395 | Expr::Vector4D(v) => Ok(Expr::Vector4D(v)), |
|
| 1401 | 1396 | Expr::Dict(d) => d |
|
| 1402 | 1397 | .into_iter() |
| 1555 | 1550 | _ => return Err(Error::UnexpectedValue(expr)), |
|
| 1556 | 1551 | }; |
|
| 1557 | 1552 | Ok(Expr::Bool(is_empty)) |
|
| 1558 | 1553 | } |
|
| 1559 | 1554 | ||
| 1560 | - | /// Create a list. |
|
| 1561 | - | pub fn list(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1562 | - | Ok(Expr::List(args)) |
|
| 1563 | - | } |
|
| 1564 | - | ||
| 1565 | 1555 | /// Create a dictionary. |
|
| 1566 | 1556 | pub fn dict(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1567 | 1557 | if args.is_empty() { |
|
| 1568 | 1558 | return Ok(Expr::Dict(HashMap::new())); |
|
| 1569 | 1559 | } |
| 1648 | 1638 | return Ok(Expr::nil()); |
|
| 1649 | 1639 | } |
|
| 1650 | 1640 | let mut args = args.into_iter(); |
|
| 1651 | 1641 | ||
| 1652 | 1642 | match args.next() { |
|
| 1643 | + | Some(Expr::List(mut l)) => { |
|
| 1644 | + | l.extend(args); |
|
| 1645 | + | Ok(Expr::Vector(l)) |
|
| 1646 | + | } |
|
| 1653 | 1647 | Some(Expr::Vector(mut v)) => { |
|
| 1654 | 1648 | v.extend(args); |
|
| 1655 | 1649 | Ok(Expr::Vector(v)) |
|
| 1656 | 1650 | } |
|
| 1657 | 1651 | _ => Err(Error::ExpectedValue("vector")), |
| 1967 | 1961 | let tokens = Scanner::new(input.as_str()); |
|
| 1968 | 1962 | let parser = Parser::new(tokens); |
|
| 1969 | 1963 | let eval = Evaluator::new(parser); |
|
| 1970 | 1964 | let result = eval.eval(&mut Env::default()).unwrap(); |
|
| 1971 | 1965 | ||
| 1972 | - | assert_eq!(result, expected); |
|
| 1966 | + | assert_eq!(result, expected, "{input}"); |
|
| 1973 | 1967 | } |
|
| 1974 | 1968 | ||
| 1975 | 1969 | #[track_caller] |
|
| 1976 | 1970 | fn expect_err(input: &[&str], err: Error) { |
|
| 1977 | 1971 | let input = input.join("\n"); |
| 1991 | 1985 | expect( |
|
| 1992 | 1986 | &["(def fnord 'answer)", "fnord"], |
|
| 1993 | 1987 | Expr::Symbol("answer".into()), |
|
| 1994 | 1988 | ); |
|
| 1995 | 1989 | expect( |
|
| 1996 | - | &["(def fnord {* 2 21})", "fnord"], |
|
| 1990 | + | &["(def fnord '(* 2 21))", "fnord"], |
|
| 1997 | 1991 | Expr::list([Expr::symbol("*"), Expr::Integer(2), Expr::Integer(21)]), |
|
| 1998 | 1992 | ); |
|
| 1999 | 1993 | expect(&["(def fnord (* 2 21))", "fnord"], Expr::Integer(42)); |
|
| 2000 | - | expect(&["(def fnord {* 2 21})", "(eval fnord)"], Expr::Integer(42)); |
|
| 1994 | + | expect( |
|
| 1995 | + | &["(def fnord '(* 2 21))", "(eval fnord)"], |
|
| 1996 | + | Expr::Integer(42), |
|
| 1997 | + | ); |
|
| 2001 | 1998 | expect( |
|
| 2002 | 1999 | &["(def f (fn [x] (* x x)))", "((first [f ()]) 3)"], |
|
| 2003 | 2000 | Expr::Integer(9), |
|
| 2004 | 2001 | ); |
|
| 2005 | 2002 | } |
| 2021 | 2018 | &[ |
|
| 2022 | 2019 | "(def fnord 42)", |
|
| 2023 | 2020 | "(def f (fn [] fnord))", |
|
| 2024 | 2021 | "(def fnord 0)", |
|
| 2025 | 2022 | "(def g (fn [] fnord))", |
|
| 2026 | - | "(list (f) (g))", |
|
| 2023 | + | "[(f) (g)]", |
|
| 2027 | 2024 | ], |
|
| 2028 | 2025 | Expr::list([Expr::Integer(42), Expr::Integer(0)]), |
|
| 2029 | 2026 | ); |
|
| 2030 | 2027 | } |
|
| 2031 | 2028 |
| 2141 | 2138 | #[test] |
|
| 2142 | 2139 | fn test_eval() { |
|
| 2143 | 2140 | expect(&["(eval 5)"], Expr::Integer(5)); |
|
| 2144 | 2141 | expect(&["(eval (+ 3 2))"], Expr::Integer(5)); |
|
| 2145 | 2142 | expect(&["(eval '(+ 3 2))"], Expr::Integer(5)); |
|
| 2146 | - | expect(&["(eval {+ 3 2})"], Expr::Integer(5)); |
|
| 2143 | + | expect(&["(eval [+ 3 2])"], Expr::Integer(5)); |
|
| 2147 | 2144 | expect(&["(eval ''symbol)"], Expr::Symbol("symbol".into())); |
|
| 2148 | 2145 | expect( |
|
| 2149 | 2146 | &["(eval ''(+ 3 2))"], |
|
| 2150 | 2147 | Expr::list([Expr::Symbol("+".into()), Expr::Integer(3), Expr::Integer(2)]), |
|
| 2151 | 2148 | ); |
| 2167 | 2164 | expect( |
|
| 2168 | 2165 | &["(rgba 1.0 0.0 0.3 1.0)"], |
|
| 2169 | 2166 | Expr::Color(Rgba::new(1.0, 0.0, 0.3, 1.0)), |
|
| 2170 | 2167 | ); |
|
| 2171 | 2168 | expect( |
|
| 2172 | - | &["{#111111 #ff00ff}"], |
|
| 2169 | + | &["'(#111111 #ff00ff)"], |
|
| 2173 | 2170 | Expr::list([ |
|
| 2174 | 2171 | Expr::Color(Rgba8::new(0x11, 0x11, 0x11, 0xff).into()), |
|
| 2175 | 2172 | Expr::Color(Rgba8::new(0xff, 0x00, 0xff, 0xff).into()), |
|
| 2176 | 2173 | ]), |
|
| 2177 | 2174 | ); |
| 2218 | 2215 | ||
| 2219 | 2216 | #[test] |
|
| 2220 | 2217 | fn test_fn_optional_args() { |
|
| 2221 | 2218 | expect( |
|
| 2222 | 2219 | &[ |
|
| 2223 | - | "(def greet (fn [x y :name \"greyface\" :age 103] (list x y name age)))", |
|
| 2220 | + | "(def greet (fn [x y :name \"greyface\" :age 103] [x y name age]))", |
|
| 2224 | 2221 | "(greet 1 2)", |
|
| 2225 | 2222 | ], |
|
| 2226 | 2223 | Expr::list([ |
|
| 2227 | 2224 | Expr::Integer(1), |
|
| 2228 | 2225 | Expr::Integer(2), |
| 2230 | 2227 | Expr::Integer(103), |
|
| 2231 | 2228 | ]), |
|
| 2232 | 2229 | ); |
|
| 2233 | 2230 | expect( |
|
| 2234 | 2231 | &[ |
|
| 2235 | - | "(def greet (fn [x y :name \"greyface\" :age 103] (list x y name age)))", |
|
| 2232 | + | "(def greet (fn [x y :name \"greyface\" :age 103] [x y name age]))", |
|
| 2236 | 2233 | "(greet 1 2 :name \"alice\" :age 31)", |
|
| 2237 | 2234 | ], |
|
| 2238 | 2235 | Expr::list([ |
|
| 2239 | 2236 | Expr::Integer(1), |
|
| 2240 | 2237 | Expr::Integer(2), |
| 2242 | 2239 | Expr::Integer(31), |
|
| 2243 | 2240 | ]), |
|
| 2244 | 2241 | ); |
|
| 2245 | 2242 | expect( |
|
| 2246 | 2243 | &[ |
|
| 2247 | - | "(def greet (fn [x y :name \"greyface\" :age 103] (list x y name age)))", |
|
| 2244 | + | "(def greet (fn [x y :name \"greyface\" :age 103] [x y name age]))", |
|
| 2248 | 2245 | "(greet 1 2 :age 33)", |
|
| 2249 | 2246 | ], |
|
| 2250 | 2247 | Expr::list([ |
|
| 2251 | 2248 | Expr::Integer(1), |
|
| 2252 | 2249 | Expr::Integer(2), |
| 2254 | 2251 | Expr::Integer(33), |
|
| 2255 | 2252 | ]), |
|
| 2256 | 2253 | ); |
|
| 2257 | 2254 | expect( |
|
| 2258 | 2255 | &[ |
|
| 2259 | - | "(def greet (fn [:name \"greyface\" :eyes :brown] (list name eyes)))", |
|
| 2256 | + | "(def greet (fn [:name \"greyface\" :eyes :brown] [name eyes]))", |
|
| 2260 | 2257 | "(greet :eyes :blue)", |
|
| 2261 | 2258 | ], |
|
| 2262 | 2259 | Expr::list([Expr::Str("greyface".to_owned()), Expr::Atom("blue".into())]), |
|
| 2263 | 2260 | ); |
|
| 2264 | 2261 | expect( |
|
| 2265 | 2262 | &[ |
|
| 2266 | - | "(def greet (fn [:name \"greyface\" :eyes :brown] (list name eyes)))", |
|
| 2263 | + | "(def greet (fn [:name \"greyface\" :eyes :brown] [name eyes]))", |
|
| 2267 | 2264 | "(greet)", |
|
| 2268 | 2265 | ], |
|
| 2269 | 2266 | Expr::list([Expr::Str("greyface".to_owned()), Expr::Atom("brown".into())]), |
|
| 2270 | 2267 | ); |
|
| 2271 | 2268 | } |
|
| 2272 | 2269 | ||
| 2273 | 2270 | #[test] |
|
| 2274 | 2271 | fn test_first() { |
|
| 2275 | - | expect(&["(first (list 4 3 1))"], Expr::Integer(4)); |
|
| 2272 | + | expect(&["(first '(4 3 1))"], Expr::Integer(4)); |
|
| 2273 | + | expect(&["(first [4 3 1])"], Expr::Integer(4)); |
|
| 2276 | 2274 | expect( |
|
| 2277 | - | &["(first (list (list 4 2) 3 1))"], |
|
| 2275 | + | &["(first [[4 2] 3 1])"], |
|
| 2278 | 2276 | Expr::list([Expr::Integer(4), Expr::Integer(2)]), |
|
| 2279 | 2277 | ); |
|
| 2280 | 2278 | expect(&["(first ())"], Expr::nil()); |
|
| 2279 | + | expect(&["(first [])"], Expr::nil()); |
|
| 2281 | 2280 | } |
|
| 2282 | 2281 | ||
| 2283 | 2282 | #[test] |
|
| 2284 | 2283 | fn test_last() { |
|
| 2285 | 2284 | expect(&["(last [4 3 1])"], Expr::Integer(1)); |
| 2310 | 2309 | ); |
|
| 2311 | 2310 | } |
|
| 2312 | 2311 | ||
| 2313 | 2312 | #[test] |
|
| 2314 | 2313 | fn test_dict() { |
|
| 2314 | + | expect( |
|
| 2315 | + | &["{:x 1 :y 2 :z 3}"], |
|
| 2316 | + | Expr::Dict(HashMap::from_iter([ |
|
| 2317 | + | (Key::Atom("x".into()), Expr::Integer(1)), |
|
| 2318 | + | (Key::Atom("y".into()), Expr::Integer(2)), |
|
| 2319 | + | (Key::Atom("z".into()), Expr::Integer(3)), |
|
| 2320 | + | ])), |
|
| 2321 | + | ); |
|
| 2315 | 2322 | expect( |
|
| 2316 | 2323 | &["(dict [:x 1 :y 2 :z (+ 2 1)])"], |
|
| 2317 | 2324 | Expr::Dict(HashMap::from_iter([ |
|
| 2318 | 2325 | (Key::Atom("x".into()), Expr::Integer(1)), |
|
| 2319 | 2326 | (Key::Atom("y".into()), Expr::Integer(2)), |
|
| 2320 | 2327 | (Key::Atom("z".into()), Expr::Integer(3)), |
|
| 2321 | 2328 | ])), |
|
| 2322 | 2329 | ); |
|
| 2323 | 2330 | expect( |
|
| 2324 | - | &["(dict {:x 1 :y 2 :z 3})"], |
|
| 2331 | + | &["{:x 1 :y 2 :z (+ 2 1)}"], |
|
| 2325 | 2332 | Expr::Dict(HashMap::from_iter([ |
|
| 2326 | 2333 | (Key::Atom("x".into()), Expr::Integer(1)), |
|
| 2327 | 2334 | (Key::Atom("y".into()), Expr::Integer(2)), |
|
| 2328 | 2335 | (Key::Atom("z".into()), Expr::Integer(3)), |
|
| 2329 | 2336 | ])), |
|
| 2330 | 2337 | ); |
|
| 2331 | 2338 | expect( |
|
| 2332 | - | &["(let [d (dict [1 :x 2 :y 3 :z])] (lookup 2 d))"], |
|
| 2339 | + | &["(let [d {1 :x 2 :y 3 :z}] (lookup 2 d))"], |
|
| 2333 | 2340 | Expr::Atom("y".into()), |
|
| 2334 | 2341 | ); |
|
| 2335 | 2342 | expect( |
|
| 2336 | 2343 | &["(dict [`x` 1 \"fnord\" 2 41 :a])"], |
|
| 2337 | 2344 | Expr::Dict(HashMap::from_iter([ |
| 2350 | 2357 | } |
|
| 2351 | 2358 | ||
| 2352 | 2359 | #[test] |
|
| 2353 | 2360 | fn test_map() { |
|
| 2354 | 2361 | expect( |
|
| 2355 | - | &["(map (fn [x] (+ x 1)) (list 1 2 3))"], |
|
| 2362 | + | &["(map (fn [x] (+ x 1)) '(1 2 3))"], |
|
| 2356 | 2363 | Expr::vector([Expr::Integer(2), Expr::Integer(3), Expr::Integer(4)]), |
|
| 2357 | 2364 | ); |
|
| 2358 | 2365 | expect( |
|
| 2359 | 2366 | &["(map (fn [x] (+ x x)) [1 2 3])"], |
|
| 2360 | 2367 | Expr::vector([Expr::Integer(2), Expr::Integer(4), Expr::Integer(6)]), |
| 2364 | 2371 | Expr::vector([Expr::Integer(-2), Expr::Integer(-4), Expr::Integer(-6)]), |
|
| 2365 | 2372 | ); |
|
| 2366 | 2373 | } |
|
| 2367 | 2374 | ||
| 2368 | 2375 | #[test] |
|
| 2369 | - | fn test_list() { |
|
| 2376 | + | fn test_vector() { |
|
| 2377 | + | expect(&["[]"], Expr::nil()); |
|
| 2370 | 2378 | expect( |
|
| 2371 | - | &["(list 1 2 3)"], |
|
| 2379 | + | &["[1 2 3]"], |
|
| 2372 | 2380 | Expr::list([Expr::Integer(1), Expr::Integer(2), Expr::Integer(3)]), |
|
| 2373 | 2381 | ); |
|
| 2374 | - | expect(&["(list)"], Expr::nil()); |
|
| 2382 | + | expect(&["[(+ 1 2)]"], Expr::list([Expr::Integer(3)])); |
|
| 2383 | + | expect_err(&["[(4 2)]"], Error::UnexpectedValue(Expr::Integer(4))); |
|
| 2384 | + | expect(&["(eq? () [])"], Expr::Bool(true)); |
|
| 2385 | + | } |
|
| 2386 | + | ||
| 2387 | + | #[test] |
|
| 2388 | + | fn test_list() { |
|
| 2375 | 2389 | expect( |
|
| 2376 | - | &["(list {+ 1})"], |
|
| 2377 | - | Expr::list([Expr::list([Expr::Symbol("+".into()), Expr::Integer(1)])]), |
|
| 2390 | + | &["'(+ 1)"], |
|
| 2391 | + | Expr::list([Expr::Symbol("+".into()), Expr::Integer(1)]), |
|
| 2378 | 2392 | ); |
|
| 2379 | - | expect(&["{symbol}"], Expr::list([Expr::Symbol("symbol".into())])); |
|
| 2393 | + | expect(&["'(symbol)"], Expr::list([Expr::Symbol("symbol".into())])); |
|
| 2380 | 2394 | expect( |
|
| 2381 | - | &["{(symbol)}"], |
|
| 2395 | + | &["'((symbol))"], |
|
| 2382 | 2396 | Expr::list([Expr::list([Expr::Symbol("symbol".into())])]), |
|
| 2383 | 2397 | ); |
|
| 2384 | 2398 | expect( |
|
| 2385 | - | &["{{symbol}}"], |
|
| 2399 | + | &["'('(symbol))"], |
|
| 2386 | 2400 | Expr::list([Expr::Quoted(Box::new(Expr::list([Expr::Symbol( |
|
| 2387 | 2401 | "symbol".into(), |
|
| 2388 | 2402 | )])))]), |
|
| 2389 | 2403 | ); |
|
| 2390 | 2404 | expect( |
|
| 2391 | - | &["{+ 1 2}"], |
|
| 2405 | + | &["'(+ 1 2)"], |
|
| 2392 | 2406 | Expr::list([Expr::Symbol("+".into()), Expr::Integer(1), Expr::Integer(2)]), |
|
| 2393 | 2407 | ); |
|
| 2394 | 2408 | expect( |
|
| 2395 | - | &["{1 (2 3)}"], |
|
| 2409 | + | &["'(1 (2 3))"], |
|
| 2396 | 2410 | Expr::list([ |
|
| 2397 | 2411 | Expr::Integer(1), |
|
| 2398 | 2412 | Expr::list([Expr::Integer(2), Expr::Integer(3)]), |
|
| 2399 | 2413 | ]), |
|
| 2400 | 2414 | ); |
|
| 2401 | - | expect(&["(eq? {} ())"], Expr::Bool(true)); |
|
| 2402 | - | expect_err(&["(list (4 2))"], Error::UnexpectedValue(Expr::Integer(4))); |
|
| 2415 | + | expect(&["(eq? '() ())"], Expr::Bool(true)); |
|
| 2403 | 2416 | } |
|
| 2404 | 2417 | ||
| 2405 | 2418 | #[test] |
|
| 2406 | 2419 | fn test_length() { |
|
| 2407 | 2420 | expect(&["(length '(1 2 3))"], Expr::Integer(3)); |
| 2577 | 2590 | "(c1)", |
|
| 2578 | 2591 | "(c1)", |
|
| 2579 | 2592 | "(def c2 (make-counter))", |
|
| 2580 | 2593 | "(c2)", |
|
| 2581 | 2594 | "(c1)", |
|
| 2582 | - | "(list (c1) (c2))", |
|
| 2595 | + | "[(c1) (c2)]", |
|
| 2583 | 2596 | ], |
|
| 2584 | 2597 | Expr::list([Expr::Integer(3), Expr::Integer(1)]), |
|
| 2585 | 2598 | ); |
|
| 2586 | 2599 | // Test modify! function. |
|
| 2587 | 2600 | expect( |
ombre/src/lisp/parser.rs
+40 -33
| 1 | 1 | //! Parser. |
|
| 2 | + | use std::collections::HashMap; |
|
| 2 | 3 | use std::fmt; |
|
| 3 | 4 | use std::iter::Peekable; |
|
| 4 | 5 | use std::rc::Rc; |
|
| 5 | 6 | use std::result; |
|
| 6 | 7 | ||
| 7 | - | use crate::lisp::evaluator::{Expr, Symbol}; |
|
| 8 | + | use crate::lisp::evaluator::{Expr, Key, Symbol}; |
|
| 8 | 9 | use crate::lisp::scanner; |
|
| 9 | 10 | use crate::lisp::scanner::Token; |
|
| 10 | 11 | ||
| 11 | 12 | /// Result type for all functions. |
|
| 12 | 13 | pub type Result<T> = result::Result<T, Error>; |
| 18 | 19 | Scanner(scanner::Error), |
|
| 19 | 20 | /// An unexpected token was encountered. |
|
| 20 | 21 | UnexpectedToken(Token<'static>), |
|
| 21 | 22 | /// An unexpected end of input was reached. |
|
| 22 | 23 | EndOfInput, |
|
| 24 | + | /// Invalid dictionary key. |
|
| 25 | + | InvalidDictKey(Expr), |
|
| 26 | + | /// Incomplete dictionary key/value pair. |
|
| 27 | + | IncompleteDictPair(Key), |
|
| 23 | 28 | } |
|
| 24 | 29 | ||
| 25 | 30 | impl fmt::Display for Error { |
|
| 26 | 31 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
| 27 | 32 | match self { |
|
| 28 | 33 | Error::Scanner(e) => write!(f, "scanner: {e}"), |
|
| 29 | 34 | Error::UnexpectedToken(token) => write!(f, "unexpected token: {token:?}"), |
|
| 30 | 35 | Error::EndOfInput => write!(f, "unexpected end of input"), |
|
| 36 | + | Error::InvalidDictKey(e) => write!(f, "invalid dictionary key {e}"), |
|
| 37 | + | Error::IncompleteDictPair(k) => write!(f, "missing value for dictionary key {k}"), |
|
| 31 | 38 | } |
|
| 32 | 39 | } |
|
| 33 | 40 | } |
|
| 34 | 41 | ||
| 35 | 42 | impl std::error::Error for Error {} |
| 70 | 77 | fn parse_expr(&mut self) -> Result<Expr> { |
|
| 71 | 78 | let token = self.peek()?; |
|
| 72 | 79 | let expr = match token { |
|
| 73 | 80 | Token::LParen => self.parse_sexpr()?, |
|
| 74 | 81 | Token::RParen => return Err(Error::UnexpectedToken(token.owned())), |
|
| 75 | - | Token::LBrace => self.parse_qexpr()?, |
|
| 82 | + | Token::LBrace => self.parse_dict()?, |
|
| 76 | 83 | Token::RBrace => return Err(Error::UnexpectedToken(token.owned())), |
|
| 77 | 84 | Token::LBracket => self.parse_vector()?, |
|
| 78 | 85 | Token::RBracket => return Err(Error::UnexpectedToken(token.owned())), |
|
| 79 | 86 | Token::Quote => self.parse_quoted()?, |
|
| 80 | 87 | Token::Symbol(s) => { |
| 115 | 122 | self.next_if(|t| t == &Token::RParen)?; |
|
| 116 | 123 | ||
| 117 | 124 | Ok(Expr::list(exprs)) |
|
| 118 | 125 | } |
|
| 119 | 126 | ||
| 120 | - | fn parse_qexpr(&mut self) -> Result<Expr> { |
|
| 127 | + | fn parse_dict(&mut self) -> Result<Expr> { |
|
| 121 | 128 | self.next_if(|t| t == &Token::LBrace)?; |
|
| 122 | - | let exprs = self.parse_until(|t| t == &Token::RBrace)?; |
|
| 129 | + | let mut exprs = self.parse_until(|t| t == &Token::RBrace)?.into_iter(); |
|
| 123 | 130 | self.next_if(|t| t == &Token::RBrace)?; |
|
| 124 | - | ||
| 125 | - | Ok(Expr::Quoted(Expr::list(exprs).into())) |
|
| 131 | + | let mut dict = HashMap::new(); |
|
| 132 | + | ||
| 133 | + | while let Some(e) = exprs.next() { |
|
| 134 | + | let k = match Key::try_from(e) { |
|
| 135 | + | Ok(k) => k, |
|
| 136 | + | Err(e) => return Err(Error::InvalidDictKey(e)), |
|
| 137 | + | }; |
|
| 138 | + | let Some(v) = exprs.next() else { |
|
| 139 | + | return Err(Error::IncompleteDictPair(k)); |
|
| 140 | + | }; |
|
| 141 | + | dict.insert(k, v); |
|
| 142 | + | } |
|
| 143 | + | Ok(Expr::Dict(dict)) |
|
| 126 | 144 | } |
|
| 127 | 145 | ||
| 128 | 146 | fn parse_vector(&mut self) -> Result<Expr> { |
|
| 129 | 147 | self.next_if(|t| t == &Token::LBracket)?; |
|
| 130 | 148 | let exprs = self.parse_until(|t| t == &Token::RBracket)?; |
| 242 | 260 | ])], |
|
| 243 | 261 | ); |
|
| 244 | 262 | } |
|
| 245 | 263 | ||
| 246 | 264 | #[test] |
|
| 247 | - | fn test_parse_qexpr() { |
|
| 248 | - | ok("{}", &[Expr::Quoted(Box::new(Expr::nil()))]); |
|
| 265 | + | fn test_parse_dict() { |
|
| 266 | + | ok("{}", &[Expr::Dict(HashMap::default())]); |
|
| 249 | 267 | ok( |
|
| 250 | - | "{+ 4 2}", |
|
| 251 | - | &[Expr::Quoted(Box::new(Expr::list([ |
|
| 252 | - | Expr::Symbol("+".into()), |
|
| 253 | - | Expr::Integer(4), |
|
| 254 | - | Expr::Integer(2), |
|
| 255 | - | ])))], |
|
| 268 | + | "{:x 32 :y 64 :z 128}", |
|
| 269 | + | &[Expr::Dict(HashMap::from_iter([ |
|
| 270 | + | (Key::Atom("x".into()), Expr::Integer(32)), |
|
| 271 | + | (Key::Atom("y".into()), Expr::Integer(64)), |
|
| 272 | + | (Key::Atom("z".into()), Expr::Integer(128)), |
|
| 273 | + | ]))], |
|
| 256 | 274 | ); |
|
| 257 | 275 | ok( |
|
| 258 | - | "{sym}", |
|
| 259 | - | &[Expr::Quoted(Box::new(Expr::list([Expr::Symbol( |
|
| 260 | - | "sym".into(), |
|
| 261 | - | )])))], |
|
| 262 | - | ); |
|
| 263 | - | ok( |
|
| 264 | - | "{{sym}}", |
|
| 265 | - | &[Expr::Quoted( |
|
| 266 | - | Expr::list([Expr::Quoted( |
|
| 267 | - | Expr::list([Expr::Symbol("sym".into())]).into(), |
|
| 268 | - | )]) |
|
| 269 | - | .into(), |
|
| 270 | - | )], |
|
| 271 | - | ); |
|
| 272 | - | ok( |
|
| 273 | - | "{(sym)}", |
|
| 274 | - | &[Expr::Quoted( |
|
| 275 | - | Expr::list([Expr::list([Expr::Symbol("sym".into())])]).into(), |
|
| 276 | - | )], |
|
| 276 | + | "{1 2}", |
|
| 277 | + | &[Expr::Dict(HashMap::from_iter([( |
|
| 278 | + | Key::Integer(1), |
|
| 279 | + | Expr::Integer(2), |
|
| 280 | + | )]))], |
|
| 277 | 281 | ); |
|
| 282 | + | err("{:x}"); |
|
| 283 | + | err("{:x 1 :y}"); |
|
| 284 | + | err("{#00ff11 42}"); |
|
| 278 | 285 | } |
|
| 279 | 286 | ||
| 280 | 287 | #[test] |
|
| 281 | 288 | fn test_parse_vector() { |
|
| 282 | 289 | ok("[]", &[Expr::vector(vec![])]); |