add support for quoted atoms to writeq

This commit is contained in:
Mark Thom
2018-07-15 17:54:37 -06:00
parent 7527bc76f8
commit f5e254dd64
6 changed files with 40 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
use prolog::ast::*;
use prolog::heap_print::*;
use prolog::machine::*;
use prolog::ordered_float::OrderedFloat;
use termion::raw::IntoRawMode;
use termion::input::TermRead;
@@ -24,17 +25,30 @@ impl fmt::Display for IndexPtr {
impl fmt::Display for Constant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn print_atom(f: &mut fmt::Formatter, atom: &ClauseName) -> fmt::Result {
let non_quoted_token = |c| {
graphic_token_char!(c) || alpha_numeric_char!(c)
};
match atom.as_str() {
";" | "!" => write!(f, "{}", atom.as_str()),
s => if s.chars().all(non_quoted_token) {
write!(f, "{}", atom.as_str())
} else {
write!(f, "{}", "'".to_owned() + atom.as_str() + "'")
}
}
}
match self {
&Constant::Atom(ref atom) =>
if atom.as_str().chars().any(|c| ".$'\" ".contains(c)) {
write!(f, "'{}'", atom.as_str())
} else {
write!(f, "{}", atom.as_str())
},
print_atom(f, atom),
&Constant::Char(c) =>
write!(f, "'{}'", c as u8),
&Constant::EmptyList =>
write!(f, "[]"),
&Constant::Number(Number::Float(ref fl)) if fl == &OrderedFloat(0f64) =>
write!(f, "0"),
&Constant::Number(ref n) =>
write!(f, "{}", n),
&Constant::String(ref s) =>