From: Mark Thom Date: Sun, 20 Apr 2025 23:53:36 +0000 (-0700) Subject: correctly increment s_offset for partial strings (#2897) X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=dff165065770d47c8f7318dc2523a83af24e36f0;p=scryer-prolog.git correctly increment s_offset for partial strings (#2897) --- diff --git a/src/machine/dispatch.rs b/src/machine/dispatch.rs index 4494b8cd..254f9adc 100644 --- a/src/machine/dispatch.rs +++ b/src/machine/dispatch.rs @@ -272,55 +272,6 @@ impl MachineState { ) } - /* - #[inline(always)] - pub(crate) fn constant_to_literal(&self, addr: HeapCellValue) -> Literal { - read_heap_cell!(addr, - (HeapCellValueTag::Char, c) => { - Literal::Char(c) - } - (HeapCellValueTag::Fixnum, n) => { - Literal::Fixnum(n) - } - (HeapCellValueTag::F64, f) => { - Literal::Float(f.as_offset()) - } - (HeapCellValueTag::Atom, (atom, arity)) => { - debug_assert_eq!(arity, 0); - Literal::Atom(atom) - } - (HeapCellValueTag::Str, s) => { - Literal::Atom(cell_as_atom_cell!(self.heap[s]).get_name()) - } - (HeapCellValueTag::Cons, cons_ptr) => { - match_untyped_arena_ptr!(cons_ptr, - (ArenaHeaderTag::Rational, r) => { - Literal::Rational(r) - } - (ArenaHeaderTag::Integer, n) => { - let result = (&*n).try_into(); - - match result { - Ok(fixnum) => if let Ok(n) = Fixnum::build_with_checked(fixnum) { - Literal::Fixnum(n) - } else { - Literal::Integer(n) - }, - Err(_) => Literal::Integer(n) - } - } - _ => { - unreachable!() - } - ) - } - _ => { - unreachable!() - } - ) - } - */ - #[inline(always)] pub(crate) fn select_switch_on_structure_index( &self, @@ -3004,14 +2955,14 @@ impl Machine { &Instruction::UnifyConstant(v) => { match self.machine_st.mode { MachineMode::Read => { - let addr = self.machine_st.read_s(); + let (addr, s_offset_incr) = self.machine_st.read_s(); unify!(&mut self.machine_st, addr, v); if self.machine_st.fail { self.machine_st.backtrack(); continue; } else { - self.machine_st.s_offset += 1; + self.machine_st.s_offset += s_offset_incr; } } MachineMode::Write => { @@ -3025,7 +2976,7 @@ impl Machine { match self.machine_st.mode { MachineMode::Read => { let reg_addr = self.machine_st[reg]; - let value = self.machine_st.read_s(); + let (value, s_offset_incr) = self.machine_st.read_s(); unify_fn!(&mut self.machine_st, reg_addr, value); @@ -3033,7 +2984,7 @@ impl Machine { self.machine_st.backtrack(); continue; } else { - self.machine_st.s_offset += 1; + self.machine_st.s_offset += s_offset_incr; } } MachineMode::Write => { @@ -3066,13 +3017,12 @@ impl Machine { &Instruction::UnifyVariable(reg) => { match self.machine_st.mode { MachineMode::Read => { - let value = self.machine_st.read_s(); + let (value, s_offset_incr) = self.machine_st.read_s(); self.machine_st[reg] = value; - self.machine_st.s_offset += 1; + self.machine_st.s_offset += s_offset_incr; } MachineMode::Write => { let h = self.machine_st.heap.cell_len(); - push_cell!(self.machine_st, heap_loc_as_cell!(h)); self.machine_st[reg] = heap_loc_as_cell!(h); } @@ -3084,7 +3034,7 @@ impl Machine { match self.machine_st.mode { MachineMode::Read => { let reg_addr = self.machine_st[reg]; - let value = self.machine_st.read_s(); + let (value, s_offset_incr) = self.machine_st.read_s(); unify_fn!(&mut self.machine_st, reg_addr, value); @@ -3092,7 +3042,7 @@ impl Machine { self.machine_st.backtrack(); continue; } else { - self.machine_st.s_offset += 1; + self.machine_st.s_offset += s_offset_incr; } } MachineMode::Write => { diff --git a/src/machine/machine_state_impl.rs b/src/machine/machine_state_impl.rs index 1d6439d5..ede17fa6 100644 --- a/src/machine/machine_state_impl.rs +++ b/src/machine/machine_state_impl.rs @@ -342,16 +342,16 @@ impl MachineState { } // return the read value and the succeeding HeapPtr - pub(crate) fn read_s(&mut self) -> HeapCellValue { + pub(crate) fn read_s(&mut self) -> (HeapCellValue, usize) { match self.s { - HeapPtr::HeapCell(h) => self.deref(self.heap[h + self.s_offset]), + HeapPtr::HeapCell(h) => (self.deref(self.heap[h + self.s_offset]), 1), HeapPtr::PStr(byte_index) => { let mut char_iter = self.heap.char_iter(byte_index); if self.s_offset == 0 { // read the car of the list let c = char_iter.next().unwrap(); - char_as_cell!(c) + (char_as_cell!(c), c.len_utf8()) } else { // read the (self.s_offset)^{th} cdr of the list // self.s_offset is the number of bytes offset into the PStr @@ -361,11 +361,11 @@ impl MachineState { if self.heap.char_iter(new_h).next().is_some() { self.s = HeapPtr::PStr(new_h); - pstr_loc_as_cell!(new_h) + (pstr_loc_as_cell!(new_h), 0) } else { let h = Heap::pstr_tail_idx(new_h); self.s = HeapPtr::HeapCell(h); - self.deref(heap_loc_as_cell!(h)) + (self.deref(heap_loc_as_cell!(h)), 0) } } }