use same logic to print Chars and Atoms (#1804)
This commit is contained in:
@@ -470,6 +470,7 @@ pub fn fmt_float(mut fl: f64) -> String {
|
|||||||
pub struct HCPrinter<'a, Outputter> {
|
pub struct HCPrinter<'a, Outputter> {
|
||||||
outputter: Outputter,
|
outputter: Outputter,
|
||||||
iter: StackfulPreOrderHeapIter<'a>,
|
iter: StackfulPreOrderHeapIter<'a>,
|
||||||
|
atom_tbl: &'a mut AtomTable,
|
||||||
op_dir: &'a OpDir,
|
op_dir: &'a OpDir,
|
||||||
state_stack: Vec<TokenOrRedirect>,
|
state_stack: Vec<TokenOrRedirect>,
|
||||||
toplevel_spec: Option<DirectedOp>,
|
toplevel_spec: Option<DirectedOp>,
|
||||||
@@ -534,6 +535,7 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
|
|||||||
impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
heap: &'a mut Heap,
|
heap: &'a mut Heap,
|
||||||
|
atom_tbl: &'a mut AtomTable,
|
||||||
op_dir: &'a OpDir,
|
op_dir: &'a OpDir,
|
||||||
output: Outputter,
|
output: Outputter,
|
||||||
cell: HeapCellValue,
|
cell: HeapCellValue,
|
||||||
@@ -541,6 +543,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
HCPrinter {
|
HCPrinter {
|
||||||
outputter: output,
|
outputter: output,
|
||||||
iter: stackful_preorder_iter(heap, cell),
|
iter: stackful_preorder_iter(heap, cell),
|
||||||
|
atom_tbl,
|
||||||
op_dir,
|
op_dir,
|
||||||
state_stack: vec![],
|
state_stack: vec![],
|
||||||
toplevel_spec: None,
|
toplevel_spec: None,
|
||||||
@@ -888,7 +891,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_atom(&mut self, atom: Atom) {
|
fn print_impromptu_atom(&mut self, atom: Atom) {
|
||||||
let result = self.print_op_addendum(atom.as_str());
|
let result = self.print_op_addendum(atom.as_str());
|
||||||
|
|
||||||
push_space_if_amb!(self, result.as_str(), {
|
push_space_if_amb!(self, result.as_str(), {
|
||||||
@@ -1404,7 +1407,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
|
|
||||||
fn print_stream(&mut self, stream: Stream, max_depth: usize) {
|
fn print_stream(&mut self, stream: Stream, max_depth: usize) {
|
||||||
if let Some(alias) = stream.options().get_alias() {
|
if let Some(alias) = stream.options().get_alias() {
|
||||||
self.print_atom(alias);
|
self.print_impromptu_atom(alias);
|
||||||
} else {
|
} else {
|
||||||
let stream_atom = atom!("$stream");
|
let stream_atom = atom!("$stream");
|
||||||
|
|
||||||
@@ -1440,15 +1443,14 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
read_heap_cell!(addr,
|
let print_atom = |printer: &mut Self, name: Atom, arity: usize| {
|
||||||
(HeapCellValueTag::Atom, (name, arity)) => {
|
|
||||||
if name == atom!("[]") && arity == 0 {
|
if name == atom!("[]") && arity == 0 {
|
||||||
if !self.at_cdr("") {
|
if !printer.at_cdr("") {
|
||||||
append_str!(self, "[]");
|
append_str!(printer, "[]");
|
||||||
}
|
}
|
||||||
} else if arity > 0 {
|
} else if arity > 0 {
|
||||||
if let Some(spec) = fetch_op_spec(name, arity, self.op_dir) {
|
if let Some(spec) = fetch_op_spec(name, arity, printer.op_dir) {
|
||||||
self.handle_op_as_struct(
|
printer.handle_op_as_struct(
|
||||||
name,
|
name,
|
||||||
arity,
|
arity,
|
||||||
&op,
|
&op,
|
||||||
@@ -1458,35 +1460,45 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
max_depth,
|
max_depth,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
push_space_if_amb!(self, name.as_str(), {
|
push_space_if_amb!(printer, name.as_str(), {
|
||||||
self.format_clause(max_depth, arity, name, None);
|
printer.format_clause(max_depth, arity, name, None);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if fetch_op_spec(name, arity, self.op_dir).is_some() {
|
} else if fetch_op_spec(name, arity, printer.op_dir).is_some() {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
|
|
||||||
if let Some(ref op) = op {
|
if let Some(ref op) = op {
|
||||||
if self.outputter.ends_with(&format!(" {}", op.as_atom().as_str())) {
|
if printer.outputter.ends_with(&format!(" {}", op.as_atom().as_str())) {
|
||||||
result.push(' ');
|
result.push(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push('(');
|
result.push('(');
|
||||||
}
|
}
|
||||||
|
|
||||||
result += &self.print_op_addendum(name.as_str());
|
result += &printer.print_op_addendum(name.as_str());
|
||||||
|
|
||||||
if op.is_some() {
|
if op.is_some() {
|
||||||
result.push(')');
|
result.push(')');
|
||||||
}
|
}
|
||||||
|
|
||||||
push_space_if_amb!(self, &result, {
|
push_space_if_amb!(printer, &result, {
|
||||||
append_str!(self, &result);
|
append_str!(printer, &result);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
push_space_if_amb!(self, name.as_str(), {
|
push_space_if_amb!(printer, name.as_str(), {
|
||||||
self.print_atom(name);
|
printer.print_impromptu_atom(name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
read_heap_cell!(addr,
|
||||||
|
(HeapCellValueTag::Atom, (name, arity)) => {
|
||||||
|
print_atom(self, name, arity);
|
||||||
|
}
|
||||||
|
(HeapCellValueTag::Char, c) => {
|
||||||
|
let name = self.atom_tbl.build_with(&String::from(c));
|
||||||
|
print_atom(self, name, 0);
|
||||||
|
// print_char!(self, self.quoted, c);
|
||||||
}
|
}
|
||||||
(HeapCellValueTag::Str, s) => {
|
(HeapCellValueTag::Str, s) => {
|
||||||
let (name, arity) = cell_as_atom_cell!(self.iter.heap[s])
|
let (name, arity) = cell_as_atom_cell!(self.iter.heap[s])
|
||||||
@@ -1534,9 +1546,6 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(HeapCellValueTag::Char, c) => {
|
|
||||||
print_char!(self, self.quoted, c);
|
|
||||||
}
|
|
||||||
(HeapCellValueTag::Cons, c) => {
|
(HeapCellValueTag::Cons, c) => {
|
||||||
match_untyped_arena_ptr!(c,
|
match_untyped_arena_ptr!(c,
|
||||||
(ArenaHeaderTag::Integer, n) => {
|
(ArenaHeaderTag::Integer, n) => {
|
||||||
@@ -1549,10 +1558,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
self.print_stream(stream, max_depth);
|
self.print_stream(stream, max_depth);
|
||||||
}
|
}
|
||||||
(ArenaHeaderTag::OssifiedOpDir, _op_dir) => {
|
(ArenaHeaderTag::OssifiedOpDir, _op_dir) => {
|
||||||
self.print_atom(atom!("$ossified_op_dir"));
|
self.print_impromptu_atom(atom!("$ossified_op_dir"));
|
||||||
}
|
}
|
||||||
(ArenaHeaderTag::Dropped, _value) => {
|
(ArenaHeaderTag::Dropped, _value) => {
|
||||||
self.print_atom(atom!("$dropped_value"));
|
self.print_impromptu_atom(atom!("$dropped_value"));
|
||||||
}
|
}
|
||||||
(ArenaHeaderTag::IndexPtr, index_ptr) => {
|
(ArenaHeaderTag::IndexPtr, index_ptr) => {
|
||||||
self.print_index_ptr(*index_ptr, max_depth);
|
self.print_index_ptr(*index_ptr, max_depth);
|
||||||
@@ -1586,7 +1595,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
|||||||
|
|
||||||
while let Some(loc_data) = self.state_stack.pop() {
|
while let Some(loc_data) = self.state_stack.pop() {
|
||||||
match loc_data {
|
match loc_data {
|
||||||
TokenOrRedirect::Atom(atom) => self.print_atom(atom),
|
TokenOrRedirect::Atom(atom) => self.print_impromptu_atom(atom),
|
||||||
TokenOrRedirect::BarAsOp => append_str!(self, " | "),
|
TokenOrRedirect::BarAsOp => append_str!(self, " | "),
|
||||||
TokenOrRedirect::Char(c) => print_char!(self, self.quoted, c),
|
TokenOrRedirect::Char(c) => print_char!(self, self.quoted, c),
|
||||||
TokenOrRedirect::Op(atom, _) => self.print_op(atom.as_str()),
|
TokenOrRedirect::Op(atom, _) => self.print_op(atom.as_str()),
|
||||||
@@ -1652,6 +1661,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let printer = HCPrinter::new(
|
let printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0)
|
heap_loc_as_cell!(0)
|
||||||
@@ -1679,6 +1689,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let printer = HCPrinter::new(
|
let printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0)
|
heap_loc_as_cell!(0)
|
||||||
@@ -1701,6 +1712,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let printer = HCPrinter::new(
|
let printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0)
|
heap_loc_as_cell!(0)
|
||||||
@@ -1712,6 +1724,7 @@ mod tests {
|
|||||||
|
|
||||||
let mut printer = HCPrinter::new(
|
let mut printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0)
|
heap_loc_as_cell!(0)
|
||||||
@@ -1743,6 +1756,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let printer = HCPrinter::new(
|
let printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0),
|
heap_loc_as_cell!(0),
|
||||||
@@ -1760,6 +1774,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let printer = HCPrinter::new(
|
let printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0),
|
heap_loc_as_cell!(0),
|
||||||
@@ -1775,6 +1790,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let mut printer = HCPrinter::new(
|
let mut printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0)
|
heap_loc_as_cell!(0)
|
||||||
@@ -1805,6 +1821,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let mut printer = HCPrinter::new(
|
let mut printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0)
|
heap_loc_as_cell!(0)
|
||||||
@@ -1826,6 +1843,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let printer = HCPrinter::new(
|
let printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
pstr_loc_as_cell!(0)
|
pstr_loc_as_cell!(0)
|
||||||
@@ -1852,6 +1870,7 @@ mod tests {
|
|||||||
{
|
{
|
||||||
let printer = HCPrinter::new(
|
let printer = HCPrinter::new(
|
||||||
&mut wam.machine_st.heap,
|
&mut wam.machine_st.heap,
|
||||||
|
&mut wam.machine_st.atom_tbl,
|
||||||
&wam.op_dir,
|
&wam.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(0),
|
heap_loc_as_cell!(0),
|
||||||
|
|||||||
@@ -765,6 +765,7 @@ impl MachineState {
|
|||||||
|
|
||||||
let mut printer = HCPrinter::new(
|
let mut printer = HCPrinter::new(
|
||||||
&mut self.heap,
|
&mut self.heap,
|
||||||
|
&mut self.atom_tbl,
|
||||||
op_dir,
|
op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
term_to_be_printed,
|
term_to_be_printed,
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ impl MockWAM {
|
|||||||
|
|
||||||
let mut printer = HCPrinter::new(
|
let mut printer = HCPrinter::new(
|
||||||
&mut self.machine_st.heap,
|
&mut self.machine_st.heap,
|
||||||
|
&mut self.machine_st.atom_tbl,
|
||||||
&self.op_dir,
|
&self.op_dir,
|
||||||
PrinterOutputter::new(),
|
PrinterOutputter::new(),
|
||||||
heap_loc_as_cell!(term_write_result.heap_loc),
|
heap_loc_as_cell!(term_write_result.heap_loc),
|
||||||
|
|||||||
Reference in New Issue
Block a user