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@717
|
33 val bless = ref (fn _ : string => true)
|
adamc@717
|
34
|
adamc@96
|
35 fun typ t = t
|
adamc@96
|
36 fun decl d = d
|
adamc@96
|
37
|
adamc@107
|
38 fun attrifyInt n =
|
adamc@107
|
39 if n < 0 then
|
adamc@107
|
40 "-" ^ Int64.toString (Int64.~ n)
|
adamc@107
|
41 else
|
adamc@107
|
42 Int64.toString n
|
adamc@107
|
43
|
adamc@107
|
44 fun attrifyFloat n =
|
adamc@107
|
45 if n < 0.0 then
|
adamc@107
|
46 "-" ^ Real.toString (Real.~ n)
|
adamc@107
|
47 else
|
adamc@107
|
48 Real.toString n
|
adamc@107
|
49
|
adamc@107
|
50 val attrifyString = String.translate (fn #"\"" => """
|
adamc@135
|
51 | #"&" => "&"
|
adamc@107
|
52 | ch => if Char.isPrint ch then
|
adamc@107
|
53 str ch
|
adamc@107
|
54 else
|
adamc@107
|
55 "&#" ^ Int.toString (ord ch) ^ ";")
|
adamc@107
|
56
|
adamc@120
|
57 val urlifyInt = attrifyInt
|
adamc@120
|
58 val urlifyFloat = attrifyFloat
|
adamc@120
|
59
|
adamc@286
|
60 val htmlifyInt = attrifyInt
|
adamc@286
|
61 val htmlifyFloat = attrifyFloat
|
adamc@135
|
62 val htmlifyString = String.translate (fn ch => case ch of
|
adamc@135
|
63 #"<" => "<"
|
adamc@135
|
64 | #"&" => "&"
|
adamc@135
|
65 | _ =>
|
adamc@135
|
66 if Char.isPrint ch orelse Char.isSpace ch then
|
adamc@135
|
67 str ch
|
adamc@135
|
68 else
|
adamc@135
|
69 "&#" ^ Int.toString (ord ch) ^ ";")
|
adamc@135
|
70
|
adamc@120
|
71 fun hexIt ch =
|
adamc@120
|
72 let
|
adamc@120
|
73 val s = Int.fmt StringCvt.HEX (ord ch)
|
adamc@120
|
74 in
|
adamc@120
|
75 case size s of
|
adamc@120
|
76 0 => "00"
|
adamc@120
|
77 | 1 => "0" ^ s
|
adamc@120
|
78 | _ => s
|
adamc@120
|
79 end
|
adamc@120
|
80
|
adamc@120
|
81 val urlifyString = String.translate (fn #" " => "+"
|
adamc@120
|
82 | ch => if Char.isAlphaNum ch then
|
adamc@120
|
83 str ch
|
adamc@120
|
84 else
|
adamc@120
|
85 "%" ^ hexIt ch)
|
adamc@253
|
86
|
adamc@253
|
87
|
adamc@281
|
88 fun sqlifyInt n = attrifyInt n ^ "::int8"
|
adamc@281
|
89 fun sqlifyFloat n = attrifyFloat n ^ "::float8"
|
adamc@253
|
90
|
adamc@259
|
91 fun sqlifyString s = "E'" ^ String.translate (fn #"'" => "\\'"
|
adamc@714
|
92 | #"\\" => "\\\\"
|
adamc@714
|
93 | ch =>
|
adamc@714
|
94 if Char.isPrint ch then
|
adamc@714
|
95 str ch
|
adamc@714
|
96 else
|
adamc@714
|
97 "\\" ^ StringCvt.padLeft #"0" 3
|
adamc@714
|
98 (Int.fmt StringCvt.OCT (ord ch)))
|
adamc@281
|
99 (String.toString s) ^ "'::text"
|
adamc@453
|
100
|
adamc@96
|
101 fun exp e =
|
adamc@96
|
102 case e of
|
adamc@96
|
103 EPrim (Prim.String s) =>
|
adamc@96
|
104 let
|
adamc@96
|
105 val (_, chs) =
|
adamc@96
|
106 CharVector.foldl (fn (ch, (lastSpace, chs)) =>
|
adamc@96
|
107 let
|
adamc@96
|
108 val isSpace = Char.isSpace ch
|
adamc@96
|
109 in
|
adamc@96
|
110 if isSpace andalso lastSpace then
|
adamc@96
|
111 (true, chs)
|
adamc@96
|
112 else
|
adamc@96
|
113 (isSpace, ch :: chs)
|
adamc@96
|
114 end)
|
adamc@96
|
115 (false, []) s
|
adamc@96
|
116 in
|
adamc@96
|
117 EPrim (Prim.String (String.implode (rev chs)))
|
adamc@96
|
118 end
|
adamc@96
|
119
|
adamc@96
|
120 | EStrcat ((EPrim (Prim.String s1), loc), (EPrim (Prim.String s2), _)) =>
|
adamc@96
|
121 let
|
adamc@96
|
122 val s =
|
adamc@96
|
123 if size s1 > 0 andalso size s2 > 0
|
adamc@96
|
124 andalso Char.isSpace (String.sub (s1, size s1 - 1))
|
adamc@96
|
125 andalso Char.isSpace (String.sub (s2, 0)) then
|
adamc@96
|
126 s1 ^ String.extract (s2, 1, NONE)
|
adamc@96
|
127 else
|
adamc@96
|
128 s1 ^ s2
|
adamc@96
|
129 in
|
adamc@96
|
130 EPrim (Prim.String s)
|
adamc@96
|
131 end
|
adamc@105
|
132
|
adamc@105
|
133 | EStrcat ((EPrim (Prim.String s1), loc), (EStrcat ((EPrim (Prim.String s2), _), rest), _)) =>
|
adamc@105
|
134 let
|
adamc@105
|
135 val s =
|
adamc@105
|
136 if size s1 > 0 andalso size s2 > 0
|
adamc@105
|
137 andalso Char.isSpace (String.sub (s1, size s1 - 1))
|
adamc@105
|
138 andalso Char.isSpace (String.sub (s2, 0)) then
|
adamc@105
|
139 s1 ^ String.extract (s2, 1, NONE)
|
adamc@105
|
140 else
|
adamc@105
|
141 s1 ^ s2
|
adamc@105
|
142 in
|
adamc@105
|
143 EStrcat ((EPrim (Prim.String s), loc), rest)
|
adamc@105
|
144 end
|
adamc@105
|
145
|
adamc@105
|
146 | EStrcat ((EStrcat (e1, e2), loc), e3) =>
|
adamc@105
|
147 optExp (EStrcat (e1, (EStrcat (e2, e3), loc)), loc)
|
adamc@105
|
148
|
adamc@106
|
149 | EWrite (EStrcat (e1, e2), loc) =>
|
adamc@106
|
150 ESeq ((optExp (EWrite e1, loc), loc),
|
adamc@106
|
151 (optExp (EWrite e2, loc), loc))
|
adamc@106
|
152
|
adamc@183
|
153 | ESeq ((EWrite (EPrim (Prim.String s1), _), loc),
|
adamc@183
|
154 (EWrite (EPrim (Prim.String s2), _), _)) =>
|
adamc@183
|
155 EWrite (EPrim (Prim.String (s1 ^ s2)), loc)
|
adamc@196
|
156 | ESeq ((EWrite (EPrim (Prim.String s1), _), loc),
|
adamc@196
|
157 (ESeq ((EWrite (EPrim (Prim.String s2), _), _),
|
adamc@196
|
158 e), _)) =>
|
adamc@196
|
159 ESeq ((EWrite (EPrim (Prim.String (s1 ^ s2)), loc), loc),
|
adamc@196
|
160 e)
|
adamc@183
|
161
|
adamc@286
|
162 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "intToString", [(EPrim (Prim.Int n), _)]), _)]) =>
|
adamc@286
|
163 EPrim (Prim.String (htmlifyInt n))
|
adamc@286
|
164 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "intToString", es), _)]) =>
|
adamc@286
|
165 EFfiApp ("Basis", "htmlifyInt", es)
|
adamc@320
|
166 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "intToString"), _),
|
adamc@320
|
167 (EPrim (Prim.Int n), _)), _)]) =>
|
adamc@320
|
168 EPrim (Prim.String (htmlifyInt n))
|
adamc@320
|
169 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "intToString"), _),
|
adamc@320
|
170 e), _)]) =>
|
adamc@320
|
171 EFfiApp ("Basis", "htmlifyInt", [e])
|
adamc@286
|
172 | EWrite (EFfiApp ("Basis", "htmlifyInt", [e]), _) =>
|
adamc@286
|
173 EFfiApp ("Basis", "htmlifyInt_w", [e])
|
adamc@286
|
174
|
adamc@286
|
175 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "floatToString", [(EPrim (Prim.Float n), _)]), _)]) =>
|
adamc@286
|
176 EPrim (Prim.String (htmlifyFloat n))
|
adamc@286
|
177 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "floatToString", es), _)]) =>
|
adamc@286
|
178 EFfiApp ("Basis", "htmlifyFloat", es)
|
adamc@320
|
179 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "floatToString"), _),
|
adamc@320
|
180 (EPrim (Prim.Float n), _)), _)]) =>
|
adamc@320
|
181 EPrim (Prim.String (htmlifyFloat n))
|
adamc@320
|
182 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "floatToString"), _),
|
adamc@320
|
183 e), _)]) =>
|
adamc@320
|
184 EFfiApp ("Basis", "htmlifyFloat", [e])
|
adamc@286
|
185 | EWrite (EFfiApp ("Basis", "htmlifyFloat", [e]), _) =>
|
adamc@286
|
186 EFfiApp ("Basis", "htmlifyFloat_w", [e])
|
adamc@286
|
187
|
adamc@286
|
188 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString",
|
adamc@286
|
189 [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]), _)]) =>
|
adamc@286
|
190 EPrim (Prim.String "True")
|
adamc@286
|
191 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString",
|
adamc@286
|
192 [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]), _)]) =>
|
adamc@286
|
193 EPrim (Prim.String "False")
|
adamc@286
|
194 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString", es), _)]) =>
|
adamc@286
|
195 EFfiApp ("Basis", "htmlifyBool", es)
|
adamc@320
|
196 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "boolToString"), _),
|
adamc@320
|
197 (ECon (Enum, PConFfi {con = "True", ...}, NONE), _)), _)]) =>
|
adamc@320
|
198 EPrim (Prim.String "True")
|
adamc@320
|
199 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "boolToString"), _),
|
adamc@320
|
200 (ECon (Enum, PConFfi {con = "False", ...}, NONE), _)), _)]) =>
|
adamc@320
|
201 EPrim (Prim.String "False")
|
adamc@320
|
202 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "boolToString"), _),
|
adamc@320
|
203 e), _)]) =>
|
adamc@320
|
204 EFfiApp ("Basis", "htmlifyBool", [e])
|
adamc@286
|
205 | EWrite (EFfiApp ("Basis", "htmlifyBool", [e]), _) =>
|
adamc@286
|
206 EFfiApp ("Basis", "htmlifyBool_w", [e])
|
adamc@286
|
207
|
adamc@436
|
208 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "timeToString"), _), e), _)]) =>
|
adamc@436
|
209 EFfiApp ("Basis", "htmlifyTime", [e])
|
adamc@436
|
210 | EFfiApp ("Basis", "htmlifyString_w", [(EApp ((EFfi ("Basis", "timeToString"), _), e), _)]) =>
|
adamc@436
|
211 EFfiApp ("Basis", "htmlifyTime_w", [e])
|
adamc@436
|
212 | EWrite (EFfiApp ("Basis", "htmlifyTime", [e]), _) =>
|
adamc@436
|
213 EFfiApp ("Basis", "htmlifyTime_w", [e])
|
adamc@436
|
214
|
adamc@135
|
215 | EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]) =>
|
adamc@135
|
216 EPrim (Prim.String (htmlifyString s))
|
adamc@135
|
217 | EWrite (EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@135
|
218 EWrite (EPrim (Prim.String (htmlifyString s)), loc)
|
adamc@135
|
219 | EWrite (EFfiApp ("Basis", "htmlifyString", [e]), _) =>
|
adamc@135
|
220 EFfiApp ("Basis", "htmlifyString_w", [e])
|
adamc@196
|
221 | EFfiApp ("Basis", "htmlifyString_w", [(EPrim (Prim.String s), loc)]) =>
|
adamc@196
|
222 EWrite (EPrim (Prim.String (htmlifyString s)), loc)
|
adamc@135
|
223
|
adamc@107
|
224 | EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@107
|
225 EPrim (Prim.String (attrifyInt n))
|
adamc@107
|
226 | EWrite (EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]), loc) =>
|
adamc@107
|
227 EWrite (EPrim (Prim.String (attrifyInt n)), loc)
|
adamc@106
|
228 | EWrite (EFfiApp ("Basis", "attrifyInt", [e]), _) =>
|
adamc@106
|
229 EFfiApp ("Basis", "attrifyInt_w", [e])
|
adamc@107
|
230
|
adamc@107
|
231 | EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@107
|
232 EPrim (Prim.String (attrifyFloat n))
|
adamc@107
|
233 | EWrite (EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]), loc) =>
|
adamc@107
|
234 EWrite (EPrim (Prim.String (attrifyFloat n)), loc)
|
adamc@106
|
235 | EWrite (EFfiApp ("Basis", "attrifyFloat", [e]), _) =>
|
adamc@106
|
236 EFfiApp ("Basis", "attrifyFloat_w", [e])
|
adamc@107
|
237
|
adamc@107
|
238 | EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]) =>
|
adamc@107
|
239 EPrim (Prim.String (attrifyString s))
|
adamc@107
|
240 | EWrite (EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@107
|
241 EWrite (EPrim (Prim.String (attrifyString s)), loc)
|
adamc@106
|
242 | EWrite (EFfiApp ("Basis", "attrifyString", [e]), _) =>
|
adamc@106
|
243 EFfiApp ("Basis", "attrifyString_w", [e])
|
adamc@106
|
244
|
adamc@721
|
245 | EFfiApp ("Basis", "attrifyCss_class", [(EPrim (Prim.String s), _)]) =>
|
adamc@721
|
246 EPrim (Prim.String s)
|
adamc@721
|
247 | EWrite (EFfiApp ("Basis", "attrifyCss_class", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@721
|
248 EWrite (EPrim (Prim.String s), loc)
|
adamc@721
|
249 | EWrite (EFfiApp ("Basis", "attrifyCss_class", [e]), _) =>
|
adamc@721
|
250 EFfiApp ("Basis", "attrifyString_w", [e])
|
adamc@721
|
251
|
adamc@120
|
252 | EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@120
|
253 EPrim (Prim.String (urlifyInt n))
|
adamc@120
|
254 | EWrite (EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]), loc) =>
|
adamc@120
|
255 EWrite (EPrim (Prim.String (urlifyInt n)), loc)
|
adamc@120
|
256 | EWrite (EFfiApp ("Basis", "urlifyInt", [e]), _) =>
|
adamc@120
|
257 EFfiApp ("Basis", "urlifyInt_w", [e])
|
adamc@120
|
258
|
adamc@120
|
259 | EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@120
|
260 EPrim (Prim.String (urlifyFloat n))
|
adamc@120
|
261 | EWrite (EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]), loc) =>
|
adamc@120
|
262 EWrite (EPrim (Prim.String (urlifyFloat n)), loc)
|
adamc@120
|
263 | EWrite (EFfiApp ("Basis", "urlifyFloat", [e]), _) =>
|
adamc@120
|
264 EFfiApp ("Basis", "urlifyFloat_w", [e])
|
adamc@120
|
265
|
adamc@120
|
266 | EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]) =>
|
adamc@120
|
267 EPrim (Prim.String (urlifyString s))
|
adamc@120
|
268 | EWrite (EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@120
|
269 EWrite (EPrim (Prim.String (urlifyString s)), loc)
|
adamc@120
|
270 | EWrite (EFfiApp ("Basis", "urlifyString", [e]), _) =>
|
adamc@120
|
271 EFfiApp ("Basis", "urlifyString_w", [e])
|
adamc@120
|
272
|
adamc@188
|
273 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]) =>
|
adamc@187
|
274 EPrim (Prim.String "1")
|
adamc@188
|
275 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]) =>
|
adamc@187
|
276 EPrim (Prim.String "0")
|
adamc@188
|
277 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]), loc) =>
|
adamc@187
|
278 EWrite (EPrim (Prim.String "1"), loc)
|
adamc@188
|
279 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]), loc) =>
|
adamc@187
|
280 EWrite (EPrim (Prim.String "0"), loc)
|
adamc@187
|
281 | EWrite (EFfiApp ("Basis", "urlifyBool", [e]), _) =>
|
adamc@187
|
282 EFfiApp ("Basis", "urlifyBool_w", [e])
|
adamc@187
|
283
|
adamc@253
|
284 | EFfiApp ("Basis", "sqlifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@253
|
285 EPrim (Prim.String (sqlifyInt n))
|
adamc@467
|
286 | EFfiApp ("Basis", "sqlifyIntN", [(ENone _, _)]) =>
|
adamc@467
|
287 EPrim (Prim.String "NULL")
|
adamc@467
|
288 | EFfiApp ("Basis", "sqlifyIntN", [(ESome (_, (EPrim (Prim.Int n), _)), _)]) =>
|
adamc@467
|
289 EPrim (Prim.String (sqlifyInt n))
|
adamc@467
|
290
|
adamc@253
|
291 | EFfiApp ("Basis", "sqlifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@253
|
292 EPrim (Prim.String (sqlifyFloat n))
|
adamc@253
|
293 | EFfiApp ("Basis", "sqlifyBool", [b as (_, loc)]) =>
|
adamc@253
|
294 optExp (ECase (b,
|
adamc@253
|
295 [((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "True", arg = NONE}, NONE), loc),
|
adamc@253
|
296 (EPrim (Prim.String "TRUE"), loc)),
|
adamc@253
|
297 ((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "False", arg = NONE}, NONE), loc),
|
adamc@253
|
298 (EPrim (Prim.String "FALSE"), loc))],
|
adamc@253
|
299 {disc = (TFfi ("Basis", "bool"), loc),
|
adamc@253
|
300 result = (TFfi ("Basis", "string"), loc)}), loc)
|
adamc@253
|
301 | EFfiApp ("Basis", "sqlifyString", [(EPrim (Prim.String n), _)]) =>
|
adamc@253
|
302 EPrim (Prim.String (sqlifyString n))
|
adamc@253
|
303
|
adamc@184
|
304 | EWrite (ECase (discE, pes, {disc, ...}), loc) =>
|
adamc@184
|
305 optExp (ECase (discE,
|
adamc@184
|
306 map (fn (p, e) => (p, (EWrite e, loc))) pes,
|
adamc@184
|
307 {disc = disc,
|
adamc@184
|
308 result = (TRecord [], loc)}), loc)
|
adamc@184
|
309
|
adamc@495
|
310 | EApp ((ECase (discE, pes, {disc, result = (TFun (_, ran), _)}), loc), arg as (ERecord [], _)) =>
|
adamc@453
|
311 let
|
adamc@453
|
312 fun doBody e =
|
adamc@453
|
313 case #1 e of
|
adamc@453
|
314 EAbs (_, _, _, body) => MonoReduce.subExpInExp (0, arg) body
|
adamc@453
|
315 | _ => (EApp (e, arg), loc)
|
adamc@453
|
316 in
|
adamc@453
|
317 optExp (ECase (discE,
|
adamc@453
|
318 map (fn (p, e) => (p, doBody e)) pes,
|
adamc@453
|
319 {disc = disc,
|
adamc@495
|
320 result = ran}), loc)
|
adamc@453
|
321 end
|
adamc@453
|
322
|
adamc@331
|
323 | EWrite (EQuery {exps, tables, state, query,
|
adamc@331
|
324 initial = (EPrim (Prim.String ""), _),
|
adamc@331
|
325 body = (EStrcat ((EPrim (Prim.String s), _),
|
adamc@331
|
326 (EStrcat ((ERel 0, _),
|
adamc@331
|
327 e'), _)), _)}, loc) =>
|
adamc@331
|
328 if CharVector.all Char.isSpace s then
|
adamc@331
|
329 EQuery {exps = exps, tables = tables, query = query,
|
adamc@331
|
330 state = (TRecord [], loc),
|
adamc@331
|
331 initial = (ERecord [], loc),
|
adamc@331
|
332 body = (optExp (EWrite e', loc), loc)}
|
adamc@331
|
333 else
|
adamc@331
|
334 e
|
adamc@331
|
335
|
adamc@334
|
336 | EWrite (EQuery {exps, tables, state, query,
|
adamc@334
|
337 initial = (EPrim (Prim.String ""), _),
|
adamc@486
|
338 body}, loc) =>
|
adamc@486
|
339 let
|
adamc@486
|
340 fun passLets (depth, (e', _), lets) =
|
adamc@486
|
341 case e' of
|
adamc@486
|
342 EStrcat ((ERel x, _), e'') =>
|
adamc@486
|
343 if x = depth then
|
adamc@486
|
344 let
|
adamc@486
|
345 val body = (optExp (EWrite e'', loc), loc)
|
adamc@486
|
346 val body = foldl (fn ((x, t, e'), e) =>
|
adamc@486
|
347 (ELet (x, t, e', e), loc))
|
adamc@486
|
348 body lets
|
adamc@486
|
349 in
|
adamc@486
|
350 EQuery {exps = exps, tables = tables, query = query,
|
adamc@486
|
351 state = (TRecord [], loc),
|
adamc@486
|
352 initial = (ERecord [], loc),
|
adamc@486
|
353 body = body}
|
adamc@486
|
354 end
|
adamc@486
|
355 else
|
adamc@486
|
356 e
|
adamc@486
|
357 | ELet (x, t, e', e'') =>
|
adamc@486
|
358 passLets (depth + 1, e'', (x, t, e') :: lets)
|
adamc@486
|
359 | _ => e
|
adamc@486
|
360 in
|
adamc@486
|
361 passLets (0, body, [])
|
adamc@486
|
362 end
|
adamc@486
|
363
|
adamc@486
|
364 (*| EWrite (EQuery {exps, tables, state, query,
|
adamc@486
|
365 initial = (EPrim (Prim.String ""), _),
|
adamc@334
|
366 body = (EStrcat ((ERel 0, _), e'), _)}, loc) =>
|
adamc@334
|
367 EQuery {exps = exps, tables = tables, query = query,
|
adamc@334
|
368 state = (TRecord [], loc),
|
adamc@334
|
369 initial = (ERecord [], loc),
|
adamc@486
|
370 body = (optExp (EWrite e', loc), loc)}*)
|
adamc@334
|
371
|
adamc@340
|
372 | EWrite (ELet (x, t, e1, e2), loc) =>
|
adamc@340
|
373 optExp (ELet (x, t, e1, (EWrite e2, loc)), loc)
|
adamc@340
|
374
|
adamc@451
|
375 | EWrite (EPrim (Prim.String ""), loc) =>
|
adamc@451
|
376 ERecord []
|
adamc@451
|
377
|
adamc@572
|
378 | ESignalBind ((ESignalReturn e1, loc), e2) =>
|
adamc@572
|
379 optExp (EApp (e2, e1), loc)
|
adamc@572
|
380
|
adamc@578
|
381 | EJavaScript (_, _, SOME (e, _)) => e
|
adamc@578
|
382
|
adamc@717
|
383 | EFfiApp ("Basis", "bless", [(se as EPrim (Prim.String s), loc)]) =>
|
adamc@717
|
384 (if !bless s then
|
adamc@717
|
385 ()
|
adamc@717
|
386 else
|
adamc@717
|
387 ErrorMsg.errorAt loc "Invalid URL passed to 'bless'";
|
adamc@717
|
388 se)
|
adamc@717
|
389
|
adamc@714
|
390 | EFfiApp ("Basis", "checkString", [(EPrim (Prim.String s), loc)]) =>
|
adamc@714
|
391 let
|
adamc@714
|
392 fun uwify (cs, acc) =
|
adamc@714
|
393 case cs of
|
adamc@714
|
394 [] => String.concat (rev acc)
|
adamc@714
|
395 | #"(" :: #"_" :: cs => uwify (cs, "(uw_" :: acc)
|
adamc@714
|
396 | #" " :: #"_" :: cs => uwify (cs, " uw_" :: acc)
|
adamc@714
|
397 | #"'" :: cs =>
|
adamc@714
|
398 let
|
adamc@714
|
399 fun waitItOut (cs, acc) =
|
adamc@714
|
400 case cs of
|
adamc@714
|
401 [] => raise Fail "MonoOpt: Unterminated SQL string literal"
|
adamc@714
|
402 | #"'" :: cs => uwify (cs, "'" :: acc)
|
adamc@714
|
403 | #"\\" :: #"'" :: cs => waitItOut (cs, "\\'" :: acc)
|
adamc@714
|
404 | #"\\" :: #"\\" :: cs => waitItOut (cs, "\\\\" :: acc)
|
adamc@714
|
405 | c :: cs => waitItOut (cs, str c :: acc)
|
adamc@714
|
406 in
|
adamc@714
|
407 waitItOut (cs, "'" :: acc)
|
adamc@714
|
408 end
|
adamc@714
|
409 | c :: cs => uwify (cs, str c :: acc)
|
adamc@714
|
410
|
adamc@714
|
411 val s = case String.explode s of
|
adamc@714
|
412 #"_" :: cs => uwify (cs, ["uw_"])
|
adamc@714
|
413 | cs => uwify (cs, [])
|
adamc@714
|
414 in
|
adamc@714
|
415 EPrim (Prim.String s)
|
adamc@714
|
416 end
|
adamc@714
|
417
|
adamc@96
|
418 | _ => e
|
adamc@96
|
419
|
adamc@105
|
420 and optExp e = #1 (U.Exp.map {typ = typ, exp = exp} e)
|
adamc@105
|
421
|
adamc@96
|
422 val optimize = U.File.map {typ = typ, exp = exp, decl = decl}
|
adamc@96
|
423
|
adamc@506
|
424 val optExp = U.Exp.map {typ = typ, exp = exp}
|
adamc@506
|
425
|
adamc@96
|
426 end
|