Skip to content

Commit

Permalink
correct depth calculation for lists that are their own car (#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthom committed Sep 30, 2023
1 parent 7d6ce11 commit b065e1c
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/heap_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
)
}

fn check_for_seen(&mut self, max_depth: usize) -> Option<HeapCellValue> {
fn check_for_seen(&mut self, max_depth: &mut usize) -> Option<HeapCellValue> {
if let Some(mut orig_cell) = self.iter.next() {
loop {
let is_cyclic = orig_cell.get_forwarding_bit();
Expand Down Expand Up @@ -899,14 +899,26 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
});
}
None => {
if self.max_depth == 0 || max_depth == 0 {
if self.max_depth == 0 || *max_depth == 0 {
// otherwise, contract it to an ellipsis.
push_space_if_amb!(self, "...", {
append_str!(self, "...");
});
} else {
debug_assert!(cell.is_ref());

// as usual, the WAM's
// optimization of the Lis tag
// (conflating the location of
// the list and that of its
// first element) needs
// special consideration here
// lest we find ourselves in
// an infinite loop.
if cell.get_tag() == HeapCellValueTag::Lis {
*max_depth -= 1;
}

let h = cell.get_value() as usize;
self.iter.push_stack(IterStackLoc::iterable_loc(h, HeapOrStackTag::Heap));

Expand Down Expand Up @@ -1363,7 +1375,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {

self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth));
self.state_stack.push(TokenOrRedirect::HeadTailSeparator); // bar
self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth));
self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth + 1));

self.open_list(switch);
}
Expand Down Expand Up @@ -1563,10 +1575,15 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
&mut self,
op: Option<DirectedOp>,
is_functor_redirect: bool,
max_depth: usize,
mut max_depth: usize,
) {
let negated_operand = negated_op_needs_bracketing(&self.iter, self.op_dir, &op);

let addr = match self.check_for_seen(&mut max_depth) {
Some(addr) => addr,
None => return,
};

let print_struct = |printer: &mut Self, name: Atom, arity: usize| {
if name == atom!("[]") && arity == 0 {
match printer.state_stack.last() {
Expand Down Expand Up @@ -1628,11 +1645,6 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
}
};

let addr = match self.check_for_seen(max_depth) {
Some(addr) => addr,
None => return,
};

if !addr.is_var()
&& !addr.is_compound(&self.iter.heap)
&& self.max_depth_exhausted(max_depth)
Expand Down

0 comments on commit b065e1c

Please sign in to comment.