Skip to content

Commit

Permalink
Merge remote-tracking branch 'cryshado/tuple-popn'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 27, 2023
2 parents 41ca91d + e0ff502 commit 8cbec4b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libs/src/Fift.fif
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ variable base
{ "" swap { 0 word 2dup $cmp } { rot swap $+ +cr swap } while 2drop } : scan-until-word
{ 0 word -trailing scan-until-word 1 'nop } ::_ $<<

{ count } : []len
{ tpop } : []pop

/* UNIMPLEMENTED
{ 0x40 runvmx } : runvmcode
{ 0x48 runvmx } : gasrunvmcode
Expand Down
12 changes: 12 additions & 0 deletions src/core/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ impl Stack {
Ok(())
}

pub fn extend_raw<T>(&mut self, items: T) -> Result<()>
where
T: IntoIterator,
T::Item: Into<Rc<dyn StackValue>>,
{
for item in items {
self.push_raw(item.into())?;
}

Ok(())
}

pub fn push_null(&mut self) -> Result<()> {
self.push_raw(Self::make_null())
}
Expand Down
16 changes: 16 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ impl FiftModule for BaseModule {
stack.push_raw(last)
}

#[cmd(name = "[]popn", stack)]
fn interpret_tuple_popn(stack: &mut Stack) -> Result<()> {
let n = stack.pop_usize()?;
let mut tuple = stack.pop_tuple()?;

let moved: Vec<_> = {
let tuple = Rc::make_mut(&mut tuple);
(0..n)
.map(|_| tuple.pop().context("Tuple underflow"))
.collect::<Result<_>>()?
};

stack.push_raw(tuple)?;
stack.extend_raw(moved.iter().rev().cloned())
}

#[cmd(name = "[]", stack)]
fn interpret_tuple_index(stack: &mut Stack) -> Result<()> {
let idx = stack.pop_usize()?;
Expand Down

0 comments on commit 8cbec4b

Please sign in to comment.