diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 9699ff8..19bcb5a 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -31,6 +31,7 @@ pub fn get_jet(jet_name: Noun) -> Result { tas!(b"add") => Ok(jet_add), tas!(b"sub") => Ok(jet_sub), tas!(b"mul") => Ok(jet_mul), + tas!(b"div") => Ok(jet_div), tas!(b"cut") => Ok(jet_cut), tas!(b"mug") => Ok(jet_mug), _ => { diff --git a/rust/ares/src/jets_math.rs b/rust/ares/src/jets_math.rs index 6de1c53..8977ffa 100644 --- a/rust/ares/src/jets_math.rs +++ b/rust/ares/src/jets_math.rs @@ -15,7 +15,7 @@ use crate::interpreter::raw_slot; use crate::jets::{JetErr, JetErr::*}; use crate::mem::NockStack; -use crate::noun::{Atom, DirectAtom, IndirectAtom, Noun, DIRECT_MAX}; +use crate::noun::{Atom, DirectAtom, IndirectAtom, Noun, D, DIRECT_MAX}; use either::Either::*; pub fn jet_dec(stack: &mut NockStack, subject: Noun) -> Result { @@ -144,3 +144,25 @@ pub fn jet_mul(stack: &mut NockStack, subject: Noun) -> Result { } } } + +pub fn jet_div(stack: &mut NockStack, subject: Noun) -> Result { + 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()) + } + } + } +}