From 45501cbb5a61c02e0e27e8c6ffd5a57139796cf0 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 20 Oct 2023 15:07:44 -0500 Subject: [PATCH 01/12] Post ++xeb jet. --- rust/ares/src/jets/bits.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index a12be17f..aaa1cc51 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -4,6 +4,7 @@ use crate::interpreter::Context; use crate::jets::util::*; use crate::jets::JetErr::*; use crate::jets::Result; +use crate::noun::DIRECT_MAX; use crate::noun::{DirectAtom, IndirectAtom, Noun, D}; use std::cmp; @@ -346,6 +347,17 @@ pub fn jet_mix(context: &mut Context, subject: Noun) -> Result { } } +pub fn jet_xeb(context: &mut Context, subject: Noun) -> Result { + let sam = slot(subject, 6)?; + let a = slot(sam, 1)?.as_atom()?; + + let syz = met(0, a) as u64; + + unsafe { + Ok(DirectAtom::new_unchecked(syz).as_atom().as_noun()) + } +} + #[cfg(test)] mod tests { use super::*; @@ -710,4 +722,17 @@ mod tests { let sam = T(s, &[a128, a0]); assert_jet(s, jet_mix, sam, a128); } + + #[test] + fn test_xeb() { + let s = &mut init_stack(); + assert_jet(s, jet_xeb, D(0), D(0)); + assert_jet(s, jet_xeb, D(1), D(1)); + assert_jet(s, jet_xeb, D(31), D(5)); + assert_jet(s, jet_xeb, D(32), D(6)); + assert_jet(s, jet_xeb, D(0xfff), D(12)); + assert_jet(s, jet_xeb, D(0xffff), D(16)); + assert_jet(s, jet_xeb, D(0x3fffffffffffffff), D(62)); + assert_jet(s, jet_xeb, D(0x4000000000000000), D(63)); + } } From 8d5eead3d5503486083e3deaaa8ef9cc1a8634ff Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 20 Oct 2023 15:46:32 -0500 Subject: [PATCH 02/12] Add ++flop. --- rust/ares/src/jets.rs | 21 +++++++++++++ rust/ares/src/jets/bits.rs | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index c5f90dab..f8812a20 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -21,6 +21,7 @@ use crate::jets::math::*; use crate::jets::nock::*; use crate::jets::sort::*; use crate::jets::text::*; +use crate::jets::text::util::*; use crate::jets::tree::*; use crate::jets::warm::Warm; use crate::mem::NockStack; @@ -286,6 +287,26 @@ pub mod util { Ok(list) } + /// Reverse order of list + pub fn flop(stack: &mut NockStack, noun: Noun) -> Result { + let mut list = noun; + let mut tsil = D(0); + loop { + if let Some(list) = list.atom() { + if list.as_bitslice().first_one().is_none() { + break; + } else { + return Err(JetErr::Deterministic); + } + } + let cell = list.as_cell()?; + tsil = Cell::new(stack, cell.head(), tsil).as_noun(); + list = cell.tail(); + } + + Ok(tsil) + } + /// Binary OR pub fn con(stack: &mut NockStack, a: Atom, b: Atom) -> Atom { let new_size = cmp::max(a.size(), b.size()); diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index aaa1cc51..a5e0b438 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -2,6 +2,7 @@ */ use crate::interpreter::Context; use crate::jets::util::*; +use crate::jets::text::util::lent; use crate::jets::JetErr::*; use crate::jets::Result; use crate::noun::DIRECT_MAX; @@ -358,6 +359,34 @@ pub fn jet_xeb(context: &mut Context, subject: Noun) -> Result { } } +pub fn jet_flop(context: &mut Context, subject: Noun) -> Result { + let sam = slot(subject, 6)?; + let src = slot(sam, 1)?; + + if unsafe { src.raw_equals(D(0)) } { + return Ok(D(0)); + } + + flop(context.stack, src) +} + +// return u3kc_rep(u3k(a), 1, u3kb_flop(u3qc_rip(a, 1, b))); +/*pub fn jet_swp(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()?; + + let new_size = cmp::max(a.size(), b.size()); + + unsafe { + let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(context.stack, new_size); + let a_bit = a.as_bitslice(); + dest[..a_bit.len()].copy_from_bitslice(a_bit); + *dest |= b.as_bitslice(); + Ok(atom.normalize_as_atom().as_noun()) + } +}*/ + #[cfg(test)] mod tests { use super::*; @@ -735,4 +764,36 @@ mod tests { assert_jet(s, jet_xeb, D(0x3fffffffffffffff), D(62)); assert_jet(s, jet_xeb, D(0x4000000000000000), D(63)); } + + #[test] + fn test_flop() { + let s = &mut init_stack(); + let sam = T(s, &[D(1), D(2), D(3), D(0)]); + let res = T(s, &[D(3), D(2), D(1), D(0)]); + assert_jet(s, jet_flop, sam, res); + + let sam = T( + s, + &[ + D(0xd), D(0xe), D(0xa), D(0xd), D(0xb), D(0xe), D(0xe), D(0xf), + D(0x1), D(0x2), D(0x3), D(0x4), D(0x5), D(0x6), D(0x7), D(0x8), + D(0xf), D(0xe), D(0xd), D(0xc), D(0xb), D(0xa), D(0x9), D(0x8), + D(0x7), D(0x6), D(0x5), D(0x4), D(0x3), D(0x2), D(0x1), D(0x0), + D(0x0), + ], + ); + #[rustfmt::skip] + let res = T( + s, + &[ + D(0x0), D(0x1), D(0x2), D(0x3), D(0x4), D(0x5), D(0x6), D(0x7), + D(0x8), D(0x9), D(0xa), D(0xb), D(0xc), D(0xd), D(0xe), D(0xf), + D(0x8), D(0x7), D(0x6), D(0x5), D(0x4), D(0x3), D(0x2), D(0x1), + D(0xf), D(0xe), D(0xe), D(0xb), D(0xd), D(0xa), D(0xe), D(0xd), + D(0x0), + ], + ); + assert_jet(s, jet_flop, sam, res); + } + } From 7ccce3b911dd461a9e5295c70dd55e83fb692cf9 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 20 Oct 2023 16:03:57 -0500 Subject: [PATCH 03/12] Push ++swp and test (WIP) --- rust/ares/src/jets/bits.rs | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index a5e0b438..c179608d 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -6,7 +6,7 @@ use crate::jets::text::util::lent; use crate::jets::JetErr::*; use crate::jets::Result; use crate::noun::DIRECT_MAX; -use crate::noun::{DirectAtom, IndirectAtom, Noun, D}; +use crate::noun::{DirectAtom, IndirectAtom, Noun, D, T}; use std::cmp; crate::gdb!(); @@ -370,22 +370,16 @@ pub fn jet_flop(context: &mut Context, subject: Noun) -> Result { flop(context.stack, src) } -// return u3kc_rep(u3k(a), 1, u3kb_flop(u3qc_rip(a, 1, b))); -/*pub fn jet_swp(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()?; - - let new_size = cmp::max(a.size(), b.size()); +pub fn jet_swp(context: &mut Context, subject: Noun) -> Result { + let sam = slot(subject, 6)?; + let bloq = slot(sam, 2)?.as_atom()?.as_direct()?.data() as usize; + let atom = slot(sam, 3)?.as_atom()?; - unsafe { - let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(context.stack, new_size); - let a_bit = a.as_bitslice(); - dest[..a_bit.len()].copy_from_bitslice(a_bit); - *dest |= b.as_bitslice(); - Ok(atom.normalize_as_atom().as_noun()) - } -}*/ + let ripper = rip(context.stack, bloq, 1, atom)?; + let flopper = flop(context.stack, ripper)?; + let sample = T(context.stack, &[D(1), flopper]); + jet_rep(context, sample) +} #[cfg(test)] mod tests { @@ -765,6 +759,17 @@ mod tests { assert_jet(s, jet_xeb, D(0x4000000000000000), D(63)); } + #[test] + fn test_swp() { + let s = &mut init_stack(); + let sam = T(s, &[D(0), D(0x18)]); + assert_jet(s, jet_swp, sam, D(0x3)); + let sam = T(s, &[D(0), D(0x80)]); + assert_jet(s, jet_swp, sam, D(0x1)); + let sam = T(s, &[D(3), D(0x636261)]); + assert_jet(s, jet_swp, sam, D(0x616263)); + } + #[test] fn test_flop() { let s = &mut init_stack(); From 758ea22692c1e492c37ec878b3f167fcae1d8e07 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 20 Oct 2023 16:17:23 -0500 Subject: [PATCH 04/12] WIP debugging jet_swp --- rust/ares/src/jets/bits.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index c179608d..ed25d2d1 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -376,8 +376,12 @@ pub fn jet_swp(context: &mut Context, subject: Noun) -> Result { let atom = slot(sam, 3)?.as_atom()?; let ripper = rip(context.stack, bloq, 1, atom)?; + println!("ripper: {:?}", ripper); let flopper = flop(context.stack, ripper)?; + println!("flopper: {:?}", flopper); let sample = T(context.stack, &[D(1), flopper]); + println!("sample: {:?}", sample); + println!("sample: {:?}", jet_rep(context, sample)); jet_rep(context, sample) } @@ -762,6 +766,8 @@ mod tests { #[test] fn test_swp() { let s = &mut init_stack(); + let sam = T(s, &[D(1), D(0x18)]); + assert_jet(s, jet_swp, sam, D(0x9)); let sam = T(s, &[D(0), D(0x18)]); assert_jet(s, jet_swp, sam, D(0x3)); let sam = T(s, &[D(0), D(0x80)]); From 8a5397b948d5e38db2fd730c2557c9cf475150df Mon Sep 17 00:00:00 2001 From: Sigilante Date: Fri, 20 Oct 2023 17:59:19 -0500 Subject: [PATCH 05/12] Post w/o swp (not really jetted efficiently in Vere either). --- rust/ares/src/jets/bits.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index ed25d2d1..417beac6 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -370,20 +370,24 @@ pub fn jet_flop(context: &mut Context, subject: Noun) -> Result { flop(context.stack, src) } -pub fn jet_swp(context: &mut Context, subject: Noun) -> Result { +// return u3kc_rep(u3k(a), 1, u3kb_flop(u3qc_rip(a, 1, b))); +// TODO +// allocate new IndirectAtom of same size and as_bitslice() +// so we can rebuild it back to front in bloq size +/*pub fn jet_swp(context: &mut Context, subject: Noun) -> Result { let sam = slot(subject, 6)?; let bloq = slot(sam, 2)?.as_atom()?.as_direct()?.data() as usize; let atom = slot(sam, 3)?.as_atom()?; let ripper = rip(context.stack, bloq, 1, atom)?; - println!("ripper: {:?}", ripper); + //println!("ripper: {:?}", ripper); let flopper = flop(context.stack, ripper)?; - println!("flopper: {:?}", flopper); + //println!("flopper: {:?}", flopper); let sample = T(context.stack, &[D(1), flopper]); - println!("sample: {:?}", sample); - println!("sample: {:?}", jet_rep(context, sample)); + //println!("sample: {:?}", sample); + //println!("sample: {:?}", jet_rep(context, sample)); jet_rep(context, sample) -} +}*/ #[cfg(test)] mod tests { @@ -763,7 +767,7 @@ mod tests { assert_jet(s, jet_xeb, D(0x4000000000000000), D(63)); } - #[test] + /*#[test] fn test_swp() { let s = &mut init_stack(); let sam = T(s, &[D(1), D(0x18)]); @@ -774,7 +778,7 @@ mod tests { assert_jet(s, jet_swp, sam, D(0x1)); let sam = T(s, &[D(3), D(0x636261)]); assert_jet(s, jet_swp, sam, D(0x616263)); - } + }*/ #[test] fn test_flop() { From b31c43728be1190918be28684020618a7539bd25 Mon Sep 17 00:00:00 2001 From: Sigilante Date: Mon, 23 Oct 2023 14:38:09 -0500 Subject: [PATCH 06/12] Register --- rust/ares/src/jets.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index f8812a20..31989451 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -87,6 +87,8 @@ pub fn get_jet(jet_name: Noun) -> Option { tas!(b"rev") => Some(jet_rev), tas!(b"rip") => Some(jet_rip), tas!(b"rsh") => Some(jet_rsh), + tas!(b"flop") => Some(jet_flop), + tas!(b"xeb") => Some(jet_xeb), // tas!(b"con") => Some(jet_con), tas!(b"dis") => Some(jet_dis), From b1a63e52ccc37d4e8da348c36dc5f21f8fd7181e Mon Sep 17 00:00:00 2001 From: Sigilante Date: Mon, 23 Oct 2023 14:41:02 -0500 Subject: [PATCH 07/12] Lint --- rust/ares/src/jets.rs | 2 +- rust/ares/src/jets/bits.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 31989451..366f5727 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -21,7 +21,7 @@ use crate::jets::math::*; use crate::jets::nock::*; use crate::jets::sort::*; use crate::jets::text::*; -use crate::jets::text::util::*; + use crate::jets::tree::*; use crate::jets::warm::Warm; use crate::mem::NockStack; diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index 417beac6..da6b4d07 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -2,11 +2,11 @@ */ use crate::interpreter::Context; use crate::jets::util::*; -use crate::jets::text::util::lent; + use crate::jets::JetErr::*; use crate::jets::Result; -use crate::noun::DIRECT_MAX; -use crate::noun::{DirectAtom, IndirectAtom, Noun, D, T}; + +use crate::noun::{DirectAtom, IndirectAtom, Noun, D}; use std::cmp; crate::gdb!(); @@ -348,7 +348,7 @@ pub fn jet_mix(context: &mut Context, subject: Noun) -> Result { } } -pub fn jet_xeb(context: &mut Context, subject: Noun) -> Result { +pub fn jet_xeb(_context: &mut Context, subject: Noun) -> Result { let sam = slot(subject, 6)?; let a = slot(sam, 1)?.as_atom()?; From 151a00961609242d754b14a7964466063d2e60d3 Mon Sep 17 00:00:00 2001 From: Alex Shelkovnykov Date: Tue, 24 Oct 2023 09:26:58 -0600 Subject: [PATCH 08/12] Group list logic jets --- rust/ares/src/jets.rs | 32 +++------- rust/ares/src/jets/bits.rs | 44 ------------- rust/ares/src/jets/list.rs | 128 +++++++++++++++++++++++++++++++++++++ rust/ares/src/jets/text.rs | 61 ------------------ rust/ares/src/serf.rs | 2 +- 5 files changed, 138 insertions(+), 129 deletions(-) create mode 100644 rust/ares/src/jets/list.rs delete mode 100644 rust/ares/src/jets/text.rs diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 2e77d078..20fddb3f 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -5,10 +5,10 @@ pub mod warm; pub mod bits; pub mod form; pub mod hash; +pub mod list; pub mod math; pub mod nock; pub mod sort; -pub mod text; pub mod tree; use crate::interpreter::{Context, Error}; @@ -17,10 +17,10 @@ use crate::jets::cold::Cold; use crate::jets::form::*; use crate::jets::hash::*; use crate::jets::hot::Hot; +use crate::jets::list::*; 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; @@ -46,6 +46,12 @@ pub enum JetErr { Fail(Error), // Error; do not retry } +impl From for JetErr { + fn from(err: Error) -> Self { + Self::Fail(err) + } +} + impl From for JetErr { fn from(_err: noun::Error) -> Self { Self::Fail(Error::Deterministic(D(0))) @@ -82,6 +88,7 @@ pub fn get_jet(jet_name: Noun) -> Option { tas!(b"cap") => Some(jet_cap), tas!(b"mas") => Some(jet_mas), // + tas!(b"flop") => Some(jet_flop), tas!(b"lent") => Some(jet_lent), // tas!(b"bex") => Some(jet_bex), @@ -96,7 +103,6 @@ pub fn get_jet(jet_name: Noun) -> Option { tas!(b"rev") => Some(jet_rev), tas!(b"rip") => Some(jet_rip), tas!(b"rsh") => Some(jet_rsh), - tas!(b"flop") => Some(jet_flop), tas!(b"xeb") => Some(jet_xeb), // tas!(b"con") => Some(jet_con), @@ -300,26 +306,6 @@ pub mod util { Ok(list) } - /// Reverse order of list - pub fn flop(stack: &mut NockStack, noun: Noun) -> Result { - let mut list = noun; - let mut tsil = D(0); - loop { - if let Some(list) = list.atom() { - if list.as_bitslice().first_one().is_none() { - break; - } else { - return Err(JetErr::Fail(Error::Deterministic(D(0)))); - } - } - let cell = list.as_cell()?; - tsil = Cell::new(stack, cell.head(), tsil).as_noun(); - list = cell.tail(); - } - - Ok(tsil) - } - /// Binary OR pub fn con(stack: &mut NockStack, a: Atom, b: Atom) -> Atom { let new_size = cmp::max(a.size(), b.size()); diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index 0910f322..eb6ec206 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -356,17 +356,6 @@ pub fn jet_xeb(_context: &mut Context, subject: Noun) -> Result { unsafe { Ok(DirectAtom::new_unchecked(syz).as_atom().as_noun()) } } -pub fn jet_flop(context: &mut Context, subject: Noun) -> Result { - let sam = slot(subject, 6)?; - let src = slot(sam, 1)?; - - if unsafe { src.raw_equals(D(0)) } { - return Ok(D(0)); - } - - flop(&mut context.stack, src) -} - // return u3kc_rep(u3k(a), 1, u3kb_flop(u3qc_rip(a, 1, b))); // TODO // allocate new IndirectAtom of same size and as_bitslice() @@ -806,37 +795,4 @@ mod tests { let sam = T(s, &[D(3), D(0x636261)]); assert_jet(s, jet_swp, sam, D(0x616263)); }*/ - - #[test] - fn test_flop() { - let c = &mut init_context(); - - let sam = T(&mut c.stack, &[D(1), D(2), D(3), D(0)]); - let res = T(&mut c.stack, &[D(3), D(2), D(1), D(0)]); - assert_jet(c, jet_flop, sam, res); - - #[rustfmt::skip] - let sam = T( - &mut c.stack, - &[ - D(0xd), D(0xe), D(0xa), D(0xd), D(0xb), D(0xe), D(0xe), D(0xf), - D(0x1), D(0x2), D(0x3), D(0x4), D(0x5), D(0x6), D(0x7), D(0x8), - D(0xf), D(0xe), D(0xd), D(0xc), D(0xb), D(0xa), D(0x9), D(0x8), - D(0x7), D(0x6), D(0x5), D(0x4), D(0x3), D(0x2), D(0x1), D(0x0), - D(0x0), - ], - ); - #[rustfmt::skip] - let res = T( - &mut c.stack, - &[ - D(0x0), D(0x1), D(0x2), D(0x3), D(0x4), D(0x5), D(0x6), D(0x7), - D(0x8), D(0x9), D(0xa), D(0xb), D(0xc), D(0xd), D(0xe), D(0xf), - D(0x8), D(0x7), D(0x6), D(0x5), D(0x4), D(0x3), D(0x2), D(0x1), - D(0xf), D(0xe), D(0xe), D(0xb), D(0xd), D(0xa), D(0xe), D(0xd), - D(0x0), - ], - ); - assert_jet(c, jet_flop, sam, res); - } } diff --git a/rust/ares/src/jets/list.rs b/rust/ares/src/jets/list.rs new file mode 100644 index 00000000..0cc00b77 --- /dev/null +++ b/rust/ares/src/jets/list.rs @@ -0,0 +1,128 @@ +/** Text processing jets + */ +use crate::interpreter::Context; +use crate::jets::util::slot; +use crate::jets::Result; +use crate::noun::{Noun, D}; + +crate::gdb!(); + +pub fn jet_flop(context: &mut Context, subject: Noun) -> Result { + let sam = slot(subject, 6)?; + let src = slot(sam, 1)?; + + if unsafe { src.raw_equals(D(0)) } { + return Ok(D(0)); + } + + // util::flop(&mut context.stack, src).map_err(|x| x.into()) + Ok(util::flop(&mut context.stack, src)?) +} + +pub fn jet_lent(_context: &mut Context, subject: Noun) -> Result { + let tape = slot(subject, 6)?; + util::lent(tape).map(|x| D(x as u64)) +} + +pub mod util { + use crate::interpreter::Error; + use crate::jets::JetErr; + use crate::mem::NockStack; + use crate::noun::{Cell, Noun, D}; + use std::result::Result; + + /// Reverse order of list + pub fn flop(stack: &mut NockStack, noun: Noun) -> Result { + let mut list = noun; + let mut tsil = D(0); + loop { + if let Some(list) = list.atom() { + if list.as_bitslice().first_one().is_none() { + break; + } else { + return Err(Error::Deterministic(D(0))); + } + } + let cell = list.as_cell()?; + tsil = Cell::new(stack, cell.head(), tsil).as_noun(); + list = cell.tail(); + } + + Ok(tsil) + } + + pub fn lent(tape: Noun) -> Result { + let mut len = 0usize; + let mut list = tape; + loop { + if let Some(atom) = list.atom() { + if atom.as_bitslice().first_one().is_none() { + break; + } else { + return Err(JetErr::Fail(Error::Deterministic(D(0)))); + } + } + let cell = list.as_cell()?; + // don't need checked_add or indirect atom result: 2^63-1 atoms would be 64 ebibytes + len += 1; + list = cell.tail(); + } + Ok(len) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::interpreter::Error; + use crate::jets::util::test::{assert_jet, assert_jet_err, init_context}; + use crate::jets::JetErr; + use crate::noun::{D, T}; + + #[test] + fn test_flop() { + let c = &mut init_context(); + + let sam = T(&mut c.stack, &[D(1), D(2), D(3), D(0)]); + let res = T(&mut c.stack, &[D(3), D(2), D(1), D(0)]); + assert_jet(c, jet_flop, sam, res); + + #[rustfmt::skip] + let sam = T( + &mut c.stack, + &[ + D(0xd), D(0xe), D(0xa), D(0xd), D(0xb), D(0xe), D(0xe), D(0xf), + D(0x1), D(0x2), D(0x3), D(0x4), D(0x5), D(0x6), D(0x7), D(0x8), + D(0xf), D(0xe), D(0xd), D(0xc), D(0xb), D(0xa), D(0x9), D(0x8), + D(0x7), D(0x6), D(0x5), D(0x4), D(0x3), D(0x2), D(0x1), D(0x0), + D(0x0), + ], + ); + #[rustfmt::skip] + let res = T( + &mut c.stack, + &[ + D(0x0), D(0x1), D(0x2), D(0x3), D(0x4), D(0x5), D(0x6), D(0x7), + D(0x8), D(0x9), D(0xa), D(0xb), D(0xc), D(0xd), D(0xe), D(0xf), + D(0x8), D(0x7), D(0x6), D(0x5), D(0x4), D(0x3), D(0x2), D(0x1), + D(0xf), D(0xe), D(0xe), D(0xb), D(0xd), D(0xa), D(0xe), D(0xd), + D(0x0), + ], + ); + assert_jet(c, jet_flop, sam, res); + } + + #[test] + fn test_lent() { + let c = &mut init_context(); + + assert_jet(c, jet_lent, D(0), D(0)); + let sam = T(&mut c.stack, &[D(1), D(2), D(3), D(0)]); + assert_jet(c, jet_lent, sam, D(3)); + let sam = T(&mut c.stack, &[D(3), D(2), D(1), D(0)]); + assert_jet(c, jet_lent, sam, D(3)); + assert_jet_err(c, jet_lent, D(1), JetErr::Fail(Error::Deterministic(D(0)))); + let sam = T(&mut c.stack, &[D(3), D(2), D(1)]); + assert_jet_err(c, jet_lent, sam, JetErr::Fail(Error::Deterministic(D(0)))); + } +} diff --git a/rust/ares/src/jets/text.rs b/rust/ares/src/jets/text.rs deleted file mode 100644 index 421f46d6..00000000 --- a/rust/ares/src/jets/text.rs +++ /dev/null @@ -1,61 +0,0 @@ -/** Text processing jets - */ -use crate::interpreter::Context; -use crate::jets::util::slot; -use crate::jets::Result; -use crate::noun::{Noun, D}; - -crate::gdb!(); - -pub fn jet_lent(_context: &mut Context, subject: Noun) -> Result { - let tape = slot(subject, 6)?; - util::lent(tape).map(|x| D(x as u64)) -} - -pub mod util { - use crate::interpreter::Error; - use crate::jets::JetErr; - use crate::noun::{Noun, D}; - - pub fn lent(tape: Noun) -> Result { - let mut len = 0usize; - let mut list = tape; - loop { - if let Some(atom) = list.atom() { - if atom.as_bitslice().first_one().is_none() { - break; - } else { - return Err(JetErr::Fail(Error::Deterministic(D(0)))); - } - } - let cell = list.as_cell()?; - // don't need checked_add or indirect atom result: 2^63-1 atoms would be 64 ebibytes - len += 1; - list = cell.tail(); - } - Ok(len) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::interpreter::Error; - use crate::jets::util::test::{assert_jet, assert_jet_err, init_context}; - use crate::jets::JetErr; - use crate::noun::{D, T}; - - #[test] - fn test_lent() { - let c = &mut init_context(); - - assert_jet(c, jet_lent, D(0), D(0)); - let sam = T(&mut c.stack, &[D(1), D(2), D(3), D(0)]); - assert_jet(c, jet_lent, sam, D(3)); - let sam = T(&mut c.stack, &[D(3), D(2), D(1), D(0)]); - assert_jet(c, jet_lent, sam, D(3)); - assert_jet_err(c, jet_lent, D(1), JetErr::Fail(Error::Deterministic(D(0)))); - let sam = T(&mut c.stack, &[D(3), D(2), D(1)]); - assert_jet_err(c, jet_lent, sam, JetErr::Fail(Error::Deterministic(D(0)))); - } -} diff --git a/rust/ares/src/serf.rs b/rust/ares/src/serf.rs index 52b180dc..1e167bbe 100644 --- a/rust/ares/src/serf.rs +++ b/rust/ares/src/serf.rs @@ -3,8 +3,8 @@ use crate::interpreter; use crate::interpreter::{inc, interpret, Error}; use crate::jets::cold::Cold; use crate::jets::hot::Hot; +use crate::jets::list::util::lent; use crate::jets::nock::util::mook; -use crate::jets::text::util::lent; use crate::jets::warm::Warm; use crate::mem::NockStack; use crate::mug::mug_u32; From 0228c4e300f29f31b3f6a42c4b51be25db0db4ac Mon Sep 17 00:00:00 2001 From: Alex Shelkovnykov Date: Tue, 24 Oct 2023 09:55:49 -0600 Subject: [PATCH 09/12] Co-locate helper functions w/ jets --- rust/ares/src/jets.rs | 121 +---------------------------------- rust/ares/src/jets/bits.rs | 126 +++++++++++++++++++++++++++++++++---- rust/ares/src/jets/math.rs | 31 ++++++++- rust/ares/src/jets/nock.rs | 2 +- rust/ares/src/jets/tree.rs | 2 + 5 files changed, 147 insertions(+), 135 deletions(-) diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 20fddb3f..4bb74e71 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -28,7 +28,6 @@ use crate::mem::NockStack; use crate::newt::Newt; use crate::noun::{self, Noun, Slots, D}; use ares_macros::tas; -use std::cmp; crate::gdb!(); @@ -137,10 +136,8 @@ pub fn get_jet_test_mode(_jet_name: Noun) -> bool { pub mod util { use super::*; - use crate::noun::Error::NotRepresentable; - use crate::noun::{Atom, Cell, DirectAtom, IndirectAtom, Noun, D}; + use crate::noun::{Noun, D}; use bitvec::prelude::{BitSlice, Lsb0}; - use ibig::UBig; use std::result; /** @@ -239,86 +236,6 @@ pub mod util { Ok(()) } - /// Subtraction - pub fn sub(stack: &mut NockStack, a: Atom, b: Atom) -> noun::Result { - if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) { - let a_small = a.data(); - let b_small = b.data(); - - if a_small < b_small { - Err(NotRepresentable) - } else { - Ok(Atom::new(stack, a_small - b_small)) - } - } else { - let a_big = a.as_ubig(stack); - let b_big = b.as_ubig(stack); - - if a_big < b_big { - Err(NotRepresentable) - } else { - let a_big = a.as_ubig(stack); - let b_big = b.as_ubig(stack); - let res = UBig::sub_stack(stack, a_big, b_big); - Ok(Atom::from_ubig(stack, &res)) - } - } - } - - /// Binary exponent - pub fn bex(stack: &mut NockStack, arg: usize) -> Atom { - unsafe { - if arg < 63 { - DirectAtom::new_unchecked(1 << arg).as_atom() - } else { - let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(stack, (arg + 7) >> 3); - dest.set(arg, true); - atom.normalize_as_atom() - } - } - } - - /// Measure the number of bloqs in an atom - pub fn met(bloq: usize, a: Atom) -> usize { - if unsafe { a.as_noun().raw_equals(D(0)) } { - 0 - } else if bloq < 6 { - (a.bit_size() + ((1 << bloq) - 1)) >> bloq - } else { - let bloq_word = bloq - 6; - (a.size() + ((1 << bloq_word) - 1)) >> bloq_word - } - } - - pub fn rip(stack: &mut NockStack, bloq: usize, step: usize, atom: Atom) -> Result { - let len = (met(bloq, atom) + step - 1) / step; - let mut list = D(0); - for i in (0..len).rev() { - let new_atom = unsafe { - let (mut new_indirect, new_slice) = - IndirectAtom::new_raw_mut_bitslice(stack, step << bloq); - chop(bloq, i * step, step, 0, new_slice, atom.as_bitslice())?; - new_indirect.normalize_as_atom() - }; - list = Cell::new(stack, new_atom.as_noun(), list).as_noun(); - } - - Ok(list) - } - - /// Binary OR - pub fn con(stack: &mut NockStack, a: Atom, b: Atom) -> Atom { - let new_size = cmp::max(a.size(), b.size()); - - unsafe { - let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(stack, new_size); - let a_bit = a.as_bitslice(); - dest[..a_bit.len()].copy_from_bitslice(a_bit); - *dest |= b.as_bitslice(); - atom.normalize_as_atom() - } - } - pub mod test { use super::*; use crate::hamt::Hamt; @@ -390,40 +307,4 @@ pub mod util { ); } } - - #[cfg(test)] - mod tests { - use super::test::{init_context, A}; - use super::*; - use ibig::ubig; - - #[test] - fn test_met() { - let c = &mut init_context(); - let s = &mut c.stack; - - let a = A(s, &ubig!(0xdeadbeef12345678fedcba9876543210)) - .as_atom() - .unwrap(); - assert_eq!(met(0, a), 128); - assert_eq!(met(1, a), 64); - assert_eq!(met(2, a), 32); - assert_eq!(met(3, a), 16); - assert_eq!(met(4, a), 8); - assert_eq!(met(5, a), 4); - assert_eq!(met(6, a), 2); - assert_eq!(met(7, a), 1); - assert_eq!(met(8, a), 1); - - let a = D(0x7fffffffffffffff).as_atom().unwrap(); - assert_eq!(met(0, a), 63); - assert_eq!(met(1, a), 32); - assert_eq!(met(2, a), 16); - assert_eq!(met(3, a), 8); - assert_eq!(met(4, a), 4); - assert_eq!(met(5, a), 2); - assert_eq!(met(6, a), 1); - assert_eq!(met(7, a), 1); - } - } } diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index eb6ec206..b9d392b0 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -14,7 +14,7 @@ crate::gdb!(); pub fn jet_bex(context: &mut Context, subject: Noun) -> Result { let arg = slot(subject, 6)?.as_direct()?.data() as usize; - Ok(bex(&mut context.stack, arg).as_noun()) + Ok(util::bex(&mut context.stack, arg).as_noun()) } pub fn jet_can(context: &mut Context, subject: Noun) -> Result { @@ -70,8 +70,8 @@ pub fn jet_cat(context: &mut Context, subject: Noun) -> Result { let a = slot(arg, 6)?.as_atom()?; let b = slot(arg, 7)?.as_atom()?; - let len_a = met(bloq, a); - let len_b = met(bloq, b); + let len_a = util::met(bloq, a); + let len_b = util::met(bloq, b); let new_len = bite_to_word(bloq, checked_add(len_a, len_b)?)?; if new_len == 0 { Ok(a.as_noun()) @@ -113,7 +113,7 @@ pub fn jet_end(context: &mut Context, subject: Noun) -> Result { if step == 0 { Ok(D(0)) - } else if step >= met(bloq, a) { + } else if step >= util::met(bloq, a) { Ok(a.as_noun()) } else { unsafe { @@ -130,7 +130,7 @@ pub fn jet_lsh(context: &mut Context, subject: Noun) -> Result { let (bloq, step) = bite(slot(arg, 2)?)?; let a = slot(arg, 3)?.as_atom()?; - let len = met(bloq, a); + let len = util::met(bloq, a); if len == 0 { return Ok(D(0)); } @@ -148,7 +148,7 @@ pub fn jet_met(_context: &mut Context, subject: Noun) -> Result { let bloq = bloq(slot(arg, 2)?)?; let a = slot(arg, 3)?.as_atom()?; - Ok(D(met(bloq, a) as u64)) + Ok(D(util::met(bloq, a) as u64)) } pub fn jet_rap(context: &mut Context, subject: Noun) -> Result { @@ -165,7 +165,7 @@ pub fn jet_rap(context: &mut Context, subject: Noun) -> Result { let cell = list.as_cell()?; - len = checked_add(len, met(bloq, cell.head().as_atom()?))?; + len = checked_add(len, util::met(bloq, cell.head().as_atom()?))?; list = cell.tail(); } @@ -185,7 +185,7 @@ pub fn jet_rap(context: &mut Context, subject: Noun) -> Result { let cell = list.as_cell()?; let atom = cell.head().as_atom()?; - let step = met(bloq, atom); + let step = util::met(bloq, atom); chop(bloq, 0, step, pos, new_slice, atom.as_bitslice())?; pos += step; @@ -282,7 +282,7 @@ pub fn jet_rip(context: &mut Context, subject: Noun) -> Result { let arg = slot(subject, 6)?; let (bloq, step) = bite(slot(arg, 2)?)?; let atom = slot(arg, 3)?.as_atom()?; - rip(&mut context.stack, bloq, step, atom) + util::rip(&mut context.stack, bloq, step, atom) } pub fn jet_rsh(context: &mut Context, subject: Noun) -> Result { @@ -290,7 +290,7 @@ pub fn jet_rsh(context: &mut Context, subject: Noun) -> Result { let (bloq, step) = bite(slot(arg, 2)?)?; let a = slot(arg, 3)?.as_atom()?; - let len = met(bloq, a); + let len = util::met(bloq, a); if step >= len { return Ok(D(0)); } @@ -312,7 +312,7 @@ pub fn jet_con(context: &mut Context, subject: Noun) -> Result { let a = slot(arg, 2)?.as_atom()?; let b = slot(arg, 3)?.as_atom()?; - Ok(con(&mut context.stack, a, b).as_noun()) + Ok(util::con(&mut context.stack, a, b).as_noun()) } pub fn jet_dis(context: &mut Context, subject: Noun) -> Result { @@ -351,7 +351,7 @@ pub fn jet_xeb(_context: &mut Context, subject: Noun) -> Result { let sam = slot(subject, 6)?; let a = slot(sam, 1)?.as_atom()?; - let syz = met(0, a) as u64; + let syz = util::met(0, a) as u64; unsafe { Ok(DirectAtom::new_unchecked(syz).as_atom().as_noun()) } } @@ -375,6 +375,108 @@ pub fn jet_xeb(_context: &mut Context, subject: Noun) -> Result { jet_rep(context, sample) }*/ +pub mod util { + use crate::jets::util::*; + use crate::jets::Result; + use crate::mem::NockStack; + use crate::noun::{Atom, Cell, DirectAtom, IndirectAtom, D}; + use std::cmp; + + /// Binary exponent + pub fn bex(stack: &mut NockStack, arg: usize) -> Atom { + unsafe { + if arg < 63 { + DirectAtom::new_unchecked(1 << arg).as_atom() + } else { + let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(stack, (arg + 7) >> 3); + dest.set(arg, true); + atom.normalize_as_atom() + } + } + } + + /// Measure the number of bloqs in an atom + pub fn met(bloq: usize, a: Atom) -> usize { + if unsafe { a.as_noun().raw_equals(D(0)) } { + 0 + } else if bloq < 6 { + (a.bit_size() + ((1 << bloq) - 1)) >> bloq + } else { + let bloq_word = bloq - 6; + (a.size() + ((1 << bloq_word) - 1)) >> bloq_word + } + } + + pub fn rip(stack: &mut NockStack, bloq: usize, step: usize, atom: Atom) -> Result { + let len = (met(bloq, atom) + step - 1) / step; + let mut list = D(0); + for i in (0..len).rev() { + let new_atom = unsafe { + let (mut new_indirect, new_slice) = + IndirectAtom::new_raw_mut_bitslice(stack, step << bloq); + chop(bloq, i * step, step, 0, new_slice, atom.as_bitslice())?; + new_indirect.normalize_as_atom() + }; + list = Cell::new(stack, new_atom.as_noun(), list).as_noun(); + } + + Ok(list) + } + + /// Binary OR + pub fn con(stack: &mut NockStack, a: Atom, b: Atom) -> Atom { + let new_size = cmp::max(a.size(), b.size()); + + unsafe { + let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(stack, new_size); + let a_bit = a.as_bitslice(); + dest[..a_bit.len()].copy_from_bitslice(a_bit); + *dest |= b.as_bitslice(); + atom.normalize_as_atom() + } + } + + #[cfg(test)] + mod tests { + use super::*; + use crate::jets::util::test::A; + use crate::noun::D; + use ibig::ubig; + + fn init_stack() -> NockStack { + NockStack::new(8 << 10 << 10, 0) + } + + #[test] + fn test_met() { + let s = &mut init_stack(); + + let a = A(s, &ubig!(0xdeadbeef12345678fedcba9876543210)) + .as_atom() + .unwrap(); + assert_eq!(met(0, a), 128); + assert_eq!(met(1, a), 64); + assert_eq!(met(2, a), 32); + assert_eq!(met(3, a), 16); + assert_eq!(met(4, a), 8); + assert_eq!(met(5, a), 4); + assert_eq!(met(6, a), 2); + assert_eq!(met(7, a), 1); + assert_eq!(met(8, a), 1); + + let a = D(0x7fffffffffffffff).as_atom().unwrap(); + assert_eq!(met(0, a), 63); + assert_eq!(met(1, a), 32); + assert_eq!(met(2, a), 16); + assert_eq!(met(3, a), 8); + assert_eq!(met(4, a), 4); + assert_eq!(met(5, a), 2); + assert_eq!(met(6, a), 1); + assert_eq!(met(7, a), 1); + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/rust/ares/src/jets/math.rs b/rust/ares/src/jets/math.rs index a5f280cf..102c01c6 100644 --- a/rust/ares/src/jets/math.rs +++ b/rust/ares/src/jets/math.rs @@ -250,12 +250,13 @@ pub fn jet_sub(context: &mut Context, subject: Noun) -> Result { let a = slot(arg, 2)?.as_atom()?; let b = slot(arg, 3)?.as_atom()?; - Ok(sub(&mut context.stack, a, b)?.as_noun()) + Ok(util::sub(&mut context.stack, a, b)?.as_noun()) } pub mod util { use crate::mem::NockStack; - use crate::noun::{Atom, Noun, NO, YES}; + use crate::noun::{Atom, Error, Noun, Result, NO, YES}; + use ibig::UBig; pub fn lth(stack: &mut NockStack, a: Atom, b: Atom) -> Noun { if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) { @@ -274,6 +275,32 @@ pub mod util { NO } } + + /// Subtraction + pub fn sub(stack: &mut NockStack, a: Atom, b: Atom) -> Result { + if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) { + let a_small = a.data(); + let b_small = b.data(); + + if a_small < b_small { + Err(Error::NotRepresentable) + } else { + Ok(Atom::new(stack, a_small - b_small)) + } + } else { + let a_big = a.as_ubig(stack); + let b_big = b.as_ubig(stack); + + if a_big < b_big { + Err(Error::NotRepresentable) + } else { + let a_big = a.as_ubig(stack); + let b_big = b.as_ubig(stack); + let res = UBig::sub_stack(stack, a_big, b_big); + Ok(Atom::from_ubig(stack, &res)) + } + } + } } #[cfg(test)] diff --git a/rust/ares/src/jets/nock.rs b/rust/ares/src/jets/nock.rs index 23b015ab..4230d68d 100644 --- a/rust/ares/src/jets/nock.rs +++ b/rust/ares/src/jets/nock.rs @@ -67,8 +67,8 @@ pub fn jet_mink(context: &mut Context, subject: Noun) -> Result { pub mod util { use crate::interpreter::{interpret, Context, Error}; use crate::jets; + use crate::jets::bits::util::rip; use crate::jets::form::util::scow; - use crate::jets::util::rip; use crate::jets::JetErr; use crate::mem::NockStack; use crate::noun::{tape, Cell, Noun, D, T}; diff --git a/rust/ares/src/jets/tree.rs b/rust/ares/src/jets/tree.rs index 29e5b118..36ca546f 100644 --- a/rust/ares/src/jets/tree.rs +++ b/rust/ares/src/jets/tree.rs @@ -1,6 +1,8 @@ /** Tree jets */ use crate::interpreter::{Context, Error}; +use crate::jets::bits::util::*; +use crate::jets::math::util::*; use crate::jets::util::*; use crate::jets::{JetErr, Result}; use crate::noun::{Noun, D}; From 5c34632f5f67517638134d28ff1d2fbe510fef1d Mon Sep 17 00:00:00 2001 From: Alex Shelkovnykov Date: Tue, 24 Oct 2023 10:13:53 -0600 Subject: [PATCH 10/12] Simplify code and tests --- rust/ares/src/jets/bits.rs | 53 ++++++-------------------------------- rust/ares/src/jets/list.rs | 23 +++++++---------- 2 files changed, 17 insertions(+), 59 deletions(-) diff --git a/rust/ares/src/jets/bits.rs b/rust/ares/src/jets/bits.rs index b9d392b0..ba05cc97 100644 --- a/rust/ares/src/jets/bits.rs +++ b/rust/ares/src/jets/bits.rs @@ -350,31 +350,9 @@ pub fn jet_mix(context: &mut Context, subject: Noun) -> Result { pub fn jet_xeb(_context: &mut Context, subject: Noun) -> Result { let sam = slot(subject, 6)?; let a = slot(sam, 1)?.as_atom()?; - - let syz = util::met(0, a) as u64; - - unsafe { Ok(DirectAtom::new_unchecked(syz).as_atom().as_noun()) } + Ok(D(util::met(0, a) as u64)) } -// return u3kc_rep(u3k(a), 1, u3kb_flop(u3qc_rip(a, 1, b))); -// TODO -// allocate new IndirectAtom of same size and as_bitslice() -// so we can rebuild it back to front in bloq size -/*pub fn jet_swp(context: &mut Context, subject: Noun) -> Result { - let sam = slot(subject, 6)?; - let bloq = slot(sam, 2)?.as_atom()?.as_direct()?.data() as usize; - let atom = slot(sam, 3)?.as_atom()?; - - let ripper = rip(context.stack, bloq, 1, atom)?; - //println!("ripper: {:?}", ripper); - let flopper = flop(context.stack, ripper)?; - //println!("flopper: {:?}", flopper); - let sample = T(context.stack, &[D(1), flopper]); - //println!("sample: {:?}", sample); - //println!("sample: {:?}", jet_rep(context, sample)); - jet_rep(context, sample) -}*/ - pub mod util { use crate::jets::util::*; use crate::jets::Result; @@ -875,26 +853,11 @@ mod tests { fn test_xeb() { let c = &mut init_context(); - assert_jet(c, jet_xeb, D(0), D(0)); - assert_jet(c, jet_xeb, D(1), D(1)); - assert_jet(c, jet_xeb, D(31), D(5)); - assert_jet(c, jet_xeb, D(32), D(6)); - assert_jet(c, jet_xeb, D(0xfff), D(12)); - assert_jet(c, jet_xeb, D(0xffff), D(16)); - assert_jet(c, jet_xeb, D(0x3fffffffffffffff), D(62)); - assert_jet(c, jet_xeb, D(0x4000000000000000), D(63)); - } - - /*#[test] - fn test_swp() { - let s = &mut init_stack(); - let sam = T(s, &[D(1), D(0x18)]); - assert_jet(s, jet_swp, sam, D(0x9)); - let sam = T(s, &[D(0), D(0x18)]); - assert_jet(s, jet_swp, sam, D(0x3)); - let sam = T(s, &[D(0), D(0x80)]); - assert_jet(s, jet_swp, sam, D(0x1)); - let sam = T(s, &[D(3), D(0x636261)]); - assert_jet(s, jet_swp, sam, D(0x616263)); - }*/ + let (a0, a24, a63, a96, a128) = atoms(&mut c.stack); + assert_jet(c, jet_xeb, a0, D(0)); + assert_jet(c, jet_xeb, a24, D(24)); + assert_jet(c, jet_xeb, a63, D(63)); + assert_jet(c, jet_xeb, a96, D(96)); + assert_jet(c, jet_xeb, a128, D(128)); + } } diff --git a/rust/ares/src/jets/list.rs b/rust/ares/src/jets/list.rs index 0cc00b77..e640018d 100644 --- a/rust/ares/src/jets/list.rs +++ b/rust/ares/src/jets/list.rs @@ -10,12 +10,6 @@ crate::gdb!(); pub fn jet_flop(context: &mut Context, subject: Noun) -> Result { let sam = slot(subject, 6)?; let src = slot(sam, 1)?; - - if unsafe { src.raw_equals(D(0)) } { - return Ok(D(0)); - } - - // util::flop(&mut context.stack, src).map_err(|x| x.into()) Ok(util::flop(&mut context.stack, src)?) } @@ -28,7 +22,7 @@ pub mod util { use crate::interpreter::Error; use crate::jets::JetErr; use crate::mem::NockStack; - use crate::noun::{Cell, Noun, D}; + use crate::noun::{Noun, D, T}; use std::result::Result; /// Reverse order of list @@ -36,15 +30,12 @@ pub mod util { let mut list = noun; let mut tsil = D(0); loop { - if let Some(list) = list.atom() { - if list.as_bitslice().first_one().is_none() { - break; - } else { - return Err(Error::Deterministic(D(0))); - } + if unsafe { list.raw_equals(D(0)) } { + break; } + let cell = list.as_cell()?; - tsil = Cell::new(stack, cell.head(), tsil).as_noun(); + tsil = T(stack, &[cell.head(), tsil]); list = cell.tail(); } @@ -110,6 +101,10 @@ mod tests { ], ); assert_jet(c, jet_flop, sam, res); + + assert_jet_err(c, jet_flop, D(1), JetErr::Fail(Error::Deterministic(D(0)))); + let sam = T(&mut c.stack, &[D(1), D(2), D(3)]); + assert_jet_err(c, jet_flop, sam, JetErr::Fail(Error::Deterministic(D(0)))); } #[test] From 764a565aa8865529cc6ad4cf397e6205f87ef4d1 Mon Sep 17 00:00:00 2001 From: Alex Shelkovnykov Date: Tue, 24 Oct 2023 10:16:47 -0600 Subject: [PATCH 11/12] Add +xeb and +flop to hot state and test pill --- hoon/scaffolding/playpen.hoon | 8 ++++++++ rust/ares/src/jets/hot.rs | 2 ++ 2 files changed, 10 insertions(+) diff --git a/hoon/scaffolding/playpen.hoon b/hoon/scaffolding/playpen.hoon index 1939da6b..f1b44edc 100644 --- a/hoon/scaffolding/playpen.hoon +++ b/hoon/scaffolding/playpen.hoon @@ -209,6 +209,7 @@ ++ flop :: reverse ~/ %flop |* a=(list) + ~> %sham.%flop => .(a (homo a)) ^+ a =+ b=`_a`~ @@ -413,6 +414,13 @@ |= [a=bloq b=@] (rep a (flop (rip a b))) :: +++ xeb :: binary logarithm + ~/ %xeb + |= a=@ + ~> %sham.%xeb + ^- @ + (met 0 a) +:: :: Modular arithmetic :: ++ fe :: modulo bloq diff --git a/rust/ares/src/jets/hot.rs b/rust/ares/src/jets/hot.rs index b808ad9a..ee63f568 100644 --- a/rust/ares/src/jets/hot.rs +++ b/rust/ares/src/jets/hot.rs @@ -25,6 +25,7 @@ const HOT_STATE: &[(&[Either], u64, Jet)] = &[ (&[A_50, Left(tas!(b"mas"))], 1, jet_mas), // (&[A_50, Left(tas!(b"lent"))], 1, jet_lent), + (&[A_50, Left(tas!(b"flop"))], 1, jet_flop), // (&[A_50, Left(tas!(b"bex"))], 1, jet_bex), (&[A_50, Left(tas!(b"can"))], 1, jet_can), @@ -38,6 +39,7 @@ const HOT_STATE: &[(&[Either], u64, Jet)] = &[ (&[A_50, Left(tas!(b"rev"))], 1, jet_rev), (&[A_50, Left(tas!(b"rip"))], 1, jet_rip), (&[A_50, Left(tas!(b"rsh"))], 1, jet_rsh), + (&[A_50, Left(tas!(b"xeb"))], 1, jet_xeb), // (&[A_50, Left(tas!(b"con"))], 1, jet_con), (&[A_50, Left(tas!(b"dis"))], 1, jet_dis), From 28e6e37c4fc8fa71d7a7b365d8bd4abb0d8ca90c Mon Sep 17 00:00:00 2001 From: Alex Shelkovnykov Date: Tue, 24 Oct 2023 10:25:30 -0600 Subject: [PATCH 12/12] Fix slot 1 lookup --- rust/ares/src/jets/list.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rust/ares/src/jets/list.rs b/rust/ares/src/jets/list.rs index e640018d..2ade24b3 100644 --- a/rust/ares/src/jets/list.rs +++ b/rust/ares/src/jets/list.rs @@ -9,8 +9,7 @@ crate::gdb!(); pub fn jet_flop(context: &mut Context, subject: Noun) -> Result { let sam = slot(subject, 6)?; - let src = slot(sam, 1)?; - Ok(util::flop(&mut context.stack, src)?) + Ok(util::flop(&mut context.stack, sam)?) } pub fn jet_lent(_context: &mut Context, subject: Noun) -> Result {