]> Repositorios git - scryer-prolog.git/commitdiff
fix toplevel heap view
authorMark Thom <[email protected]>
Sun, 24 Mar 2019 15:55:20 +0000 (09:55 -0600)
committerMark Thom <[email protected]>
Sun, 24 Mar 2019 15:55:20 +0000 (09:55 -0600)
Cargo.toml
src/prolog/machine/mod.rs
src/prolog/write.rs
src/tests.rs

index bee4e8c4dc5634e120ffa3af5c80f1d24e6d55f5..3e537a6b2f80b51a7333d9437506b20ee874b865 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "scryer-prolog"
-version = "0.8.15"
+version = "0.8.16"
 authors = ["Mark Thom <[email protected]>"]
 repository = "https://github.com/mthom/scryer-prolog"
 description = "A modern Prolog implementation written mostly in Rust."
index 01e8527ea77271c48e6fad9567aa5b813ed20c8a..ccdb2623bca510019cd925ce38ec3406a1dafb51 100644 (file)
@@ -249,17 +249,7 @@ impl Machine {
 
         Ok(())
     }
-    
-    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 new_addr.is_ref() {
-                    heap_locs.remove(var);
-                }
-            }
-        }
-    }
-    
+        
     pub fn add_batched_code(&mut self, code: Code, code_dir: CodeDir)
     {
         // error detection has finished, so update the master index of keys.
@@ -422,7 +412,27 @@ impl Machine {
         }
     }
 
-    pub fn heap_view<Outputter>(&self, var_dir: &HeapVarDict, mut output: Outputter) -> Outputter
+    pub fn toplevel_heap_view<Outputter>(&self, var_dir: &HeapVarDict, mut output: Outputter) -> Outputter
+       where Outputter: HCValueOutputter
+    {
+        let mut sorted_vars: Vec<(&Rc<Var>, &Addr)> = var_dir.iter().collect();
+        sorted_vars.sort_by_key(|ref v| v.0);
+
+        for (var, addr) in sorted_vars {
+            let addr = self.machine_st.store(self.machine_st.deref(addr.clone()));
+
+            if addr.is_ref() {
+                continue;
+            }
+            
+            output = self.machine_st.print_var_eq(var.clone(), addr, var_dir, output);
+        }
+
+        output
+    }
+
+    #[cfg(test)]
+    pub fn test_heap_view<Outputter>(&self, var_dir: &HeapVarDict, mut output: Outputter) -> Outputter
        where Outputter: HCValueOutputter
     {
         let mut sorted_vars: Vec<(&Rc<Var>, &Addr)> = var_dir.iter().collect();
index c7da956e7bfbb3a7495f606d4da568eb6b3b4cb6..721394d9f1a0b36a47643a6f61dae5cd0ad105b6 100644 (file)
@@ -371,15 +371,11 @@ fn next_step(mut stdout: RawTerminal<std::io::Stdout>) -> ContinueResult
 
 pub fn print(wam: &mut Machine, result: EvalSession) {
     match result {
-        EvalSession::InitialQuerySuccess(alloc_locs, mut heap_locs) => {
-            let orig_heap_locs = heap_locs.clone();
-
+        EvalSession::InitialQuerySuccess(alloc_locs, mut heap_locs) =>
             loop {
-                wam.remove_unbound_vars(&orig_heap_locs, &mut heap_locs);
-
                 if wam.or_stack_is_empty() {
                     if heap_locs.is_empty() {
-                        let attr_goals = wam.attribute_goals(&orig_heap_locs);
+                        let attr_goals = wam.attribute_goals(&heap_locs);
 
                         if !attr_goals.is_empty() {
                             println!("{}.", attr_goals);
@@ -389,29 +385,30 @@ pub fn print(wam: &mut Machine, result: EvalSession) {
 
                         return;
                     }
-                } else if heap_locs.is_empty() && orig_heap_locs.is_empty() {
+                } else if heap_locs.is_empty() {
                     print!("true");
                     stdout().flush().unwrap();
                 }
 
                 let mut raw_stdout = stdout().into_raw_mode().unwrap();
 
-                if !heap_locs.is_empty() {
+                let bindings = if !heap_locs.is_empty() {
                     let mut output = PrinterOutputter::new();
-                    let bindings = wam.heap_view(&heap_locs, output).result();
-
-                    write!(raw_stdout, "{}", bindings).unwrap();
-                    raw_stdout.flush().unwrap();
-                }
+                    wam.toplevel_heap_view(&heap_locs, output).result()
+                } else {
+                    "".to_string()
+                };
 
-                let attr_goals = wam.attribute_goals(&orig_heap_locs);
+                let attr_goals = wam.attribute_goals(&heap_locs);
 
                 if !attr_goals.is_empty() {
-                    if heap_locs.is_empty() {
+                    if bindings.is_empty() {
                         write!(raw_stdout, "{}", attr_goals).unwrap();
                     } else {
-                        write!(raw_stdout, ", {}", attr_goals).unwrap();
+                        write!(raw_stdout, "{}, {}", bindings, attr_goals).unwrap();
                     }
+                } else {
+                    write!(raw_stdout, "{}", bindings).unwrap();
                 }
 
                 if !wam.or_stack_is_empty() {
@@ -440,7 +437,7 @@ pub fn print(wam: &mut Machine, result: EvalSession) {
                         return;
                     }
                 } else {
-                    if heap_locs.is_empty() && attr_goals.is_empty() {
+                    if bindings.is_empty() && attr_goals.is_empty() {
                         write!(raw_stdout, "true.\r\n").unwrap();
                     } else {
                         write!(raw_stdout, ".\r\n").unwrap();
@@ -448,8 +445,7 @@ pub fn print(wam: &mut Machine, result: EvalSession) {
 
                     break;
                 }
-            }
-        },
+            },        
         EvalSession::Error(e) => println!("{}", e),
         _ => {}
     };
index 1804324fc99165dadfe56f57eb739397c7621b5e..c499f0913299de192f143f749ff31eec7c65f184 100644 (file)
@@ -86,12 +86,12 @@ pub fn collect_test_output(wam: &mut Machine, alloc_locs: AllocVarDict, mut heap
 {
     let mut output = TestOutputter::new();
 
-    output = wam.heap_view(&heap_locs, output);
+    output = wam.test_heap_view(&heap_locs, output);
     output.cache();
 
     while let EvalSession::SubsequentQuerySuccess = wam.continue_query(&alloc_locs, &mut heap_locs)
     {
-        output = wam.heap_view(&heap_locs, output);
+        output = wam.test_heap_view(&heap_locs, output);
         output.cache();
     }
 
@@ -104,7 +104,7 @@ pub fn collect_test_output_with_limit(wam: &mut Machine, alloc_locs: AllocVarDic
 {
     let mut output = TestOutputter::new();
 
-    output = wam.heap_view(&heap_locs, output);
+    output = wam.test_heap_view(&heap_locs, output);
     output.cache();
 
     let mut count  = 1;
@@ -115,7 +115,7 @@ pub fn collect_test_output_with_limit(wam: &mut Machine, alloc_locs: AllocVarDic
 
     while let EvalSession::SubsequentQuerySuccess = wam.continue_query(&alloc_locs, &mut heap_locs)
     {
-        output = wam.heap_view(&heap_locs, output);
+        output = wam.test_heap_view(&heap_locs, output);
         output.cache();
 
         count += 1;