Resolve lints and format
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use crate::arena::*;
|
||||
use crate::atom_table::*;
|
||||
use crate::parser::ast::*;
|
||||
use crate::parser::dashu::{ibig, Integer, Rational};
|
||||
use crate::parser::dashu::base::RemEuclid;
|
||||
use crate::parser::dashu::integer::Sign;
|
||||
use crate::parser::dashu::{ibig, Integer, Rational};
|
||||
use crate::{
|
||||
alpha_numeric_char, capital_letter_char, cut_char, decimal_digit_char, graphic_token_char,
|
||||
is_fx, is_infix, is_postfix, is_prefix, is_xf, is_xfx, is_xfy, is_yfx, semicolon_char,
|
||||
@@ -20,6 +20,7 @@ use crate::machine::stack::*;
|
||||
use crate::machine::streams::*;
|
||||
use crate::types::*;
|
||||
|
||||
use dashu::base::Signed;
|
||||
use ordered_float::OrderedFloat;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
@@ -258,9 +259,7 @@ pub(crate) fn requires_space(atom: &str, op: &str) -> bool {
|
||||
oc == '(' || alpha_numeric_char!(oc)
|
||||
} else if graphic_token_char!(ac) {
|
||||
graphic_token_char!(oc)
|
||||
} else if variable_indicator_char!(ac) {
|
||||
alpha_numeric_char!(oc)
|
||||
} else if capital_letter_char!(ac) {
|
||||
} else if variable_indicator_char!(ac) || capital_letter_char!(ac) {
|
||||
alpha_numeric_char!(oc)
|
||||
} else if sign_char!(ac) {
|
||||
sign_char!(oc) || decimal_digit_char!(oc)
|
||||
@@ -277,7 +276,7 @@ pub(crate) fn requires_space(atom: &str, op: &str) -> bool {
|
||||
|
||||
fn non_quoted_graphic_token<Iter: Iterator<Item = char>>(mut iter: Iter, c: char) -> bool {
|
||||
if c == '/' {
|
||||
return match iter.next() {
|
||||
match iter.next() {
|
||||
None => true,
|
||||
Some('*') => false, // if we start with comment token, we must quote.
|
||||
Some(c) => {
|
||||
@@ -287,9 +286,9 @@ fn non_quoted_graphic_token<Iter: Iterator<Item = char>>(mut iter: Iter, c: char
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
} else if c == '.' {
|
||||
return match iter.next() {
|
||||
match iter.next() {
|
||||
None => false,
|
||||
Some(c) => {
|
||||
if graphic_token_char!(c) {
|
||||
@@ -298,7 +297,7 @@ fn non_quoted_graphic_token<Iter: Iterator<Item = char>>(mut iter: Iter, c: char
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
} else {
|
||||
iter.all(|c| graphic_token_char!(c))
|
||||
}
|
||||
@@ -310,9 +309,7 @@ pub(super) fn non_quoted_token<Iter: Iterator<Item = char>>(mut iter: Iter) -> b
|
||||
iter.all(|c| alpha_numeric_char!(c))
|
||||
} else if graphic_token_char!(c) {
|
||||
non_quoted_graphic_token(iter, c)
|
||||
} else if semicolon_char!(c) {
|
||||
iter.next().is_none()
|
||||
} else if cut_char!(c) {
|
||||
} else if semicolon_char!(c) || cut_char!(c) {
|
||||
iter.next().is_none()
|
||||
} else if c == '[' {
|
||||
iter.next() == Some(']') && iter.next().is_none()
|
||||
@@ -328,6 +325,7 @@ pub(super) fn non_quoted_token<Iter: Iterator<Item = char>>(mut iter: Iter) -> b
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::len_without_is_empty)]
|
||||
pub trait HCValueOutputter {
|
||||
type Output;
|
||||
|
||||
@@ -370,7 +368,7 @@ impl HCValueOutputter for PrinterOutputter {
|
||||
}
|
||||
|
||||
fn begin_new_var(&mut self) {
|
||||
if self.contents.len() != 0 {
|
||||
if !self.contents.is_empty() {
|
||||
self.contents += ", ";
|
||||
}
|
||||
}
|
||||
@@ -415,9 +413,9 @@ fn negated_op_needs_bracketing(
|
||||
op.is_negative_sign()
|
||||
&& iter.leftmost_leaf_has_property(op_dir, |addr| match Number::try_from(addr) {
|
||||
Ok(Number::Fixnum(n)) => n.get_num() > 0,
|
||||
Ok(Number::Float(f)) => f > OrderedFloat(0f64),
|
||||
Ok(Number::Integer(n)) => &*n > &Integer::from(0),
|
||||
Ok(Number::Rational(n)) => &*n > &Rational::from(0),
|
||||
Ok(Number::Float(OrderedFloat(f))) => f > 0f64,
|
||||
Ok(Number::Integer(n)) => n.is_positive(),
|
||||
Ok(Number::Rational(n)) => n.is_positive(),
|
||||
_ => false,
|
||||
})
|
||||
} else {
|
||||
@@ -537,20 +535,10 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
|
||||
}
|
||||
|
||||
match Number::try_from(addr) {
|
||||
Ok(Number::Fixnum(n)) => {
|
||||
if n.get_num() >= 0 {
|
||||
Some(numbervar(offset + Integer::from(n.get_num())))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Ok(Number::Integer(n)) => {
|
||||
if &*n >= &Integer::from(0) {
|
||||
Some(numbervar(Integer::from(offset + &*n)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
Ok(Number::Fixnum(n)) if n.get_num() >= 0 => {
|
||||
Some(numbervar(offset + Integer::from(n.get_num())))
|
||||
}
|
||||
Ok(Number::Integer(n)) if !n.is_negative() => Some(numbervar(Integer::from(offset + &*n))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -640,12 +628,9 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
self.state_stack.push(TokenOrRedirect::Op(name, spec));
|
||||
}
|
||||
} else {
|
||||
match &*name.as_str() {
|
||||
"|" => {
|
||||
self.format_bar_separator_op(max_depth, name, spec);
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
if let "|" = &*name.as_str() {
|
||||
self.format_bar_separator_op(max_depth, name, spec);
|
||||
return;
|
||||
};
|
||||
|
||||
if self.max_depth_exhausted(max_depth) {
|
||||
@@ -657,22 +642,19 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
if is_xfy!(spec.get_spec()) {
|
||||
let left_directed_op = DirectedOp::Left(name, spec);
|
||||
|
||||
self.state_stack.push(TokenOrRedirect::CompositeRedirect(
|
||||
0,
|
||||
left_directed_op,
|
||||
));
|
||||
self.state_stack
|
||||
.push(TokenOrRedirect::CompositeRedirect(0, left_directed_op));
|
||||
|
||||
self.state_stack.push(TokenOrRedirect::Op(name, spec));
|
||||
self.state_stack.push(TokenOrRedirect::StackPop);
|
||||
} else { // is_yfx!
|
||||
} else {
|
||||
// is_yfx!
|
||||
let right_directed_op = DirectedOp::Right(name, spec);
|
||||
|
||||
self.state_stack.push(TokenOrRedirect::StackPop);
|
||||
self.state_stack.push(TokenOrRedirect::Op(name, spec));
|
||||
self.state_stack.push(TokenOrRedirect::CompositeRedirect(
|
||||
0,
|
||||
right_directed_op,
|
||||
));
|
||||
self.state_stack
|
||||
.push(TokenOrRedirect::CompositeRedirect(0, right_directed_op));
|
||||
}
|
||||
} else {
|
||||
let left_directed_op = DirectedOp::Left(name, spec);
|
||||
@@ -783,7 +765,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
let h = self.iter.stack_last().unwrap();
|
||||
|
||||
let cell = self.iter.read_cell(h);
|
||||
let cell = heap_bound_store(&self.iter.heap, heap_bound_deref(&self.iter.heap, cell));
|
||||
let cell = heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, cell));
|
||||
|
||||
// 7.10.4
|
||||
if let Some(var) = numbervar(&self.numbervars_offset, cell) {
|
||||
@@ -802,20 +784,16 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
name: Atom,
|
||||
op_desc: Option<OpDesc>,
|
||||
) -> bool {
|
||||
if self.numbervars && is_numbered_var(name, arity) {
|
||||
if self.format_numbered_vars() {
|
||||
return true;
|
||||
}
|
||||
if self.numbervars && is_numbered_var(name, arity) && self.format_numbered_vars() {
|
||||
return true;
|
||||
}
|
||||
|
||||
let dot_atom = atom!(".");
|
||||
|
||||
if let Some(spec) = op_desc {
|
||||
if dot_atom == name && is_infix!(spec.get_spec()) {
|
||||
if !self.ignore_ops {
|
||||
self.push_list(max_depth);
|
||||
return true;
|
||||
}
|
||||
if dot_atom == name && is_infix!(spec.get_spec()) && !self.ignore_ops {
|
||||
self.push_list(max_depth);
|
||||
return true;
|
||||
}
|
||||
|
||||
if !self.ignore_ops && spec.get_prec() > 0 {
|
||||
@@ -824,10 +802,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
}
|
||||
}
|
||||
|
||||
return match (name, arity) {
|
||||
match (name, arity) {
|
||||
(atom!("{}"), 1) if !self.ignore_ops => self.format_curly_braces(max_depth),
|
||||
_ => self.format_struct(max_depth, arity, name),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn offset_as_string(&mut self, h: IterStackLoc) -> Option<String> {
|
||||
@@ -866,7 +844,8 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
loop {
|
||||
let is_cyclic = orig_cell.get_forwarding_bit();
|
||||
|
||||
let cell = heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, orig_cell));
|
||||
let cell =
|
||||
heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, orig_cell));
|
||||
let cell = unmark_cell_bits!(cell);
|
||||
|
||||
match self.var_names.get(&cell).cloned() {
|
||||
@@ -933,7 +912,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
}
|
||||
|
||||
let h = cell.get_value() as usize;
|
||||
self.iter.push_stack(IterStackLoc::iterable_loc(h, HeapOrStackTag::Heap));
|
||||
self.iter.push_stack(IterStackLoc::iterable_loc(
|
||||
h,
|
||||
HeapOrStackTag::Heap,
|
||||
));
|
||||
|
||||
if let Some(cell) = self.iter.next() {
|
||||
orig_cell = cell;
|
||||
@@ -951,13 +933,13 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while let Some(_) = self.iter.pop_stack() {}
|
||||
while self.iter.pop_stack().is_none() {}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
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(), {
|
||||
append_str!(self, &result);
|
||||
@@ -1145,8 +1127,6 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
self.state_stack.push(TokenOrRedirect::Open);
|
||||
self.state_stack.push(TokenOrRedirect::Atom(rdiv_ct));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -1242,7 +1222,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
let mut heap_pstr_iter = HeapPStrIter::new(self.iter.heap, focus.value() as usize);
|
||||
|
||||
if heap_pstr_iter.next().is_some() {
|
||||
while let Some(_) = heap_pstr_iter.next() {}
|
||||
for _ in heap_pstr_iter.by_ref() {}
|
||||
} else {
|
||||
return self.push_list(max_depth);
|
||||
}
|
||||
@@ -1258,11 +1238,9 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
|
||||
let at_cdr = self.outputter.ends_with("|");
|
||||
|
||||
if self.double_quotes {
|
||||
if !self.ignore_ops && end_cell.is_string_terminator(&self.iter.heap) {
|
||||
self.remove_list_children(focus.value() as usize);
|
||||
return self.print_proper_string(focus.value() as usize, max_depth);
|
||||
}
|
||||
if self.double_quotes && !self.ignore_ops && end_cell.is_string_terminator(self.iter.heap) {
|
||||
self.remove_list_children(focus.value() as usize);
|
||||
return self.print_proper_string(focus.value() as usize, max_depth);
|
||||
}
|
||||
|
||||
if self.ignore_ops {
|
||||
@@ -1275,8 +1253,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
append_str!(self, "[]");
|
||||
}
|
||||
} else {
|
||||
self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth));
|
||||
self.iter.push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap));
|
||||
self.state_stack
|
||||
.push(TokenOrRedirect::FunctorRedirect(max_depth));
|
||||
self.iter
|
||||
.push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -1287,7 +1267,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
|
||||
read_heap_cell!(value,
|
||||
(HeapCellValueTag::Lis) => {
|
||||
return self.push_list(max_depth);
|
||||
self.push_list(max_depth)
|
||||
}
|
||||
_ => {
|
||||
let switch = Rc::new(Cell::new((!at_cdr, 0)));
|
||||
@@ -1386,13 +1366,16 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
|
||||
let switch = self.close_list(cell);
|
||||
|
||||
self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth));
|
||||
self.state_stack
|
||||
.push(TokenOrRedirect::FunctorRedirect(max_depth));
|
||||
self.state_stack.push(TokenOrRedirect::HeadTailSeparator); // bar
|
||||
self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth + 1));
|
||||
self.state_stack
|
||||
.push(TokenOrRedirect::FunctorRedirect(max_depth + 1));
|
||||
|
||||
self.open_list(switch);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn handle_op_as_struct(
|
||||
&mut self,
|
||||
name: Atom,
|
||||
@@ -1448,7 +1431,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
for op in &[op, parent_op] {
|
||||
if let Some(ref op) = &op {
|
||||
if op.is_left()
|
||||
&& (op.is_prefix() || requires_space(&*op.as_atom().as_str(), "("))
|
||||
&& (op.is_prefix() || requires_space(&op.as_atom().as_str(), "("))
|
||||
{
|
||||
self.state_stack.push(TokenOrRedirect::Space);
|
||||
return;
|
||||
@@ -1463,7 +1446,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn print_tcp_listener(&mut self, tcp_listener: &TcpListener, max_depth: usize) {
|
||||
let (ip, port) = if let Some(addr) = tcp_listener.local_addr().ok() {
|
||||
let (ip, port) = if let Ok(addr) = tcp_listener.local_addr() {
|
||||
(addr.ip(), addr.port())
|
||||
} else {
|
||||
let disconnected_atom = atom!("$disconnected_tcp_listener");
|
||||
@@ -1545,24 +1528,33 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
}
|
||||
|
||||
fn print_comma_separated_char_list(&mut self, char_list: CommaSeparatedCharList) {
|
||||
let CommaSeparatedCharList { pstr, offset, max_depth, end_cell, end_h } = char_list;
|
||||
let CommaSeparatedCharList {
|
||||
pstr,
|
||||
offset,
|
||||
max_depth,
|
||||
end_cell,
|
||||
end_h,
|
||||
} = char_list;
|
||||
let pstr_str = pstr.as_str_from(offset);
|
||||
|
||||
if let Some(c) = pstr_str.chars().next() {
|
||||
let offset = offset + c.len_utf8();
|
||||
|
||||
if !self.max_depth_exhausted(max_depth) {
|
||||
self.state_stack.push(TokenOrRedirect::CommaSeparatedCharList(CommaSeparatedCharList {
|
||||
pstr,
|
||||
offset,
|
||||
max_depth: max_depth.saturating_sub(1),
|
||||
end_cell,
|
||||
end_h,
|
||||
}));
|
||||
self.state_stack
|
||||
.push(TokenOrRedirect::CommaSeparatedCharList(
|
||||
CommaSeparatedCharList {
|
||||
pstr,
|
||||
offset,
|
||||
max_depth: max_depth.saturating_sub(1),
|
||||
end_cell,
|
||||
end_h,
|
||||
},
|
||||
));
|
||||
|
||||
let max_depth_allows = self.max_depth == 0 || max_depth > 1;
|
||||
|
||||
if max_depth_allows && pstr_str.chars().skip(1).next().is_some() {
|
||||
if max_depth_allows && pstr_str.chars().nth(1).is_some() {
|
||||
self.state_stack.push(TokenOrRedirect::Comma);
|
||||
}
|
||||
|
||||
@@ -1576,10 +1568,12 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
self.state_stack.push(TokenOrRedirect::HeadTailSeparator);
|
||||
} else if end_cell != empty_list_as_cell!() {
|
||||
if let Some(end_h) = end_h {
|
||||
self.iter.push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap));
|
||||
self.iter
|
||||
.push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap));
|
||||
}
|
||||
|
||||
self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth + 1));
|
||||
self.state_stack
|
||||
.push(TokenOrRedirect::FunctorRedirect(max_depth + 1));
|
||||
self.state_stack.push(TokenOrRedirect::HeadTailSeparator);
|
||||
}
|
||||
}
|
||||
@@ -1599,13 +1593,12 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
|
||||
let print_struct = |printer: &mut Self, name: Atom, arity: usize| {
|
||||
if name == atom!("[]") && arity == 0 {
|
||||
match printer.state_stack.last() {
|
||||
Some(TokenOrRedirect::CloseList(_) | TokenOrRedirect::ChildCloseList) => {
|
||||
if printer.at_cdr("") {
|
||||
return;
|
||||
}
|
||||
if let Some(TokenOrRedirect::CloseList(_) | TokenOrRedirect::ChildCloseList) =
|
||||
printer.state_stack.last()
|
||||
{
|
||||
if printer.at_cdr("") {
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
append_str!(printer, "[]");
|
||||
@@ -1642,7 +1635,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
result.push('(');
|
||||
}
|
||||
|
||||
result += &printer.print_op_addendum(&*name.as_str());
|
||||
result += &printer.print_op_addendum(&name.as_str());
|
||||
|
||||
if op.is_some() {
|
||||
result.push(')');
|
||||
@@ -1652,14 +1645,14 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
append_str!(printer, &result);
|
||||
});
|
||||
} else {
|
||||
push_space_if_amb!(printer, &*name.as_str(), {
|
||||
push_space_if_amb!(printer, &name.as_str(), {
|
||||
printer.print_impromptu_atom(name);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if !addr.is_var()
|
||||
&& !addr.is_compound(&self.iter.heap)
|
||||
&& !addr.is_compound(self.iter.heap)
|
||||
&& self.max_depth_exhausted(max_depth)
|
||||
{
|
||||
if !(addr == atom_as_cell!(atom!("[]")) && self.at_cdr("")) {
|
||||
@@ -1772,7 +1765,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
|
||||
TokenOrRedirect::BarAsOp => append_str!(self, " | "),
|
||||
TokenOrRedirect::Char(c) => print_char!(self, self.quoted, c),
|
||||
TokenOrRedirect::Op(atom, op) => {
|
||||
self.print_op(&*atom.as_str());
|
||||
self.print_op(&atom.as_str());
|
||||
|
||||
if is_prefix!(op.get_spec()) {
|
||||
self.set_parent_of_first_op(Some(DirectedOp::Left(atom, op)));
|
||||
|
||||
Reference in New Issue
Block a user