jets: add dor, mor, gor

This commit is contained in:
Matthew LeVan 2023-10-05 16:25:31 -04:00
parent 57452b236c
commit ddeac1d774
3 changed files with 180 additions and 0 deletions

View File

@ -3,6 +3,7 @@ pub mod form;
pub mod hash;
pub mod math;
pub mod nock;
pub mod order;
pub mod text;
pub mod tree;
@ -11,6 +12,7 @@ use crate::jets::form::*;
use crate::jets::hash::*;
use crate::jets::math::*;
use crate::jets::nock::*;
use crate::jets::order::*;
use crate::jets::text::*;
use crate::jets::tree::*;
use crate::mem::NockStack;
@ -87,6 +89,10 @@ pub fn get_jet(jet_name: Noun) -> Option<Jet> {
tas!(b"scow") => Some(jet_scow),
//
tas!(b"mink") => Some(jet_mink),
//
tas!(b"dor") => Some(jet_dor),
tas!(b"gor") => Some(jet_gor),
tas!(b"mor") => Some(jet_mor),
_ => {
// eprintln!("Unknown jet: {:?}", jet_name);
None

View File

@ -303,6 +303,29 @@ pub fn jet_sub(
Ok(sub(stack, a, b)?.as_noun())
}
pub mod util {
use crate::noun::{Atom, Noun, YES, NO};
use crate::mem::NockStack;
pub fn lth(stack: &mut NockStack, a: Atom, b: Atom) -> Noun {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
if a.data() < b.data() {
YES
} else {
NO
}
} else if a.bit_size() < b.bit_size() {
YES
} else if a.bit_size() > b.bit_size() {
NO
} else if a.as_ubig(stack) < b.as_ubig(stack) {
YES
} else {
NO
}
}
}
#[cfg(test)]
mod tests {
use super::*;

151
rust/ares/src/jets/order.rs Normal file
View File

@ -0,0 +1,151 @@
use crate::jets::util::slot;
/** Text processing jets
*/
use crate::jets;
use crate::jets::order::util::dor;
use crate::mem::NockStack;
use crate::mug::mug;
use crate::newt::Newt;
use crate::noun::{Noun, NO, YES};
crate::gdb!();
pub fn jet_mor(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let sam = slot(subject, 6)?;
let a = slot(sam, 2)?;
let b = slot(sam, 3)?;
let c = mug(stack, a);
let d = mug(stack, b);
let e = mug(stack, c.as_noun());
let f = mug(stack, d.as_noun());
if e.data() == f.data() {
dor(stack, a, b)
} else {
if e.data() < f.data() {
Ok(YES)
} else {
Ok(NO)
}
}
}
pub fn jet_gor(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let sam = slot(subject, 6)?;
let a = slot(sam, 2)?;
let b = slot(sam, 3)?;
let c = mug(stack, a);
let d = mug(stack, b);
if c.data() == d.data() {
dor(stack, a, b)
} else {
if c.data() < d.data() {
Ok(YES)
} else {
Ok(NO)
}
}
}
pub fn jet_dor(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let sam = slot(subject, 6)?;
let a = slot(sam, 2)?;
let b = slot(sam, 3)?;
dor(stack, a, b)
}
pub mod util {
use crate::jets::util::slot;
use crate::jets::math::util::lth;
use crate::jets::Result;
use crate::mem::NockStack;
use crate::noun::{Noun, YES, NO};
pub fn dor(stack: &mut NockStack, a: Noun, b: Noun) -> Result {
if unsafe { a.raw_equals(b) } {
Ok(YES)
} else {
if a.is_atom() {
if b.is_atom() {
Ok(lth(stack, a.as_atom()?, b.as_atom()?))
} else {
Ok(YES)
}
} else {
if b.is_atom() {
Ok(NO)
} else {
let a = slot(a, 2).unwrap();
let b = slot(b, 2).unwrap();
if unsafe { a.raw_equals(b) } {
Ok(dor(stack, slot(a, 3)?, slot(b, 3)?)?)
} else {
Ok(dor(stack, slot(a, 2)?, slot(b, 2)?)?)
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use ibig::ubig;
use crate::jets::util::test::{A, assert_jet, init_stack};
use crate::noun::{D, T};
#[test]
fn test_dor() {
let s = &mut init_stack();
let sam = T(s, &[D(1), D(1)]);
assert_jet(s, jet_dor, sam, YES);
let a = A(s, &ubig!(_0x3fffffffffffffff));
let sam = T(s, &[a, D(1)]);
assert_jet(s, jet_dor, sam, NO);
let a = A(s, &ubig!(_0x3fffffffffffffff));
let sam = T(s, &[a, a]);
assert_jet(s, jet_dor, sam, YES);
}
#[test]
fn test_gor() {
let s = &mut init_stack();
let sam = T(s, &[D(1), D(1)]);
assert_jet(s, jet_gor, sam, YES);
let a = A(s, &ubig!(_0x3fffffffffffffff));
let sam = T(s, &[a, a]);
assert_jet(s, jet_gor, sam, YES);
}
#[test]
fn test_mor() {
let s = &mut init_stack();
let sam = T(s, &[D(1), D(1)]);
assert_jet(s, jet_mor, sam, YES);
let a = A(s, &ubig!(_0x3fffffffffffffff));
let sam = T(s, &[a, a]);
assert_jet(s, jet_mor, sam, YES);
}
}