From 69b4bbeeaef26f03223b427c6c643c8d09097398 Mon Sep 17 00:00:00 2001 From: Philip Monk Date: Wed, 1 Feb 2023 20:59:17 -0700 Subject: [PATCH 1/9] add naive jet dashboard with two jets --- rust/ares/src/interpreter.rs | 15 ++++++++ rust/ares/src/jets.rs | 68 ++++++++++++++++++++++++++++++++++++ rust/ares/src/lib.rs | 1 + 3 files changed, 84 insertions(+) create mode 100644 rust/ares/src/jets.rs diff --git a/rust/ares/src/interpreter.rs b/rust/ares/src/interpreter.rs index 6604d45..bb086b4 100644 --- a/rust/ares/src/interpreter.rs +++ b/rust/ares/src/interpreter.rs @@ -1,4 +1,5 @@ use self::NockWork::*; +use crate::jets::get_jet; use crate::mem::unifying_equality; use crate::mem::NockStack; use crate::noun::{Atom, Cell, DirectAtom, IndirectAtom, Noun}; @@ -312,6 +313,20 @@ pub fn interpret(stack: &mut NockStack, mut subject: Noun, formula: Noun) -> Nou Nock11ComputeHint => unsafe { let hint = *stack.local_noun_pointer(1); if let Ok(hint_cell) = hint.as_cell() { + if hint_cell + .head() + .raw_equals(DirectAtom::new_unchecked(0x706f7270).as_noun()) + { + if let Ok(jet_formula) = hint_cell.tail().as_cell() { + let jet_name = jet_formula.tail(); + let jet = get_jet(jet_name); + if let Ok(jet) = jet { + res = jet(stack, subject); + stack.pop(&mut res); + continue; + } + } + } *(stack.local_noun_pointer(0)) = work_to_noun(Nock11ComputeResult); push_formula(stack, hint_cell.tail()); } else { diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs new file mode 100644 index 0000000..91882e7 --- /dev/null +++ b/rust/ares/src/jets.rs @@ -0,0 +1,68 @@ +use crate::interpreter::raw_slot; +use crate::mem::NockStack; +use crate::noun::{DirectAtom, IndirectAtom, Noun}; +use either::Either::*; + +pub fn get_jet(jet_name: Noun) -> Result Noun, ()> { + if let Ok(jet_direct) = jet_name.as_direct() { + match jet_direct.data() { + 0x636564 => Ok(jet_dec), + 0x747563 => Ok(jet_cut), + _ => Err(()), + } + } else { + Err(()) + } +} + +fn jet_dec(stack: &mut NockStack, subject: Noun) -> Noun { + let arg = raw_slot(subject, 6); + if let Ok(atom) = arg.as_atom() { + match atom.as_either() { + Left(direct) => { + if direct.data() == 0 { + panic!("Decrementing 0"); + } else { + unsafe { DirectAtom::new_unchecked(direct.data() - 1) }.as_noun() + } + } + Right(indirect) => { + let indirect_slice = indirect.as_bitslice(); + match indirect_slice.first_one() { + None => { + panic!("Decrementing 0"); + } + Some(first_one) => { + let (mut new_indirect, new_slice) = + unsafe { IndirectAtom::new_raw_mut_bitslice(stack, indirect.size()) }; + if first_one > 0 { + new_slice[..first_one].fill(true); + } + new_slice.set(first_one, false); + new_slice[first_one + 1..] + .copy_from_bitslice(&indirect_slice[first_one + 1..]); + let res = unsafe { new_indirect.normalize_as_atom() }; + res.as_noun() + } + } + } + } + } else { + panic!("Argument to dec must be atom"); + } +} + +fn jet_cut(stack: &mut NockStack, subject: Noun) -> Noun { + let arg = raw_slot(subject, 6); + let bloq = raw_slot(arg, 2).as_direct().unwrap().data(); + let start = raw_slot(arg, 12).as_direct().unwrap().data(); + let run = raw_slot(arg, 13).as_direct().unwrap().data(); + let atom = raw_slot(arg, 7).as_atom().unwrap(); + let slice = atom.as_bitslice(); + let unit = 1 << bloq; + let (mut new_indirect, new_slice) = + unsafe { IndirectAtom::new_raw_mut_bitslice(stack, ((run * unit + 63) >> 6) as usize) }; + new_slice[..(unit * run) as usize] + .copy_from_bitslice(&slice[(unit * start) as usize..(unit * (start + run)) as usize]); + unsafe { new_indirect.normalize_as_atom().as_noun() } +} diff --git a/rust/ares/src/lib.rs b/rust/ares/src/lib.rs index 55d0089..c70f0b7 100644 --- a/rust/ares/src/lib.rs +++ b/rust/ares/src/lib.rs @@ -1,6 +1,7 @@ #[macro_use] extern crate num_derive; pub mod interpreter; +pub mod jets; pub mod mem; pub mod mug; pub mod noun; From ac6103da99789a045f975867e6824d1f03a01dbd Mon Sep 17 00:00:00 2001 From: Philip Monk Date: Thu, 2 Feb 2023 17:19:13 -0700 Subject: [PATCH 2/9] [ares] style fixes --- rust/ares/src/interpreter.rs | 5 ++--- rust/ares/src/jets.rs | 12 ++++-------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/rust/ares/src/interpreter.rs b/rust/ares/src/interpreter.rs index 980ecae..e459ce0 100644 --- a/rust/ares/src/interpreter.rs +++ b/rust/ares/src/interpreter.rs @@ -1,5 +1,5 @@ use self::NockWork::*; -use crate::jets::get_jet; +use crate::jets; use crate::mem::unifying_equality; use crate::mem::NockStack; use crate::noun::{Atom, Cell, DirectAtom, IndirectAtom, Noun}; @@ -319,8 +319,7 @@ pub fn interpret(stack: &mut NockStack, mut subject: Noun, formula: Noun) -> Nou { if let Ok(jet_formula) = hint_cell.tail().as_cell() { let jet_name = jet_formula.tail(); - let jet = get_jet(jet_name); - if let Ok(jet) = jet { + if let Ok(jet) = jets::get_jet(jet_name) { res = jet(stack, subject); stack.pop(&mut res); continue; diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 91882e7..194797c 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -4,14 +4,10 @@ use crate::noun::{DirectAtom, IndirectAtom, Noun}; use either::Either::*; pub fn get_jet(jet_name: Noun) -> Result Noun, ()> { - if let Ok(jet_direct) = jet_name.as_direct() { - match jet_direct.data() { - 0x636564 => Ok(jet_dec), - 0x747563 => Ok(jet_cut), - _ => Err(()), - } - } else { - Err(()) + match jet_name.as_direct()?.data() { + 0x636564 => Ok(jet_dec), + 0x747563 => Ok(jet_cut), + _ => Err(()), } } From 38fd0349842004b891ec717602492b5a1159aee3 Mon Sep 17 00:00:00 2001 From: Peter McEvoy Date: Fri, 3 Feb 2023 11:07:39 -0500 Subject: [PATCH 3/9] Sort dependencies alphabetically --- rust/ares/Cargo.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rust/ares/Cargo.toml b/rust/ares/Cargo.toml index 42967d4..3452b31 100644 --- a/rust/ares/Cargo.toml +++ b/rust/ares/Cargo.toml @@ -8,14 +8,14 @@ edition = "2018" [dependencies] bitvec = "1.0.0" -either = "1.6.1" -libc = "0.2.126" -murmur3 = { git = "https://github.com/tloncorp/murmur3", branch = "eamsden/non_copying" } -memmap = "0.7.0" -intmap = "1.1.0" -num-traits = "0.2" -num-derive = "0.3" criterion = "0.4" +either = "1.6.1" +intmap = "1.1.0" +libc = "0.2.126" +memmap = "0.7.0" +murmur3 = { git = "https://github.com/tloncorp/murmur3", branch = "eamsden/non_copying" } +num-derive = "0.3" +num-traits = "0.2" [[bin]] name = "cue_pill" From d072c3ecea32291af4f1a9b1c90114d78433d63d Mon Sep 17 00:00:00 2001 From: Peter McEvoy Date: Fri, 3 Feb 2023 12:10:04 -0500 Subject: [PATCH 4/9] Implement tas!() in separate crate --- rust/ares/Cargo.lock | 9 ++++++++ rust/ares/Cargo.toml | 1 + rust/ares_macros/Cargo.lock | 46 +++++++++++++++++++++++++++++++++++++ rust/ares_macros/Cargo.toml | 12 ++++++++++ rust/ares_macros/src/lib.rs | 22 ++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 rust/ares_macros/Cargo.lock create mode 100644 rust/ares_macros/Cargo.toml create mode 100644 rust/ares_macros/src/lib.rs diff --git a/rust/ares/Cargo.lock b/rust/ares/Cargo.lock index 41f0663..19b07a1 100644 --- a/rust/ares/Cargo.lock +++ b/rust/ares/Cargo.lock @@ -12,6 +12,7 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" name = "ares" version = "0.1.0" dependencies = [ + "ares_macros", "bitvec", "criterion", "either", @@ -23,6 +24,14 @@ dependencies = [ "num-traits", ] +[[package]] +name = "ares_macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "atty" version = "0.2.14" diff --git a/rust/ares/Cargo.toml b/rust/ares/Cargo.toml index 3452b31..39b1341 100644 --- a/rust/ares/Cargo.toml +++ b/rust/ares/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ares_macros = { path = "../ares_macros" } bitvec = "1.0.0" criterion = "0.4" either = "1.6.1" diff --git a/rust/ares_macros/Cargo.lock b/rust/ares_macros/Cargo.lock new file mode 100644 index 0000000..396e62a --- /dev/null +++ b/rust/ares_macros/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ares_macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" diff --git a/rust/ares_macros/Cargo.toml b/rust/ares_macros/Cargo.toml new file mode 100644 index 0000000..dd2bd71 --- /dev/null +++ b/rust/ares_macros/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "ares_macros" +version = "0.1.0" +edition = "2021" + + +[lib] +proc-macro = true + +[dependencies] +quote = "1.0" +syn = "1.0" diff --git a/rust/ares_macros/src/lib.rs b/rust/ares_macros/src/lib.rs new file mode 100644 index 0000000..f5344b5 --- /dev/null +++ b/rust/ares_macros/src/lib.rs @@ -0,0 +1,22 @@ +use proc_macro::TokenStream; +use quote::quote; +use std::mem::size_of; +use syn::{self, LitByteStr}; + +#[proc_macro] +pub fn tas(input: TokenStream) -> TokenStream { + let byte_str: LitByteStr = syn::parse(input).expect("failed to parse input"); + let bytes = byte_str.value(); + if bytes.len() > size_of::() { + panic!( + "\"{}\" does not fit in a u64: must be 8 or fewer characters, not {}", + byte_str.token(), + bytes.len() + ); + } + let mut val: u64 = 0; + for byte in bytes.into_iter().rev() { + val = (val << u8::BITS) | u64::from(byte); + } + quote!(#val).into() +} From f41cf8cc389be298a7486c4302ae582a524c6d7e Mon Sep 17 00:00:00 2001 From: Peter McEvoy Date: Fri, 3 Feb 2023 11:59:53 -0500 Subject: [PATCH 5/9] Test tas!() --- rust/ares/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/ares/src/lib.rs b/rust/ares/src/lib.rs index c70f0b7..7a6fb1c 100644 --- a/rust/ares/src/lib.rs +++ b/rust/ares/src/lib.rs @@ -6,3 +6,14 @@ pub mod mem; pub mod mug; pub mod noun; pub mod serialization; + +#[cfg(test)] +mod tests { + + #[test] + fn tas() { + use ares_macros::tas; + assert_eq!(tas!(b"cut"), 0x747563); + assert_eq!(tas!(b"dec"), 0x636564); + } +} From 27f64b96ea05251e3629fda17905c4c7dad891f7 Mon Sep 17 00:00:00 2001 From: Peter McEvoy Date: Fri, 3 Feb 2023 12:00:02 -0500 Subject: [PATCH 6/9] Use tas!() in jets dashboard --- rust/ares/src/jets.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 194797c..af9a7f0 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -1,12 +1,13 @@ use crate::interpreter::raw_slot; use crate::mem::NockStack; use crate::noun::{DirectAtom, IndirectAtom, Noun}; +use ares_macros::tas; use either::Either::*; pub fn get_jet(jet_name: Noun) -> Result Noun, ()> { match jet_name.as_direct()?.data() { - 0x636564 => Ok(jet_dec), - 0x747563 => Ok(jet_cut), + tas!(b"dec") => Ok(jet_dec), + tas!(b"cut") => Ok(jet_cut), _ => Err(()), } } From 83b4fe308ad6e45a706b3cdf1906564365f89da3 Mon Sep 17 00:00:00 2001 From: Peter McEvoy Date: Fri, 3 Feb 2023 12:15:56 -0500 Subject: [PATCH 7/9] Revert "Sort dependencies alphabetically" This reverts commit 38fd0349842004b891ec717602492b5a1159aee3. --- rust/ares/Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/ares/Cargo.toml b/rust/ares/Cargo.toml index 39b1341..02c73d9 100644 --- a/rust/ares/Cargo.toml +++ b/rust/ares/Cargo.toml @@ -9,14 +9,14 @@ edition = "2018" [dependencies] ares_macros = { path = "../ares_macros" } bitvec = "1.0.0" -criterion = "0.4" either = "1.6.1" -intmap = "1.1.0" libc = "0.2.126" -memmap = "0.7.0" murmur3 = { git = "https://github.com/tloncorp/murmur3", branch = "eamsden/non_copying" } -num-derive = "0.3" +memmap = "0.7.0" +intmap = "1.1.0" num-traits = "0.2" +num-derive = "0.3" +criterion = "0.4" [[bin]] name = "cue_pill" From 7765c020cff77e34b85206a4a0ece440d21f3901 Mon Sep 17 00:00:00 2001 From: Philip Monk Date: Fri, 3 Feb 2023 10:19:23 -0700 Subject: [PATCH 8/9] [ares] rename %prop->%sham and add comment --- rust/ares/src/interpreter.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/ares/src/interpreter.rs b/rust/ares/src/interpreter.rs index e459ce0..c4a98c3 100644 --- a/rust/ares/src/interpreter.rs +++ b/rust/ares/src/interpreter.rs @@ -313,9 +313,10 @@ pub fn interpret(stack: &mut NockStack, mut subject: Noun, formula: Noun) -> Nou Nock11ComputeHint => unsafe { let hint = *stack.local_noun_pointer(1); if let Ok(hint_cell) = hint.as_cell() { + // match %sham hints, which are scaffolding until we have a real jet dashboard if hint_cell .head() - .raw_equals(DirectAtom::new_unchecked(0x706f7270).as_noun()) + .raw_equals(DirectAtom::new_unchecked(0x6d616873).as_noun()) { if let Ok(jet_formula) = hint_cell.tail().as_cell() { let jet_name = jet_formula.tail(); From 3de42389c101fdf058f90026d16e3b5d47ec290b Mon Sep 17 00:00:00 2001 From: Peter McEvoy Date: Fri, 3 Feb 2023 12:19:32 -0500 Subject: [PATCH 9/9] Use tas!() when computing Nock 11 --- rust/ares/src/interpreter.rs | 3 ++- rust/ares/src/lib.rs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rust/ares/src/interpreter.rs b/rust/ares/src/interpreter.rs index e459ce0..1d2eca8 100644 --- a/rust/ares/src/interpreter.rs +++ b/rust/ares/src/interpreter.rs @@ -3,6 +3,7 @@ use crate::jets; use crate::mem::unifying_equality; use crate::mem::NockStack; use crate::noun::{Atom, Cell, DirectAtom, IndirectAtom, Noun}; +use ares_macros::tas; use bitvec::prelude::{BitSlice, Lsb0}; use either::Either::*; use num_traits::cast::{FromPrimitive, ToPrimitive}; @@ -315,7 +316,7 @@ pub fn interpret(stack: &mut NockStack, mut subject: Noun, formula: Noun) -> Nou if let Ok(hint_cell) = hint.as_cell() { if hint_cell .head() - .raw_equals(DirectAtom::new_unchecked(0x706f7270).as_noun()) + .raw_equals(DirectAtom::new_unchecked(tas!(b"prop")).as_noun()) { if let Ok(jet_formula) = hint_cell.tail().as_cell() { let jet_name = jet_formula.tail(); diff --git a/rust/ares/src/lib.rs b/rust/ares/src/lib.rs index 7a6fb1c..8f2ff19 100644 --- a/rust/ares/src/lib.rs +++ b/rust/ares/src/lib.rs @@ -15,5 +15,6 @@ mod tests { use ares_macros::tas; assert_eq!(tas!(b"cut"), 0x747563); assert_eq!(tas!(b"dec"), 0x636564); + assert_eq!(tas!(b"prop"), 0x706f7270); } }