]> Repositorios git - scryer-prolog.git/commitdiff
print strings as strings only in the term expander
authorMark Thom <[email protected]>
Sat, 14 Mar 2020 03:05:25 +0000 (21:05 -0600)
committerMark Thom <[email protected]>
Sat, 14 Mar 2020 03:05:25 +0000 (21:05 -0600)
src/prolog/heap_print.rs
src/prolog/machine/term_expansion.rs

index 05d9c1d40f02f5c4ebc6ffb51ebd2ce7f4234205..48ebb726bc12332913f1f363e200b381a279f459 100644 (file)
@@ -320,6 +320,7 @@ pub struct HCPrinter<'a, Outputter> {
     pub(crate) numbervars: bool,
     pub(crate) quoted: bool,
     pub(crate) ignore_ops: bool,
+    pub(crate) print_strings_as_strs: bool,
 }
 
 macro_rules! push_space_if_amb {
@@ -446,6 +447,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
             ignore_ops: false,
             cyclic_terms: IndexMap::new(),
             var_names: IndexMap::new(),
+            print_strings_as_strs: false,
         }
     }
 
@@ -840,13 +842,20 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
             Constant::Integer(n) => self.print_number(Number::Integer(n), op),
             Constant::Float(n) => self.print_number(Number::Float(n), op),
             Constant::Rational(n) => self.print_number(Number::Rational(n), op),
-            Constant::String(n, s) => self.print_string(iter, n, s),
+            Constant::String(n, s) if self.print_strings_as_strs =>
+                self.print_string_as_str(iter, n, s),
+            Constant::String(n, s) => self.print_string(n, s),
             Constant::Usize(i) => self.append_str(&format!("u{}", i)),
         }
     }
 
-    fn print_string(&mut self, iter: &mut HCPreOrderIterator, offset: usize, s: Rc<String>) {
-        let atom = String::from_iter(s[offset ..].chars().map(|c| {
+    fn print_string_as_str(
+        &mut self,
+        iter: &mut HCPreOrderIterator,
+        offset: usize,
+        s: Rc<String>,
+    ) {
+         let atom = String::from_iter(s[offset ..].chars().map(|c| {
             char_to_string(true, c)
         }));
 
@@ -862,6 +871,28 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
         iter_stack.pop();
         iter_stack.pop();
     }
+    
+    fn print_string(&mut self, offset: usize, s: Rc<String>) {
+        if !self.machine_st.machine_flags().double_quotes.is_atom() {
+            if !s[offset ..].is_empty() {
+                if self.ignore_ops {
+                    self.format_struct(2, clause_name!("."));
+                } else {
+                    self.push_list();
+                }
+            } else if !self.at_cdr("") {
+                self.append_str("[]");
+            }
+        } else {
+            let atom = String::from_iter(s[offset ..].chars().map(|c| {
+                char_to_string(self.quoted, c)
+            }));
+        
+            self.push_char('"');
+            self.append_str(&atom);
+            self.push_char('"');
+        }
+    }        
 
     fn push_list(&mut self) {
         let cell = Rc::new(Cell::new(true));
index 20ea91d68bbd71306324d7a0db0870ede7519385..5339a512ed1623ce260d3da9a6e3ea3287442852 100644 (file)
@@ -315,12 +315,14 @@ impl MachineState {
 
         printer.quoted = true;
         printer.numbervars = true;
+        
         // the purpose of the offset is to avoid clashes with variable names that might
         // occur after the addresses in the expanded term are substituted with the variable
         // names in the pre-expansion term. This formula ensures that all generated "numbervars"-
         // style variable names will be longer than the keys of the var_dict, and therefore
         // not equal to any of them.
         printer.numbervars_offset = Integer::from(10).pow(max_var_length as u32) * 26;
+        printer.print_strings_as_strs = true;
         printer.drop_toplevel_spec();
 
         printer.see_all_locs();