-
Notifications
You must be signed in to change notification settings - Fork 659
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 support for underscores in numeric literals #2538 #2545
Conversation
Updates:
Description of changes:
Testing:
|
Codecov Report
@@ Coverage Diff @@
## testnet3 #2545 +/- ##
============================================
+ Coverage 79.03% 80.10% +1.07%
============================================
Files 159 158 -1
Lines 5309 5309
Branches 5309 5309
============================================
+ Hits 4196 4253 +57
+ Misses 1113 1056 -57
... and 48 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
…us type error fix, revised tuple indexing test)
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.
LGTM!
Motivation
As specified in #2538 the goal is to add support for underscores in numeric literals such as writing
1_000_000
instead of1000000
to improve readability. In this way the Leo language further aligns with the syntax of Rust. Here are some examples.Description of changes
While making changes to the compiler I noticed that adding this feature results in tuple indexing inconsistent with Rust. For example, while rust does allow numeric literals like
1_1
and01
it does not allow them to index tuples meaningx.1_1
andx.01
should fail when trying to access 11th and 1st field of tuple x.In order to fix this, I made it so the lexer prepends a 0 to any literals with an underscore in them. Previously,
01
would be lexed as01
, and when tuple indexing is parsed it throws an error if there are any leading zeros (and the number has more than one digit). This solution is a little bit ugly, and other alternatives that touch onlylexer.rs
might be preferable.Test Plan
I ran the test suite and everything passed. I also added new test files
parser/literal/underscore.leo
andparser/literal/underscore_fail.leo
to test underscore usage across fields, groups, integers, and scalars. Lastly, I added new tests inunreachable/eat_int.leo
to check that tuple indexing is consistent with Rust.Related PRs
Leo Grammar PR