Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed Dec 31, 2024
1 parent fc24ba1 commit 89ec972
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 8 additions & 5 deletions r7rs/src/small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ impl<D: Device, F: FileSystem, P: ProcessContext, C: Clock> SmallPrimitiveSet<D,
.map(|cons| Number::new(cons.tag() as _))
.unwrap_or_default()
.into()
})
})?;

Ok(())
}
}

Expand All @@ -111,10 +113,11 @@ impl<D: Device, F: FileSystem, P: ProcessContext, C: Clock> PrimitiveSet
Type::Procedure as _,
)?;
}
Primitive::IS_RIB => memory
.operate_top::<Error>(|memory, value| memory.boolean(value.is_cons()).into())?,
Primitive::CAR => memory.operate_top::<Error>(Memory::car_value)?,
Primitive::CDR => memory.operate_top::<Error>(Memory::cdr_value)?,
Primitive::IS_RIB => {
memory.operate_top(|memory, value| memory.boolean(value.is_cons()).into())?
}
Primitive::CAR => memory.operate_top(Memory::car_value)?,
Primitive::CDR => memory.operate_top(Memory::cdr_value)?,
Primitive::TAG => Self::tag(memory, Memory::cdr_value)?,
Primitive::SET_CAR => Self::set_field(memory, Memory::set_car_value)?,
Primitive::SET_CDR => Self::set_field(memory, Memory::set_cdr_value)?,
Expand Down
17 changes: 12 additions & 5 deletions vm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,8 @@ impl<'a> Memory<'a> {
list
}

/// Operates a top value on a stack.
pub fn operate_top<E: From<Error>>(
&mut self,
operate: impl Fn(&Self, Value) -> Value,
) -> Result<(), E> {
/// Executes an operation against a value at the top of a stack.
pub fn operate_top(&mut self, operate: impl Fn(&Self, Value) -> Value) -> Result<(), Error> {
let value = self.pop();
self.push(operate(self, value))?;
Ok(())
Expand All @@ -380,6 +377,16 @@ impl<'a> Memory<'a> {
Ok(())
}

/// Executes an operation that returns `Option<Value>`.
pub fn operate_option(
&mut self,
mut operate: impl FnMut(&mut Memory<'a>) -> Option<Value>,
) -> Result<(), Error> {
let value = operate(self).unwrap_or_else(|| self.boolean(false).into());
self.push(value)?;
Ok(())
}

// Garbage collection

fn collect_garbages(&mut self, cons: Option<&mut Cons>) -> Result<(), Error> {
Expand Down

0 comments on commit 89ec972

Please sign in to comment.