Skip to content

Commit

Permalink
Merge pull request #106 from urbit/msl/ordering
Browse files Browse the repository at this point in the history
jets: add `dor`, `mor`, `gor`
  • Loading branch information
ashelkovnykov authored Oct 20, 2023
2 parents 7be7c3b + 086822f commit 3f4bd26
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 16 deletions.
6 changes: 6 additions & 0 deletions rust/ares/src/jets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod form;
pub mod hash;
pub mod math;
pub mod nock;
pub mod sort;
pub mod text;
pub mod tree;

Expand All @@ -18,6 +19,7 @@ use crate::jets::hash::*;
use crate::jets::hot::Hot;
use crate::jets::math::*;
use crate::jets::nock::*;
use crate::jets::sort::*;
use crate::jets::text::*;
use crate::jets::tree::*;
use crate::jets::warm::Warm;
Expand Down Expand Up @@ -91,6 +93,10 @@ pub fn get_jet(jet_name: Noun) -> Option<Jet> {
//
tas!(b"mug") => Some(jet_mug),
//
tas!(b"dor") => Some(jet_dor),
tas!(b"gor") => Some(jet_gor),
tas!(b"mor") => Some(jet_mor),
//
tas!(b"scow") => Some(jet_scow),
//
tas!(b"mink") => Some(jet_mink),
Expand Down
4 changes: 4 additions & 0 deletions rust/ares/src/jets/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const HOT_STATE: &[(&[Either<u64, (u64, u64)>], u64, Jet)] = &[
//
(&[A_50, Left(tas!(b"mug"))], 1, jet_mug),
//
(&[A_50, Left(tas!(b"dor"))], 1, jet_dor),
(&[A_50, Left(tas!(b"gor"))], 1, jet_gor),
(&[A_50, Left(tas!(b"mor"))], 1, jet_mor),
//
(&[A_50, Left(tas!(b"scow"))], 1, jet_scow),
//
(&[A_50, Left(tas!(b"mink"))], 1, jet_mink),
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 @@ -190,26 +190,12 @@ pub fn jet_lte(context: &mut Context, subject: Noun) -> Result {
})
}

pub fn jet_lth(context: &mut Context, subject: Noun) -> Result {
pub fn jet_lth(_context: &mut Context, subject: Noun) -> 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(context.stack) < b.as_ubig(context.stack) {
YES
} else {
NO
})
Ok(util::lth(a, b))
}

pub fn jet_mod(context: &mut Context, subject: Noun) -> Result {
Expand Down Expand Up @@ -264,6 +250,31 @@ pub fn jet_sub(context: &mut Context, subject: Noun) -> Result {
Ok(sub(context.stack, a, b)?.as_noun())
}

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

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
133 changes: 133 additions & 0 deletions rust/ares/src/jets/sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/** Sorting jets
*/
use crate::interpreter::Context;
use crate::jets;
use crate::jets::util::slot;
use crate::mug::mug;
use crate::noun::{Noun, NO, YES};
use std::cmp::Ordering;

crate::gdb!();

pub fn jet_mor(context: &mut Context, subject: Noun) -> jets::Result {
let stack = &mut context.stack;

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());

match e.data().cmp(&f.data()) {
Ordering::Greater => Ok(NO),
Ordering::Less => Ok(YES),
Ordering::Equal => Ok(util::dor(a, b)),
}
}

pub fn jet_gor(context: &mut Context, subject: Noun) -> jets::Result {
let stack = &mut context.stack;

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);

match c.data().cmp(&d.data()) {
Ordering::Greater => Ok(NO),
Ordering::Less => Ok(YES),
Ordering::Equal => Ok(util::dor(a, b)),
}
}

pub fn jet_dor(_context: &mut Context, 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 crate::jets::math::util::lth;
use crate::jets::util::slot;
use crate::noun::{Noun, NO, YES};
use either::{Left, Right};

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 crate::jets::util::test::{assert_jet, init_stack, A};
use crate::noun::{D, T};
use ibig::ubig;

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

0 comments on commit 3f4bd26

Please sign in to comment.