summaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2024-04-28 17:53:25 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2024-04-28 17:53:25 +0200
commit89e0405a5818339179cffba1b16ef09db31dea5e (patch)
tree7a0747f7f422cb0a08c4e34add4e6048893ee236 /crates
parentd9f17fc0025272dace18fcc6286dc3515cfbef43 (diff)
downloadrebel-89e0405a5818339179cffba1b16ef09db31dea5e.tar
rebel-89e0405a5818339179cffba1b16ef09db31dea5e.zip
rebel-lang: implement array subtraction
Diffstat (limited to 'crates')
-rw-r--r--crates/rebel-lang/src/typing.rs4
-rw-r--r--crates/rebel-lang/src/value.rs6
2 files changed, 10 insertions, 0 deletions
diff --git a/crates/rebel-lang/src/typing.rs b/crates/rebel-lang/src/typing.rs
index 5b62cd9..1455732 100644
--- a/crates/rebel-lang/src/typing.rs
+++ b/crates/rebel-lang/src/typing.rs
@@ -131,6 +131,10 @@ impl Type {
ArrayLen::add(l1, l2)?,
),
(Int, Sub, Int) => Int,
+ (Array(t1, _), Sub, Array(t2, _)) => {
+ (*t1).clone().unify(*t2, Coerce::Dynamic)?;
+ Array(t1, ArrayLen::Dynamic)
+ }
(Int, Mul, Int) => Int,
(Int, Div, Int) => Int,
(Int, Rem, Int) => Int,
diff --git a/crates/rebel-lang/src/value.rs b/crates/rebel-lang/src/value.rs
index 4c5dc25..1e5750e 100644
--- a/crates/rebel-lang/src/value.rs
+++ b/crates/rebel-lang/src/value.rs
@@ -99,6 +99,12 @@ impl Value {
(Integer(i1), Add, Integer(i2)) => Integer(i1.checked_add(i2).ok_or(EvalError)?),
(Array(elems1), Add, Array(elems2)) => Array([elems1, elems2].concat()),
(Integer(i1), Sub, Integer(i2)) => Integer(i1.checked_sub(i2).ok_or(EvalError)?),
+ (Array(elems1), Sub, Array(elems2)) => Array(
+ elems1
+ .into_iter()
+ .filter(|elem| !elems2.contains(elem))
+ .collect(),
+ ),
(Integer(i1), Mul, Integer(i2)) => Integer(i1.checked_mul(i2).ok_or(EvalError)?),
(Integer(i1), Div, Integer(i2)) => Integer(i1.checked_div(i2).ok_or(EvalError)?),
(Integer(i1), Rem, Integer(i2)) => Integer(i1.checked_rem(i2).ok_or(EvalError)?),