annotate src/mono_opt.sml @ 1124:2f53e0deffb3

Makefile fix for release
author Adam Chlipala <adamc@hcoop.net>
date Tue, 12 Jan 2010 09:58:14 -0500
parents 217eb87dde31
children 51e596feec37
rev   line source
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@1011 48 fun attrifyChar ch =
adamc@1011 49 case ch of
adamc@1011 50 #"\"" => "&quot;"
adamc@1011 51 | #"&" => "&amp;"
adamc@1059 52 | ch => str ch
adamc@1059 53
adamc@1059 54 val attrifyString = String.translate attrifyChar
adamc@1059 55
adamc@1011 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@1053 62
adamc@1059 63 val htmlifyString = String.translate (fn #"<" => "&lt;"
adamc@1059 64 | #"&" => "&amp;"
adamc@1059 65 | ch => str 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@925 77 fun urlifyString s =
adamc@925 78 case s of
adamc@925 79 "" => "_"
adamc@925 80 | _ =>
adamc@925 81 (if String.sub (s, 0) = #"_" then
adamc@925 82 "_"
adamc@925 83 else
adamc@925 84 "")
adamc@925 85 ^ String.translate (fn #" " => "+"
adamc@925 86 | ch => if Char.isAlphaNum ch then
adamc@925 87 str ch
adamc@925 88 else
adamc@925 89 "%" ^ hexIt ch) s
adamc@253 90
adamc@253 91
adamc@877 92 fun sqlifyInt n = #p_cast (Settings.currentDbms ()) (attrifyInt n, Settings.Int)
adamc@877 93 fun sqlifyFloat n = #p_cast (Settings.currentDbms ()) (attrifyFloat n, Settings.Float)
adamc@253 94
adamc@874 95 fun sqlifyString s = #sqlifyString (Settings.currentDbms ()) s
adamc@1011 96 fun sqlifyChar ch = #sqlifyString (Settings.currentDbms ()) (str ch)
adamc@874 97
adamc@874 98 fun unAs s =
adamc@874 99 let
adamc@874 100 fun doChars (cs, acc) =
adamc@874 101 case cs of
adamc@998 102 #"T" :: #"_" :: #"T" :: #"." :: cs => doChars (cs, acc)
adamc@874 103 | #"'" :: cs => doString (cs, acc)
adamc@874 104 | ch :: cs => doChars (cs, ch :: acc)
adamc@874 105 | [] => String.implode (rev acc)
adamc@874 106
adamc@874 107 and doString (cs, acc) =
adamc@874 108 case cs of
adamc@874 109 #"\\" :: #"\\" :: cs => doString (cs, #"\\" :: #"\\" :: acc)
adamc@874 110 | #"\\" :: #"'" :: cs => doString (cs, #"'" :: #"\\" :: acc)
adamc@874 111 | #"'" :: cs => doChars (cs, #"'" :: acc)
adamc@874 112 | ch :: cs => doString (cs, ch :: acc)
adamc@874 113 | [] => String.implode (rev acc)
adamc@874 114 in
adamc@874 115 doChars (String.explode s, [])
adamc@874 116 end
adamc@453 117
adamc@1065 118 fun checkUrl s = CharVector.all Char.isGraph s andalso Settings.checkUrl s
adamc@1065 119
adamc@96 120 fun exp e =
adamc@96 121 case e of
adamc@96 122 EPrim (Prim.String s) =>
adamc@96 123 let
adamc@96 124 val (_, chs) =
adamc@96 125 CharVector.foldl (fn (ch, (lastSpace, chs)) =>
adamc@96 126 let
adamc@96 127 val isSpace = Char.isSpace ch
adamc@96 128 in
adamc@96 129 if isSpace andalso lastSpace then
adamc@96 130 (true, chs)
adamc@96 131 else
adamc@96 132 (isSpace, ch :: chs)
adamc@96 133 end)
adamc@96 134 (false, []) s
adamc@96 135 in
adamc@96 136 EPrim (Prim.String (String.implode (rev chs)))
adamc@96 137 end
adamc@96 138
adamc@96 139 | EStrcat ((EPrim (Prim.String s1), loc), (EPrim (Prim.String s2), _)) =>
adamc@96 140 let
adamc@96 141 val s =
adamc@96 142 if size s1 > 0 andalso size s2 > 0
adamc@96 143 andalso Char.isSpace (String.sub (s1, size s1 - 1))
adamc@96 144 andalso Char.isSpace (String.sub (s2, 0)) then
adamc@96 145 s1 ^ String.extract (s2, 1, NONE)
adamc@96 146 else
adamc@96 147 s1 ^ s2
adamc@96 148 in
adamc@96 149 EPrim (Prim.String s)
adamc@96 150 end
adamc@105 151
adamc@105 152 | EStrcat ((EPrim (Prim.String s1), loc), (EStrcat ((EPrim (Prim.String s2), _), rest), _)) =>
adamc@105 153 let
adamc@105 154 val s =
adamc@105 155 if size s1 > 0 andalso size s2 > 0
adamc@105 156 andalso Char.isSpace (String.sub (s1, size s1 - 1))
adamc@105 157 andalso Char.isSpace (String.sub (s2, 0)) then
adamc@105 158 s1 ^ String.extract (s2, 1, NONE)
adamc@105 159 else
adamc@105 160 s1 ^ s2
adamc@105 161 in
adamc@105 162 EStrcat ((EPrim (Prim.String s), loc), rest)
adamc@105 163 end
adamc@105 164
adamc@105 165 | EStrcat ((EStrcat (e1, e2), loc), e3) =>
adamc@105 166 optExp (EStrcat (e1, (EStrcat (e2, e3), loc)), loc)
adamc@105 167
adamc@106 168 | EWrite (EStrcat (e1, e2), loc) =>
adamc@106 169 ESeq ((optExp (EWrite e1, loc), loc),
adamc@106 170 (optExp (EWrite e2, loc), loc))
adamc@106 171
adamc@183 172 | ESeq ((EWrite (EPrim (Prim.String s1), _), loc),
adamc@183 173 (EWrite (EPrim (Prim.String s2), _), _)) =>
adamc@183 174 EWrite (EPrim (Prim.String (s1 ^ s2)), loc)
adamc@196 175 | ESeq ((EWrite (EPrim (Prim.String s1), _), loc),
adamc@196 176 (ESeq ((EWrite (EPrim (Prim.String s2), _), _),
adamc@196 177 e), _)) =>
adamc@196 178 ESeq ((EWrite (EPrim (Prim.String (s1 ^ s2)), loc), loc),
adamc@196 179 e)
adamc@183 180
adamc@286 181 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "intToString", [(EPrim (Prim.Int n), _)]), _)]) =>
adamc@286 182 EPrim (Prim.String (htmlifyInt n))
adamc@286 183 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "intToString", es), _)]) =>
adamc@286 184 EFfiApp ("Basis", "htmlifyInt", es)
adamc@320 185 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "intToString"), _),
adamc@320 186 (EPrim (Prim.Int n), _)), _)]) =>
adamc@320 187 EPrim (Prim.String (htmlifyInt n))
adamc@320 188 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "intToString"), _),
adamc@320 189 e), _)]) =>
adamc@320 190 EFfiApp ("Basis", "htmlifyInt", [e])
adamc@286 191 | EWrite (EFfiApp ("Basis", "htmlifyInt", [e]), _) =>
adamc@286 192 EFfiApp ("Basis", "htmlifyInt_w", [e])
adamc@286 193
adamc@286 194 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "floatToString", [(EPrim (Prim.Float n), _)]), _)]) =>
adamc@286 195 EPrim (Prim.String (htmlifyFloat n))
adamc@286 196 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "floatToString", es), _)]) =>
adamc@286 197 EFfiApp ("Basis", "htmlifyFloat", es)
adamc@320 198 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "floatToString"), _),
adamc@320 199 (EPrim (Prim.Float n), _)), _)]) =>
adamc@320 200 EPrim (Prim.String (htmlifyFloat n))
adamc@320 201 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "floatToString"), _),
adamc@320 202 e), _)]) =>
adamc@320 203 EFfiApp ("Basis", "htmlifyFloat", [e])
adamc@286 204 | EWrite (EFfiApp ("Basis", "htmlifyFloat", [e]), _) =>
adamc@286 205 EFfiApp ("Basis", "htmlifyFloat_w", [e])
adamc@286 206
adamc@286 207 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString",
adamc@286 208 [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]), _)]) =>
adamc@286 209 EPrim (Prim.String "True")
adamc@286 210 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString",
adamc@286 211 [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]), _)]) =>
adamc@286 212 EPrim (Prim.String "False")
adamc@286 213 | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString", es), _)]) =>
adamc@286 214 EFfiApp ("Basis", "htmlifyBool", es)
adamc@320 215 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "boolToString"), _),
adamc@320 216 (ECon (Enum, PConFfi {con = "True", ...}, NONE), _)), _)]) =>
adamc@320 217 EPrim (Prim.String "True")
adamc@320 218 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "boolToString"), _),
adamc@320 219 (ECon (Enum, PConFfi {con = "False", ...}, NONE), _)), _)]) =>
adamc@320 220 EPrim (Prim.String "False")
adamc@320 221 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "boolToString"), _),
adamc@320 222 e), _)]) =>
adamc@320 223 EFfiApp ("Basis", "htmlifyBool", [e])
adamc@286 224 | EWrite (EFfiApp ("Basis", "htmlifyBool", [e]), _) =>
adamc@286 225 EFfiApp ("Basis", "htmlifyBool_w", [e])
adamc@286 226
adamc@436 227 | EFfiApp ("Basis", "htmlifyString", [(EApp ((EFfi ("Basis", "timeToString"), _), e), _)]) =>
adamc@436 228 EFfiApp ("Basis", "htmlifyTime", [e])
adamc@436 229 | EFfiApp ("Basis", "htmlifyString_w", [(EApp ((EFfi ("Basis", "timeToString"), _), e), _)]) =>
adamc@436 230 EFfiApp ("Basis", "htmlifyTime_w", [e])
adamc@436 231 | EWrite (EFfiApp ("Basis", "htmlifyTime", [e]), _) =>
adamc@436 232 EFfiApp ("Basis", "htmlifyTime_w", [e])
adamc@436 233
adamc@135 234 | EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]) =>
adamc@135 235 EPrim (Prim.String (htmlifyString s))
adamc@135 236 | EWrite (EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]), loc) =>
adamc@135 237 EWrite (EPrim (Prim.String (htmlifyString s)), loc)
adamc@135 238 | EWrite (EFfiApp ("Basis", "htmlifyString", [e]), _) =>
adamc@135 239 EFfiApp ("Basis", "htmlifyString_w", [e])
adamc@196 240 | EFfiApp ("Basis", "htmlifyString_w", [(EPrim (Prim.String s), loc)]) =>
adamc@196 241 EWrite (EPrim (Prim.String (htmlifyString s)), loc)
adamc@135 242
adamc@107 243 | EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]) =>
adamc@107 244 EPrim (Prim.String (attrifyInt n))
adamc@107 245 | EWrite (EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]), loc) =>
adamc@107 246 EWrite (EPrim (Prim.String (attrifyInt n)), loc)
adamc@106 247 | EWrite (EFfiApp ("Basis", "attrifyInt", [e]), _) =>
adamc@106 248 EFfiApp ("Basis", "attrifyInt_w", [e])
adamc@107 249
adamc@107 250 | EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]) =>
adamc@107 251 EPrim (Prim.String (attrifyFloat n))
adamc@107 252 | EWrite (EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]), loc) =>
adamc@107 253 EWrite (EPrim (Prim.String (attrifyFloat n)), loc)
adamc@106 254 | EWrite (EFfiApp ("Basis", "attrifyFloat", [e]), _) =>
adamc@106 255 EFfiApp ("Basis", "attrifyFloat_w", [e])
adamc@107 256
adamc@107 257 | EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]) =>
adamc@107 258 EPrim (Prim.String (attrifyString s))
adamc@107 259 | EWrite (EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]), loc) =>
adamc@107 260 EWrite (EPrim (Prim.String (attrifyString s)), loc)
adamc@106 261 | EWrite (EFfiApp ("Basis", "attrifyString", [e]), _) =>
adamc@106 262 EFfiApp ("Basis", "attrifyString_w", [e])
adamc@106 263
adamc@1011 264 | EFfiApp ("Basis", "attrifyChar", [(EPrim (Prim.Char s), _)]) =>
adamc@1011 265 EPrim (Prim.String (attrifyChar s))
adamc@1011 266 | EWrite (EFfiApp ("Basis", "attrifyChar", [(EPrim (Prim.Char s), _)]), loc) =>
adamc@1011 267 EWrite (EPrim (Prim.String (attrifyChar s)), loc)
adamc@1011 268 | EWrite (EFfiApp ("Basis", "attrifyChar", [e]), _) =>
adamc@1011 269 EFfiApp ("Basis", "attrifyChar_w", [e])
adamc@1011 270
adamc@721 271 | EFfiApp ("Basis", "attrifyCss_class", [(EPrim (Prim.String s), _)]) =>
adamc@721 272 EPrim (Prim.String s)
adamc@721 273 | EWrite (EFfiApp ("Basis", "attrifyCss_class", [(EPrim (Prim.String s), _)]), loc) =>
adamc@721 274 EWrite (EPrim (Prim.String s), loc)
adamc@721 275 | EWrite (EFfiApp ("Basis", "attrifyCss_class", [e]), _) =>
adamc@721 276 EFfiApp ("Basis", "attrifyString_w", [e])
adamc@721 277
adamc@120 278 | EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]) =>
adamc@120 279 EPrim (Prim.String (urlifyInt n))
adamc@120 280 | EWrite (EFfiApp ("Basis", "urlifyInt", [(EPrim (Prim.Int n), _)]), loc) =>
adamc@120 281 EWrite (EPrim (Prim.String (urlifyInt n)), loc)
adamc@120 282 | EWrite (EFfiApp ("Basis", "urlifyInt", [e]), _) =>
adamc@120 283 EFfiApp ("Basis", "urlifyInt_w", [e])
adamc@120 284
adamc@120 285 | EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]) =>
adamc@120 286 EPrim (Prim.String (urlifyFloat n))
adamc@120 287 | EWrite (EFfiApp ("Basis", "urlifyFloat", [(EPrim (Prim.Float n), _)]), loc) =>
adamc@120 288 EWrite (EPrim (Prim.String (urlifyFloat n)), loc)
adamc@120 289 | EWrite (EFfiApp ("Basis", "urlifyFloat", [e]), _) =>
adamc@120 290 EFfiApp ("Basis", "urlifyFloat_w", [e])
adamc@120 291
adamc@120 292 | EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]) =>
adamc@120 293 EPrim (Prim.String (urlifyString s))
adamc@120 294 | EWrite (EFfiApp ("Basis", "urlifyString", [(EPrim (Prim.String s), _)]), loc) =>
adamc@120 295 EWrite (EPrim (Prim.String (urlifyString s)), loc)
adamc@120 296 | EWrite (EFfiApp ("Basis", "urlifyString", [e]), _) =>
adamc@120 297 EFfiApp ("Basis", "urlifyString_w", [e])
adamc@120 298
adamc@188 299 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]) =>
adamc@187 300 EPrim (Prim.String "1")
adamc@188 301 | EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]) =>
adamc@187 302 EPrim (Prim.String "0")
adamc@188 303 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]), loc) =>
adamc@187 304 EWrite (EPrim (Prim.String "1"), loc)
adamc@188 305 | EWrite (EFfiApp ("Basis", "urlifyBool", [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]), loc) =>
adamc@187 306 EWrite (EPrim (Prim.String "0"), loc)
adamc@187 307 | EWrite (EFfiApp ("Basis", "urlifyBool", [e]), _) =>
adamc@187 308 EFfiApp ("Basis", "urlifyBool_w", [e])
adamc@187 309
adamc@253 310 | EFfiApp ("Basis", "sqlifyInt", [(EPrim (Prim.Int n), _)]) =>
adamc@253 311 EPrim (Prim.String (sqlifyInt n))
adamc@467 312 | EFfiApp ("Basis", "sqlifyIntN", [(ENone _, _)]) =>
adamc@467 313 EPrim (Prim.String "NULL")
adamc@467 314 | EFfiApp ("Basis", "sqlifyIntN", [(ESome (_, (EPrim (Prim.Int n), _)), _)]) =>
adamc@467 315 EPrim (Prim.String (sqlifyInt n))
adamc@467 316
adamc@253 317 | EFfiApp ("Basis", "sqlifyFloat", [(EPrim (Prim.Float n), _)]) =>
adamc@253 318 EPrim (Prim.String (sqlifyFloat n))
adamc@253 319 | EFfiApp ("Basis", "sqlifyBool", [b as (_, loc)]) =>
adamc@253 320 optExp (ECase (b,
adamc@253 321 [((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "True", arg = NONE}, NONE), loc),
adamc@1014 322 (EPrim (Prim.String (#trueString (Settings.currentDbms ()))), loc)),
adamc@253 323 ((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "False", arg = NONE}, NONE), loc),
adamc@1014 324 (EPrim (Prim.String (#falseString (Settings.currentDbms ()))), loc))],
adamc@253 325 {disc = (TFfi ("Basis", "bool"), loc),
adamc@253 326 result = (TFfi ("Basis", "string"), loc)}), loc)
adamc@253 327 | EFfiApp ("Basis", "sqlifyString", [(EPrim (Prim.String n), _)]) =>
adamc@253 328 EPrim (Prim.String (sqlifyString n))
adamc@1011 329 | EFfiApp ("Basis", "sqlifyChar", [(EPrim (Prim.Char n), _)]) =>
adamc@1011 330 EPrim (Prim.String (sqlifyChar n))
adamc@253 331
adamc@184 332 | EWrite (ECase (discE, pes, {disc, ...}), loc) =>
adamc@184 333 optExp (ECase (discE,
adamc@184 334 map (fn (p, e) => (p, (EWrite e, loc))) pes,
adamc@184 335 {disc = disc,
adamc@184 336 result = (TRecord [], loc)}), loc)
adamc@184 337
adamc@495 338 | EApp ((ECase (discE, pes, {disc, result = (TFun (_, ran), _)}), loc), arg as (ERecord [], _)) =>
adamc@453 339 let
adamc@453 340 fun doBody e =
adamc@453 341 case #1 e of
adamc@453 342 EAbs (_, _, _, body) => MonoReduce.subExpInExp (0, arg) body
adamc@453 343 | _ => (EApp (e, arg), loc)
adamc@453 344 in
adamc@453 345 optExp (ECase (discE,
adamc@453 346 map (fn (p, e) => (p, doBody e)) pes,
adamc@453 347 {disc = disc,
adamc@495 348 result = ran}), loc)
adamc@453 349 end
adamc@453 350
adamc@331 351 | EWrite (EQuery {exps, tables, state, query,
adamc@331 352 initial = (EPrim (Prim.String ""), _),
adamc@331 353 body = (EStrcat ((EPrim (Prim.String s), _),
adamc@331 354 (EStrcat ((ERel 0, _),
adamc@331 355 e'), _)), _)}, loc) =>
adamc@331 356 if CharVector.all Char.isSpace s then
adamc@331 357 EQuery {exps = exps, tables = tables, query = query,
adamc@331 358 state = (TRecord [], loc),
adamc@331 359 initial = (ERecord [], loc),
adamc@331 360 body = (optExp (EWrite e', loc), loc)}
adamc@331 361 else
adamc@331 362 e
adamc@331 363
adamc@334 364 | EWrite (EQuery {exps, tables, state, query,
adamc@334 365 initial = (EPrim (Prim.String ""), _),
adamc@486 366 body}, loc) =>
adamc@486 367 let
adamc@486 368 fun passLets (depth, (e', _), lets) =
adamc@486 369 case e' of
adamc@486 370 EStrcat ((ERel x, _), e'') =>
adamc@486 371 if x = depth then
adamc@486 372 let
adamc@486 373 val body = (optExp (EWrite e'', loc), loc)
adamc@486 374 val body = foldl (fn ((x, t, e'), e) =>
adamc@486 375 (ELet (x, t, e', e), loc))
adamc@486 376 body lets
adamc@486 377 in
adamc@486 378 EQuery {exps = exps, tables = tables, query = query,
adamc@486 379 state = (TRecord [], loc),
adamc@486 380 initial = (ERecord [], loc),
adamc@486 381 body = body}
adamc@486 382 end
adamc@486 383 else
adamc@486 384 e
adamc@486 385 | ELet (x, t, e', e'') =>
adamc@486 386 passLets (depth + 1, e'', (x, t, e') :: lets)
adamc@486 387 | _ => e
adamc@486 388 in
adamc@486 389 passLets (0, body, [])
adamc@486 390 end
adamc@486 391
adamc@486 392 (*| EWrite (EQuery {exps, tables, state, query,
adamc@486 393 initial = (EPrim (Prim.String ""), _),
adamc@334 394 body = (EStrcat ((ERel 0, _), e'), _)}, loc) =>
adamc@334 395 EQuery {exps = exps, tables = tables, query = query,
adamc@334 396 state = (TRecord [], loc),
adamc@334 397 initial = (ERecord [], loc),
adamc@486 398 body = (optExp (EWrite e', loc), loc)}*)
adamc@334 399
adamc@340 400 | EWrite (ELet (x, t, e1, e2), loc) =>
adamc@340 401 optExp (ELet (x, t, e1, (EWrite e2, loc)), loc)
adamc@340 402
adamc@451 403 | EWrite (EPrim (Prim.String ""), loc) =>
adamc@451 404 ERecord []
adamc@451 405
adamc@572 406 | ESignalBind ((ESignalReturn e1, loc), e2) =>
adamc@572 407 optExp (EApp (e2, e1), loc)
adamc@572 408
adamc@717 409 | EFfiApp ("Basis", "bless", [(se as EPrim (Prim.String s), loc)]) =>
adamc@1065 410 (if checkUrl s then
adamc@717 411 ()
adamc@717 412 else
adamc@769 413 ErrorMsg.errorAt loc ("Invalid URL " ^ s ^ " passed to 'bless'");
adamc@717 414 se)
adamc@1065 415 | EFfiApp ("Basis", "checkUrl", [(se as EPrim (Prim.String s), loc)]) =>
adamc@1065 416 (if checkUrl s then
adamc@1065 417 ESome ((TFfi ("Basis", "string"), loc), (se, loc))
adamc@1065 418 else
adamc@1065 419 ENone (TFfi ("Basis", "string"), loc))
adamc@741 420 | EFfiApp ("Basis", "blessMime", [(se as EPrim (Prim.String s), loc)]) =>
adamc@769 421 (if Settings.checkMime s then
adamc@741 422 ()
adamc@741 423 else
adamc@769 424 ErrorMsg.errorAt loc ("Invalid string " ^ s ^ " passed to 'blessMime'");
adamc@741 425 se)
adamc@717 426
adamc@714 427 | EFfiApp ("Basis", "checkString", [(EPrim (Prim.String s), loc)]) =>
adamc@714 428 let
adamc@714 429 fun uwify (cs, acc) =
adamc@714 430 case cs of
adamc@714 431 [] => String.concat (rev acc)
adamc@714 432 | #"(" :: #"_" :: cs => uwify (cs, "(uw_" :: acc)
adamc@714 433 | #" " :: #"_" :: cs => uwify (cs, " uw_" :: acc)
adamc@714 434 | #"'" :: cs =>
adamc@714 435 let
adamc@714 436 fun waitItOut (cs, acc) =
adamc@714 437 case cs of
adamc@714 438 [] => raise Fail "MonoOpt: Unterminated SQL string literal"
adamc@714 439 | #"'" :: cs => uwify (cs, "'" :: acc)
adamc@714 440 | #"\\" :: #"'" :: cs => waitItOut (cs, "\\'" :: acc)
adamc@714 441 | #"\\" :: #"\\" :: cs => waitItOut (cs, "\\\\" :: acc)
adamc@714 442 | c :: cs => waitItOut (cs, str c :: acc)
adamc@714 443 in
adamc@714 444 waitItOut (cs, "'" :: acc)
adamc@714 445 end
adamc@714 446 | c :: cs => uwify (cs, str c :: acc)
adamc@714 447
adamc@714 448 val s = case String.explode s of
adamc@714 449 #"_" :: cs => uwify (cs, ["uw_"])
adamc@714 450 | cs => uwify (cs, [])
adamc@714 451 in
adamc@714 452 EPrim (Prim.String s)
adamc@714 453 end
adamc@714 454
adamc@754 455 | EFfiApp ("Basis", "viewify", [(EPrim (Prim.String s), loc)]) =>
adamc@754 456 let
adamc@754 457 fun uwify (cs, acc) =
adamc@754 458 case cs of
adamc@754 459 [] => String.concat (rev acc)
adamc@754 460 | #"A" :: #"S" :: #" " :: #"_" :: cs => uwify (cs, "AS uw_" :: acc)
adamc@754 461 | #"'" :: cs =>
adamc@754 462 let
adamc@754 463 fun waitItOut (cs, acc) =
adamc@754 464 case cs of
adamc@754 465 [] => raise Fail "MonoOpt: Unterminated SQL string literal"
adamc@754 466 | #"'" :: cs => uwify (cs, "'" :: acc)
adamc@754 467 | #"\\" :: #"'" :: cs => waitItOut (cs, "\\'" :: acc)
adamc@754 468 | #"\\" :: #"\\" :: cs => waitItOut (cs, "\\\\" :: acc)
adamc@754 469 | c :: cs => waitItOut (cs, str c :: acc)
adamc@754 470 in
adamc@754 471 waitItOut (cs, "'" :: acc)
adamc@754 472 end
adamc@754 473 | c :: cs => uwify (cs, str c :: acc)
adamc@754 474
adamc@754 475 val s = uwify (String.explode s, [])
adamc@754 476 in
adamc@754 477 EPrim (Prim.String s)
adamc@754 478 end
adamc@754 479
adamc@874 480 | EFfiApp ("Basis", "unAs", [(EPrim (Prim.String s), _)]) =>
adamc@874 481 EPrim (Prim.String (unAs s))
adamc@874 482 | EFfiApp ("Basis", "unAs", [e']) =>
adamc@874 483 let
adamc@874 484 fun parts (e as (_, loc)) =
adamc@874 485 case #1 e of
adamc@874 486 EStrcat (s1, s2) =>
adamc@874 487 (case (parts s1, parts s2) of
adamc@874 488 (SOME p1, SOME p2) => SOME (p1 @ p2)
adamc@874 489 | _ => NONE)
adamc@874 490 | EPrim (Prim.String s) => SOME [(EPrim (Prim.String (unAs s)), loc)]
adamc@874 491 | EFfiApp ("Basis", f, [_]) =>
adamc@874 492 if String.isPrefix "sqlify" f then
adamc@874 493 SOME [e]
adamc@874 494 else
adamc@874 495 NONE
adamc@874 496 | _ => NONE
adamc@874 497 in
adamc@874 498 case parts e' of
adamc@874 499 SOME [e] => #1 e
adamc@874 500 | SOME es =>
adamc@874 501 (case rev es of
adamc@874 502 (e as (_, loc)) :: es => #1 (foldl (fn (e, es) => (EStrcat (e, es), loc)) e es)
adamc@874 503 | [] => raise Fail "MonoOpt impossible nil")
adamc@874 504 | NONE => e
adamc@874 505 end
adamc@1024 506
adamc@1024 507 | EFfiApp ("Basis", "str1", [(EPrim (Prim.Char ch), _)]) =>
adamc@1024 508 EPrim (Prim.String (str ch))
adamc@1024 509 | EFfiApp ("Basis", "attrifyString", [(EFfiApp ("Basis", "str1", [e]), _)]) =>
adamc@1024 510 EFfiApp ("Basis", "attrifyChar", [e])
adamc@1024 511 | EFfiApp ("Basis", "attrifyString_w", [(EFfiApp ("Basis", "str1", [e]), _)]) =>
adamc@1024 512 EFfiApp ("Basis", "attrifyChar_w", [e])
adamc@874 513
adamc@96 514 | _ => e
adamc@96 515
adamc@105 516 and optExp e = #1 (U.Exp.map {typ = typ, exp = exp} e)
adamc@105 517
adamc@96 518 val optimize = U.File.map {typ = typ, exp = exp, decl = decl}
adamc@96 519
adamc@506 520 val optExp = U.Exp.map {typ = typ, exp = exp}
adamc@506 521
adamc@96 522 end