add support for quoted atoms to writeq
This commit is contained in:
@@ -795,7 +795,7 @@ pub enum BuiltInClauseType {
|
|||||||
Compare,
|
Compare,
|
||||||
CompareTerm(CompareTermQT),
|
CompareTerm(CompareTermQT),
|
||||||
CyclicTerm,
|
CyclicTerm,
|
||||||
Display,
|
Writeq,
|
||||||
DuplicateTerm,
|
DuplicateTerm,
|
||||||
Eq,
|
Eq,
|
||||||
Functor,
|
Functor,
|
||||||
@@ -898,7 +898,7 @@ impl BuiltInClauseType {
|
|||||||
&BuiltInClauseType::Compare => clause_name!("compare"),
|
&BuiltInClauseType::Compare => clause_name!("compare"),
|
||||||
&BuiltInClauseType::CompareTerm(qt) => clause_name!(qt.name()),
|
&BuiltInClauseType::CompareTerm(qt) => clause_name!(qt.name()),
|
||||||
&BuiltInClauseType::CyclicTerm => clause_name!("cyclic_term"),
|
&BuiltInClauseType::CyclicTerm => clause_name!("cyclic_term"),
|
||||||
&BuiltInClauseType::Display => clause_name!("display"),
|
&BuiltInClauseType::Writeq => clause_name!("writeq"),
|
||||||
&BuiltInClauseType::DuplicateTerm => clause_name!("duplicate_term"),
|
&BuiltInClauseType::DuplicateTerm => clause_name!("duplicate_term"),
|
||||||
&BuiltInClauseType::Eq => clause_name!("=="),
|
&BuiltInClauseType::Eq => clause_name!("=="),
|
||||||
&BuiltInClauseType::Functor => clause_name!("functor"),
|
&BuiltInClauseType::Functor => clause_name!("functor"),
|
||||||
@@ -917,7 +917,7 @@ impl BuiltInClauseType {
|
|||||||
&BuiltInClauseType::Compare => 2,
|
&BuiltInClauseType::Compare => 2,
|
||||||
&BuiltInClauseType::CompareTerm(_) => 2,
|
&BuiltInClauseType::CompareTerm(_) => 2,
|
||||||
&BuiltInClauseType::CyclicTerm => 1,
|
&BuiltInClauseType::CyclicTerm => 1,
|
||||||
&BuiltInClauseType::Display => 1,
|
&BuiltInClauseType::Writeq => 1,
|
||||||
&BuiltInClauseType::DuplicateTerm => 2,
|
&BuiltInClauseType::DuplicateTerm => 2,
|
||||||
&BuiltInClauseType::Eq => 2,
|
&BuiltInClauseType::Eq => 2,
|
||||||
&BuiltInClauseType::Functor => 3,
|
&BuiltInClauseType::Functor => 3,
|
||||||
@@ -941,7 +941,7 @@ impl BuiltInClauseType {
|
|||||||
("@=<", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::LessThanOrEqual)),
|
("@=<", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::LessThanOrEqual)),
|
||||||
("\\=@=", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::NotEqual)),
|
("\\=@=", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::NotEqual)),
|
||||||
("=@=", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::Equal)),
|
("=@=", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::Equal)),
|
||||||
("display", 1) => Some(BuiltInClauseType::Display),
|
("writeq", 1) => Some(BuiltInClauseType::Writeq),
|
||||||
("duplicate_term", 2) => Some(BuiltInClauseType::DuplicateTerm),
|
("duplicate_term", 2) => Some(BuiltInClauseType::DuplicateTerm),
|
||||||
("==", 2) => Some(BuiltInClauseType::Eq),
|
("==", 2) => Some(BuiltInClauseType::Eq),
|
||||||
("functor", 3) => Some(BuiltInClauseType::Functor),
|
("functor", 3) => Some(BuiltInClauseType::Functor),
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use prolog::ast::*;
|
use prolog::ast::*;
|
||||||
use prolog::heap_print::*;
|
use prolog::heap_print::*;
|
||||||
use prolog::machine::*;
|
use prolog::machine::*;
|
||||||
|
use prolog::ordered_float::OrderedFloat;
|
||||||
|
|
||||||
use termion::raw::IntoRawMode;
|
use termion::raw::IntoRawMode;
|
||||||
use termion::input::TermRead;
|
use termion::input::TermRead;
|
||||||
@@ -24,17 +25,30 @@ impl fmt::Display for IndexPtr {
|
|||||||
|
|
||||||
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) =>
|
||||||
if atom.as_str().chars().any(|c| ".$'\" ".contains(c)) {
|
print_atom(f, atom),
|
||||||
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) =>
|
||||||
|
|||||||
@@ -492,7 +492,7 @@ pub(crate) trait CallPolicy: Any {
|
|||||||
machine_st.fail = !machine_st.is_cyclic_term(addr);
|
machine_st.fail = !machine_st.is_cyclic_term(addr);
|
||||||
return_from_clause!(machine_st.last_call, machine_st)
|
return_from_clause!(machine_st.last_call, machine_st)
|
||||||
},
|
},
|
||||||
&BuiltInClauseType::Display => {
|
&BuiltInClauseType::Writeq => {
|
||||||
let output = machine_st.print_term(machine_st[temp_v!(1)].clone(),
|
let output = machine_st.print_term(machine_st[temp_v!(1)].clone(),
|
||||||
DisplayFormatter {},
|
DisplayFormatter {},
|
||||||
PrinterOutputter::new());
|
PrinterOutputter::new());
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ pub mod fixtures;
|
|||||||
pub mod heap_iter;
|
pub mod heap_iter;
|
||||||
pub mod heap_print;
|
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 targets;
|
pub mod targets;
|
||||||
pub mod tabled_rc;
|
pub mod tabled_rc;
|
||||||
|
|||||||
Submodule src/prolog/parser updated: a7b81b591b...6a7545257b
28
src/tests.rs
28
src/tests.rs
@@ -1207,8 +1207,8 @@ fn test_queries_on_conditionals()
|
|||||||
{
|
{
|
||||||
let mut wam = Machine::new();
|
let mut wam = Machine::new();
|
||||||
|
|
||||||
submit(&mut wam, "test(A) :- ( A =:= 2 -> display(\"A is 2\")
|
submit(&mut wam, "test(A) :- ( A =:= 2 -> writeq(\"A is 2\")
|
||||||
; A =:= 3 -> display(\"A is 3\")
|
; A =:= 3 -> writeq(\"A is 3\")
|
||||||
; A = \"not 2 or 3\"
|
; A = \"not 2 or 3\"
|
||||||
).");
|
).");
|
||||||
|
|
||||||
@@ -1551,9 +1551,9 @@ fn test_queries_on_builtins()
|
|||||||
[["G = 2", "B = 3"]]);
|
[["G = 2", "B = 3"]]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
assert_prolog_success!(&mut wam, "?- call_with_inference_limit((setup_call_cleanup(S=1,(G=2;fail),display(S+G>B)), B=3, !), 100, R).",
|
assert_prolog_success!(&mut wam, "?- call_with_inference_limit((setup_call_cleanup(S=1,(G=2;fail),writeq(S+G>B)), B=3, !), 100, R).",
|
||||||
[["G = 2", "B = 3", "R = !", "S = 1"]]);
|
[["G = 2", "B = 3", "R = !", "S = 1"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- call_with_inference_limit((setup_call_cleanup(S=1,(G=2;fail),display(S+G>B)), B=3, !), 10, R).",
|
assert_prolog_success!(&mut wam, "?- call_with_inference_limit((setup_call_cleanup(S=1,(G=2;fail),writeq(S+G>B)), B=3, !), 10, R).",
|
||||||
[["S = _1", "G = _4", "B = _14", "R = inference_limit_exceeded"]]);
|
[["S = _1", "G = _4", "B = _14", "R = inference_limit_exceeded"]]);
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
@@ -1582,32 +1582,32 @@ fn test_queries_on_setup_call_cleanup()
|
|||||||
[["S = 1", "G = 2", "C = 3"]]);
|
[["S = 1", "G = 2", "C = 3"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- setup_call_cleanup((S=1;S=2), G=3, C=4).",
|
assert_prolog_success!(&mut wam, "?- setup_call_cleanup((S=1;S=2), G=3, C=4).",
|
||||||
[["S = 1", "G = 3", "C = 4"]]);
|
[["S = 1", "G = 3", "C = 4"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- setup_call_cleanup(S=1, G=2, display(S+G)).",
|
assert_prolog_success!(&mut wam, "?- setup_call_cleanup(S=1, G=2, writeq(S+G)).",
|
||||||
[["S = 1", "G = 2"]]);
|
[["S = 1", "G = 2"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- setup_call_cleanup(S=1, (G=2;G=3), display(S+G)).",
|
assert_prolog_success!(&mut wam, "?- setup_call_cleanup(S=1, (G=2;G=3), writeq(S+G)).",
|
||||||
[["S = 1", "G = 2"],
|
[["S = 1", "G = 2"],
|
||||||
["S = 1", "G = 3"]]);
|
["S = 1", "G = 3"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- setup_call_cleanup(S=1, G=2, display(S+G>A+B)), A=3, B=4.",
|
assert_prolog_success!(&mut wam, "?- setup_call_cleanup(S=1, G=2, writeq(S+G>A+B)), A=3, B=4.",
|
||||||
[["S = 1", "G = 2", "A = 3", "B = 4"]]);
|
[["S = 1", "G = 2", "A = 3", "B = 4"]]);
|
||||||
assert_prolog_success!(&mut wam,
|
assert_prolog_success!(&mut wam,
|
||||||
"?- catch(setup_call_cleanup(S=1, (G=2;G=3,throw(x)), display(S+G)), E, true).",
|
"?- catch(setup_call_cleanup(S=1, (G=2;G=3,throw(x)), writeq(S+G)), E, true).",
|
||||||
[["S = 1", "G = 2", "E = _26"], ["G = _4", "E = x", "S = _1"]]);
|
[["S = 1", "G = 2", "E = _26"], ["G = _4", "E = x", "S = _1"]]);
|
||||||
assert_prolog_success!(&mut wam,
|
assert_prolog_success!(&mut wam,
|
||||||
"?- setup_call_cleanup(S=1, (G=2;G=3),display(S+G>B)), B=4, !.",
|
"?- setup_call_cleanup(S=1, (G=2;G=3),writeq(S+G>B)), B=4, !.",
|
||||||
[["S = 1", "B = 4", "G = 2"]]);
|
[["S = 1", "B = 4", "G = 2"]]);
|
||||||
assert_prolog_success!(&mut wam,
|
assert_prolog_success!(&mut wam,
|
||||||
"?- setup_call_cleanup(S=1,G=2,display(S+G>B)),B=3,!.",
|
"?- setup_call_cleanup(S=1,G=2,writeq(S+G>B)),B=3,!.",
|
||||||
[["S = 1", "G = 2", "B = 3"]]);
|
[["S = 1", "G = 2", "B = 3"]]);
|
||||||
assert_prolog_success!(&mut wam,
|
assert_prolog_success!(&mut wam,
|
||||||
"?- setup_call_cleanup(S=1,(G=2;false),display(S+G>B)),B=3,!.",
|
"?- setup_call_cleanup(S=1,(G=2;false),writeq(S+G>B)),B=3,!.",
|
||||||
[["S = 1", "G = 2", "B = 3"]]);
|
[["S = 1", "G = 2", "B = 3"]]);
|
||||||
assert_prolog_success!(&mut wam,
|
assert_prolog_success!(&mut wam,
|
||||||
"?- setup_call_cleanup(S=1,(G=2;S=2),display(S+G>B)), B=3, !.",
|
"?- setup_call_cleanup(S=1,(G=2;S=2),writeq(S+G>B)), B=3, !.",
|
||||||
[["S = 1", "B = 3", "G = 2"]]);
|
[["S = 1", "B = 3", "G = 2"]]);
|
||||||
assert_prolog_failure!(&mut wam,
|
assert_prolog_failure!(&mut wam,
|
||||||
"?- setup_call_cleanup(S=1,(G=2;G=3), display(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), display(a)), setup_call_cleanup(true,(Y=1;Y=2),display(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