[jets] add mod jet

This commit is contained in:
Philip Monk 2023-02-13 22:05:48 -07:00
parent 025ce511f0
commit 8c1f2b668f
2 changed files with 24 additions and 0 deletions

View File

@ -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,
}
}

View File

@ -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())
}
}
}
}