Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jets: add dor, mor, gor #106

Merged
merged 8 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rust/ares/src/jets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod form;
pub mod hash;
pub mod math;
pub mod nock;
pub mod sort;
pub mod text;
pub mod tree;

Expand All @@ -11,6 +12,7 @@ use crate::jets::form::*;
use crate::jets::hash::*;
use crate::jets::math::*;
use crate::jets::nock::*;
use crate::jets::sort::*;
use crate::jets::text::*;
use crate::jets::tree::*;
use crate::mem::NockStack;
Expand Down Expand Up @@ -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
Expand Down
43 changes: 27 additions & 16 deletions rust/ares/src/jets/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,29 +216,15 @@ pub fn jet_lte(
}

pub fn jet_lth(
stack: &mut NockStack,
_stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let a = slot(arg, 2)?.as_atom()?;
let b = slot(arg, 3)?.as_atom()?;

Ok(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
})
Ok(util::lth(a, b))
}

pub fn jet_mod(
Expand Down Expand Up @@ -303,6 +289,31 @@ pub fn jet_sub(
Ok(sub(stack, a, b)?.as_noun())
}

pub mod util {
use crate::jets::util::test::init_stack;
use crate::noun::{Atom, Noun, YES, NO};

pub fn lth(a: Atom, b: Atom) -> Noun {
let s = &mut init_stack();

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(s) < b.as_ubig(s) {
YES
} else {
NO
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
149 changes: 149 additions & 0 deletions rust/ares/src/jets/sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/** Sorting jets
*/
use crate::jets;
use crate::jets::util::slot;
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() {
Ok(util::dor(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() {
Ok(util::dor(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)?;

Ok(util::dor(a, b))
}

pub mod util {
use either::{Left, Right};
use crate::jets::util::slot;
use crate::jets::math::util::lth;
use crate::noun::{Noun, YES, NO};

pub fn dor(a: Noun, b: Noun) -> Noun {
if unsafe { a.raw_equals(b) } {
YES
} else {
match (a.as_either_atom_cell(), b.as_either_atom_cell()) {
(Left(atom_a), Left(atom_b)) => lth(atom_a, atom_b),
(Left(_), Right(_)) => YES,
(Right(_), Left(_)) => NO,
(Right(cell_a), Right(cell_b)) => {
let a_head = match slot(cell_a.as_noun(), 2) {
Ok(n) => n,
Err(_) => return NO,
};
let b_head = slot(cell_b.as_noun(), 2).unwrap();
let a_tail = slot(cell_a.as_noun(), 3).unwrap();
let b_tail = slot(cell_b.as_noun(), 3).unwrap();
if unsafe { a_head.raw_equals(b_head) } {
dor(a_tail, b_tail)
} else {
dor(a_head, b_head)
}
}
}
}
}
}

#[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);
}
}
2 changes: 1 addition & 1 deletion rust/ares/src/jets/text.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::jets::util::slot;
/** Text processing jets
*/
use crate::jets::Result;
use crate::jets::util::slot;
use crate::mem::NockStack;
use crate::newt::Newt;
use crate::noun::{Noun, D};
Expand Down