-
Notifications
You must be signed in to change notification settings - Fork 187
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
Pretty printing of Fe AST #540
Conversation
@g-r-a-n-t Does Display give us enough functionality to indent the tree so that it's pretty? Also, if I had my druthers, the parser ast tests would compare asts with spans and the text content of the span, but other ast printing would ignore the spans. Eg from rowan https://crates.io/crates/rowan
Not suggesting you have to implement this; just something to consider as you ponder the design. |
} | ||
} | ||
|
||
impl fmt::Display for SimpleImportName { |
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.
I just left the imports out for now, since we may not be settled on their design.
write!(f, "{}{}", op.kind, operand) | ||
} | ||
}, | ||
Expr::CompOperation { left, op, right } => { |
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.
do these need parens?
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.
I dont think so
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.
Yeah, eg (1 == 2) == (3 == 4)
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.
(a == b) == (c == d)
is equivalent to a == b == (c == d)
, right? this is at least what the tests are telling me, as the first set of parens are dropped when printing
90f122f
to
3a3aadc
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #540 +/- ##
==========================================
- Coverage 87.92% 87.28% -0.64%
==========================================
Files 87 87
Lines 5654 5907 +253
==========================================
+ Hits 4971 5156 +185
- Misses 683 751 +68 ☔ View full report in Codecov by Sentry. |
3c25a9d
to
861d321
Compare
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.
noticed a few more things while working on #546
crates/parser/src/ast.rs
Outdated
fn inner_expr_needs_parens(outer: &Expr, inner: &Expr) -> bool { | ||
expr_power(outer) > expr_power(inner) | ||
} |
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.
The paren insertion logic isn't right. Eg the parens of 2 * (3 / 4)
are dropped. For left-associative ops, using >
for the lhs of a binop, and >=
for the rhs would suffice, but **
is right-associative, so (2 ** 3) ** 4
needs to keep its parens but 2 ** (3 ** 4)
doesn't.
Maybe there's a more elegant way, but I think of it as left and right binding power, ie the amount of attraction power an op has on its left and right operands, as in infix_binding_power
. A single expr_power
fn isn't sufficient, as the power an outer expr exerts on its lhs and rhs might be different, and the power an inner expr responds with may differ depending on whether it's on the left or right.
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.
Ok, updated it to use left and right binding power. I think it's correct now.
write!(f, "{}{}", op.kind, operand) | ||
} | ||
}, | ||
Expr::CompOperation { left, op, right } => { |
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.
Yeah, eg (1 == 2) == (3 == 4)
dbf3d82
to
80af5ed
Compare
80af5ed
to
7beb5cd
Compare
I'm merging this @sbillig. I'm fairly confident the paren stuff is correct now. It's also only going to be used for testing, so it's not something that needs to be perfect. |
What was wrong?
We can't print the AST.
I wanted to fix #531 by adding a
$
to the front of lowered tuple struct names (e.g.$tuple_bool_u256
), but wasn't able to do so without breaking the lowering tests. This is because the lowering tests compare the RON serialized AST of a lowered source file to that of an AST from a parsed file. So by adding a character to the lowered AST that is illegal to the parser, we are unable to validate that part of the lowering phase.By making the AST printable, we can replace the existing lowering tests with insta snapshots and not have to be bound by grammar rules when lowering.
How was it fixed?
To-Do
OPTIONAL: Update Spec if applicable
Add entry to the release notes (may forgo for trivial changes)
Clean up commit history