[jets] add div jet

This commit is contained in:
Philip Monk 2023-02-13 21:47:50 -07:00
parent f6e5b3898d
commit 099ec9243f
2 changed files with 24 additions and 1 deletions

View File

@ -31,6 +31,7 @@ pub fn get_jet(jet_name: Noun) -> Result<Jet, ()> {
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),
_ => {

View File

@ -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<Noun, JetErr> {
@ -144,3 +144,25 @@ pub fn jet_mul(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
}
}
}
pub fn jet_div(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())
}
}
}
}