From 11198fdb31d622657e0dacb33aacdb1d4224834f Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 24 Mar 2019 00:26:29 -0600 Subject: [PATCH] print attribute goals alongside variable bindings as a single, unified goals --- src/prolog/lib/freeze.pl | 2 +- src/prolog/machine/mod.rs | 11 +++++------ src/prolog/write.rs | 39 ++++++++++++++++++++++++++------------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/prolog/lib/freeze.pl b/src/prolog/lib/freeze.pl index 2eb24951..d3a92eaf 100644 --- a/src/prolog/lib/freeze.pl +++ b/src/prolog/lib/freeze.pl @@ -22,7 +22,7 @@ freeze(X, Goal) :- gather_freeze_goals(Attrs, _, _) :- var(Attrs), !. -gather_freeze_goals([frozen(X) | _], Var, [frozen(Var, X) | _]) :- +gather_freeze_goals([frozen(X) | _], Var, [freeze(Var, X) | _]) :- !. gather_freeze_goals([_ | Attrs], Var, Goals) :- gather_freeze_goals(Attrs, Var, Goals). diff --git a/src/prolog/machine/mod.rs b/src/prolog/machine/mod.rs index 584aad87..01e8527e 100644 --- a/src/prolog/machine/mod.rs +++ b/src/prolog/machine/mod.rs @@ -250,13 +250,12 @@ impl Machine { Ok(()) } - pub fn remove_unbound_vars(&self, heap_locs: &mut HeapVarDict) { - for (var, addr) in heap_locs.clone() { + pub fn remove_unbound_vars(&self, orig_heap_locs: &HeapVarDict, heap_locs: &mut HeapVarDict) { + for (var, addr) in orig_heap_locs.iter() { match self.machine_st.store(self.machine_st.deref(addr.clone())) { - new_addr => - if addr.is_ref() && new_addr == addr { - heap_locs.remove(&var); - } + new_addr => if new_addr.is_ref() { + heap_locs.remove(var); + } } } } diff --git a/src/prolog/write.rs b/src/prolog/write.rs index 78c59ba8..c7da956e 100644 --- a/src/prolog/write.rs +++ b/src/prolog/write.rs @@ -371,33 +371,46 @@ fn next_step(mut stdout: RawTerminal) -> ContinueResult pub fn print(wam: &mut Machine, result: EvalSession) { match result { - EvalSession::InitialQuerySuccess(alloc_locs, mut heap_locs) => { + EvalSession::InitialQuerySuccess(alloc_locs, mut heap_locs) => { + let orig_heap_locs = heap_locs.clone(); + loop { - wam.remove_unbound_vars(&mut heap_locs); - + wam.remove_unbound_vars(&orig_heap_locs, &mut heap_locs); + if wam.or_stack_is_empty() { if heap_locs.is_empty() { - println!("true."); + let attr_goals = wam.attribute_goals(&orig_heap_locs); + + if !attr_goals.is_empty() { + println!("{}.", attr_goals); + } else { + println!("true."); + } + return; } - } else if heap_locs.is_empty() { + } else if heap_locs.is_empty() && orig_heap_locs.is_empty() { print!("true"); stdout().flush().unwrap(); } - + let mut raw_stdout = stdout().into_raw_mode().unwrap(); if !heap_locs.is_empty() { - let mut output = PrinterOutputter::new(); + let mut output = PrinterOutputter::new(); let bindings = wam.heap_view(&heap_locs, output).result(); - + write!(raw_stdout, "{}", bindings).unwrap(); raw_stdout.flush().unwrap(); + } - let attr_goals = wam.attribute_goals(&heap_locs); + let attr_goals = wam.attribute_goals(&orig_heap_locs); - if !attr_goals.is_empty() { - write!(raw_stdout, "\r\n{}\r\n", attr_goals).unwrap(); + if !attr_goals.is_empty() { + if heap_locs.is_empty() { + write!(raw_stdout, "{}", attr_goals).unwrap(); + } else { + write!(raw_stdout, ", {}", attr_goals).unwrap(); } } @@ -425,9 +438,9 @@ pub fn print(wam: &mut Machine, result: EvalSession) { write!(raw_stdout, "{}\r\n", error_string(e)).unwrap(); raw_stdout.flush().unwrap(); return; - } + } } else { - if heap_locs.is_empty() { + if heap_locs.is_empty() && attr_goals.is_empty() { write!(raw_stdout, "true.\r\n").unwrap(); } else { write!(raw_stdout, ".\r\n").unwrap(); -- 2.54.0