Fixed operator printing

pull/2/head
Mark 2023-04-06 10:30:50 -07:00
parent f112326620
commit a3ada7c70c
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 15 additions and 19 deletions

View File

@ -63,6 +63,17 @@ impl Operator {
return astr; return astr;
} }
#[inline(always)]
fn add_parens_to_arg_strict(&self, arg: &Token) -> String {
let mut astr: String = arg.print();
if let Token::Operator(o,_) = arg {
if o <= self {
astr = format!("({})", astr);
}
}
return astr;
}
pub fn print(&self, args: &VecDeque<Token>) -> String { pub fn print(&self, args: &VecDeque<Token>) -> String {
match self { match self {
Operator::ImplicitMultiply | Operator::ImplicitMultiply |
@ -130,9 +141,9 @@ impl Operator {
let (b, sub) = (b, sub); let (b, sub) = (b, sub);
if sub { if sub {
return format!("{} - {}", self.add_parens_to_arg(a), self.add_parens_to_arg(&b)); return format!("{} - {}", self.add_parens_to_arg(a), self.add_parens_to_arg(b));
} else { } else {
return format!("{} + {}", self.add_parens_to_arg(a), self.add_parens_to_arg(&b)); return format!("{} + {}", self.add_parens_to_arg(a), self.add_parens_to_arg(b));
} }
}, },
@ -149,25 +160,10 @@ impl Operator {
} }
let (b, div) = (b, div); let (b, div) = (b, div);
let mut astr: String = a.print();
if let Token::Operator(o,_) = a {
if o < self {
astr = format!("({})", astr);
}
}
let mut bstr: String = b.print();
if let Token::Operator(o,_) = b {
if o < self {
bstr = format!("({})", astr);
}
}
if div { if div {
return format!("{} ÷ {}", astr, bstr); return format!("{} ÷ {}", self.add_parens_to_arg_strict(a), self.add_parens_to_arg_strict(b));
} else { } else {
return format!("{} × {}", astr, bstr); return format!("{} × {}", self.add_parens_to_arg_strict(a), self.add_parens_to_arg_strict(b));
} }
}, },