ombre: Improve dict/vec access
c5bc36bc26c3064620ce81153581a29afffb295c
1 parent
d198be57
ombre/src/lang/evaluator.rs
+77 -40
| 1114 | 1114 | "contains?", |
|
| 1115 | 1115 | "Check if a collection contains the given key", |
|
| 1116 | 1116 | intrinsic::contains, |
|
| 1117 | 1117 | ); |
|
| 1118 | 1118 | env.intrinsic("empty?", "Check if a collection is empty", intrinsic::empty); |
|
| 1119 | + | env.intrinsic("nil?", "Check if a value is nil", intrinsic::is_nil); |
|
| 1119 | 1120 | env.intrinsic( |
|
| 1120 | 1121 | "push", |
|
| 1121 | 1122 | "Push elements to the end of a vector", |
|
| 1122 | 1123 | intrinsic::push, |
|
| 1123 | 1124 | ); |
| 1521 | 1522 | ||
| 1522 | 1523 | /// Function application. |
|
| 1523 | 1524 | fn apply(f: Expr, args: impl Iterator<Item = Expr>, env: &mut Env<'_>) -> Result<Expr> { |
|
| 1524 | 1525 | match eval(f, env)? { |
|
| 1525 | 1526 | Expr::Function(f) => f.apply(args, env), |
|
| 1526 | - | Expr::Atom(s) => { |
|
| 1527 | - | let args: Vec<_> = args.map(|a| eval(a, env)).collect::<Result<_>>()?; |
|
| 1528 | - | let arity = args.len(); |
|
| 1529 | - | let Ok([value]) = <[_; 1]>::try_from(args) else { |
|
| 1530 | - | return Err(Error::WrongArgumentCount { |
|
| 1531 | - | expected: 1, |
|
| 1532 | - | actual: arity, |
|
| 1533 | - | }); |
|
| 1527 | + | Expr::Dict(d) => { |
|
| 1528 | + | let (expr,) = <(Expr,)>::try_from_args(args.collect())?; |
|
| 1529 | + | let expr = eval(expr, env)?; |
|
| 1530 | + | let Expr::Atom(a) = expr else { |
|
| 1531 | + | return Err(Error::UnexpectedValue(expr)); |
|
| 1534 | 1532 | }; |
|
| 1535 | - | match (s.as_ref(), value) { |
|
| 1536 | - | ("x", Expr::Vector2D(v)) => Ok(Expr::Float(v.x)), |
|
| 1537 | - | ("y", Expr::Vector2D(v)) => Ok(Expr::Float(v.y)), |
|
| 1538 | - | ("x", Expr::Vector3D(v)) => Ok(Expr::Float(v.x)), |
|
| 1539 | - | ("y", Expr::Vector3D(v)) => Ok(Expr::Float(v.y)), |
|
| 1540 | - | ("z", Expr::Vector3D(v)) => Ok(Expr::Float(v.z)), |
|
| 1541 | - | ("x", Expr::Vector4D(v)) => Ok(Expr::Float(v.x)), |
|
| 1542 | - | ("y", Expr::Vector4D(v)) => Ok(Expr::Float(v.y)), |
|
| 1543 | - | ("z", Expr::Vector4D(v)) => Ok(Expr::Float(v.z)), |
|
| 1544 | - | ("w", Expr::Vector4D(v)) => Ok(Expr::Float(v.w)), |
|
| 1545 | - | (_, Expr::Dict(dict)) => { |
|
| 1546 | - | if let Some(value) = dict.borrow().get(&Key::Atom(s)) { |
|
| 1547 | - | Ok(value.clone()) |
|
| 1548 | - | } else { |
|
| 1549 | - | Ok(Expr::Nil) |
|
| 1550 | - | } |
|
| 1551 | - | } |
|
| 1533 | + | if let Some(value) = d.borrow().get(&Key::Atom(a)) { |
|
| 1534 | + | Ok(value.clone()) |
|
| 1535 | + | } else { |
|
| 1536 | + | Ok(Expr::Nil) |
|
| 1537 | + | } |
|
| 1538 | + | } |
|
| 1539 | + | Expr::Vector2D(v) => { |
|
| 1540 | + | let (expr,) = <(Expr,)>::try_from_args(args.collect())?; |
|
| 1541 | + | let expr = eval(expr, env)?; |
|
| 1542 | + | let Expr::Atom(a) = expr else { |
|
| 1543 | + | return Err(Error::UnexpectedValue(expr)); |
|
| 1544 | + | }; |
|
| 1545 | + | match a.as_ref() { |
|
| 1546 | + | "x" => Ok(Expr::Float(v.x)), |
|
| 1547 | + | "y" => Ok(Expr::Float(v.y)), |
|
| 1548 | + | _ => Ok(Expr::Nil), |
|
| 1549 | + | } |
|
| 1550 | + | } |
|
| 1551 | + | Expr::Vector3D(v) => { |
|
| 1552 | + | let (expr,) = <(Expr,)>::try_from_args(args.collect())?; |
|
| 1553 | + | let expr = eval(expr, env)?; |
|
| 1554 | + | let Expr::Atom(a) = expr else { |
|
| 1555 | + | return Err(Error::UnexpectedValue(expr)); |
|
| 1556 | + | }; |
|
| 1557 | + | match a.as_ref() { |
|
| 1558 | + | "x" => Ok(Expr::Float(v.x)), |
|
| 1559 | + | "y" => Ok(Expr::Float(v.y)), |
|
| 1560 | + | "z" => Ok(Expr::Float(v.z)), |
|
| 1561 | + | _ => Ok(Expr::Nil), |
|
| 1562 | + | } |
|
| 1563 | + | } |
|
| 1564 | + | Expr::Vector4D(v) => { |
|
| 1565 | + | let (expr,) = <(Expr,)>::try_from_args(args.collect())?; |
|
| 1566 | + | let expr = eval(expr, env)?; |
|
| 1567 | + | let Expr::Atom(a) = expr else { |
|
| 1568 | + | return Err(Error::UnexpectedValue(expr)); |
|
| 1569 | + | }; |
|
| 1570 | + | match a.as_ref() { |
|
| 1571 | + | "x" => Ok(Expr::Float(v.x)), |
|
| 1572 | + | "y" => Ok(Expr::Float(v.y)), |
|
| 1573 | + | "z" => Ok(Expr::Float(v.z)), |
|
| 1574 | + | "w" => Ok(Expr::Float(v.w)), |
|
| 1552 | 1575 | _ => Ok(Expr::Nil), |
|
| 1553 | 1576 | } |
|
| 1554 | 1577 | } |
|
| 1555 | 1578 | e => Err(Error::UnexpectedValue(e)), |
|
| 1556 | 1579 | } |
| 1694 | 1717 | _ => return Err(Error::UnexpectedValue(expr)), |
|
| 1695 | 1718 | }; |
|
| 1696 | 1719 | Ok(Expr::Bool(is_empty)) |
|
| 1697 | 1720 | } |
|
| 1698 | 1721 | ||
| 1722 | + | /// Check if a value is nil. |
|
| 1723 | + | pub fn is_nil(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1724 | + | let (expr,) = <(Expr,)>::try_from_args(args)?; |
|
| 1725 | + | Ok(Expr::Bool(expr.is_nil())) |
|
| 1726 | + | } |
|
| 1727 | + | ||
| 1699 | 1728 | /// Create a dictionary. |
|
| 1700 | 1729 | pub fn dict(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1701 | 1730 | if args.is_empty() { |
|
| 1702 | 1731 | return Ok(Expr::dict(BTreeMap::new())); |
|
| 1703 | 1732 | } |
| 2750 | 2779 | expect(&["(empty? \"\")"], Expr::Bool(true)); |
|
| 2751 | 2780 | expect(&["(empty? (dict [:x 1]))"], Expr::Bool(false)); |
|
| 2752 | 2781 | expect(&["(empty? (dict))"], Expr::Bool(true)); |
|
| 2753 | 2782 | } |
|
| 2754 | 2783 | ||
| 2784 | + | #[test] |
|
| 2785 | + | fn test_nil() { |
|
| 2786 | + | expect(&["(nil? nil)"], Expr::Bool(true)); |
|
| 2787 | + | expect(&["(nil? 42)"], Expr::Bool(false)); |
|
| 2788 | + | expect(&["(nil? [])"], Expr::Bool(false)); |
|
| 2789 | + | expect(&["(nil? (first []))"], Expr::Bool(true)); |
|
| 2790 | + | } |
|
| 2791 | + | ||
| 2755 | 2792 | #[test] |
|
| 2756 | 2793 | fn test_contains() { |
|
| 2757 | 2794 | expect(&["(contains? 2 '(1 2 3))"], Expr::Bool(true)); |
|
| 2758 | 2795 | expect(&["(contains? 4 '(1 2 3))"], Expr::Bool(false)); |
|
| 2759 | 2796 | expect(&["(contains? 2 [1 2 3])"], Expr::Bool(true)); |
| 2958 | 2995 | ); |
|
| 2959 | 2996 | } |
|
| 2960 | 2997 | ||
| 2961 | 2998 | #[test] |
|
| 2962 | 2999 | fn test_atom_dict_lookup() { |
|
| 2963 | - | expect(&["(def d (dict [:x 1 :y 2]))", "(:x d)"], Expr::Integer(1)); |
|
| 3000 | + | expect(&["(def d (dict [:x 1 :y 2]))", "(d :x)"], Expr::Integer(1)); |
|
| 2964 | 3001 | expect( |
|
| 2965 | - | &["(def d (dict [:a \"hello\" :b \"world\"]))", "(:b d)"], |
|
| 3002 | + | &["(def d (dict [:a \"hello\" :b \"world\"]))", "(d :b)"], |
|
| 2966 | 3003 | Expr::Str("world".to_owned()), |
|
| 2967 | 3004 | ); |
|
| 2968 | - | expect(&["(def d (dict [:x 1]))", "(:y d)"], Expr::Nil); |
|
| 3005 | + | expect(&["(def d (dict [:x 1]))", "(d :y)"], Expr::Nil); |
|
| 2969 | 3006 | } |
|
| 2970 | 3007 | ||
| 2971 | 3008 | #[test] |
|
| 2972 | 3009 | fn test_vectors() { |
|
| 2973 | 3010 | expect(&["(vec2 1 2)"], Expr::Vector2D(Vector2D::new(1.0, 2.0))); |
| 3046 | 3083 | ); |
|
| 3047 | 3084 | } |
|
| 3048 | 3085 | ||
| 3049 | 3086 | #[test] |
|
| 3050 | 3087 | fn test_vector_field_access() { |
|
| 3051 | - | expect(&["(:x (vec2 1 2))"], Expr::Float(1.0)); |
|
| 3052 | - | expect(&["(:y (vec2 1 2))"], Expr::Float(2.0)); |
|
| 3088 | + | expect(&["((vec2 1 2) :x)"], Expr::Float(1.0)); |
|
| 3089 | + | expect(&["((vec2 1 2) :y)"], Expr::Float(2.0)); |
|
| 3053 | 3090 | ||
| 3054 | - | expect(&["(:x (vec3 1 2 3))"], Expr::Float(1.0)); |
|
| 3055 | - | expect(&["(:y (vec3 1 2 3))"], Expr::Float(2.0)); |
|
| 3056 | - | expect(&["(:z (vec3 1 2 3))"], Expr::Float(3.0)); |
|
| 3091 | + | expect(&["((vec3 1 2 3) :x)"], Expr::Float(1.0)); |
|
| 3092 | + | expect(&["((vec3 1 2 3) :y)"], Expr::Float(2.0)); |
|
| 3093 | + | expect(&["((vec3 1 2 3) :z)"], Expr::Float(3.0)); |
|
| 3057 | 3094 | ||
| 3058 | - | expect(&["(:x (vec4 1 2 3 4))"], Expr::Float(1.0)); |
|
| 3059 | - | expect(&["(:y (vec4 1 2 3 4))"], Expr::Float(2.0)); |
|
| 3060 | - | expect(&["(:z (vec4 1 2 3 4))"], Expr::Float(3.0)); |
|
| 3061 | - | expect(&["(:w (vec4 1 2 3 4))"], Expr::Float(4.0)); |
|
| 3095 | + | expect(&["((vec4 1 2 3 4) :x)"], Expr::Float(1.0)); |
|
| 3096 | + | expect(&["((vec4 1 2 3 4) :y)"], Expr::Float(2.0)); |
|
| 3097 | + | expect(&["((vec4 1 2 3 4) :z)"], Expr::Float(3.0)); |
|
| 3098 | + | expect(&["((vec4 1 2 3 4) :w)"], Expr::Float(4.0)); |
|
| 3062 | 3099 | ||
| 3063 | - | expect(&["(:w (vec2 1 2))"], Expr::Nil); |
|
| 3064 | - | expect(&["(:w (vec3 1 2 3))"], Expr::Nil); |
|
| 3065 | - | expect(&["(:q (vec4 1 2 3 4))"], Expr::Nil); |
|
| 3100 | + | expect(&["((vec2 1 2) :w)"], Expr::Nil); |
|
| 3101 | + | expect(&["((vec3 1 2 3) :w)"], Expr::Nil); |
|
| 3102 | + | expect(&["((vec4 1 2 3 4) :q)"], Expr::Nil); |
|
| 3066 | 3103 | } |
|
| 3067 | 3104 | ||
| 3068 | 3105 | #[test] |
|
| 3069 | 3106 | #[ignore = "not implemented yet"] |
|
| 3070 | 3107 | fn test_modules_nested() { |
ombre/tests/basic.om
+21 -1
| 51 | 51 | (assert (eq? (pairs {:x 1 :y 2}) [[:x 1] [:y 2]])) |
|
| 52 | 52 | (assert (eq? (merge {:x 1 :y 2} {:y 3 :z 4}) {:x 1 :y 3 :z 4})) |
|
| 53 | 53 | (assert (eq? (merge {:a 1} {:b 2}) {:a 1 :b 2})) |
|
| 54 | 54 | (assert (eq? (merge {} {:x 1}) {:x 1})) |
|
| 55 | 55 | ||
| 56 | + | ;; Test dictionary as function |
|
| 57 | + | (do |
|
| 58 | + | (def d {:x 1 :y 2}) |
|
| 59 | + | (assert (eq? (d :x) 1)) |
|
| 60 | + | (assert (eq? (d :y) 2)) |
|
| 61 | + | (assert (eq? (d :z) nil))) |
|
| 62 | + | ||
| 56 | 63 | ;; Test dictionary mutation |
|
| 57 | 64 | (do |
|
| 58 | 65 | (def d {:x 1 :y 2 :z 3}) |
|
| 59 | 66 | (assert (eq? (get d :x) 1)) |
|
| 60 | 67 | (assert (eq? (get d :y) 2)) |
| 134 | 141 | ;; Test remainder operations |
|
| 135 | 142 | (assert (= 7 (% 20 13)) "rem 1") |
|
| 136 | 143 | (assert (= -7 (% -20 13)) "rem 2") |
|
| 137 | 144 | (assert (= 7 (% 20 -13)) "rem 3") |
|
| 138 | 145 | (assert (= -7 (% -20 -13)) "rem 4") |
|
| 139 | - | ; (assert (nan? (% 20 0)) "rem 5") |
|
| 140 | 146 | ||
| 141 | 147 | (assert (= 7 (mod 20 13)) "mod 1") |
|
| 142 | 148 | (assert (= 6 (mod -20 13)) "mod 2") |
|
| 143 | 149 | (assert (= -6 (mod 20 -13)) "mod 3") |
|
| 144 | 150 | (assert (= -7 (mod -20 -13)) "mod 4") |
| 166 | 172 | (assert (eq? (range 17 0 -4) [17 13 9 5 1]) "(range 17 0 -4)") |
|
| 167 | 173 | (assert (eq? (length (range 10)) 10) "(range 10)") |
|
| 168 | 174 | (assert (eq? (length (range -10)) 0) "(range -10)") |
|
| 169 | 175 | (assert (eq? (length (range 1 10)) 9) "(range 1 10)") |
|
| 170 | 176 | ||
| 177 | + | ;; Test vector access |
|
| 178 | + | (assert (eq? ((vec2 1 2) :x) 1.0)) |
|
| 179 | + | (assert (eq? ((vec2 1 2) :y) 2.0)) |
|
| 180 | + | (assert (eq? ((vec3 1 2 3) :x) 1.0)) |
|
| 181 | + | (assert (eq? ((vec3 1 2 3) :y) 2.0)) |
|
| 182 | + | (assert (eq? ((vec3 1 2 3) :z) 3.0)) |
|
| 183 | + | (assert (eq? ((vec4 1 2 3 4) :x) 1.0)) |
|
| 184 | + | (assert (eq? ((vec4 1 2 3 4) :y) 2.0)) |
|
| 185 | + | (assert (eq? ((vec4 1 2 3 4) :z) 3.0)) |
|
| 186 | + | (assert (eq? ((vec4 1 2 3 4) :w) 4.0)) |
|
| 187 | + | (assert (nil? ((vec2 1 2) :w))) |
|
| 188 | + | (assert (nil? ((vec3 1 2 3) :w))) |
|
| 189 | + | (assert (nil? ((vec4 1 2 3 4) :q))) |
|
| 190 | + | ||
| 171 | 191 | ; vim: filetype=clojure |