-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more order of operation comparisons #13
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use crate::subset::ast::*; | ||
use crate::util::pretty_print::{block, intersperse, PrettyHelper, PrettyPrint}; | ||
use crate::util::pretty_print::{ | ||
block, intersperse, PrettyHelper, PrettyPrint, | ||
}; | ||
use core::cmp::Ordering; | ||
use itertools::Itertools; | ||
use pretty::RcDoc; | ||
|
@@ -8,32 +10,32 @@ use pretty::RcDoc; | |
/// operator with stronger binding. | ||
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
enum ParenCtx { | ||
Op, | ||
Not, | ||
And, | ||
Or, | ||
Index, | ||
Mul, | ||
AddSub, | ||
Shift, | ||
Cmp, | ||
Equal, | ||
BAnd, | ||
BOr, | ||
And, | ||
Or, | ||
} | ||
|
||
impl From<&Binop> for ParenCtx { | ||
fn from(s: &Binop) -> Self { | ||
match s { | ||
Binop::BitOr => ParenCtx::BOr, | ||
Binop::IndexBit => ParenCtx::Index, | ||
Binop::Mul => ParenCtx::Mul, | ||
Binop::Add | Binop::Sub => ParenCtx::AddSub, | ||
Binop::ShiftLeft => ParenCtx::Shift, | ||
Binop::Lt | Binop::Gt | Binop::Geq | Binop::Leq => ParenCtx::Cmp, | ||
Binop::Equal | Binop::NotEqual => ParenCtx::Equal, | ||
Binop::BitAnd => ParenCtx::BAnd, | ||
Binop::LogOr => ParenCtx::Or, | ||
Binop::BitOr => ParenCtx::BOr, | ||
Binop::LogAnd => ParenCtx::And, | ||
Binop::Add | ||
| Binop::Sub | ||
| Binop::Mul | ||
| Binop::Lt | ||
| Binop::Gt | ||
| Binop::Geq | ||
| Binop::Leq | ||
| Binop::Equal | ||
| Binop::NotEqual | ||
| Binop::IndexBit | ||
| Binop::ShiftLeft => ParenCtx::Op, | ||
Binop::LogOr => ParenCtx::Or, | ||
} | ||
} | ||
} | ||
|
@@ -47,18 +49,63 @@ impl Ord for ParenCtx { | |
match (self, other) { | ||
(P::Not, _) => Ordering::Greater, | ||
|
||
(P::Op, P::Not) => Ordering::Less, | ||
(P::Op, _) => Ordering::Greater, | ||
(P::Index, P::Not) => Ordering::Less, | ||
(P::Index, _) => Ordering::Greater, | ||
|
||
(P::Mul, P::Not) | (P::Mul, P::Index) => Ordering::Less, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this code was written, rust started supporting nested patterns so you can write something like:
Which might be easier to read |
||
(P::Mul, _) => Ordering::Greater, | ||
|
||
(P::AddSub, P::Not) | ||
| (P::AddSub, P::Index) | ||
| (P::AddSub, P::Mul) => Ordering::Less, | ||
(P::AddSub, _) => Ordering::Greater, | ||
|
||
(P::Shift, P::Not) | ||
| (P::Shift, P::Index) | ||
| (P::Shift, P::Mul) | ||
| (P::Shift, P::AddSub) => Ordering::Less, | ||
(P::Shift, _) => Ordering::Greater, | ||
|
||
(P::BAnd, P::Not) | (P::BAnd, P::Op) => Ordering::Less, | ||
(P::Cmp, P::Not) | ||
| (P::Cmp, P::Index) | ||
| (P::Cmp, P::Mul) | ||
| (P::Cmp, P::AddSub) | ||
| (P::Cmp, P::Shift) => Ordering::Greater, | ||
(P::Cmp, _) => Ordering::Less, | ||
|
||
(P::Equal, P::Not) | ||
| (P::Equal, P::Index) | ||
| (P::Equal, P::Mul) | ||
| (P::Equal, P::AddSub) | ||
| (P::Equal, P::Shift) | ||
| (P::Equal, P::Cmp) => Ordering::Greater, | ||
(P::Equal, _) => Ordering::Less, | ||
|
||
(P::BAnd, P::Not) | ||
| (P::BAnd, P::Mul) | ||
| (P::BAnd, P::AddSub) | ||
| (P::BAnd, P::Shift) | ||
| (P::BAnd, P::Cmp) | ||
| (P::BAnd, P::Equal) => Ordering::Less, | ||
(P::BAnd, _) => Ordering::Greater, | ||
|
||
(P::BOr, P::Not) | (P::BOr, P::Op) | (P::BOr, P::BAnd) => Ordering::Less, | ||
(P::BOr, P::Not) | ||
| (P::BOr, P::Mul) | ||
| (P::BOr, P::AddSub) | ||
| (P::BOr, P::Shift) | ||
| (P::BOr, P::Cmp) | ||
| (P::BOr, P::Equal) | ||
| (P::BOr, P::BAnd) => Ordering::Less, | ||
(P::BOr, _) => Ordering::Greater, | ||
|
||
(P::And, P::Not) | (P::And, P::Op) | (P::And, P::BAnd) | (P::And, P::BOr) => { | ||
Ordering::Less | ||
} | ||
(P::And, P::Not) | ||
| (P::And, P::Mul) | ||
| (P::And, P::AddSub) | ||
| (P::And, P::Shift) | ||
| (P::And, P::Cmp) | ||
| (P::And, P::Equal) | ||
| (P::And, P::BOr) | ||
| (P::And, P::BAnd) => Ordering::Less, | ||
(P::And, _) => Ordering::Greater, | ||
|
||
(P::Or, _) => Ordering::Less, | ||
|
@@ -174,7 +221,9 @@ impl PrettyPrint for Expr { | |
path.to_doc() | ||
} | ||
} | ||
Expr::Unop(op, input) => op.to_doc().append(print_expr(input, ParenCtx::Not)), | ||
Expr::Unop(op, input) => { | ||
op.to_doc().append(print_expr(input, ParenCtx::Not)) | ||
} | ||
Expr::Binop(Binop::IndexBit, lhs, rhs) => { | ||
print_expr(lhs, ParenCtx::Not).append(rhs.to_doc().brackets()) | ||
} | ||
|
@@ -207,15 +256,17 @@ impl PrettyPrint for Expr { | |
.append(lo.to_doc()) | ||
.brackets(), | ||
), | ||
Expr::Terop(Terop::IndexSlice, var, lo, width) => var.to_doc().append( | ||
lo.to_doc() | ||
.append(RcDoc::space()) | ||
.append(RcDoc::text("+")) | ||
.append(RcDoc::text(":")) | ||
.append(RcDoc::space()) | ||
.append(width.to_doc()) | ||
.brackets(), | ||
), | ||
Expr::Terop(Terop::IndexSlice, var, lo, width) => { | ||
var.to_doc().append( | ||
lo.to_doc() | ||
.append(RcDoc::space()) | ||
.append(RcDoc::text("+")) | ||
.append(RcDoc::text(":")) | ||
.append(RcDoc::space()) | ||
.append(width.to_doc()) | ||
.brackets(), | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting change |
||
Expr::Concat(concat) => concat.to_doc(), | ||
Expr::Repeat(times, expr) => RcDoc::text(times.to_string()) | ||
.append(expr.to_doc().braces()) | ||
|
@@ -269,13 +320,13 @@ impl PrettyPrint for AssignTy { | |
impl PrettyPrint for Map { | ||
fn to_doc(&self) -> RcDoc<()> { | ||
intersperse( | ||
self.iter() | ||
.sorted_by_key(|(id, _)| (*id).clone()) | ||
.map(|(id, expr)| { | ||
self.iter().sorted_by_key(|(id, _)| (*id).clone()).map( | ||
|(id, expr)| { | ||
RcDoc::text(".") | ||
.append(RcDoc::as_string(id)) | ||
.append(expr.to_doc().parens()) | ||
}), | ||
}, | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting change |
||
RcDoc::text(",").append(RcDoc::hardline()), | ||
) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting change