mirror of
https://github.com/urbit/ares.git
synced 2024-12-24 13:55:23 +03:00
[jets] add div jet
This commit is contained in:
parent
f6e5b3898d
commit
099ec9243f
@ -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),
|
||||
_ => {
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user