Move fix to lexer as suggested by @altgr

This commit is contained in:
Denis Merigoux 2022-05-06 10:29:39 +02:00
parent 5ec91c7b00
commit a52cc0d881
No known key found for this signature in database
GPG Key ID: EE99DCFA365C3EE3
2 changed files with 4 additions and 9 deletions

View File

@ -197,21 +197,13 @@ let rec translate_expr
decimal_of_string (i ^ "." ^ f) /& decimal_of_string "100"))
| LBool b -> Desugared.Ast.ELit (Dcalc.Ast.LBool b)
| LMoneyAmount i ->
(* If the user has written $0.3 it means 30 cents so we have to pad
with a 0 *)
let reg = Re.compile (Re.seq [ Re.start; Re.digit; Re.stop ]) in
let cents_correct =
Re.replace reg
~f:(fun group -> Re.Group.get group 0 ^ "0")
i.money_amount_cents
in
Desugared.Ast.ELit
(Dcalc.Ast.LMoney
Runtime.(
money_of_cents_integer
(integer_of_string i.money_amount_units
*! integer_of_int 100
+! integer_of_string cents_correct)))
+! integer_of_string i.money_amount_cents)))
| LNumber ((Int i, _), Some (Year, _)) ->
Desugared.Ast.ELit
(Dcalc.Ast.LDuration

View File

@ -567,6 +567,9 @@ let rec lex_code (lexbuf : lexbuf) : token =
| MC_DECIMAL_SEPARATOR -> buf := cents
| _ -> ()
done;
(* If the user has written $0.3 it means 30 cents so we have to pad
with a 0 *)
Buffer.add_string cents (String.make (2 - Buffer.length cents) '0');
L.update_acc lexbuf;
MONEY_AMOUNT (Buffer.contents units, Buffer.contents cents)
| Plus digit, MC_DECIMAL_SEPARATOR, Star digit ->