From ffdd91c34101a9c3732a8cca2958e787b222ed2b Mon Sep 17 00:00:00 2001 From: Edward Amsden Date: Sat, 16 Jul 2022 00:12:13 -0500 Subject: [PATCH] now runs to completion on hurray.jam without crashing --- rust/iron-planet/src/interpreter.rs | 1 + rust/iron-planet/src/main.rs | 2 +- rust/iron-planet/src/mem.rs | 6 +++--- rust/iron-planet/src/noun.rs | 10 +++++----- rust/iron-planet/src/serialization.rs | 14 +++++++------- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/rust/iron-planet/src/interpreter.rs b/rust/iron-planet/src/interpreter.rs index fee95ba..1292fe8 100644 --- a/rust/iron-planet/src/interpreter.rs +++ b/rust/iron-planet/src/interpreter.rs @@ -72,6 +72,7 @@ pub fn interpret(stack: &mut NockStack, mut subject: Noun, formula: Noun) -> Nou loop { match unsafe { noun_to_work(*(stack.local_noun_pointer(0))) } { Done => { + stack.pop(&mut res); break; } NockCellComputeHead => { diff --git a/rust/iron-planet/src/main.rs b/rust/iron-planet/src/main.rs index 3957153..d922c3c 100644 --- a/rust/iron-planet/src/main.rs +++ b/rust/iron-planet/src/main.rs @@ -37,7 +37,7 @@ fn main() -> io::Result<()> { let input_cell = input.as_cell().expect("Input must be jam of subject/formula pair"); let result = interpret(&mut stack, input_cell.head(), input_cell.tail()); let jammed_result = jam(&mut stack, result); - let f_out = OpenOptions::new().write(true).open(output_filename)?; + let f_out = OpenOptions::new().read(true).write(true).create(true).open(output_filename)?; f_out.set_len((jammed_result.size() << 3) as u64)?; unsafe { let mut out_map = MmapMut::map_mut(&f_out)?; diff --git a/rust/iron-planet/src/mem.rs b/rust/iron-planet/src/mem.rs index 8f09618..1615f64 100644 --- a/rust/iron-planet/src/mem.rs +++ b/rust/iron-planet/src/mem.rs @@ -198,7 +198,7 @@ impl NockStack { } unsafe fn top_in_previous_frame_west(&mut self) -> *mut T { - let prev_stack_pointer_pointer = self.previous_stack_pointer_pointer_east(); + let prev_stack_pointer_pointer = self.previous_stack_pointer_pointer_west(); *prev_stack_pointer_pointer as *mut T } @@ -298,7 +298,7 @@ impl NockStack { unsafe fn copy_east(&mut self, noun: &mut Noun) { let noun_ptr = noun as *mut Noun; let work_start = self.stack_pointer; - let mut other_stack_pointer = *self.previous_stack_pointer_pointer_east(); + let mut other_stack_pointer = *(self.previous_stack_pointer_pointer_east()); self.stack_pointer = self.stack_pointer.sub(2); *(self.stack_pointer as *mut Noun) = *noun; *(self.stack_pointer.add(1) as *mut *mut Noun) = noun_ptr; @@ -406,7 +406,7 @@ impl NockStack { unsafe fn copy_west(&mut self, noun: &mut Noun) { let noun_ptr = noun as *mut Noun; let work_start = self.stack_pointer; - let mut other_stack_pointer = *self.previous_stack_pointer_pointer_west(); + let mut other_stack_pointer = *(self.previous_stack_pointer_pointer_west()); self.stack_pointer = self.stack_pointer.add(2); *(self.stack_pointer.sub(2) as *mut Noun) = *noun; *(self.stack_pointer.sub(1) as *mut *mut Noun) = noun_ptr; diff --git a/rust/iron-planet/src/noun.rs b/rust/iron-planet/src/noun.rs index d79c495..2b0211e 100644 --- a/rust/iron-planet/src/noun.rs +++ b/rust/iron-planet/src/noun.rs @@ -130,8 +130,8 @@ impl IndirectAtom { pub unsafe fn forwarding_pointer(&self) -> Option { let size_raw = *self.to_raw_pointer().add(1); - if size_raw | FORWARDING_MASK == FORWARDING_TAG { - // we can replace this by masking out thge forwarding pointer and putting in the + if size_raw & FORWARDING_MASK == FORWARDING_TAG { + // we can replace this by masking out the forwarding pointer and putting in the // indirect tag Some(Self::from_raw_pointer((size_raw << 3) as *const u64)) } else { @@ -280,7 +280,7 @@ impl Cell { pub unsafe fn forwarding_pointer(&self) -> Option { let head_raw = (*self.to_raw_pointer()).head.raw; - if head_raw | FORWARDING_MASK == FORWARDING_TAG { + if head_raw & FORWARDING_MASK == FORWARDING_TAG { // we can replace this by masking out the forwarding pointer and putting in the cell // tag Some(Self::from_raw_pointer((head_raw << 3) as *const CellMemory)) @@ -305,11 +305,11 @@ impl Cell { } pub fn head(&self) -> Noun { - unsafe { (*self.to_raw_pointer()).head } + unsafe { (*(self.to_raw_pointer())).head } } pub fn tail(&self) -> Noun { - unsafe { (*self.to_raw_pointer()).tail } + unsafe { (*(self.to_raw_pointer())).tail } } pub fn as_allocated(&self) -> Allocated { diff --git a/rust/iron-planet/src/serialization.rs b/rust/iron-planet/src/serialization.rs index 6f63a23..48c2412 100644 --- a/rust/iron-planet/src/serialization.rs +++ b/rust/iron-planet/src/serialization.rs @@ -87,10 +87,10 @@ fn get_size(cursor: &mut usize, buffer: &BitSlice) -> usize { let mut bitsize: usize = 0; loop { if buffer[*cursor + bitsize] { + break; + } else { bitsize += 1; continue; - } else { - break; } } if bitsize == 0 { @@ -171,7 +171,7 @@ pub fn jam(stack: &mut NockStack, noun: Noun) -> Atom { if unsafe { stack.prev_stack_pointer_equals_local(0) } { break; } else { - let mut noun = unsafe { *(stack.top_in_previous_frame()) }; + let mut noun = unsafe { *(stack.top_in_previous_frame::()) }; let mug = mug_u32(stack, noun); match backref_map.get_mut(mug as u64) { None => {} @@ -334,13 +334,13 @@ fn mat( if state.cursor + c_b_size + c_b_size + b_atom_size > state.slice.len() { Err(()) } else { - state.slice[state.cursor..state.cursor + c_b_size + 1].fill(true); // a 1 bit for each bit in the atom size - state.slice.set(state.cursor + c_b_size + 1, false); // a terminating 0 bit - state.slice[state.cursor + c_b_size + 2..state.cursor + c_b_size + c_b_size] + state.slice[state.cursor..state.cursor + c_b_size].fill(false); // a 1 bit for each bit in the atom size + state.slice.set(state.cursor + c_b_size, true); // a terminating 1 bit + state.slice[state.cursor + c_b_size + 1..state.cursor + c_b_size + c_b_size] .copy_from_bitslice(&b_atom_size_atom.as_bitslice()[0..c_b_size - 1]); // the atom size excepting the most significant 1 (since we know where that is from the size-of-the-size) state.slice[state.cursor + c_b_size + c_b_size ..state.cursor + c_b_size + c_b_size + b_atom_size] - .copy_from_bitslice(atom.as_bitslice()); // the atom itself + .copy_from_bitslice(&atom.as_bitslice()[0..b_atom_size]); // the atom itself state.cursor += c_b_size + c_b_size + b_atom_size; Ok(()) }