use ellipses to indicate infinite terms in printer
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "scryer-prolog"
|
name = "scryer-prolog"
|
||||||
version = "0.8.65"
|
version = "0.8.66"
|
||||||
authors = ["Mark Thom <markjordanthom@gmail.com>"]
|
authors = ["Mark Thom <markjordanthom@gmail.com>"]
|
||||||
repository = "https://github.com/mthom/scryer-prolog"
|
repository = "https://github.com/mthom/scryer-prolog"
|
||||||
description = "A modern Prolog implementation written mostly in Rust."
|
description = "A modern Prolog implementation written mostly in Rust."
|
||||||
|
|||||||
@@ -293,6 +293,7 @@ pub struct HCPrinter<'a, Outputter> {
|
|||||||
heap_locs: ReverseHeapVarDict,
|
heap_locs: ReverseHeapVarDict,
|
||||||
printed_vars: HashSet<Addr>,
|
printed_vars: HashSet<Addr>,
|
||||||
last_item_idx: usize,
|
last_item_idx: usize,
|
||||||
|
cyclic_terms: HashMap<Addr, usize>,
|
||||||
pub(crate) numbervars_offset: BigInt,
|
pub(crate) numbervars_offset: BigInt,
|
||||||
pub(crate) numbervars: bool,
|
pub(crate) numbervars: bool,
|
||||||
pub(crate) quoted: bool,
|
pub(crate) quoted: bool,
|
||||||
@@ -407,7 +408,8 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter>
|
|||||||
numbervars: false,
|
numbervars: false,
|
||||||
numbervars_offset: BigInt::zero(),
|
numbervars_offset: BigInt::zero(),
|
||||||
quoted: false,
|
quoted: false,
|
||||||
ignore_ops: false }
|
ignore_ops: false,
|
||||||
|
cyclic_terms: HashMap::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_heap_locs(machine_st: &'a MachineState, op_dir: &'a OpDir, output: Outputter,
|
pub fn from_heap_locs(machine_st: &'a MachineState, op_dir: &'a OpDir, output: Outputter,
|
||||||
@@ -601,24 +603,26 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter>
|
|||||||
return None;
|
return None;
|
||||||
},
|
},
|
||||||
None => if self.machine_st.is_cyclic_term(addr.clone()) {
|
None => if self.machine_st.is_cyclic_term(addr.clone()) {
|
||||||
if self.printed_vars.contains(&addr) {
|
match self.cyclic_terms.get(&addr).cloned() {
|
||||||
iter.stack().pop();
|
Some(reps) =>
|
||||||
|
if reps > 0 {
|
||||||
|
self.cyclic_terms.insert(addr, reps - 1);
|
||||||
|
iter.next()
|
||||||
|
} else {
|
||||||
|
if !self.at_cdr(", ...") {
|
||||||
|
push_space_if_amb!(self, "...", {
|
||||||
|
self.append_str("...");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(offset_str) = self.offset_as_string(addr) {
|
iter.stack().pop();
|
||||||
push_space_if_amb!(self, &offset_str, {
|
self.cyclic_terms.remove(&addr);
|
||||||
self.append_str(&offset_str);
|
None
|
||||||
});
|
},
|
||||||
|
None => {
|
||||||
|
self.cyclic_terms.insert(addr, 2);
|
||||||
|
iter.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
if let Some(s) = self.offset_as_string(addr.clone()) {
|
|
||||||
let var = Rc::new(s);
|
|
||||||
self.heap_locs.insert(addr.clone(), var);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.printed_vars.insert(addr);
|
|
||||||
iter.next()
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
iter.next()
|
iter.next()
|
||||||
@@ -720,16 +724,16 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter>
|
|||||||
if self.outputter.ends_with(&format!(" {}", op.as_str())) {
|
if self.outputter.ends_with(&format!(" {}", op.as_str())) {
|
||||||
result.push(' ');
|
result.push(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push('(');
|
result.push('(');
|
||||||
}
|
}
|
||||||
|
|
||||||
result += &self.print_op_addendum(atom.as_str());
|
result += &self.print_op_addendum(atom.as_str());
|
||||||
|
|
||||||
if op.is_some() {
|
if op.is_some() {
|
||||||
result.push(')');
|
result.push(')');
|
||||||
}
|
}
|
||||||
|
|
||||||
push_space_if_amb!(self, &result, {
|
push_space_if_amb!(self, &result, {
|
||||||
self.append_str(&result);
|
self.append_str(&result);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1526,7 +1526,7 @@ fn test_queries_on_builtins()
|
|||||||
assert_prolog_success!(&mut wam, "X = g(X, Y), Y = f(X), copy_term(Y, f(Z)).",
|
assert_prolog_success!(&mut wam, "X = g(X, Y), Y = f(X), copy_term(Y, f(Z)).",
|
||||||
[["Y = f(g(X, Y))", "X = g(X, f(X))", "Z = g(Z, f(Z))"]]);
|
[["Y = f(g(X, Y))", "X = g(X, f(X))", "Z = g(Z, f(Z))"]]);
|
||||||
assert_prolog_success!(&mut wam, "X = g(X, Y), Y = f(X), copy_term(Y, V).",
|
assert_prolog_success!(&mut wam, "X = g(X, Y), Y = f(X), copy_term(Y, V).",
|
||||||
[["Y = f(g(X, Y))", "X = g(X, f(X))", "V = f(g(_9, V))"]]);
|
[["V = f(g(g(g(..., V), V), V))", "X = g(X, f(X))", "Y = f(g(X, Y))"]]);
|
||||||
assert_prolog_success!(&mut wam, "f(Y,Y,[X,a,[],Y]) = Term, copy_term(Term, NewTerm).",
|
assert_prolog_success!(&mut wam, "f(Y,Y,[X,a,[],Y]) = Term, copy_term(Term, NewTerm).",
|
||||||
[["NewTerm = f(_16, _16, [_19, a, [], _16])",
|
[["NewTerm = f(_16, _16, [_19, a, [], _16])",
|
||||||
"Term = f(_0, Y, [_6, a, [], Y])",
|
"Term = f(_0, Y, [_6, a, [], Y])",
|
||||||
@@ -1638,7 +1638,7 @@ fn test_queries_on_builtins()
|
|||||||
|
|
||||||
assert_prolog_failure!(&mut wam, "Pairs = [a-a|Pairs], keysort(Pairs, _).");
|
assert_prolog_failure!(&mut wam, "Pairs = [a-a|Pairs], keysort(Pairs, _).");
|
||||||
assert_prolog_success!(&mut wam, "Pairs = [a-a|Pairs], catch(keysort(Pairs, _), error(E, _), true).",
|
assert_prolog_success!(&mut wam, "Pairs = [a-a|Pairs], catch(keysort(Pairs, _), error(E, _), true).",
|
||||||
[["E = type_error(list, [a-a | _25])", "Pairs = [a-a | Pairs]"]]);
|
[["E = type_error(list, [a-a, a-a, a-a, ...])", "Pairs = [a-a | Pairs]"]]);
|
||||||
|
|
||||||
assert_prolog_success!(&mut wam, "keysort([], L).",
|
assert_prolog_success!(&mut wam, "keysort([], L).",
|
||||||
[["L = []"]]);
|
[["L = []"]]);
|
||||||
|
|||||||
Reference in New Issue
Block a user