Skip to content
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

Fix money parsing #256

Merged
merged 5 commits into from
May 6, 2022
Merged

Fix money parsing #256

merged 5 commits into from
May 6, 2022

Conversation

denismerigoux
Copy link
Contributor

Embarrasingly, there was a bug in the parsing of money literals like 0,3 € or $0.3 : they were counted as "3 cents" instead of "30 cents" due to a string to integer conversion error. This PR fixes this behavior by padding a 0 on the right to these kinds of money literals.

@denismerigoux denismerigoux added ❌ bug Something isn't working 🔧 compiler Issue concerns the compiler labels May 5, 2022
@AltGr
Copy link
Contributor

AltGr commented May 5, 2022

Ouch! Shouldn't that rather be handled by the lexer directly ?

diff --git a/compiler/surface/lexer.cppo.ml b/compiler/surface/lexer.cppo.ml
index ce3a0ba3..f745961e 100644
--- a/compiler/surface/lexer.cppo.ml
+++ b/compiler/surface/lexer.cppo.ml
@@ -563,6 +563,7 @@ let rec lex_code (lexbuf : lexbuf) : token =
         | MC_DECIMAL_SEPARATOR -> buf := cents
         | _ -> ()
       done;
+      Buffer.add_string cents (String.make (2 - Buffer.length cents) '0');
       L.update_acc lexbuf;
       MONEY_AMOUNT (Runtime.integer_of_string (Buffer.contents units), Runtime.integer_of_string (Buffer.contents cents))
   | Plus digit, MC_DECIMAL_SEPARATOR, Star digit ->

@denismerigoux
Copy link
Contributor Author

Yes you're right. I implemented your suggestion.

@denismerigoux denismerigoux merged commit 274d0a1 into master May 6, 2022
@denismerigoux denismerigoux deleted the fix_money_rounding branch September 6, 2022 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❌ bug Something isn't working 🔧 compiler Issue concerns the compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants