jets: boolean helpers for comparator jets

This commit is contained in:
Alex Shelkovnykov 2023-12-19 16:44:51 -03:00
parent e1e79bbef4
commit 5bc00a175b
2 changed files with 63 additions and 35 deletions

View File

@ -15,7 +15,7 @@
use crate::interpreter::{Context, Error};
use crate::jets::util::*;
use crate::jets::{JetErr, Result};
use crate::noun::{Atom, DirectAtom, IndirectAtom, Noun, D, DIRECT_MAX, NO, T, YES};
use crate::noun::{Atom, DirectAtom, IndirectAtom, Noun, D, DIRECT_MAX, T};
use either::{Left, Right};
use ibig::ops::DivRem;
use ibig::UBig;
@ -120,7 +120,7 @@ pub fn jet_gte(context: &mut Context, subject: Noun) -> Result {
let a = slot(arg, 2)?.as_atom()?;
let b = slot(arg, 3)?.as_atom()?;
Ok(if util::gte(stack, a, b) { YES } else { NO })
Ok(util::gte(stack, a, b))
}
pub fn jet_gth(context: &mut Context, subject: Noun) -> Result {
@ -129,7 +129,7 @@ pub fn jet_gth(context: &mut Context, subject: Noun) -> Result {
let a = slot(arg, 2)?.as_atom()?;
let b = slot(arg, 3)?.as_atom()?;
Ok(if util::gth(stack, a, b) { YES } else { NO })
Ok(util::gth(stack, a, b))
}
pub fn jet_lte(context: &mut Context, subject: Noun) -> Result {
@ -138,7 +138,7 @@ pub fn jet_lte(context: &mut Context, subject: Noun) -> Result {
let a = slot(arg, 2)?.as_atom()?;
let b = slot(arg, 3)?.as_atom()?;
Ok(if util::lte(stack, a, b) { YES } else { NO })
Ok(util::lte(stack, a, b))
}
pub fn jet_lth(context: &mut Context, subject: Noun) -> Result {
@ -265,8 +265,30 @@ pub mod util {
}
}
/// Greater than
pub fn gth(stack: &mut NockStack, a: Atom, b: Atom) -> bool {
/// Greater than or equal to (boolean)
pub fn gte_b(stack: &mut NockStack, a: Atom, b: Atom) -> bool {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
a.data() >= b.data()
} else if a.bit_size() > b.bit_size() {
true
} else if a.bit_size() < b.bit_size() {
false
} else {
a.as_ubig(stack) >= b.as_ubig(stack)
}
}
/// Greater than or equal to
pub fn gte(stack: &mut NockStack, a: Atom, b: Atom) -> Noun {
if gte_b(stack, a, b) {
YES
} else {
NO
}
}
/// Greater than (boolean)
pub fn gth_b(stack: &mut NockStack, a: Atom, b: Atom) -> bool {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
a.data() > b.data()
} else if a.bit_size() > b.bit_size() {
@ -278,27 +300,17 @@ pub mod util {
}
}
/// Less than
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) {
/// Greater than
pub fn gth(stack: &mut NockStack, a: Atom, b: Atom) -> Noun {
if gth_b(stack, a, b) {
YES
} else {
NO
}
}
/// Less than or equal to
pub fn lte(stack: &mut NockStack, a: Atom, b: Atom) -> bool {
/// Less than or equal to (boolean)
pub fn lte_b(stack: &mut NockStack, a: Atom, b: Atom) -> bool {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
a.data() <= b.data()
} else if a.bit_size() < b.bit_size() {
@ -310,16 +322,34 @@ pub mod util {
}
}
/// Greater than or equal to
pub fn gte(stack: &mut NockStack, a: Atom, b: Atom) -> bool {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
a.data() >= b.data()
} else if a.bit_size() > b.bit_size() {
true
} else if a.bit_size() < b.bit_size() {
false
/// Less than or equal to
pub fn lte(stack: &mut NockStack, a: Atom, b: Atom) -> Noun {
if lte_b(stack, a, b) {
YES
} else {
a.as_ubig(stack) >= b.as_ubig(stack)
NO
}
}
/// Less than (boolean)
pub fn lth_b(stack: &mut NockStack, a: Atom, b: Atom) -> bool {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
a.data() < b.data()
} else if a.bit_size() > b.bit_size() {
false
} else if a.bit_size() < b.bit_size() {
true
} else {
a.as_ubig(stack) < b.as_ubig(stack)
}
}
/// Less than
pub fn lth(stack: &mut NockStack, a: Atom, b: Atom) -> Noun {
if lth_b(stack, a, b) {
YES
} else {
NO
}
}

View File

@ -1,15 +1,13 @@
/** Parsing jets
*/
use crate::interpreter::Context;
use crate::jets::math::util::{gte, lte};
use crate::jets::math::util::{gte_b, lte_b, lth};
use crate::jets::util::{kick, slam, slot};
use crate::jets::Result;
use crate::mem::NockStack;
use crate::noun::{Noun, D, T, YES};
use either::{Left, Right};
use super::math::util::lth;
crate::gdb!();
//
@ -423,8 +421,8 @@ pub fn jet_stew(context: &mut Context, subject: Noun) -> Result {
let iq_tub_atom = iq_tub.as_atom()?;
let hpn_hel_atom = hpn_hel.as_atom()?;
let tpn_hel_atom = tpn_hel.as_atom()?;
bit = gte(&mut context.stack, iq_tub_atom, hpn_hel_atom)
&& lte(&mut context.stack, iq_tub_atom, tpn_hel_atom);
bit = gte_b(&mut context.stack, iq_tub_atom, hpn_hel_atom)
&& lte_b(&mut context.stack, iq_tub_atom, tpn_hel_atom);
}
}