From: Mark Date: Sat, 30 Sep 2023 22:47:13 +0000 (-0600) Subject: substitute names for cyclic variables permitted by max_depth > 0 in check_for_seen... X-Git-Tag: remove~65 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=7d6ce119f59177b9c0992eba57f4e356b513a1ad;p=scryer-prolog.git substitute names for cyclic variables permitted by max_depth > 0 in check_for_seen using a loop (#2057) --- diff --git a/src/heap_print.rs b/src/heap_print.rs index 0e002c78..daf339c8 100644 --- a/src/heap_print.rs +++ b/src/heap_print.rs @@ -862,61 +862,67 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { } fn check_for_seen(&mut self, max_depth: usize) -> Option { - if let Some(cell) = self.iter.next() { - let is_cyclic = cell.get_forwarding_bit(); + if let Some(mut orig_cell) = self.iter.next() { + loop { + let is_cyclic = orig_cell.get_forwarding_bit(); - let cell = heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, cell)); - let cell = unmark_cell_bits!(cell); + let cell = heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, orig_cell)); + let cell = unmark_cell_bits!(cell); - match self.var_names.get(&cell).cloned() { - Some(var) if cell.is_var() => { - // If cell is an unbound variable and maps to - // a name via heap_locs, append the name to - // the current output, and return None. None - // short-circuits handle_heap_term. - // self.iter.pop_stack(); + match self.var_names.get(&cell).cloned() { + Some(var) if cell.is_var() => { + // If cell is an unbound variable and maps to + // a name via heap_locs, append the name to + // the current output, and return None. None + // short-circuits handle_heap_term. + // self.iter.pop_stack(); - let var_str = var.borrow().to_string(); + let var_str = var.borrow().to_string(); - push_space_if_amb!(self, &var_str, { - append_str!(self, &var_str); - }); + push_space_if_amb!(self, &var_str, { + append_str!(self, &var_str); + }); - None - } - var_opt => { - if is_cyclic && cell.is_compound(self.iter.heap) { - // self-referential variables are marked "cyclic". - match var_opt { - Some(var) => { - // If the term is bound to a named variable, - // print the variable's name to output. - let var_str = var.borrow().to_string(); - - push_space_if_amb!(self, &var_str, { - append_str!(self, &var_str); - }); - } - None => { - if self.max_depth == 0 || max_depth == 0 { - // otherwise, contract it to an ellipsis. - push_space_if_amb!(self, "...", { - append_str!(self, "..."); + return None; + } + var_opt => { + if is_cyclic && cell.is_compound(self.iter.heap) { + // self-referential variables are marked "cyclic". + match var_opt { + Some(var) => { + // If the term is bound to a named variable, + // print the variable's name to output. + let var_str = var.borrow().to_string(); + + push_space_if_amb!(self, &var_str, { + append_str!(self, &var_str); }); - } else { - debug_assert!(cell.is_ref()); + } + None => { + 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()); + + let h = cell.get_value() as usize; + self.iter.push_stack(IterStackLoc::iterable_loc(h, HeapOrStackTag::Heap)); - let h = cell.get_value() as usize; - self.iter.push_stack(IterStackLoc::iterable_loc(h, HeapOrStackTag::Heap)); - return self.iter.next(); + if let Some(cell) = self.iter.next() { + orig_cell = cell; + continue; + } + } } } + + return None; } - return None; + return Some(cell); } - - Some(cell) } } } else {