From: Mark Thom Date: Thu, 20 Dec 2018 04:25:06 +0000 (-0700) Subject: fix ambiguity_check X-Git-Tag: v0.8.110~306 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=40fa795d49262c9f15fd14d4a976e870870c913e;p=scryer-prolog.git fix ambiguity_check --- diff --git a/src/prolog/heap_print.rs b/src/prolog/heap_print.rs index 8e1bcc5e..efca3742 100644 --- a/src/prolog/heap_print.rs +++ b/src/prolog/heap_print.rs @@ -53,7 +53,7 @@ pub trait HCValueOutputter { fn result(self) -> Self::Output; fn ends_with(&self, &str) -> bool; fn len(&self) -> usize; - fn truncate(&mut self, usize); + fn truncate(&mut self, usize); } pub struct PrinterOutputter { @@ -151,7 +151,12 @@ pub struct HCPrinter<'a, Outputter> { macro_rules! push_space_if_amb { ($self:expr, $atom:expr, $op:expr, $action:block) => ( match $self.ambiguity_check($atom, $op) { - Some(DirectedOp::Left(..)) => { + Some(DirectedOp::Left(_, false)) => { + $self.outputter.push_char(' '); + $action; + }, + Some(DirectedOp::Left(lop, true)) => { + $self.outputter.insert_from_end(lop.as_str().len(), ' '); $self.outputter.push_char(' '); $action; }, @@ -254,18 +259,34 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> ignore_ops: false } } + pub fn from_heap_locs(machine_st: &'a MachineState, output: Outputter, + heap_locs: &'a HeapVarDict) + -> Self + { + let mut printer = Self::new(machine_st, output); + + printer.heap_locs = reverse_heap_locs(machine_st, heap_locs); + printer + } + + #[inline] + pub fn see_all_locs(&mut self) { + for key in self.heap_locs.keys().cloned() { + self.printed_vars.insert(key); + } + } + // return op itself if there is an ambiguity to indicate the direction the op // lies, None otherwise. - fn ambiguity_check(&mut self, atom: &str, op: &Option) -> Option + fn ambiguity_check(&self, atom: &str, op: &Option) -> Option { match op { &Some(DirectedOp::Left(ref lop, false)) if continues_with_append(lop.as_str(), atom) => Some(DirectedOp::Left(lop.clone(), false)), &Some(DirectedOp::Left(ref lop, true)) => if self.outputter.ends_with(&format!(" {}", lop.as_str())) { - Some(DirectedOp::Left(lop.clone(), true)) + Some(DirectedOp::Left(lop.clone(), false)) } else if continues_with_append(lop.as_str(), atom) { - self.outputter.insert_from_end(lop.as_str().len(), ' '); Some(DirectedOp::Left(lop.clone(), true)) } else { None @@ -277,22 +298,6 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> } } - pub fn from_heap_locs(machine_st: &'a MachineState, output: Outputter, - heap_locs: &'a HeapVarDict) - -> Self - { - let mut printer = Self::new(machine_st, output); - - printer.heap_locs = reverse_heap_locs(machine_st, heap_locs); - printer - } - - #[inline] - pub fn see_all_locs(&mut self) { - for key in self.heap_locs.keys().cloned() { - self.printed_vars.insert(key); - } - } fn enqueue_op(&mut self, ct: ClauseType, fixity: Fixity) { match fixity { @@ -449,7 +454,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> if self.outputter.ends_with(&format!(" {}", op.as_str())) { self.outputter.push_char(' '); } - + self.outputter.push_char('('); } @@ -547,10 +552,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> if let Some(ref op) = op { self.state_stack.push(TokenOrRedirect::Open); - + if self.outputter.ends_with(&format!(" {}", op.as_str())) { self.state_stack.push(TokenOrRedirect::Space); - } + } } }, HeapCellValue::NamedStr(0, name, fixity) =>