mirror of
https://github.com/urbit/ares.git
synced 2024-11-22 15:08:54 +03:00
[jets] add mod jet
This commit is contained in:
parent
025ce511f0
commit
8c1f2b668f
@ -33,6 +33,7 @@ pub fn get_jet(jet_name: Noun) -> Result<Jet, ()> {
|
||||
tas!(b"mul") => Ok(jet_mul),
|
||||
tas!(b"div") => Ok(jet_div),
|
||||
tas!(b"dvr") => Ok(jet_dvr),
|
||||
tas!(b"mod") => Ok(jet_mod),
|
||||
tas!(b"cut") => Ok(jet_cut),
|
||||
tas!(b"mug") => Ok(jet_mug),
|
||||
_ => {
|
||||
@ -46,6 +47,7 @@ pub fn get_jet_test_mode(jet_name: Noun) -> bool {
|
||||
match jet_name.as_direct().unwrap().data() {
|
||||
tas!(b"cut") => true,
|
||||
tas!(b"dvr") => true,
|
||||
tas!(b"mod") => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
@ -200,3 +200,25 @@ pub fn jet_dvr(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
|
||||
Ok(T(stack, &[div, rem]))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn jet_mod(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
|
||||
let arg = raw_slot(subject, 6);
|
||||
let a = raw_slot(arg, 2).as_atom()?;
|
||||
let b = raw_slot(arg, 3).as_atom()?;
|
||||
|
||||
if unsafe { b.as_noun().raw_equals(D(0)) } {
|
||||
Err(Deterministic)
|
||||
} else {
|
||||
match (a.as_direct(), b.as_direct()) {
|
||||
(Ok(a), Ok(b)) => {
|
||||
Ok(unsafe { DirectAtom::new_unchecked(a.data() % b.data()) }.as_noun())
|
||||
}
|
||||
(_, _) => {
|
||||
let a_int = a.as_ubig();
|
||||
let b_int = b.as_ubig();
|
||||
let res = a_int % b_int;
|
||||
Ok(Atom::from_ubig(stack, &res).as_noun())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user