annotate src/cjr_print.sml @ 143:4b9c2bd6157c

Almost ready to have a form work
author Adam Chlipala <adamc@hcoop.net>
date Sun, 20 Jul 2008 13:30:19 -0400
parents 133fa2d51bb4
children f0d3402184d1
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 (* Pretty-printing C jr. *)
adamc@29 29
adamc@29 30 structure CjrPrint :> CJR_PRINT = struct
adamc@29 31
adamc@29 32 open Print.PD
adamc@29 33 open Print
adamc@29 34
adamc@29 35 open Cjr
adamc@29 36
adamc@29 37 structure E = CjrEnv
adamc@29 38 structure EM = ErrorMsg
adamc@29 39
adamc@29 40 val debug = ref false
adamc@29 41
adamc@29 42 val dummyTyp = (TNamed 0, ErrorMsg.dummySpan)
adamc@29 43
adamc@29 44 fun p_typ' par env (t, loc) =
adamc@29 45 case t of
adamc@101 46 TTop => string "void*"
adamc@109 47 | TFun (t1, t2) => parenIf par (box [p_typ' true env t2,
adamc@109 48 space,
adamc@109 49 string "(*)",
adamc@109 50 space,
adamc@109 51 string "(",
adamc@109 52 p_typ env t1,
adamc@109 53 string ")"])
adamc@29 54 | TRecord i => box [string "struct",
adamc@29 55 space,
adamc@29 56 string "__lws_",
adamc@29 57 string (Int.toString i)]
adamc@29 58 | TNamed n =>
adamc@29 59 (string ("__lwt_" ^ #1 (E.lookupTNamed env n) ^ "_" ^ Int.toString n)
adamc@29 60 handle CjrEnv.UnboundNamed _ => string ("__lwt_UNBOUND__" ^ Int.toString n))
adamc@53 61 | TFfi (m, x) => box [string "lw_", string m, string "_", string x]
adamc@29 62
adamc@29 63 and p_typ env = p_typ' false env
adamc@29 64
adamc@29 65 fun p_rel env n = string ("__lwr_" ^ #1 (E.lookupERel env n) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 66 handle CjrEnv.UnboundRel _ => string ("__lwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 67
adamc@109 68 fun p_enamed env n =
adamc@109 69 string ("__lwn_" ^ #1 (E.lookupENamed env n) ^ "_" ^ Int.toString n)
adamc@109 70 handle CjrEnv.UnboundNamed _ => string ("__lwn_UNBOUND_" ^ Int.toString n)
adamc@109 71
adamc@29 72 fun p_exp' par env (e, _) =
adamc@29 73 case e of
adamc@29 74 EPrim p => Prim.p_t p
adamc@29 75 | ERel n => p_rel env n
adamc@109 76 | ENamed n => p_enamed env n
adamc@109 77
adamc@53 78 | EFfi (m, x) => box [string "lw_", string m, string "_", string x]
adamc@53 79 | EFfiApp (m, x, es) => box [string "lw_",
adamc@53 80 string m,
adamc@53 81 string "_",
adamc@53 82 string x,
adamc@117 83 string "(ctx, ",
adamc@53 84 p_list (p_exp env) es,
adamc@53 85 string ")"]
adamc@129 86 | EApp (e1, e2) =>
adamc@129 87 let
adamc@129 88 fun unravel (f, acc) =
adamc@129 89 case #1 f of
adamc@129 90 EApp (f', arg) => unravel (f', arg :: acc)
adamc@129 91 | _ => (f, acc)
adamc@129 92
adamc@129 93 val (f, args) = unravel (e1, [e2])
adamc@129 94 in
adamc@129 95 parenIf par (box [p_exp' true env e1,
adamc@129 96 string "(ctx,",
adamc@129 97 space,
adamc@129 98 p_list_sep (box [string ",", space]) (p_exp env) args,
adamc@129 99 string ")"])
adamc@129 100 end
adamc@29 101
adamc@29 102 | ERecord (i, xes) => box [string "({",
adamc@29 103 space,
adamc@29 104 string "struct",
adamc@29 105 space,
adamc@29 106 string ("__lws_" ^ Int.toString i),
adamc@29 107 space,
adamc@29 108 string "__lw_tmp",
adamc@29 109 space,
adamc@29 110 string "=",
adamc@29 111 space,
adamc@29 112 string "{",
adamc@29 113 p_list (fn (_, e) =>
adamc@29 114 p_exp env e) xes,
adamc@29 115 string "};",
adamc@29 116 space,
adamc@29 117 string "__lw_tmp;",
adamc@29 118 space,
adamc@29 119 string "})" ]
adamc@29 120 | EField (e, x) =>
adamc@29 121 box [p_exp' true env e,
adamc@29 122 string ".",
adamc@29 123 string x]
adamc@29 124
adamc@117 125 | EWrite e => box [string "(lw_write(ctx, ",
adamc@102 126 p_exp env e,
adamc@102 127 string "), lw_unit_v)"]
adamc@102 128
adamc@106 129 | ESeq (e1, e2) => box [string "(",
adamc@106 130 p_exp env e1,
adamc@106 131 string ",",
adamc@106 132 space,
adamc@106 133 p_exp env e2,
adamc@106 134 string ")"]
adamc@106 135
adamc@29 136 and p_exp env = p_exp' false env
adamc@29 137
adamc@129 138 fun p_fun env (fx, n, args, ran, e) =
adamc@129 139 let
adamc@129 140 val nargs = length args
adamc@129 141 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
adamc@129 142 in
adamc@129 143 box [string "static",
adamc@129 144 space,
adamc@129 145 p_typ env ran,
adamc@129 146 space,
adamc@129 147 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
adamc@129 148 string "(",
adamc@129 149 p_list_sep (box [string ",", space]) (fn x => x)
adamc@129 150 (string "lw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
adamc@129 151 box [p_typ env dom,
adamc@129 152 space,
adamc@129 153 p_rel env' (nargs - i - 1)]) args),
adamc@129 154 string ")",
adamc@129 155 space,
adamc@129 156 string "{",
adamc@129 157 newline,
adamc@129 158 box[string "return(",
adamc@129 159 p_exp env' e,
adamc@129 160 string ");"],
adamc@129 161 newline,
adamc@129 162 string "}"]
adamc@129 163 end
adamc@129 164
adamc@129 165 fun p_decl env (dAll as (d, _) : decl) =
adamc@29 166 case d of
adamc@29 167 DStruct (n, xts) =>
adamc@29 168 box [string "struct",
adamc@29 169 space,
adamc@29 170 string ("__lws_" ^ Int.toString n),
adamc@29 171 space,
adamc@29 172 string "{",
adamc@29 173 newline,
adamc@29 174 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@29 175 space,
adamc@29 176 string x,
adamc@29 177 string ";",
adamc@29 178 newline]) xts,
adamc@29 179 string "};"]
adamc@29 180
adamc@29 181 | DVal (x, n, t, e) =>
adamc@29 182 box [p_typ env t,
adamc@29 183 space,
adamc@29 184 string ("__lwn_" ^ x ^ "_" ^ Int.toString n),
adamc@29 185 space,
adamc@29 186 string "=",
adamc@29 187 space,
adamc@29 188 p_exp env e,
adamc@29 189 string ";"]
adamc@129 190 | DFun vi => p_fun env vi
adamc@129 191 | DFunRec vis =>
adamc@29 192 let
adamc@129 193 val env = E.declBinds env dAll
adamc@29 194 in
adamc@129 195 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
adamc@129 196 box [string "static",
adamc@129 197 space,
adamc@129 198 p_typ env ran,
adamc@129 199 space,
adamc@129 200 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
adamc@129 201 string "(lw_context,",
adamc@129 202 space,
adamc@129 203 p_list_sep (box [string ",", space])
adamc@129 204 (fn (_, dom) => p_typ env dom) args,
adamc@129 205 string ");"]) vis,
adamc@29 206 newline,
adamc@129 207 p_list_sep newline (p_fun env) vis,
adamc@129 208 newline]
adamc@29 209 end
adamc@29 210
adamc@143 211 fun unurlify env (t, loc) =
adamc@120 212 case t of
adamc@120 213 TFfi ("Basis", "int") => string "lw_unurlifyInt(&request)"
adamc@120 214 | TFfi ("Basis", "float") => string "lw_unurlifyFloat(&request)"
adamc@136 215 | TFfi ("Basis", "string") => string "lw_unurlifyString(ctx, &request)"
adamc@120 216
adamc@120 217 | TRecord 0 => string "lw_unit_v"
adamc@143 218 | TRecord i =>
adamc@143 219 let
adamc@143 220 val xts = E.lookupStruct env i
adamc@143 221 in
adamc@143 222 box [string "({",
adamc@143 223 newline,
adamc@143 224 box (map (fn (x, t) =>
adamc@143 225 box [p_typ env t,
adamc@143 226 space,
adamc@143 227 string x,
adamc@143 228 space,
adamc@143 229 string "=",
adamc@143 230 space,
adamc@143 231 unurlify env t,
adamc@143 232 string ";",
adamc@143 233 newline]) xts),
adamc@143 234 string "struct",
adamc@143 235 space,
adamc@143 236 string "__lws_",
adamc@143 237 string (Int.toString i),
adamc@143 238 space,
adamc@143 239 string "__lw_tmp",
adamc@143 240 space,
adamc@143 241 string "=",
adamc@143 242 space,
adamc@143 243 string "{",
adamc@143 244 space,
adamc@143 245 p_list_sep (box [string ",", space]) (fn (x, _) => string x) xts,
adamc@143 246 space,
adamc@143 247 string "};",
adamc@143 248 newline,
adamc@143 249 string "__lw_tmp;",
adamc@143 250 newline,
adamc@143 251 string "})"]
adamc@143 252 end
adamc@120 253
adamc@120 254 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
adamc@120 255 space)
adamc@120 256
adamc@120 257 fun p_page env (s, n, ts) =
adamc@120 258 box [string "if (!strncmp(request, \"",
adamc@116 259 string (String.toString s),
adamc@120 260 string "\", ",
adamc@120 261 string (Int.toString (size s)),
adamc@120 262 string ")) {",
adamc@116 263 newline,
adamc@120 264 string "request += ",
adamc@120 265 string (Int.toString (size s)),
adamc@120 266 string ";",
adamc@116 267 newline,
adamc@120 268 string "if (*request == '/') ++request;",
adamc@119 269 newline,
adamc@120 270 box [string "{",
adamc@120 271 newline,
adamc@120 272 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
adamc@120 273 space,
adamc@120 274 string "arg",
adamc@120 275 string (Int.toString i),
adamc@120 276 space,
adamc@120 277 string "=",
adamc@120 278 space,
adamc@143 279 unurlify env t,
adamc@120 280 string ";",
adamc@120 281 newline]) ts),
adamc@120 282 p_enamed env n,
adamc@120 283 string "(",
adamc@120 284 p_list_sep (box [string ",", space])
adamc@120 285 (fn x => x)
adamc@120 286 (string "ctx" :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
adamc@120 287 string ");",
adamc@120 288 newline,
adamc@120 289 string "return;",
adamc@120 290 newline,
adamc@120 291 string "}",
adamc@120 292 newline,
adamc@120 293 string "}"]
adamc@120 294 ]
adamc@101 295
adamc@101 296 fun p_file env (ds, ps) =
adamc@29 297 let
adamc@101 298 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 299 (p_decl env d,
adamc@31 300 E.declBinds env d))
adamc@101 301 env ds
adamc@101 302 val pds' = map (p_page env) ps
adamc@29 303 in
adamc@101 304 box [string "#include \"lacweb.h\"",
adamc@101 305 newline,
adamc@101 306 newline,
adamc@101 307 p_list_sep newline (fn x => x) pds,
adamc@101 308 newline,
adamc@117 309 string "void lw_handle(lw_context ctx, char *request) {",
adamc@101 310 newline,
adamc@101 311 p_list_sep newline (fn x => x) pds',
adamc@101 312 newline,
adamc@101 313 string "}",
adamc@101 314 newline]
adamc@29 315 end
adamc@29 316
adamc@29 317 end