From 4e292b88a0342fe655ed8444ea8e39b743c8b8cd Mon Sep 17 00:00:00 2001 From: Edward Amsden Date: Wed, 27 Sep 2023 22:32:40 -0500 Subject: [PATCH] interpreter: formatting --- rust/ares/src/hamt.rs | 68 ++++++++++++++++++------------------ rust/ares/src/interpreter.rs | 48 +++++++++++++++++-------- rust/ares/src/jets/cold.rs | 3 +- rust/ares/src/main.rs | 12 +++++-- rust/ares/src/mem.rs | 12 +++++-- rust/ares/src/serf.rs | 25 ++++++++++--- 6 files changed, 107 insertions(+), 61 deletions(-) diff --git a/rust/ares/src/hamt.rs b/rust/ares/src/hamt.rs index 00de453..4d340dc 100644 --- a/rust/ares/src/hamt.rs +++ b/rust/ares/src/hamt.rs @@ -434,47 +434,47 @@ impl Default for Hamt { impl Preserve for Hamt { unsafe fn assert_in_stack(&self, stack: &NockStack) { - stack.struct_is_in(self.0.buffer, self.0.size()); - let mut traversal_stack: [Option<(Stem, u32)>; 6] = [None; 6]; - traversal_stack[0] = Some((self.0, 0)); - let mut traversal_depth = 1; - 'check: loop { - if traversal_depth == 0 { - break; + stack.struct_is_in(self.0.buffer, self.0.size()); + let mut traversal_stack: [Option<(Stem, u32)>; 6] = [None; 6]; + traversal_stack[0] = Some((self.0, 0)); + let mut traversal_depth = 1; + 'check: loop { + if traversal_depth == 0 { + break; + } + let (stem, mut position) = traversal_stack[traversal_depth - 1] + .expect("Attempted to access uninitialized array element"); + // can we loop over the size and count leading 0s remaining in the bitmap? + 'check_stem: loop { + if position >= 32 { + traversal_depth -= 1; + continue 'check; } - let (stem, mut position) = traversal_stack[traversal_depth - 1] - .expect("Attempted to access uninitialized array element"); - // can we loop over the size and count leading 0s remaining in the bitmap? - 'check_stem: loop { - if position >= 32 { - traversal_depth -= 1; + match stem.entry(position) { + None => { + position += 1; + continue 'check_stem; + } + Some((Left(next_stem), idx)) => { + stack.struct_is_in(next_stem.buffer, next_stem.size()); + assert!(traversal_depth <= 5); // will increment + traversal_stack[traversal_depth - 1].as_mut().unwrap().1 = position + 1; + traversal_stack[traversal_depth] = Some((next_stem, 0)); + traversal_depth += 1; continue 'check; } - match stem.entry(position) { - None => { - position += 1; - continue 'check_stem; - } - Some((Left(next_stem), idx)) => { - stack.struct_is_in(next_stem.buffer, next_stem.size()); - assert!(traversal_depth <= 5); // will increment - traversal_stack[traversal_depth - 1].as_mut().unwrap().1 = position + 1; - traversal_stack[traversal_depth] = Some((next_stem, 0)); - traversal_depth += 1; - continue 'check; - } - Some((Right(leaf), idx)) => { - stack.struct_is_in(leaf.buffer, leaf.len); - for pair in leaf.to_mut_slice().iter() { - pair.0.assert_in_stack(stack); - pair.1.assert_in_stack(stack); - } - position += 1; - continue 'check_stem; + Some((Right(leaf), idx)) => { + stack.struct_is_in(leaf.buffer, leaf.len); + for pair in leaf.to_mut_slice().iter() { + pair.0.assert_in_stack(stack); + pair.1.assert_in_stack(stack); } + position += 1; + continue 'check_stem; } } } + } } unsafe fn preserve(&mut self, stack: &mut NockStack) { if stack.is_in_frame(self.0.buffer) { diff --git a/rust/ares/src/interpreter.rs b/rust/ares/src/interpreter.rs index 19e2cd0..abe09cc 100644 --- a/rust/ares/src/interpreter.rs +++ b/rust/ares/src/interpreter.rs @@ -1,14 +1,14 @@ use crate::hamt::Hamt; use crate::jets; use crate::jets::cold::Cold; -use crate::jets::warm::Warm; use crate::jets::hot::Hot; +use crate::jets::warm::Warm; use crate::mem::unifying_equality; use crate::mem::NockStack; use crate::newt::Newt; use crate::noun::{Atom, Cell, DirectAtom, IndirectAtom, Noun, D, T}; use ares_macros::tas; -use assert_no_alloc::{assert_no_alloc}; +use assert_no_alloc::assert_no_alloc; use bitvec::prelude::{BitSlice, Lsb0}; use either::Either::*; @@ -311,8 +311,10 @@ pub fn interpret( push_formula(stack, vale.formula, false); } Todo2::ComputeResult => { - if let Some(jet) = warm.find_jet(stack, &mut vale.subject, &mut res) { // a jet match - if let Ok(jet_res) = jet(stack, vale.subject) { // XX TODO: nondeterministic errors + if let Some(jet) = warm.find_jet(stack, &mut vale.subject, &mut res) { + // a jet match + if let Ok(jet_res) = jet(stack, vale.subject) { + // XX TODO: nondeterministic errors res = jet_res; stack.pop::(); continue; @@ -461,8 +463,10 @@ pub fn interpret( } Todo9::ComputeResult => { let mut formula = slot(res, kale.axis.as_bitslice()); - if let Some(jet) = warm.find_jet(stack, &mut res, &mut formula) { // a jet match - if let Ok(jet_res) = jet(stack, res) { // XX TODO: nondeterministic errors + if let Some(jet) = warm.find_jet(stack, &mut res, &mut formula) { + // a jet match + if let Ok(jet_res) = jet(stack, res) { + // XX TODO: nondeterministic errors res = jet_res; stack.pop::(); continue; @@ -534,7 +538,9 @@ pub fn interpret( } Todo11D::Done => { let hint = Cell::new(stack, dint.tag.as_noun(), dint.hint).as_noun(); - let _ = match_post_hinted(stack, subject, hint, res, &mut cache, cold, warm, hot); + let _ = match_post_hinted( + stack, subject, hint, res, &mut cache, cold, warm, hot, + ); stack.pop::(); } }, @@ -553,8 +559,16 @@ pub fn interpret( } } Todo11S::Done => { - let _ = - match_post_hinted(stack, subject, sint.tag.as_noun(), res, &mut cache, cold, warm, hot); + let _ = match_post_hinted( + stack, + subject, + sint.tag.as_noun(), + res, + &mut cache, + cold, + warm, + hot, + ); stack.pop::(); } }, @@ -931,7 +945,7 @@ fn match_post_hinted( cache: &mut Hamt, cold: &mut Cold, warm: &mut Warm, - hot: Hot + hot: Hot, ) -> Result<(), ()> { let direct = hint.as_cell()?.head().as_direct()?; match direct.data() { @@ -940,7 +954,7 @@ fn match_post_hinted( let mut key = Cell::new(stack, subject, formula).as_noun(); *cache = cache.insert(stack, &mut key, res); Ok(()) - }, + } tas!(b"fast") => { let clue = raw_slot_result(hint, 7)?; let chum = raw_slot_result(clue, 2)?; @@ -948,15 +962,19 @@ fn match_post_hinted( let parent_formula_ax = raw_slot_result(clue, 13)?.as_atom()?; if parent_formula_op.data() == 1 { if parent_formula_ax.as_direct()?.data() == 0 { - let changed = cold.register(stack, res, parent_formula_ax, chum)?; - if changed { *warm = Warm::init(stack, cold, hot); } - Ok(()) + let changed = cold.register(stack, res, parent_formula_ax, chum)?; + if changed { + *warm = Warm::init(stack, cold, hot); + } + Ok(()) } else { Err(()) } } else { let changed = cold.register(stack, res, parent_formula_ax, chum)?; - if changed { *warm = Warm::init(stack, cold, hot); } + if changed { + *warm = Warm::init(stack, cold, hot); + } Ok(()) } } diff --git a/rust/ares/src/jets/cold.rs b/rust/ares/src/jets/cold.rs index 4428a1a..9a7245c 100644 --- a/rust/ares/src/jets/cold.rs +++ b/rust/ares/src/jets/cold.rs @@ -32,7 +32,7 @@ impl Preserve for Batteries { break; }; cursor = (*cursor.0).parent_batteries; - }; + } } unsafe fn preserve(&mut self, stack: &mut NockStack) { if self.0.is_null() { @@ -130,7 +130,6 @@ impl Preserve for BatteriesList { }; cursor = (*cursor.0).next; } - } unsafe fn preserve(&mut self, stack: &mut NockStack) { if self.0.is_null() { diff --git a/rust/ares/src/main.rs b/rust/ares/src/main.rs index f46a2d3..6b06a50 100644 --- a/rust/ares/src/main.rs +++ b/rust/ares/src/main.rs @@ -1,7 +1,7 @@ use ares::interpreter::interpret; use ares::jets::cold::Cold; -use ares::jets::warm::Warm; use ares::jets::hot::Hot; +use ares::jets::warm::Warm; use ares::mem::NockStack; use ares::noun::IndirectAtom; use ares::serf::serf; @@ -58,7 +58,15 @@ fn main() -> io::Result<()> { let input_cell = input .as_cell() .expect("Input must be jam of subject/formula pair"); - let result = interpret(&mut stack, &mut None, &mut cold, &mut warm, hot, input_cell.head(), input_cell.tail()); + let result = interpret( + &mut stack, + &mut None, + &mut cold, + &mut warm, + hot, + input_cell.head(), + input_cell.tail(), + ); if let Ok(atom) = result.as_atom() { println!("Result: {}", atom); } diff --git a/rust/ares/src/mem.rs b/rust/ares/src/mem.rs index 11d603d..a221bf2 100644 --- a/rust/ares/src/mem.rs +++ b/rust/ares/src/mem.rs @@ -441,7 +441,7 @@ impl NockStack { assert_acyclic!(*noun); } - pub unsafe fn struct_is_in(&self, ptr: *const T, count: usize ) { + pub unsafe fn struct_is_in(&self, ptr: *const T, count: usize) { let ap = (if self.pc { *(self.prev_alloc_pointer_pointer()) } else { @@ -458,7 +458,13 @@ impl NockStack { } else if (ptr as usize) >= hi && (ptr.add(count) as usize) > hi { return; } - panic!("Use after free: allocation from {:#x} to {:#x}, free space from {:#x} to {:#x}", ptr as usize, ptr.add(count) as usize, low, hi); + panic!( + "Use after free: allocation from {:#x} to {:#x}, free space from {:#x} to {:#x}", + ptr as usize, + ptr.add(count) as usize, + low, + hi + ); } unsafe fn noun_in(&self, noun: Noun) { @@ -480,7 +486,7 @@ impl NockStack { if let Ok(a) = subnoun.as_allocated() { let np = a.to_raw_pointer() as usize; if np >= low && np < hi { - panic!("noun not in {:?}: {:?}", (low,hi), subnoun); + panic!("noun not in {:?}: {:?}", (low, hi), subnoun); } if let Right(c) = a.as_either() { dbg_stack.push(c.tail()); diff --git a/rust/ares/src/serf.rs b/rust/ares/src/serf.rs index 6112249..4a8a6ae 100644 --- a/rust/ares/src/serf.rs +++ b/rust/ares/src/serf.rs @@ -1,7 +1,7 @@ use crate::interpreter::{interpret, raw_slot}; use crate::jets::cold::Cold; -use crate::jets::warm::Warm; use crate::jets::hot::Hot; +use crate::jets::warm::Warm; use crate::mem::NockStack; use crate::mug::mug_u32; use crate::newt::Newt; @@ -82,7 +82,8 @@ pub fn serf() -> io::Result<()> { let lit = raw_slot(writ, 7); let sub = T(stack, &[D(0), D(3)]); let lyf = T(stack, &[D(2), sub, D(0), D(2)]); - let gat = interpret(stack, &mut Some(newt), &mut cold, &mut warm, hot, lit, lyf); + let gat = + interpret(stack, &mut Some(newt), &mut cold, &mut warm, hot, lit, lyf); arvo = raw_slot(gat, 7); false } else { @@ -96,7 +97,10 @@ pub fn serf() -> io::Result<()> { while let Ok(cell) = lit.as_cell() { if run { let ovo = cell.head(); - let res = slam(stack, newt, &mut cold, &mut warm, hot, arvo, POKE_AXIS, ovo).as_cell().unwrap(); + let res = + slam(stack, newt, &mut cold, &mut warm, hot, arvo, POKE_AXIS, ovo) + .as_cell() + .unwrap(); arvo = res.tail(); } event_number += 1; @@ -108,7 +112,9 @@ pub fn serf() -> io::Result<()> { } tas!(b"work") => { let ovo = raw_slot(writ, 7); - let res = slam(stack, newt, &mut cold, &mut warm, hot, arvo, POKE_AXIS, ovo).as_cell().unwrap(); + let res = slam(stack, newt, &mut cold, &mut warm, hot, arvo, POKE_AXIS, ovo) + .as_cell() + .unwrap(); let fec = res.head(); arvo = res.tail(); snap.save(stack, &mut arvo); @@ -124,7 +130,16 @@ pub fn serf() -> io::Result<()> { Ok(()) } -pub fn slam(stack: &mut NockStack, newt: &mut Newt, cold: &mut Cold, warm: &mut Warm, hot: Hot, core: Noun, axis: u64, ovo: Noun) -> Noun { +pub fn slam( + stack: &mut NockStack, + newt: &mut Newt, + cold: &mut Cold, + warm: &mut Warm, + hot: Hot, + core: Noun, + axis: u64, + ovo: Noun, +) -> Noun { let pul = T(stack, &[D(9), D(axis), D(0), D(2)]); let sam = T(stack, &[D(6), D(0), D(7)]); let fol = T(stack, &[D(8), pul, D(9), D(2), D(10), sam, D(0), D(2)]);