Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vec-set #239

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/sort/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl VecSort {
"vec-contains".into(),
"vec-length".into(),
"vec-get".into(),
"vec-set".into(),
]
}

Expand Down Expand Up @@ -138,7 +139,12 @@ impl Sort for VecSort {
});
typeinfo.add_primitive(Get {
name: "vec-get".into(),
vec: self,
vec: self.clone(),
i64: typeinfo.get_sort(),
});
typeinfo.add_primitive(Set {
name: "vec-set".into(),
vec: self.clone(),
i64: typeinfo.get_sort(),
})
}
Expand Down Expand Up @@ -422,3 +428,35 @@ impl PrimitiveLike for Get {
vec.get(index as usize).copied()
}
}

struct Set {
name: Symbol,
vec: Arc<VecSort>,
i64: Arc<I64Sort>,
}

impl PrimitiveLike for Set {
fn name(&self) -> Symbol {
self.name
}

fn accept(&self, types: &[ArcSort]) -> Option<ArcSort> {
match types {
[vec, index, el]
if vec.name() == self.vec.name
&& index.name() == "i64".into()
&& el.name() == self.vec.element_name() =>
{
Some(self.vec.clone())
}
_ => None,
}
}

fn apply(&self, values: &[Value]) -> Option<Value> {
let mut vec = ValueVec::load(&self.vec, &values[0]);
let index = i64::load(&self.i64, &values[1]);
vec[index as usize] = values[2];
vec.store(&self.vec)
}
}
3 changes: 3 additions & 0 deletions tests/vec.egg
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@

; Test vec-get
(check (= (vec-get (vec-of 1 2 3) 1) 2))

; Test vec-set
(check (= (vec-set (vec-of 1 2 3) 1 4) (vec-of 1 4 3)))