adamc@96
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@96
|
2 * All rights reserved.
|
adamc@96
|
3 *
|
adamc@96
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@96
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@96
|
6 *
|
adamc@96
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@96
|
8 * this list of conditions and the following disclaimer.
|
adamc@96
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@96
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@96
|
11 * and/or other materials provided with the distribution.
|
adamc@96
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@96
|
13 * derived from this software without specific prior written permission.
|
adamc@96
|
14 *
|
adamc@96
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@96
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@96
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@96
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@96
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@96
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@96
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@96
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@96
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@96
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@96
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@96
|
26 *)
|
adamc@96
|
27
|
adamc@96
|
28 structure MonoOpt :> MONO_OPT = struct
|
adamc@96
|
29
|
adamc@96
|
30 open Mono
|
adamc@96
|
31 structure U = MonoUtil
|
adamc@96
|
32
|
adamc@96
|
33 fun typ t = t
|
adamc@96
|
34 fun decl d = d
|
adamc@96
|
35
|
adamc@107
|
36 fun attrifyInt n =
|
adamc@107
|
37 if n < 0 then
|
adamc@107
|
38 "-" ^ Int64.toString (Int64.~ n)
|
adamc@107
|
39 else
|
adamc@107
|
40 Int64.toString n
|
adamc@107
|
41
|
adamc@107
|
42 fun attrifyFloat n =
|
adamc@107
|
43 if n < 0.0 then
|
adamc@107
|
44 "-" ^ Real.toString (Real.~ n)
|
adamc@107
|
45 else
|
adamc@107
|
46 Real.toString n
|
adamc@107
|
47
|
adamc@107
|
48 val attrifyString = String.translate (fn #"\"" => """
|
adamc@135
|
49 | #"&" => "&"
|
adamc@107
|
50 | ch => if Char.isPrint ch then
|
adamc@107
|
51 str ch
|
adamc@107
|
52 else
|
adamc@107
|
53 "&#" ^ Int.toString (ord ch) ^ ";")
|
adamc@107
|
54
|
adamc@120
|
55 val urlifyInt = attrifyInt
|
adamc@120
|
56 val urlifyFloat = attrifyFloat
|
adamc@120
|
57
|
adamc@135
|
58 val htmlifyString = String.translate (fn ch => case ch of
|
adamc@135
|
59 #"<" => "<"
|
adamc@135
|
60 | #"&" => "&"
|
adamc@135
|
61 | _ =>
|
adamc@135
|
62 if Char.isPrint ch orelse Char.isSpace ch then
|
adamc@135
|
63 str ch
|
adamc@135
|
64 else
|
adamc@135
|
65 "&#" ^ Int.toString (ord ch) ^ ";")
|
adamc@135
|
66
|
adamc@120
|
67 fun hexIt ch =
|
adamc@120
|
68 let
|
adamc@120
|
69 val s = Int.fmt StringCvt.HEX (ord ch)
|
adamc@120
|
70 in
|
adamc@120
|
71 case size s of
|
adamc@120
|
72 0 => "00"
|
adamc@120
|
73 | 1 => "0" ^ s
|
adamc@120
|
74 | _ => s
|
adamc@120
|
75 end
|
adamc@120
|
76
|
adamc@120
|
77 val urlifyString = String.translate (fn #" " => "+"
|
adamc@120
|
78 | ch => if Char.isAlphaNum ch then
|
adamc@120
|
79 str ch
|
adamc@120
|
80 else
|
adamc@120
|
81 "%" ^ hexIt ch)
|
adamc@253
|
82
|
adamc@253
|
83
|
adamc@253
|
84 val sqlifyInt = attrifyInt
|
adamc@253
|
85 val sqlifyFloat = attrifyFloat
|
adamc@253
|
86
|
adamc@253
|
87 fun sqlifyString s = "E'" ^ String.toString s ^ "'"
|
adamc@183
|
88
|
adamc@96
|
89 fun exp e =
|
adamc@96
|
90 case e of
|
adamc@96
|
91 EPrim (Prim.String s) =>
|
adamc@96
|
92 let
|
adamc@96
|
93 val (_, chs) =
|
adamc@96
|
94 CharVector.foldl (fn (ch, (lastSpace, chs)) =>
|
adamc@96
|
95 let
|
adamc@96
|
96 val isSpace = Char.isSpace ch
|
adamc@96
|
97 in
|
adamc@96
|
98 if isSpace andalso lastSpace then
|
adamc@96
|
99 (true, chs)
|
adamc@96
|
100 else
|
adamc@96
|
101 (isSpace, ch :: chs)
|
adamc@96
|
102 end)
|
adamc@96
|
103 (false, []) s
|
adamc@96
|
104 in
|
adamc@96
|
105 EPrim (Prim.String (String.implode (rev chs)))
|
adamc@96
|
106 end
|
adamc@96
|
107
|
adamc@96
|
108 | EStrcat ((EPrim (Prim.String s1), loc), (EPrim (Prim.String s2), _)) =>
|
adamc@96
|
109 let
|
adamc@96
|
110 val s =
|
adamc@96
|
111 if size s1 > 0 andalso size s2 > 0
|
adamc@96
|
112 andalso Char.isSpace (String.sub (s1, size s1 - 1))
|
adamc@96
|
113 andalso Char.isSpace (String.sub (s2, 0)) then
|
adamc@96
|
114 s1 ^ String.extract (s2, 1, NONE)
|
adamc@96
|
115 else
|
adamc@96
|
116 s1 ^ s2
|
adamc@96
|
117 in
|
adamc@96
|
118 EPrim (Prim.String s)
|
adamc@96
|
119 end
|
adamc@105
|
120
|
adamc@105
|
121 | EStrcat ((EPrim (Prim.String s1), loc), (EStrcat ((EPrim (Prim.String s2), _), rest), _)) =>
|
adamc@105
|
122 let
|
adamc@105
|
123 val s =
|
adamc@105
|
124 if size s1 > 0 andalso size s2 > 0
|
adamc@105
|
125 andalso Char.isSpace (String.sub (s1, size s1 - 1))
|
adamc@105
|
126 andalso Char.isSpace (String.sub (s2, 0)) then
|
adamc@105
|
127 s1 ^ String.extract (s2, 1, NONE)
|
adamc@105
|
128 else
|
adamc@105
|
129 s1 ^ s2
|
adamc@105
|
130 in
|
adamc@105
|
131 EStrcat ((EPrim (Prim.String s), loc), rest)
|
adamc@105
|
132 end
|
adamc@105
|
133
|
adamc@105
|
134 | EStrcat ((EStrcat (e1, e2), loc), e3) =>
|
adamc@105
|
135 optExp (EStrcat (e1, (EStrcat (e2, e3), loc)), loc)
|
adamc@105
|
136
|
adamc@106
|
137 | EWrite (EStrcat (e1, e2), loc) =>
|
adamc@106
|
138 ESeq ((optExp (EWrite e1, loc), loc),
|
adamc@106
|
139 (optExp (EWrite e2, loc), loc))
|
adamc@106
|
140
|
adamc@183
|
141 | ESeq ((EWrite (EPrim (Prim.String s1), _), loc),
|
adamc@183
|
142 (EWrite (EPrim (Prim.String s2), _), _)) =>
|
adamc@183
|
143 EWrite (EPrim (Prim.String (s1 ^ s2)), loc)
|
adamc@196
|
144 | ESeq ((EWrite (EPrim (Prim.String s1), _), loc),
|
adamc@196
|
145 (ESeq ((EWrite (EPrim (Prim.String s2), _), _),
|
adamc@196
|
146 e), _)) =>
|
adamc@196
|
147 ESeq ((EWrite (EPrim (Prim.String (s1 ^ s2)), loc), loc),
|
adamc@196
|
148 e)
|
adamc@183
|
149
|
adamc@135
|
150 | EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]) =>
|
adamc@135
|
151 EPrim (Prim.String (htmlifyString s))
|
adamc@135
|
152 | EWrite (EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@135
|
153 EWrite (EPrim (Prim.String (htmlifyString s)), loc)
|
adamc@135
|
154 | EWrite (EFfiApp ("Basis", "htmlifyString", [e]), _) =>
|
adamc@135
|
155 EFfiApp ("Basis", "htmlifyString_w", [e])
|
adamc@196
|
156 | EFfiApp ("Basis", "htmlifyString_w", [(EPrim (Prim.String s), loc)]) =>
|
adamc@196
|
157 EWrite (EPrim (Prim.String (htmlifyString s)), loc)
|
adamc@135
|
158
|
adamc@107
|
159 | EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@107
|
160 EPrim (Prim.String (attrifyInt n))
|
adamc@107
|
161 | EWrite (EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]), loc) =>
|
adamc@107
|
162 EWrite (EPrim (Prim.String (attrifyInt n)), loc)
|
adamc@106
|
163 | EWrite (EFfiApp ("Basis", "attrifyInt", [e]), _) =>
|
adamc@106
|
164 EFfiApp ("Basis", "attrifyInt_w", [e])
|
adamc@107
|
165
|
adamc@107
|
166 | EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@107
|
167 EPrim (Prim.String (attrifyFloat n))
|
adamc@107
|
168 | EWrite (EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]), loc) =>
|
adamc@107
|
169 EWrite (EPrim (Prim.String (attrifyFloat n)), loc)
|
adamc@106
|
170 | EWrite (EFfiApp ("Basis", "attrifyFloat", [e]), _) =>
|
adamc@106
|
171 EFfiApp ("Basis", "attrifyFloat_w", [e])
|
adamc@107
|
172
|
adamc@107
|
173 | EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]) =>
|
adamc@107
|
174 EPrim (Prim.String (attrifyString s))
|
adamc@107
|
175 | EWrite (EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@107
|
176 EWrite (EPrim (Prim.String (attrifyString s)), loc)
|
adamc@106
|
177 | EWrite (EFfiApp ("Basis", "attrifyString", [e]), _) =>
|
adamc@106
|
178 EFfiApp ("Basis", "attrifyString_w", [e])
|
adamc@106
|
179
|
adamc@120
|
180 | EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@120
|
181 EPrim (Prim.String (urlifyInt n))
|
adamc@120
|
182 | EWrite (EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]), loc) =>
|
adamc@120
|
183 EWrite (EPrim (Prim.String (urlifyInt n)), loc)
|
adamc@120
|
184 | EWrite (EFfiApp ("Basis", "urlifyInt", [e]), _) =>
|
adamc@120
|
185 EFfiApp ("Basis", "urlifyInt_w", [e])
|
adamc@120
|
186
|
adamc@120
|
187 | EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@120
|
188 EPrim (Prim.String (urlifyFloat n))
|
adamc@120
|
189 | EWrite (EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]), loc) =>
|
adamc@120
|
190 EWrite (EPrim (Prim.String (urlifyFloat n)), loc)
|
adamc@120
|
191 | EWrite (EFfiApp ("Basis", "urlifyFloat", [e]), _) =>
|
adamc@120
|
192 EFfiApp ("Basis", "urlifyFloat_w", [e])
|
adamc@120
|
193
|
adamc@120
|
194 | EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]) =>
|
adamc@120
|
195 EPrim (Prim.String (urlifyString s))
|
adamc@120
|
196 | EWrite (EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@120
|
197 EWrite (EPrim (Prim.String (urlifyString s)), loc)
|
adamc@120
|
198 | EWrite (EFfiApp ("Basis", "urlifyString", [e]), _) =>
|
adamc@120
|
199 EFfiApp ("Basis", "urlifyString_w", [e])
|
adamc@120
|
200
|
adamc@188
|
201 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]) =>
|
adamc@187
|
202 EPrim (Prim.String "1")
|
adamc@188
|
203 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]) =>
|
adamc@187
|
204 EPrim (Prim.String "0")
|
adamc@188
|
205 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]), loc) =>
|
adamc@187
|
206 EWrite (EPrim (Prim.String "1"), loc)
|
adamc@188
|
207 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]), loc) =>
|
adamc@187
|
208 EWrite (EPrim (Prim.String "0"), loc)
|
adamc@187
|
209 | EWrite (EFfiApp ("Basis", "urlifyBool", [e]), _) =>
|
adamc@187
|
210 EFfiApp ("Basis", "urlifyBool_w", [e])
|
adamc@187
|
211
|
adamc@253
|
212 | EFfiApp ("Basis", "sqlifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@253
|
213 EPrim (Prim.String (sqlifyInt n))
|
adamc@253
|
214 | EFfiApp ("Basis", "sqlifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@253
|
215 EPrim (Prim.String (sqlifyFloat n))
|
adamc@253
|
216 | EFfiApp ("Basis", "sqlifyBool", [b as (_, loc)]) =>
|
adamc@253
|
217 optExp (ECase (b,
|
adamc@253
|
218 [((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "True", arg = NONE}, NONE), loc),
|
adamc@253
|
219 (EPrim (Prim.String "TRUE"), loc)),
|
adamc@253
|
220 ((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "False", arg = NONE}, NONE), loc),
|
adamc@253
|
221 (EPrim (Prim.String "FALSE"), loc))],
|
adamc@253
|
222 {disc = (TFfi ("Basis", "bool"), loc),
|
adamc@253
|
223 result = (TFfi ("Basis", "string"), loc)}), loc)
|
adamc@253
|
224 | EFfiApp ("Basis", "sqlifyString", [(EPrim (Prim.String n), _)]) =>
|
adamc@253
|
225 EPrim (Prim.String (sqlifyString n))
|
adamc@253
|
226
|
adamc@184
|
227 | EWrite (ECase (discE, pes, {disc, ...}), loc) =>
|
adamc@184
|
228 optExp (ECase (discE,
|
adamc@184
|
229 map (fn (p, e) => (p, (EWrite e, loc))) pes,
|
adamc@184
|
230 {disc = disc,
|
adamc@184
|
231 result = (TRecord [], loc)}), loc)
|
adamc@184
|
232
|
adamc@96
|
233 | _ => e
|
adamc@96
|
234
|
adamc@105
|
235 and optExp e = #1 (U.Exp.map {typ = typ, exp = exp} e)
|
adamc@105
|
236
|
adamc@96
|
237 val optimize = U.File.map {typ = typ, exp = exp, decl = decl}
|
adamc@96
|
238
|
adamc@96
|
239 end
|