Skip to content

Commit

Permalink
Add build_list function
Browse files Browse the repository at this point in the history
  • Loading branch information
CeleritasCelery committed Oct 24, 2023
1 parent 9c6dd6e commit 6f535a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 15 additions & 3 deletions src/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ pub(crate) fn slice_into_list<'ob>(
from_end.fold(tail.into(), |acc, obj| cons!(*obj, acc; cx))
}

pub(crate) fn build_list<'ob, E>(
mut iter: impl Iterator<Item = Result<GcObj<'ob>, E>>,
cx: &'ob Context,
) -> Result<GcObj<'ob>, E> {
let Some(first) = iter.next() else { return Ok(nil()) };
let mut prev = cons!(first?; cx);
for elem in iter {
let new = cons!(elem?; cx);
prev.as_cons().set_cdr(new).unwrap();
prev = new;
}
Ok(prev)
}

#[defun]
pub(crate) fn eq(obj1: GcObj, obj2: GcObj) -> bool {
obj1.ptr_eq(obj2)
Expand Down Expand Up @@ -224,9 +238,7 @@ fn join<'ob>(list: &mut Vec<GcObj<'ob>>, seq: Gc<List<'ob>>) -> Result<()> {
#[defun]
fn take<'ob>(n: i64, list: Gc<List<'ob>>, cx: &'ob Context) -> Result<GcObj<'ob>> {
let Ok(n) = usize::try_from(n) else { return Ok(nil()) };
// TODO: remove this intermediate vector
let result: Vec<_> = list.elements().fallible().take(n).collect()?;
Ok(slice_into_list(&result, None, cx))
Ok(build_list(list.elements().take(n), cx)?)
}

#[defun]
Expand Down
8 changes: 5 additions & 3 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ impl Interpreter<'_> {
return Ok(form.bind(cx));
}
let env = {
// TODO: remove temp vector
let env: Vec<_> = self.vars.iter().map(|x| x.bind(cx).into()).collect();
crate::fns::slice_into_list(env.as_slice(), Some(cons!(true; cx)), cx)
let mut tail = cons!(true; cx);
for var in self.vars.iter().rev() {
tail = cons!(var.bind(cx), tail; cx);
}
tail
};
Self::replace_doc_symbol(cons, cx)?;
if let Some(closure_fn) = self.env.vars.get(sym::INTERNAL_MAKE_INTERPRETED_CLOSURE_FUNCTION)
Expand Down

0 comments on commit 6f535a5

Please sign in to comment.