add display predicate

This commit is contained in:
Mark Thom
2018-01-14 22:11:02 -07:00
parent 58ba368280
commit 08b94dcdf5
9 changed files with 80 additions and 32 deletions

View File

@@ -2,6 +2,8 @@ use prolog::and_stack::*;
use prolog::ast::*;
use prolog::builtins::*;
use prolog::copier::*;
use prolog::heap_iter::*;
use prolog::heap_print::*;
use prolog::num::{Integer, ToPrimitive, Zero};
use prolog::num::bigint::{BigInt, BigUint};
use prolog::num::rational::Ratio;
@@ -254,7 +256,29 @@ impl MachineState {
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) {
let mut pdl = vec![a1, a2];
@@ -1498,6 +1522,18 @@ impl MachineState {
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) =>
self.try_execute_predicate(code_dir, name.clone(), arity),
&ControlInstruction::ExecuteN(arity) =>

View File

@@ -1,7 +1,6 @@
use prolog::ast::*;
use prolog::builtins::*;
use prolog::codegen::*;
use prolog::heap_iter::*;
use prolog::heap_print::*;
use prolog::fixtures::*;
@@ -265,7 +264,9 @@ impl Machine {
let h = self.ms.heap.h;
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 {
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 {
let mut result = String::new();
@@ -378,7 +357,7 @@ impl Machine {
result += var.as_str();
result += " = ";
result += self.print_term(addr).as_str();
result += self.ms.print_term(addr, TermFormatter {}).as_str();
}
result