correctly increment s_offset for partial strings (#2897)

This commit is contained in:
Mark Thom
2025-04-20 16:53:36 -07:00
committed by Mark Thom
parent e1e52338d6
commit 6bcbb91e08
2 changed files with 13 additions and 63 deletions

View File

@@ -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)] #[inline(always)]
pub(crate) fn select_switch_on_structure_index( pub(crate) fn select_switch_on_structure_index(
&self, &self,
@@ -3004,14 +2955,14 @@ impl Machine {
&Instruction::UnifyConstant(v) => { &Instruction::UnifyConstant(v) => {
match self.machine_st.mode { match self.machine_st.mode {
MachineMode::Read => { 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); unify!(&mut self.machine_st, addr, v);
if self.machine_st.fail { if self.machine_st.fail {
self.machine_st.backtrack(); self.machine_st.backtrack();
continue; continue;
} else { } else {
self.machine_st.s_offset += 1; self.machine_st.s_offset += s_offset_incr;
} }
} }
MachineMode::Write => { MachineMode::Write => {
@@ -3025,7 +2976,7 @@ impl Machine {
match self.machine_st.mode { match self.machine_st.mode {
MachineMode::Read => { MachineMode::Read => {
let reg_addr = self.machine_st[reg]; 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); unify_fn!(&mut self.machine_st, reg_addr, value);
@@ -3033,7 +2984,7 @@ impl Machine {
self.machine_st.backtrack(); self.machine_st.backtrack();
continue; continue;
} else { } else {
self.machine_st.s_offset += 1; self.machine_st.s_offset += s_offset_incr;
} }
} }
MachineMode::Write => { MachineMode::Write => {
@@ -3066,13 +3017,12 @@ impl Machine {
&Instruction::UnifyVariable(reg) => { &Instruction::UnifyVariable(reg) => {
match self.machine_st.mode { match self.machine_st.mode {
MachineMode::Read => { 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[reg] = value;
self.machine_st.s_offset += 1; self.machine_st.s_offset += s_offset_incr;
} }
MachineMode::Write => { MachineMode::Write => {
let h = self.machine_st.heap.cell_len(); let h = self.machine_st.heap.cell_len();
push_cell!(self.machine_st, heap_loc_as_cell!(h)); push_cell!(self.machine_st, heap_loc_as_cell!(h));
self.machine_st[reg] = 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 { match self.machine_st.mode {
MachineMode::Read => { MachineMode::Read => {
let reg_addr = self.machine_st[reg]; 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); unify_fn!(&mut self.machine_st, reg_addr, value);
@@ -3092,7 +3042,7 @@ impl Machine {
self.machine_st.backtrack(); self.machine_st.backtrack();
continue; continue;
} else { } else {
self.machine_st.s_offset += 1; self.machine_st.s_offset += s_offset_incr;
} }
} }
MachineMode::Write => { MachineMode::Write => {

View File

@@ -342,16 +342,16 @@ impl MachineState {
} }
// return the read value and the succeeding HeapPtr // 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 { 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) => { HeapPtr::PStr(byte_index) => {
let mut char_iter = self.heap.char_iter(byte_index); let mut char_iter = self.heap.char_iter(byte_index);
if self.s_offset == 0 { if self.s_offset == 0 {
// read the car of the list // read the car of the list
let c = char_iter.next().unwrap(); let c = char_iter.next().unwrap();
char_as_cell!(c) (char_as_cell!(c), c.len_utf8())
} else { } else {
// read the (self.s_offset)^{th} cdr of the list // read the (self.s_offset)^{th} cdr of the list
// self.s_offset is the number of bytes offset into the PStr // 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() { if self.heap.char_iter(new_h).next().is_some() {
self.s = HeapPtr::PStr(new_h); self.s = HeapPtr::PStr(new_h);
pstr_loc_as_cell!(new_h) (pstr_loc_as_cell!(new_h), 0)
} else { } else {
let h = Heap::pstr_tail_idx(new_h); let h = Heap::pstr_tail_idx(new_h);
self.s = HeapPtr::HeapCell(h); self.s = HeapPtr::HeapCell(h);
self.deref(heap_loc_as_cell!(h)) (self.deref(heap_loc_as_cell!(h)), 0)
} }
} }
} }