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