Skip to content

Commit

Permalink
use copy_term/3 to print residual goals (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthom committed Apr 5, 2020
1 parent 141f3bc commit bbf1aa1
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/prolog/machine/copier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,27 @@ impl<T: CopierTarget> CopyTermState<T> {
&mut self.target[scan]
}

fn trail_list_cell(&mut self, addr: usize, threshold: usize) {
let trail_item = mem::replace(
&mut self.target[addr],
HeapCellValue::Addr(Addr::Lis(threshold)),
);

self.trail.push((
Ref::HeapCell(addr),
trail_item,
));
}

fn copy_list(&mut self, addr: usize) {
if let Addr::Lis(h) = self.target[addr + 1].as_addr(addr + 1) {
if h >= self.old_h {
*self.value_at_scan() = HeapCellValue::Addr(Addr::Lis(h));
self.scan += 1;
for offset in 0 .. 2 {
if let Addr::Lis(h) = self.target[addr + offset].as_addr(addr + offset) {
if h >= self.old_h {
*self.value_at_scan() = HeapCellValue::Addr(Addr::Lis(h));
self.scan += 1;

return;
return;
}
}
}

Expand All @@ -74,15 +88,14 @@ impl<T: CopierTarget> CopyTermState<T> {

let cdr = self.target.store(self.target.deref(Addr::HeapCell(addr + 1)));

if let Addr::Lis(_) = cdr {
let tail_addr = self.target[addr + 1].as_addr(addr + 1);

self.trail.push((
Ref::HeapCell(addr + 1),
HeapCellValue::Addr(tail_addr),
));
if !cdr.is_ref() {
self.trail_list_cell(addr + 1, threshold);
} else {
let car = self.target.store(self.target.deref(Addr::HeapCell(addr)));

self.target[addr + 1] = HeapCellValue::Addr(Addr::Lis(threshold));
if !car.is_ref() {
self.trail_list_cell(addr, threshold);
}
}

self.scan += 1;
Expand Down Expand Up @@ -163,7 +176,7 @@ impl<T: CopierTarget> CopyTermState<T> {
};

self.target[frontier] = HeapCellValue::Addr(Addr::HeapCell(threshold));
self.target[h] = HeapCellValue::Addr(Addr::HeapCell(frontier));
self.target[h] = HeapCellValue::Addr(Addr::HeapCell(threshold));

self.trail.push((
Ref::AttrVar(h),
Expand Down Expand Up @@ -271,6 +284,9 @@ impl<T: CopierTarget> CopyTermState<T> {
*self.value_at_scan() = HeapCellValue::Addr(addr);
}
}
Addr::Lis(h) if h >= self.old_h => {
self.scan += 1;
}
Addr::Lis(h) => {
self.copy_list(h);
}
Expand Down

0 comments on commit bbf1aa1

Please sign in to comment.