From: Mark Thom Date: Sat, 14 Mar 2020 03:05:25 +0000 (-0600) Subject: print strings as strings only in the term expander X-Git-Tag: v0.8.119~44 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=beed4e8aa8e6fa7cd10a129a99749f4e82c29ad0;p=scryer-prolog.git print strings as strings only in the term expander --- diff --git a/src/prolog/heap_print.rs b/src/prolog/heap_print.rs index 05d9c1d4..48ebb726 100644 --- a/src/prolog/heap_print.rs +++ b/src/prolog/heap_print.rs @@ -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) { - let atom = String::from_iter(s[offset ..].chars().map(|c| { + fn print_string_as_str( + &mut self, + iter: &mut HCPreOrderIterator, + offset: usize, + s: Rc, + ) { + 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) { + 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)); diff --git a/src/prolog/machine/term_expansion.rs b/src/prolog/machine/term_expansion.rs index 20ea91d6..5339a512 100644 --- a/src/prolog/machine/term_expansion.rs +++ b/src/prolog/machine/term_expansion.rs @@ -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();