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 2 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 order;
ashelkovnykov marked this conversation as resolved.
Show resolved Hide resolved
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::order::*;
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
23 changes: 23 additions & 0 deletions rust/ares/src/jets/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
ashelkovnykov marked this conversation as resolved.
Show resolved Hide resolved
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::*;
Expand Down
153 changes: 153 additions & 0 deletions rust/ares/src/jets/order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use crate::jets::util::slot;
ashelkovnykov marked this conversation as resolved.
Show resolved Hide resolved
/** Text processing jets
*/
ashelkovnykov marked this conversation as resolved.
Show resolved Hide resolved
use crate::jets;
use crate::jets::order::util::dor;
ashelkovnykov marked this conversation as resolved.
Show resolved Hide resolved
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 {
ashelkovnykov marked this conversation as resolved.
Show resolved Hide resolved
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_head = slot(a, 2)?;
let b_head = slot(b, 2)?;
let a_tail = slot(a, 3)?;
let b_tail = slot(b, 3)?;
if unsafe { a_head.raw_equals(b_head) } {
Ok(dor(stack, a_tail, b_tail)?)
} else {
Ok(dor(stack, a_head, b_head)?)
}
matthew-levan marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
}

#[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);
}
}