print strings as strings only in the term expander

This commit is contained in:
Mark Thom
2020-03-13 21:05:25 -06:00
parent c60a0bc062
commit beed4e8aa8
2 changed files with 36 additions and 3 deletions

View 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,12 +842,19 @@ 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>) {
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)
}));
@@ -863,6 +872,28 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
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));

View 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();