add display predicate
This commit is contained in:
@@ -84,6 +84,7 @@ The following predicates are built-in to rusty-wam.
|
|||||||
* `atomic/1`
|
* `atomic/1`
|
||||||
* `call/N` (1 <= N <= 63)
|
* `call/N` (1 <= N <= 63)
|
||||||
* `catch/3`
|
* `catch/3`
|
||||||
|
* `display/1`
|
||||||
* `duplicate_term/2`
|
* `duplicate_term/2`
|
||||||
* `false/0`
|
* `false/0`
|
||||||
* `functor/3`
|
* `functor/3`
|
||||||
|
|||||||
@@ -335,8 +335,9 @@ pub enum CompareNumberQT {
|
|||||||
pub enum QueryTerm {
|
pub enum QueryTerm {
|
||||||
Arg(Vec<Box<Term>>),
|
Arg(Vec<Box<Term>>),
|
||||||
CallN(Vec<Box<Term>>),
|
CallN(Vec<Box<Term>>),
|
||||||
Catch(Vec<Box<Term>>),
|
Catch(Vec<Box<Term>>),
|
||||||
Cut,
|
Cut,
|
||||||
|
Display(Vec<Box<Term>>),
|
||||||
Functor(Vec<Box<Term>>),
|
Functor(Vec<Box<Term>>),
|
||||||
Inlined(InlinedQueryTerm),
|
Inlined(InlinedQueryTerm),
|
||||||
Is(Vec<Box<Term>>),
|
Is(Vec<Box<Term>>),
|
||||||
@@ -350,6 +351,7 @@ impl QueryTerm {
|
|||||||
&QueryTerm::Arg(_) => 3,
|
&QueryTerm::Arg(_) => 3,
|
||||||
&QueryTerm::Catch(_) => 3,
|
&QueryTerm::Catch(_) => 3,
|
||||||
&QueryTerm::Throw(_) => 1,
|
&QueryTerm::Throw(_) => 1,
|
||||||
|
&QueryTerm::Display(_) => 1,
|
||||||
&QueryTerm::Functor(_) => 3,
|
&QueryTerm::Functor(_) => 3,
|
||||||
&QueryTerm::Inlined(ref term) => term.arity(),
|
&QueryTerm::Inlined(ref term) => term.arity(),
|
||||||
&QueryTerm::Is(_) => 2,
|
&QueryTerm::Is(_) => 2,
|
||||||
@@ -747,6 +749,8 @@ pub enum ControlInstruction {
|
|||||||
CatchCall,
|
CatchCall,
|
||||||
CatchExecute,
|
CatchExecute,
|
||||||
Deallocate,
|
Deallocate,
|
||||||
|
DisplayCall,
|
||||||
|
DisplayExecute,
|
||||||
Execute(Rc<Atom>, usize),
|
Execute(Rc<Atom>, usize),
|
||||||
ExecuteN(usize),
|
ExecuteN(usize),
|
||||||
FunctorCall,
|
FunctorCall,
|
||||||
@@ -767,6 +771,8 @@ impl ControlInstruction {
|
|||||||
&ControlInstruction::Call(_, _, _) => true,
|
&ControlInstruction::Call(_, _, _) => true,
|
||||||
&ControlInstruction::CatchCall => true,
|
&ControlInstruction::CatchCall => true,
|
||||||
&ControlInstruction::CatchExecute => true,
|
&ControlInstruction::CatchExecute => true,
|
||||||
|
&ControlInstruction::DisplayCall => true,
|
||||||
|
&ControlInstruction::DisplayExecute => true,
|
||||||
&ControlInstruction::Execute(_, _) => true,
|
&ControlInstruction::Execute(_, _) => true,
|
||||||
&ControlInstruction::CallN(_) => true,
|
&ControlInstruction::CallN(_) => true,
|
||||||
&ControlInstruction::ExecuteN(_) => true,
|
&ControlInstruction::ExecuteN(_) => true,
|
||||||
|
|||||||
@@ -247,6 +247,8 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker>
|
|||||||
},
|
},
|
||||||
&QueryTerm::Catch(_) =>
|
&QueryTerm::Catch(_) =>
|
||||||
code.push(Line::Control(ControlInstruction::CatchCall)),
|
code.push(Line::Control(ControlInstruction::CatchCall)),
|
||||||
|
&QueryTerm::Display(_) =>
|
||||||
|
code.push(Line::Control(ControlInstruction::DisplayCall)),
|
||||||
&QueryTerm::Functor(_) =>
|
&QueryTerm::Functor(_) =>
|
||||||
code.push(Line::Control(ControlInstruction::FunctorCall)),
|
code.push(Line::Control(ControlInstruction::FunctorCall)),
|
||||||
&QueryTerm::Inlined(_) =>
|
&QueryTerm::Inlined(_) =>
|
||||||
@@ -281,6 +283,8 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker>
|
|||||||
*ctrl = ControlInstruction::Execute(name, arity),
|
*ctrl = ControlInstruction::Execute(name, arity),
|
||||||
ControlInstruction::CallN(arity) =>
|
ControlInstruction::CallN(arity) =>
|
||||||
*ctrl = ControlInstruction::ExecuteN(arity),
|
*ctrl = ControlInstruction::ExecuteN(arity),
|
||||||
|
ControlInstruction::DisplayCall =>
|
||||||
|
*ctrl = ControlInstruction::DisplayExecute,
|
||||||
ControlInstruction::FunctorCall =>
|
ControlInstruction::FunctorCall =>
|
||||||
*ctrl = ControlInstruction::FunctorExecute,
|
*ctrl = ControlInstruction::FunctorExecute,
|
||||||
ControlInstruction::CatchCall =>
|
ControlInstruction::CatchCall =>
|
||||||
|
|||||||
@@ -44,10 +44,19 @@ pub trait HeapCellValueFormatter {
|
|||||||
pub struct DisplayFormatter {}
|
pub struct DisplayFormatter {}
|
||||||
|
|
||||||
impl HeapCellValueFormatter for DisplayFormatter {
|
impl HeapCellValueFormatter for DisplayFormatter {
|
||||||
fn format_clause(&self, arity: usize, name: Rc<Atom>, _: Option<Fixity>,
|
fn format_clause(&self, arity: usize, name: Rc<Atom>, fixity: Option<Fixity>,
|
||||||
state_stack: &mut Vec<TokenOrRedirect>)
|
state_stack: &mut Vec<TokenOrRedirect>)
|
||||||
{
|
{
|
||||||
self.format_struct(arity, name, state_stack);
|
if fixity.is_some() {
|
||||||
|
let mut new_name = String::from("'");
|
||||||
|
new_name += name.as_ref();
|
||||||
|
new_name += "'";
|
||||||
|
|
||||||
|
let name = Rc::new(new_name);
|
||||||
|
self.format_struct(arity, name, state_stack);
|
||||||
|
} else {
|
||||||
|
self.format_struct(arity, name, state_stack);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -109,13 +109,17 @@ impl fmt::Display for ControlInstruction {
|
|||||||
&ControlInstruction::CatchCall =>
|
&ControlInstruction::CatchCall =>
|
||||||
write!(f, "call_catch"),
|
write!(f, "call_catch"),
|
||||||
&ControlInstruction::CatchExecute =>
|
&ControlInstruction::CatchExecute =>
|
||||||
write!(f, "execute_catch"),
|
write!(f, "execute_catch"),
|
||||||
|
&ControlInstruction::DisplayCall =>
|
||||||
|
write!(f, "call_display"),
|
||||||
|
&ControlInstruction::DisplayExecute =>
|
||||||
|
write!(f, "execute_display"),
|
||||||
&ControlInstruction::ExecuteN(arity) =>
|
&ControlInstruction::ExecuteN(arity) =>
|
||||||
write!(f, "execute_N {}", arity),
|
write!(f, "execute_N {}", arity),
|
||||||
&ControlInstruction::FunctorCall =>
|
&ControlInstruction::FunctorCall =>
|
||||||
write!(f, "functor_call"),
|
write!(f, "call_functor"),
|
||||||
&ControlInstruction::FunctorExecute =>
|
&ControlInstruction::FunctorExecute =>
|
||||||
write!(f, "functor_execute"),
|
write!(f, "execute_functor"),
|
||||||
&ControlInstruction::Deallocate =>
|
&ControlInstruction::Deallocate =>
|
||||||
write!(f, "deallocate"),
|
write!(f, "deallocate"),
|
||||||
&ControlInstruction::Execute(ref name, arity) =>
|
&ControlInstruction::Execute(ref name, arity) =>
|
||||||
|
|||||||
@@ -40,6 +40,10 @@ impl<'a> QueryIterator<'a> {
|
|||||||
let state = TermIterState::Clause(0, ClauseType::Catch, terms);
|
let state = TermIterState::Clause(0, ClauseType::Catch, terms);
|
||||||
QueryIterator { state_stack: vec![state] }
|
QueryIterator { state_stack: vec![state] }
|
||||||
},
|
},
|
||||||
|
&QueryTerm::Display(ref terms) => {
|
||||||
|
let state = TermIterState::Clause(0, ClauseType::Root, terms);
|
||||||
|
QueryIterator { state_stack: vec![state] }
|
||||||
|
},
|
||||||
&QueryTerm::Arg(ref terms)
|
&QueryTerm::Arg(ref terms)
|
||||||
| &QueryTerm::Functor(ref terms) => {
|
| &QueryTerm::Functor(ref terms) => {
|
||||||
let state = TermIterState::Clause(0, ClauseType::Root, terms);
|
let state = TermIterState::Clause(0, ClauseType::Root, terms);
|
||||||
@@ -277,6 +281,11 @@ impl<'a> ChunkedIterator<'a>
|
|||||||
arity = child_terms.len();
|
arity = child_terms.len();
|
||||||
break;
|
break;
|
||||||
},
|
},
|
||||||
|
&QueryTerm::Display(_) => {
|
||||||
|
result.push(term);
|
||||||
|
arity = 1;
|
||||||
|
break;
|
||||||
|
},
|
||||||
&QueryTerm::Arg(_)
|
&QueryTerm::Arg(_)
|
||||||
| &QueryTerm::Functor(_) => {
|
| &QueryTerm::Functor(_) => {
|
||||||
result.push(term);
|
result.push(term);
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ use prolog::and_stack::*;
|
|||||||
use prolog::ast::*;
|
use prolog::ast::*;
|
||||||
use prolog::builtins::*;
|
use prolog::builtins::*;
|
||||||
use prolog::copier::*;
|
use prolog::copier::*;
|
||||||
|
use prolog::heap_iter::*;
|
||||||
|
use prolog::heap_print::*;
|
||||||
use prolog::num::{Integer, ToPrimitive, Zero};
|
use prolog::num::{Integer, ToPrimitive, Zero};
|
||||||
use prolog::num::bigint::{BigInt, BigUint};
|
use prolog::num::bigint::{BigInt, BigUint};
|
||||||
use prolog::num::rational::Ratio;
|
use prolog::num::rational::Ratio;
|
||||||
@@ -254,7 +256,29 @@ impl MachineState {
|
|||||||
|
|
||||||
self.trail(r1);
|
self.trail(r1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn print_var<Fmt>(&self, r: Ref, fmt: Fmt) -> String
|
||||||
|
where Fmt: HeapCellValueFormatter
|
||||||
|
{
|
||||||
|
let iter = HeapCellIterator::new(&self, r);
|
||||||
|
let mut printer = HeapCellPrinter::new(iter, fmt);
|
||||||
|
|
||||||
|
printer.print()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn print_term<Fmt>(&self, addr: &Addr, fmt: Fmt) -> String
|
||||||
|
where Fmt: HeapCellValueFormatter
|
||||||
|
{
|
||||||
|
match addr {
|
||||||
|
&Addr::Con(ref c) =>
|
||||||
|
format!("{}", c),
|
||||||
|
&Addr::Lis(h) | &Addr::HeapCell(h) | &Addr::Str(h) =>
|
||||||
|
self.print_var(Ref::HeapCell(h), fmt),
|
||||||
|
&Addr::StackCell(fr, sc) =>
|
||||||
|
self.print_var(Ref::StackCell(fr, sc), fmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn unify(&mut self, a1: Addr, a2: Addr) {
|
fn unify(&mut self, a1: Addr, a2: Addr) {
|
||||||
let mut pdl = vec![a1, a2];
|
let mut pdl = vec![a1, a2];
|
||||||
|
|
||||||
@@ -1498,6 +1522,18 @@ impl MachineState {
|
|||||||
|
|
||||||
self.p += 1;
|
self.p += 1;
|
||||||
},
|
},
|
||||||
|
&ControlInstruction::DisplayCall => {
|
||||||
|
let result = self.print_term(&self[temp_v!(1)], DisplayFormatter {});
|
||||||
|
println!("{}", result);
|
||||||
|
|
||||||
|
self.p += 1;
|
||||||
|
},
|
||||||
|
&ControlInstruction::DisplayExecute => {
|
||||||
|
let result = self.print_term(&self[temp_v!(1)], DisplayFormatter {});
|
||||||
|
println!("{}", result);
|
||||||
|
|
||||||
|
self.p = self.cp;
|
||||||
|
},
|
||||||
&ControlInstruction::Execute(ref name, arity) =>
|
&ControlInstruction::Execute(ref name, arity) =>
|
||||||
self.try_execute_predicate(code_dir, name.clone(), arity),
|
self.try_execute_predicate(code_dir, name.clone(), arity),
|
||||||
&ControlInstruction::ExecuteN(arity) =>
|
&ControlInstruction::ExecuteN(arity) =>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use prolog::ast::*;
|
use prolog::ast::*;
|
||||||
use prolog::builtins::*;
|
use prolog::builtins::*;
|
||||||
use prolog::codegen::*;
|
use prolog::codegen::*;
|
||||||
use prolog::heap_iter::*;
|
|
||||||
use prolog::heap_print::*;
|
use prolog::heap_print::*;
|
||||||
use prolog::fixtures::*;
|
use prolog::fixtures::*;
|
||||||
|
|
||||||
@@ -265,7 +264,9 @@ impl Machine {
|
|||||||
let h = self.ms.heap.h;
|
let h = self.ms.heap.h;
|
||||||
self.ms.copy_and_align_ball_to_heap();
|
self.ms.copy_and_align_ball_to_heap();
|
||||||
|
|
||||||
EvalSession::QueryFailureWithException(self.print_term(&Addr::HeapCell(h)))
|
let msg = self.ms.print_term(&Addr::HeapCell(h), TermFormatter {});
|
||||||
|
|
||||||
|
EvalSession::QueryFailureWithException(msg)
|
||||||
} else {
|
} else {
|
||||||
EvalSession::QueryFailure
|
EvalSession::QueryFailure
|
||||||
}
|
}
|
||||||
@@ -345,28 +346,6 @@ impl Machine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_var(&self, r: Ref) -> String
|
|
||||||
{
|
|
||||||
let disp = TermFormatter {};
|
|
||||||
let iter = HeapCellIterator::new(&self.ms, r);
|
|
||||||
|
|
||||||
let mut printer = HeapCellPrinter::new(iter, disp);
|
|
||||||
|
|
||||||
printer.print()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_term(&self, addr: &Addr) -> String
|
|
||||||
{
|
|
||||||
match addr {
|
|
||||||
&Addr::Con(ref c) =>
|
|
||||||
format!("{}", c),
|
|
||||||
&Addr::Lis(h) | &Addr::HeapCell(h) | &Addr::Str(h) =>
|
|
||||||
self.print_var(Ref::HeapCell(h)),
|
|
||||||
&Addr::StackCell(fr, sc) =>
|
|
||||||
self.print_var(Ref::StackCell(fr, sc))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn heap_view(&self, var_dir: &HeapVarDict) -> String {
|
pub fn heap_view(&self, var_dir: &HeapVarDict) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
|
|
||||||
@@ -378,7 +357,7 @@ impl Machine {
|
|||||||
result += var.as_str();
|
result += var.as_str();
|
||||||
result += " = ";
|
result += " = ";
|
||||||
|
|
||||||
result += self.print_term(addr).as_str();
|
result += self.ms.print_term(addr, TermFormatter {}).as_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|||||||
Submodule src/prolog/parser updated: 159f82cd83...d2d6cd53af
Reference in New Issue
Block a user