Skip to content

Commit

Permalink
Overhaul object merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Jan 9, 2024
1 parent 7c81ed6 commit f24da82
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions jaq-interpret/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ impl core::ops::Sub for Val {
}
}

fn obj_merge(l: &mut Rc<Map<Rc<String>, Val>>, r: Rc<Map<Rc<String>, Val>>) {
let l = Rc::make_mut(l);
let r = rc_unwrap_or_clone(r).into_iter();
r.for_each(|(k, v)| match (l.get_mut(&k), v) {
(Some(Val::Obj(l)), Val::Obj(r)) => obj_merge(l, r),
(Some(l), r) => *l = r,
(None, r) => {
l.insert(k, r);
}
})
}

impl core::ops::Mul for Val {
type Output = ValR;
fn mul(self, rhs: Self) -> Self::Output {
Expand All @@ -390,14 +402,7 @@ impl core::ops::Mul for Val {
(Num(n), r) => Self::from_dec_str(&n) * r,
(l, Num(n)) => l * Self::from_dec_str(&n),
(Obj(mut l), Obj(r)) => {
let inner = Rc::make_mut(&mut l);
for (k, v) in r.iter() {
let merged = match (inner.get(k), v) {
(Some(nl @ Obj(_)), nr @ Obj(_)) => nl.clone() * nr.clone(),
_ => Ok(v.clone()),
}?;
inner.insert(k.clone(), merged);
}
obj_merge(&mut l, r);
Ok(Obj(l))
}
(l, r) => Err(Error::MathOp(l, MathOp::Mul, r)),
Expand Down

0 comments on commit f24da82

Please sign in to comment.