[m-rev.] diff: erlang float syntax

Peter Wang novalazy at gmail.com
Mon Mar 7 13:27:39 AEDT 2011


Branches: main, 11.01

The output of `io.write_float' changed some time in 2010 such that it may omit
the decimal point and fractional part when followed by an exponent, e.g.
"1e-15".  This is in line with Mercury syntax but invalid for Erlang.

compiler/elds_to_erlang.m:
        Compensate for the above when writing floats to Erlang source files.

diff --git a/compiler/elds_to_erlang.m b/compiler/elds_to_erlang.m
index 6beb095..4f9f755 100644
--- a/compiler/elds_to_erlang.m
+++ b/compiler/elds_to_erlang.m
@@ -737,7 +737,7 @@ output_term(ModuleInfo, VarSet, Indent, Term, !IO) :-
         space(!IO)
     ;
         Term = elds_float(Float),
-        io.write_float(Float, !IO),
+        output_float(Float, !IO),
         space(!IO)
     ;
         Term = elds_binary(String),
@@ -786,6 +786,37 @@ output_term(ModuleInfo, VarSet, Indent, Term, !IO) :-
         output_var_string(Name, !IO)
     ).
 
+:- pred output_float(float::in, io::di, io::uo) is det.
+
+output_float(Float, !IO) :-
+    S = string.from_float(Float),
+    ( digit_then_e(S, no, 0, Pos) ->
+        io.write_string(string.substring(S, 0, Pos), !IO),
+        io.write_string(".0", !IO),
+        io.write_string(string.substring(S, Pos, length(S)), !IO)
+    ;
+        io.write_string(S, !IO)
+    ).
+
+:- pred digit_then_e(string::in, bool::in, int::in, int::out) is semidet.
+
+digit_then_e(String, PrevDigit, Pos0, Pos) :-
+    string.index(String, Pos0, Char),
+    Char \= ('.'),
+    ( is_e(Char) ->
+        PrevDigit = yes,
+        Pos = Pos0
+    ; is_digit(Char) ->
+        digit_then_e(String, yes, Pos0 + 1, Pos)
+    ;
+        digit_then_e(String, no, Pos0 + 1, Pos)
+    ).
+
+:- pred is_e(char::in) is semidet.
+
+is_e('e').
+is_e('E').
+
 :- pred output_tuple(module_info::in, prog_varset::in, indent::in,
     list(elds_expr)::in, io::di, io::uo) is det.
 

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list