-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from urbit/msl/ordering
jets: add `dor`, `mor`, `gor`
- Loading branch information
Showing
4 changed files
with
170 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |