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@120
|
245 | EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@120
|
246 EPrim (Prim.String (urlifyInt n))
|
adamc@120
|
247 | EWrite (EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]), loc) =>
|
adamc@120
|
248 EWrite (EPrim (Prim.String (urlifyInt n)), loc)
|
adamc@120
|
249 | EWrite (EFfiApp ("Basis", "urlifyInt", [e]), _) =>
|
adamc@120
|
250 EFfiApp ("Basis", "urlifyInt_w", [e])
|
adamc@120
|
251
|
adamc@120
|
252 | EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@120
|
253 EPrim (Prim.String (urlifyFloat n))
|
adamc@120
|
254 | EWrite (EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]), loc) =>
|
adamc@120
|
255 EWrite (EPrim (Prim.String (urlifyFloat n)), loc)
|
adamc@120
|
256 | EWrite (EFfiApp ("Basis", "urlifyFloat", [e]), _) =>
|
adamc@120
|
257 EFfiApp ("Basis", "urlifyFloat_w", [e])
|
adamc@120
|
258
|
adamc@120
|
259 | EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]) =>
|
adamc@120
|
260 EPrim (Prim.String (urlifyString s))
|
adamc@120
|
261 | EWrite (EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]), loc) =>
|
adamc@120
|
262 EWrite (EPrim (Prim.String (urlifyString s)), loc)
|
adamc@120
|
263 | EWrite (EFfiApp ("Basis", "urlifyString", [e]), _) =>
|
adamc@120
|
264 EFfiApp ("Basis", "urlifyString_w", [e])
|
adamc@120
|
265
|
adamc@188
|
266 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]) =>
|
adamc@187
|
267 EPrim (Prim.String "1")
|
adamc@188
|
268 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]) =>
|
adamc@187
|
269 EPrim (Prim.String "0")
|
adamc@188
|
270 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]), loc) =>
|
adamc@187
|
271 EWrite (EPrim (Prim.String "1"), loc)
|
adamc@188
|
272 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]), loc) =>
|
adamc@187
|
273 EWrite (EPrim (Prim.String "0"), loc)
|
adamc@187
|
274 | EWrite (EFfiApp ("Basis", "urlifyBool", [e]), _) =>
|
adamc@187
|
275 EFfiApp ("Basis", "urlifyBool_w", [e])
|
adamc@187
|
276
|
adamc@253
|
277 | EFfiApp ("Basis", "sqlifyInt", [(EPrim (Prim.Int n), _)]) =>
|
adamc@253
|
278 EPrim (Prim.String (sqlifyInt n))
|
adamc@467
|
279 | EFfiApp ("Basis", "sqlifyIntN", [(ENone _, _)]) =>
|
adamc@467
|
280 EPrim (Prim.String "NULL")
|
adamc@467
|
281 | EFfiApp ("Basis", "sqlifyIntN", [(ESome (_, (EPrim (Prim.Int n), _)), _)]) =>
|
adamc@467
|
282 EPrim (Prim.String (sqlifyInt n))
|
adamc@467
|
283
|
adamc@253
|
284 | EFfiApp ("Basis", "sqlifyFloat", [(EPrim (Prim.Float n), _)]) =>
|
adamc@253
|
285 EPrim (Prim.String (sqlifyFloat n))
|
adamc@253
|
286 | EFfiApp ("Basis", "sqlifyBool", [b as (_, loc)]) =>
|
adamc@253
|
287 optExp (ECase (b,
|
adamc@253
|
288 [((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "True", arg = NONE}, NONE), loc),
|
adamc@253
|
289 (EPrim (Prim.String "TRUE"), loc)),
|
adamc@253
|
290 ((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "False", arg = NONE}, NONE), loc),
|
adamc@253
|
291 (EPrim (Prim.String "FALSE"), loc))],
|
adamc@253
|
292 {disc = (TFfi ("Basis", "bool"), loc),
|
adamc@253
|
293 result = (TFfi ("Basis", "string"), loc)}), loc)
|
adamc@253
|
294 | EFfiApp ("Basis", "sqlifyString", [(EPrim (Prim.String n), _)]) =>
|
adamc@253
|
295 EPrim (Prim.String (sqlifyString n))
|
adamc@253
|
296
|
adamc@184
|
297 | EWrite (ECase (discE, pes, {disc, ...}), loc) =>
|
adamc@184
|
298 optExp (ECase (discE,
|
adamc@184
|
299 map (fn (p, e) => (p, (EWrite e, loc))) pes,
|
adamc@184
|
300 {disc = disc,
|
adamc@184
|
301 result = (TRecord [], loc)}), loc)
|
adamc@184
|
302
|
adamc@495
|
303 | EApp ((ECase (discE, pes, {disc, result = (TFun (_, ran), _)}), loc), arg as (ERecord [], _)) =>
|
adamc@453
|
304 let
|
adamc@453
|
305 fun doBody e =
|
adamc@453
|
306 case #1 e of
|
adamc@453
|
307 EAbs (_, _, _, body) => MonoReduce.subExpInExp (0, arg) body
|
adamc@453
|
308 | _ => (EApp (e, arg), loc)
|
adamc@453
|
309 in
|
adamc@453
|
310 optExp (ECase (discE,
|
adamc@453
|
311 map (fn (p, e) => (p, doBody e)) pes,
|
adamc@453
|
312 {disc = disc,
|
adamc@495
|
313 result = ran}), loc)
|
adamc@453
|
314 end
|
adamc@453
|
315
|
adamc@331
|
316 | EWrite (EQuery {exps, tables, state, query,
|
adamc@331
|
317 initial = (EPrim (Prim.String ""), _),
|
adamc@331
|
318 body = (EStrcat ((EPrim (Prim.String s), _),
|
adamc@331
|
319 (EStrcat ((ERel 0, _),
|
adamc@331
|
320 e'), _)), _)}, loc) =>
|
adamc@331
|
321 if CharVector.all Char.isSpace s then
|
adamc@331
|
322 EQuery {exps = exps, tables = tables, query = query,
|
adamc@331
|
323 state = (TRecord [], loc),
|
adamc@331
|
324 initial = (ERecord [], loc),
|
adamc@331
|
325 body = (optExp (EWrite e', loc), loc)}
|
adamc@331
|
326 else
|
adamc@331
|
327 e
|
adamc@331
|
328
|
adamc@334
|
329 | EWrite (EQuery {exps, tables, state, query,
|
adamc@334
|
330 initial = (EPrim (Prim.String ""), _),
|
adamc@486
|
331 body}, loc) =>
|
adamc@486
|
332 let
|
adamc@486
|
333 fun passLets (depth, (e', _), lets) =
|
adamc@486
|
334 case e' of
|
adamc@486
|
335 EStrcat ((ERel x, _), e'') =>
|
adamc@486
|
336 if x = depth then
|
adamc@486
|
337 let
|
adamc@486
|
338 val body = (optExp (EWrite e'', loc), loc)
|
adamc@486
|
339 val body = foldl (fn ((x, t, e'), e) =>
|
adamc@486
|
340 (ELet (x, t, e', e), loc))
|
adamc@486
|
341 body lets
|
adamc@486
|
342 in
|
adamc@486
|
343 EQuery {exps = exps, tables = tables, query = query,
|
adamc@486
|
344 state = (TRecord [], loc),
|
adamc@486
|
345 initial = (ERecord [], loc),
|
adamc@486
|
346 body = body}
|
adamc@486
|
347 end
|
adamc@486
|
348 else
|
adamc@486
|
349 e
|
adamc@486
|
350 | ELet (x, t, e', e'') =>
|
adamc@486
|
351 passLets (depth + 1, e'', (x, t, e') :: lets)
|
adamc@486
|
352 | _ => e
|
adamc@486
|
353 in
|
adamc@486
|
354 passLets (0, body, [])
|
adamc@486
|
355 end
|
adamc@486
|
356
|
adamc@486
|
357 (*| EWrite (EQuery {exps, tables, state, query,
|
adamc@486
|
358 initial = (EPrim (Prim.String ""), _),
|
adamc@334
|
359 body = (EStrcat ((ERel 0, _), e'), _)}, loc) =>
|
adamc@334
|
360 EQuery {exps = exps, tables = tables, query = query,
|
adamc@334
|
361 state = (TRecord [], loc),
|
adamc@334
|
362 initial = (ERecord [], loc),
|
adamc@486
|
363 body = (optExp (EWrite e', loc), loc)}*)
|
adamc@334
|
364
|
adamc@340
|
365 | EWrite (ELet (x, t, e1, e2), loc) =>
|
adamc@340
|
366 optExp (ELet (x, t, e1, (EWrite e2, loc)), loc)
|
adamc@340
|
367
|
adamc@451
|
368 | EWrite (EPrim (Prim.String ""), loc) =>
|
adamc@451
|
369 ERecord []
|
adamc@451
|
370
|
adamc@572
|
371 | ESignalBind ((ESignalReturn e1, loc), e2) =>
|
adamc@572
|
372 optExp (EApp (e2, e1), loc)
|
adamc@572
|
373
|
adamc@578
|
374 | EJavaScript (_, _, SOME (e, _)) => e
|
adamc@578
|
375
|
adamc@717
|
376 | EFfiApp ("Basis", "bless", [(se as EPrim (Prim.String s), loc)]) =>
|
adamc@717
|
377 (if !bless s then
|
adamc@717
|
378 ()
|
adamc@717
|
379 else
|
adamc@717
|
380 ErrorMsg.errorAt loc "Invalid URL passed to 'bless'";
|
adamc@717
|
381 se)
|
adamc@717
|
382
|
adamc@714
|
383 | EFfiApp ("Basis", "checkString", [(EPrim (Prim.String s), loc)]) =>
|
adamc@714
|
384 let
|
adamc@714
|
385 fun uwify (cs, acc) =
|
adamc@714
|
386 case cs of
|
adamc@714
|
387 [] => String.concat (rev acc)
|
adamc@714
|
388 | #"(" :: #"_" :: cs => uwify (cs, "(uw_" :: acc)
|
adamc@714
|
389 | #" " :: #"_" :: cs => uwify (cs, " uw_" :: acc)
|
adamc@714
|
390 | #"'" :: cs =>
|
adamc@714
|
391 let
|
adamc@714
|
392 fun waitItOut (cs, acc) =
|
adamc@714
|
393 case cs of
|
adamc@714
|
394 [] => raise Fail "MonoOpt: Unterminated SQL string literal"
|
adamc@714
|
395 | #"'" :: cs => uwify (cs, "'" :: acc)
|
adamc@714
|
396 | #"\\" :: #"'" :: cs => waitItOut (cs, "\\'" :: acc)
|
adamc@714
|
397 | #"\\" :: #"\\" :: cs => waitItOut (cs, "\\\\" :: acc)
|
adamc@714
|
398 | c :: cs => waitItOut (cs, str c :: acc)
|
adamc@714
|
399 in
|
adamc@714
|
400 waitItOut (cs, "'" :: acc)
|
adamc@714
|
401 end
|
adamc@714
|
402 | c :: cs => uwify (cs, str c :: acc)
|
adamc@714
|
403
|
adamc@714
|
404 val s = case String.explode s of
|
adamc@714
|
405 #"_" :: cs => uwify (cs, ["uw_"])
|
adamc@714
|
406 | cs => uwify (cs, [])
|
adamc@714
|
407 in
|
adamc@714
|
408 EPrim (Prim.String s)
|
adamc@714
|
409 end
|
adamc@714
|
410
|
adamc@96
|
411 | _ => e
|
adamc@96
|
412
|
adamc@105
|
413 and optExp e = #1 (U.Exp.map {typ = typ, exp = exp} e)
|
adamc@105
|
414
|
adamc@96
|
415 val optimize = U.File.map {typ = typ, exp = exp, decl = decl}
|
adamc@96
|
416
|
adamc@506
|
417 val optExp = U.Exp.map {typ = typ, exp = exp}
|
adamc@506
|
418
|
adamc@96
|
419 end
|