move print_constant logic to heap_print.rs
This commit is contained in:
@@ -134,7 +134,6 @@ The following predicates are built-in to rusty-wam.
|
|||||||
* `compare/3`
|
* `compare/3`
|
||||||
* `compound/1`
|
* `compound/1`
|
||||||
* `cyclic_term/1`
|
* `cyclic_term/1`
|
||||||
* `display/1`
|
|
||||||
* `duplicate_term/2`
|
* `duplicate_term/2`
|
||||||
* `false/0`
|
* `false/0`
|
||||||
* `float/1`
|
* `float/1`
|
||||||
@@ -158,6 +157,7 @@ The following predicates are built-in to rusty-wam.
|
|||||||
* `throw/1`
|
* `throw/1`
|
||||||
* `true/0`
|
* `true/0`
|
||||||
* `var/1`
|
* `var/1`
|
||||||
|
* `writeq/1`
|
||||||
|
|
||||||
## Tutorial
|
## Tutorial
|
||||||
To enter a multi-clause predicate, the brackets ":{" and "}:" are used
|
To enter a multi-clause predicate, the brackets ":{" and "}:" are used
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use prolog::ast::*;
|
use prolog::ast::*;
|
||||||
use prolog::heap_iter::*;
|
use prolog::heap_iter::*;
|
||||||
use prolog::machine::machine_state::MachineState;
|
use prolog::machine::machine_state::MachineState;
|
||||||
|
use prolog::ordered_float::OrderedFloat;
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
@@ -233,9 +234,26 @@ impl<'a, Formatter: HCValueFormatter, Outputter: HCValueOutputter>
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn print_atom(&mut self, atom: &ClauseName) {
|
||||||
|
let non_quoted_token = |c| {
|
||||||
|
graphic_token_char!(c) || alpha_numeric_char!(c)
|
||||||
|
};
|
||||||
|
|
||||||
|
match atom.as_str() {
|
||||||
|
";" | "!" => self.outputter.append(atom.as_str()),
|
||||||
|
s => if s.chars().all(non_quoted_token) {
|
||||||
|
self.outputter.append(atom.as_str());
|
||||||
|
} else {
|
||||||
|
self.outputter.append(&("'".to_owned() + atom.as_str() + "'"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn print_constant(&mut self, c: Constant) {
|
fn print_constant(&mut self, c: Constant) {
|
||||||
match c {
|
match c {
|
||||||
|
Constant::Atom(ref atom) =>
|
||||||
|
self.print_atom(atom),
|
||||||
Constant::Char(c) if c == '\n' =>
|
Constant::Char(c) if c == '\n' =>
|
||||||
self.outputter.append("'\\n'"),
|
self.outputter.append("'\\n'"),
|
||||||
Constant::Char(c) if c == '\r' =>
|
Constant::Char(c) if c == '\r' =>
|
||||||
@@ -255,8 +273,23 @@ impl<'a, Formatter: HCValueFormatter, Outputter: HCValueOutputter>
|
|||||||
self.outputter.push_char(c);
|
self.outputter.push_char(c);
|
||||||
self.outputter.append("'");
|
self.outputter.append("'");
|
||||||
},
|
},
|
||||||
_ =>
|
Constant::EmptyList =>
|
||||||
self.outputter.append(format!("{}", c).as_str())
|
self.outputter.append("[]"),
|
||||||
|
Constant::Number(Number::Float(fl)) =>
|
||||||
|
if &fl == &OrderedFloat(0f64) {
|
||||||
|
self.outputter.append("0");
|
||||||
|
} else {
|
||||||
|
self.outputter.append(&format!("{}", fl));
|
||||||
|
},
|
||||||
|
Constant::Number(n) =>
|
||||||
|
self.outputter.append(&format!("{}", n)),
|
||||||
|
Constant::String(s) => {
|
||||||
|
self.outputter.append("\"");
|
||||||
|
self.outputter.append(s.as_str());
|
||||||
|
self.outputter.append("\"");
|
||||||
|
},
|
||||||
|
Constant::Usize(i) =>
|
||||||
|
self.outputter.append(&format!("u{}", i))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,32 +23,25 @@ impl fmt::Display for IndexPtr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ClauseName {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Constant {
|
impl fmt::Display for Constant {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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 {
|
match self {
|
||||||
&Constant::Atom(ref atom) =>
|
&Constant::Atom(ref atom) =>
|
||||||
print_atom(f, atom),
|
if atom.as_str().chars().any(|c| "`.$'\" ".contains(c)) {
|
||||||
|
write!(f, "'{}'", atom.as_str())
|
||||||
|
} else {
|
||||||
|
write!(f, "{}", atom.as_str())
|
||||||
|
},
|
||||||
&Constant::Char(c) =>
|
&Constant::Char(c) =>
|
||||||
write!(f, "'{}'", c as u8),
|
write!(f, "'{}'", c as u8),
|
||||||
&Constant::EmptyList =>
|
&Constant::EmptyList =>
|
||||||
write!(f, "[]"),
|
write!(f, "[]"),
|
||||||
&Constant::Number(Number::Float(ref fl)) if fl == &OrderedFloat(0f64) =>
|
|
||||||
write!(f, "0"),
|
|
||||||
&Constant::Number(ref n) =>
|
&Constant::Number(ref n) =>
|
||||||
write!(f, "{}", n),
|
write!(f, "{}", n),
|
||||||
&Constant::String(ref s) =>
|
&Constant::String(ref s) =>
|
||||||
@@ -59,12 +52,6 @@ impl fmt::Display for Constant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for ClauseName {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "{}", self.as_str())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for FactInstruction {
|
impl fmt::Display for FactInstruction {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
|||||||
@@ -17,12 +17,12 @@ pub mod copier;
|
|||||||
pub mod debray_allocator;
|
pub mod debray_allocator;
|
||||||
pub mod fixtures;
|
pub mod fixtures;
|
||||||
pub mod heap_iter;
|
pub mod heap_iter;
|
||||||
pub mod heap_print;
|
|
||||||
pub mod indexing;
|
pub mod indexing;
|
||||||
|
pub mod io;
|
||||||
pub mod iterators;
|
pub mod iterators;
|
||||||
pub mod or_stack;
|
pub mod or_stack;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
pub mod io;
|
pub mod heap_print;
|
||||||
pub mod targets;
|
pub mod targets;
|
||||||
pub mod tabled_rc;
|
pub mod tabled_rc;
|
||||||
|
|||||||
@@ -1607,7 +1607,7 @@ fn test_queries_on_setup_call_cleanup()
|
|||||||
assert_prolog_failure!(&mut wam,
|
assert_prolog_failure!(&mut wam,
|
||||||
"?- setup_call_cleanup(S=1,(G=2;G=3), writeq(S+G>B)), B=4, !, throw(x).");
|
"?- setup_call_cleanup(S=1,(G=2;G=3), writeq(S+G>B)), B=4, !, throw(x).");
|
||||||
assert_prolog_success!(&mut wam,
|
assert_prolog_success!(&mut wam,
|
||||||
"?- setup_call_cleanup(true, (X=1;X=2), writeq(a)), setup_call_cleanup(true,(Y=1;Y=2),writeq(b)), !.",
|
"?- setup_call_cleanup(true, (X=1;X=2), writeq(a)), setup_call_cleanup(true,(Y=1;Y=2),writeq(b)), !.",
|
||||||
[["Y = 1", "X = 1"]]);
|
[["Y = 1", "X = 1"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- catch(setup_call_cleanup(true,throw(goal),throw(cl)), Pat, true).",
|
assert_prolog_success!(&mut wam, "?- catch(setup_call_cleanup(true,throw(goal),throw(cl)), Pat, true).",
|
||||||
[["Pat = goal"]]);
|
[["Pat = goal"]]);
|
||||||
|
|||||||
Reference in New Issue
Block a user