Skip to content

Commit

Permalink
Fix arithmetic evaluation order + float printing (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
denismerigoux authored Sep 23, 2022
2 parents c97d31d + b362d47 commit e4c7bce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
12 changes: 8 additions & 4 deletions src/mlang/backend_compilers/bir_to_dgfip_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ let rec format_dexpr (dgfip_flags : Dgfip_options.flags)
match de with
| Done -> Format.fprintf fmt "1"
| Dzero -> Format.fprintf fmt "0"
| Dlit f ->
(* Print literal floats as precisely as possible, without cluttering the
generated code *)
Format.fprintf fmt "%#.19g" f
| Dlit f -> (
match Float.modf f with
| 0., _ ->
(* Print at least one decimal, distinction from integers *)
Format.fprintf fmt "%.1f" f
| _ ->
(* Print literal floats as precisely as possible *)
Format.fprintf fmt "%#.19g" f)
| Dvar (evar, dflag) -> format_expr_var dgfip_flags vm fmt (evar, dflag)
| Dand (de1, de2) -> format_dexpr fmt (Dbinop ("&&", de1, de2))
| Dor (de1, de2) -> format_dexpr fmt (Dbinop ("||", de1, de2))
Expand Down
19 changes: 3 additions & 16 deletions src/mlang/m_frontend/mparser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -412,33 +412,20 @@ expression:
| OR { (Or, mk_position $sloc) }

sum_expression:
| e = diff_expression { e }
| e1 = diff_expression op = sum_operator e2 = sum_expression { (Binop (op, e1, e2), mk_position $sloc) }

diff_expression:
| e = product_expression { e }
| e1 = diff_expression op = diff_operator e2 = product_expression { (Binop (op, e1, e2), mk_position $sloc) }
| e1 = sum_expression op = sum_operator e2 = product_expression { (Binop (op, e1, e2), mk_position $sloc) }

sum_operator:
| PLUS { (Add, mk_position $sloc) }

diff_operator:
| MINUS { (Sub, mk_position $sloc) }

product_expression:
| e = div_expression { e }
| e1 = product_expression op = product_operator e2 = div_expression
| e = factor { e }
| e1 = product_expression op = product_operator e2 = factor
{ (Binop (op, e1, e2), mk_position $sloc) }

product_operator:
| TIMES { (Mul, mk_position $sloc) }

div_expression:
| e = factor { e }
| e1 = div_expression op = div_operator e2 = factor
{ (Binop (op, e1, e2), mk_position $sloc) }

div_operator:
| DIV { (Div, mk_position $sloc) }

table_index_name:
Expand Down

0 comments on commit e4c7bce

Please sign in to comment.