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@144
|
40 structure SK = struct
|
adamc@144
|
41 type ord_key = string
|
adamc@144
|
42 val compare = String.compare
|
adamc@144
|
43 end
|
adamc@144
|
44
|
adamc@144
|
45 structure SS = BinarySetFn(SK)
|
adamc@144
|
46 structure SM = BinaryMapFn(SK)
|
adamc@144
|
47 structure IS = IntBinarySet
|
adamc@144
|
48
|
adamc@144
|
49 structure CM = BinaryMapFn(struct
|
adamc@144
|
50 type ord_key = char
|
adamc@144
|
51 val compare = Char.compare
|
adamc@144
|
52 end)
|
adamc@144
|
53
|
adamc@29
|
54 val debug = ref false
|
adamc@29
|
55
|
adamc@196
|
56 val dummyTyp = (TDatatype (Enum, 0, ref []), ErrorMsg.dummySpan)
|
adamc@29
|
57
|
adamc@29
|
58 fun p_typ' par env (t, loc) =
|
adamc@29
|
59 case t of
|
adamc@101
|
60 TTop => string "void*"
|
adamc@109
|
61 | TFun (t1, t2) => parenIf par (box [p_typ' true env t2,
|
adamc@109
|
62 space,
|
adamc@109
|
63 string "(*)",
|
adamc@109
|
64 space,
|
adamc@109
|
65 string "(",
|
adamc@109
|
66 p_typ env t1,
|
adamc@109
|
67 string ")"])
|
adamc@29
|
68 | TRecord i => box [string "struct",
|
adamc@29
|
69 space,
|
adamc@29
|
70 string "__lws_",
|
adamc@29
|
71 string (Int.toString i)]
|
adamc@188
|
72 | TDatatype (Enum, n, _) =>
|
adamc@188
|
73 (box [string "enum",
|
adamc@188
|
74 space,
|
adamc@188
|
75 string ("__lwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
|
adamc@188
|
76 handle CjrEnv.UnboundNamed _ => string ("__lwd_UNBOUND__" ^ Int.toString n))
|
adamc@198
|
77 | TDatatype (Option, n, xncs) =>
|
adamc@198
|
78 (case ListUtil.search #3 (!xncs) of
|
adamc@198
|
79 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
|
adamc@198
|
80 | SOME t =>
|
adamc@198
|
81 case #1 t of
|
adamc@198
|
82 TDatatype _ => p_typ' par env t
|
adamc@198
|
83 | _ => box [p_typ' par env t,
|
adamc@198
|
84 string "*"])
|
adamc@188
|
85 | TDatatype (Default, n, _) =>
|
adamc@165
|
86 (box [string "struct",
|
adamc@165
|
87 space,
|
adamc@166
|
88 string ("__lwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
|
adamc@166
|
89 handle CjrEnv.UnboundNamed _ => string ("__lwd_UNBOUND__" ^ Int.toString n))
|
adamc@53
|
90 | TFfi (m, x) => box [string "lw_", string m, string "_", string x]
|
adamc@29
|
91
|
adamc@29
|
92 and p_typ env = p_typ' false env
|
adamc@29
|
93
|
adamc@29
|
94 fun p_rel env n = string ("__lwr_" ^ #1 (E.lookupERel env n) ^ "_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@29
|
95 handle CjrEnv.UnboundRel _ => string ("__lwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@29
|
96
|
adamc@109
|
97 fun p_enamed env n =
|
adamc@109
|
98 string ("__lwn_" ^ #1 (E.lookupENamed env n) ^ "_" ^ Int.toString n)
|
adamc@109
|
99 handle CjrEnv.UnboundNamed _ => string ("__lwn_UNBOUND_" ^ Int.toString n)
|
adamc@109
|
100
|
adamc@182
|
101 fun p_con_named env n =
|
adamc@182
|
102 string ("__lwc_" ^ #1 (E.lookupConstructor env n) ^ "_" ^ Int.toString n)
|
adamc@182
|
103 handle CjrEnv.UnboundNamed _ => string ("__lwc_UNBOUND_" ^ Int.toString n)
|
adamc@182
|
104
|
adamc@182
|
105 fun p_pat_preamble env (p, _) =
|
adamc@182
|
106 case p of
|
adamc@182
|
107 PWild => (box [],
|
adamc@182
|
108 env)
|
adamc@182
|
109 | PVar (x, t) => (box [p_typ env t,
|
adamc@182
|
110 space,
|
adamc@182
|
111 string "__lwr_",
|
adamc@182
|
112 string x,
|
adamc@182
|
113 string "_",
|
adamc@182
|
114 string (Int.toString (E.countERels env)),
|
adamc@182
|
115 string ";",
|
adamc@182
|
116 newline],
|
adamc@196
|
117 E.pushERel env x t)
|
adamc@182
|
118 | PPrim _ => (box [], env)
|
adamc@188
|
119 | PCon (_, _, NONE) => (box [], env)
|
adamc@188
|
120 | PCon (_, _, SOME p) => p_pat_preamble env p
|
adamc@182
|
121 | PRecord xps =>
|
adamc@182
|
122 foldl (fn ((_, p, _), (pp, env)) =>
|
adamc@182
|
123 let
|
adamc@182
|
124 val (pp', env) = p_pat_preamble env p
|
adamc@182
|
125 in
|
adamc@182
|
126 (box [pp', pp], env)
|
adamc@182
|
127 end) (box [], env) xps
|
adamc@182
|
128
|
adamc@182
|
129 fun p_patCon env pc =
|
adamc@182
|
130 case pc of
|
adamc@182
|
131 PConVar n => p_con_named env n
|
adamc@186
|
132 | PConFfi {mod = m, con, ...} => string ("lw_" ^ m ^ "_" ^ con)
|
adamc@182
|
133
|
adamc@182
|
134 fun p_pat (env, exit, depth) (p, _) =
|
adamc@182
|
135 case p of
|
adamc@182
|
136 PWild =>
|
adamc@182
|
137 (box [], env)
|
adamc@182
|
138 | PVar (x, t) =>
|
adamc@182
|
139 (box [string "__lwr_",
|
adamc@182
|
140 string x,
|
adamc@182
|
141 string "_",
|
adamc@182
|
142 string (Int.toString (E.countERels env)),
|
adamc@182
|
143 space,
|
adamc@182
|
144 string "=",
|
adamc@182
|
145 space,
|
adamc@182
|
146 string "disc",
|
adamc@182
|
147 string (Int.toString depth),
|
adamc@182
|
148 string ";"],
|
adamc@182
|
149 E.pushERel env x t)
|
adamc@182
|
150 | PPrim (Prim.Int n) =>
|
adamc@182
|
151 (box [string "if",
|
adamc@182
|
152 space,
|
adamc@182
|
153 string "(disc",
|
adamc@182
|
154 string (Int.toString depth),
|
adamc@182
|
155 space,
|
adamc@182
|
156 string "!=",
|
adamc@182
|
157 space,
|
adamc@182
|
158 Prim.p_t (Prim.Int n),
|
adamc@182
|
159 string ")",
|
adamc@182
|
160 space,
|
adamc@182
|
161 exit],
|
adamc@182
|
162 env)
|
adamc@182
|
163 | PPrim (Prim.String s) =>
|
adamc@182
|
164 (box [string "if",
|
adamc@182
|
165 space,
|
adamc@182
|
166 string "(strcmp(disc",
|
adamc@182
|
167 string (Int.toString depth),
|
adamc@182
|
168 string ",",
|
adamc@182
|
169 space,
|
adamc@182
|
170 Prim.p_t (Prim.String s),
|
adamc@182
|
171 string "))",
|
adamc@182
|
172 space,
|
adamc@182
|
173 exit],
|
adamc@182
|
174 env)
|
adamc@182
|
175 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
|
adamc@182
|
176
|
adamc@188
|
177 | PCon (dk, pc, po) =>
|
adamc@182
|
178 let
|
adamc@182
|
179 val (p, env) =
|
adamc@182
|
180 case po of
|
adamc@182
|
181 NONE => (box [], env)
|
adamc@182
|
182 | SOME p =>
|
adamc@182
|
183 let
|
adamc@182
|
184 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
185
|
adamc@182
|
186 val (x, to) = case pc of
|
adamc@182
|
187 PConVar n =>
|
adamc@182
|
188 let
|
adamc@182
|
189 val (x, to, _) = E.lookupConstructor env n
|
adamc@182
|
190 in
|
adamc@196
|
191 ("lw_" ^ x, to)
|
adamc@182
|
192 end
|
adamc@188
|
193 | PConFfi {mod = m, con, arg, ...} =>
|
adamc@188
|
194 ("lw_" ^ m ^ "_" ^ con, arg)
|
adamc@182
|
195
|
adamc@182
|
196 val t = case to of
|
adamc@182
|
197 NONE => raise Fail "CjrPrint: Constructor mismatch"
|
adamc@182
|
198 | SOME t => t
|
adamc@182
|
199 in
|
adamc@182
|
200 (box [string "{",
|
adamc@182
|
201 newline,
|
adamc@182
|
202 p_typ env t,
|
adamc@182
|
203 space,
|
adamc@182
|
204 string "disc",
|
adamc@182
|
205 string (Int.toString (depth + 1)),
|
adamc@182
|
206 space,
|
adamc@182
|
207 string "=",
|
adamc@182
|
208 space,
|
adamc@198
|
209 case dk of
|
adamc@198
|
210 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
|
adamc@198
|
211 | Default => box [string "disc",
|
adamc@198
|
212 string (Int.toString depth),
|
adamc@198
|
213 string "->data.",
|
adamc@198
|
214 string x]
|
adamc@198
|
215 | Option =>
|
adamc@198
|
216 case #1 t of
|
adamc@198
|
217 TDatatype _ => box [string "disc",
|
adamc@198
|
218 string (Int.toString depth)]
|
adamc@198
|
219 | _ => box [string "*disc",
|
adamc@198
|
220 string (Int.toString depth)],
|
adamc@182
|
221 string ";",
|
adamc@182
|
222 newline,
|
adamc@182
|
223 p,
|
adamc@182
|
224 newline,
|
adamc@182
|
225 string "}"],
|
adamc@182
|
226 env)
|
adamc@182
|
227 end
|
adamc@182
|
228 in
|
adamc@182
|
229 (box [string "if",
|
adamc@182
|
230 space,
|
adamc@182
|
231 string "(disc",
|
adamc@182
|
232 string (Int.toString depth),
|
adamc@198
|
233 case (dk, po) of
|
adamc@198
|
234 (Enum, _) => box [space,
|
adamc@198
|
235 string "!=",
|
adamc@198
|
236 space,
|
adamc@198
|
237 p_patCon env pc]
|
adamc@198
|
238 | (Default, _) => box [string "->tag",
|
adamc@198
|
239 space,
|
adamc@198
|
240 string "!=",
|
adamc@198
|
241 space,
|
adamc@198
|
242 p_patCon env pc]
|
adamc@198
|
243 | (Option, NONE) => box [space,
|
adamc@198
|
244 string "!=",
|
adamc@198
|
245 space,
|
adamc@198
|
246 string "NULL"]
|
adamc@198
|
247 | (Option, SOME _) => box [space,
|
adamc@198
|
248 string "==",
|
adamc@198
|
249 space,
|
adamc@198
|
250 string "NULL"],
|
adamc@182
|
251 string ")",
|
adamc@182
|
252 space,
|
adamc@182
|
253 exit,
|
adamc@182
|
254 newline,
|
adamc@182
|
255 p],
|
adamc@182
|
256 env)
|
adamc@182
|
257 end
|
adamc@182
|
258
|
adamc@182
|
259 | PRecord xps =>
|
adamc@182
|
260 let
|
adamc@182
|
261 val (xps, env) =
|
adamc@182
|
262 ListUtil.foldlMap (fn ((x, p, t), env) =>
|
adamc@182
|
263 let
|
adamc@182
|
264 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
265
|
adamc@182
|
266 val p = box [string "{",
|
adamc@182
|
267 newline,
|
adamc@182
|
268 p_typ env t,
|
adamc@182
|
269 space,
|
adamc@182
|
270 string "disc",
|
adamc@182
|
271 string (Int.toString (depth + 1)),
|
adamc@182
|
272 space,
|
adamc@182
|
273 string "=",
|
adamc@182
|
274 space,
|
adamc@182
|
275 string "disc",
|
adamc@182
|
276 string (Int.toString depth),
|
adamc@196
|
277 string ".__lwf_",
|
adamc@182
|
278 string x,
|
adamc@182
|
279 string ";",
|
adamc@182
|
280 newline,
|
adamc@182
|
281 p,
|
adamc@182
|
282 newline,
|
adamc@182
|
283 string "}"]
|
adamc@182
|
284 in
|
adamc@182
|
285 (p, env)
|
adamc@182
|
286 end) env xps
|
adamc@182
|
287 in
|
adamc@182
|
288 (p_list_sep newline (fn x => x) xps,
|
adamc@182
|
289 env)
|
adamc@182
|
290 end
|
adamc@182
|
291
|
adamc@182
|
292 local
|
adamc@182
|
293 val count = ref 0
|
adamc@182
|
294 in
|
adamc@182
|
295 fun newGoto () =
|
adamc@182
|
296 let
|
adamc@182
|
297 val r = !count
|
adamc@182
|
298 in
|
adamc@182
|
299 count := r + 1;
|
adamc@182
|
300 string ("L" ^ Int.toString r)
|
adamc@182
|
301 end
|
adamc@182
|
302 end
|
adamc@182
|
303
|
adamc@185
|
304 fun patConInfo env pc =
|
adamc@185
|
305 case pc of
|
adamc@185
|
306 PConVar n =>
|
adamc@185
|
307 let
|
adamc@185
|
308 val (x, _, dn) = E.lookupConstructor env n
|
adamc@185
|
309 val (dx, _) = E.lookupDatatype env dn
|
adamc@185
|
310 in
|
adamc@185
|
311 ("__lwd_" ^ dx ^ "_" ^ Int.toString dn,
|
adamc@196
|
312 "__lwc_" ^ x ^ "_" ^ Int.toString n,
|
adamc@196
|
313 "lw_" ^ x)
|
adamc@185
|
314 end
|
adamc@186
|
315 | PConFfi {mod = m, datatyp, con, ...} =>
|
adamc@185
|
316 ("lw_" ^ m ^ "_" ^ datatyp,
|
adamc@196
|
317 "lw_" ^ m ^ "_" ^ con,
|
adamc@196
|
318 "lw_" ^ con)
|
adamc@185
|
319
|
adamc@182
|
320 fun p_exp' par env (e, loc) =
|
adamc@29
|
321 case e of
|
adamc@29
|
322 EPrim p => Prim.p_t p
|
adamc@29
|
323 | ERel n => p_rel env n
|
adamc@109
|
324 | ENamed n => p_enamed env n
|
adamc@188
|
325 | ECon (Enum, pc, _) => p_patCon env pc
|
adamc@198
|
326 | ECon (Option, pc, NONE) => string "NULL"
|
adamc@198
|
327 | ECon (Option, pc, SOME e) =>
|
adamc@198
|
328 let
|
adamc@198
|
329 val to = case pc of
|
adamc@198
|
330 PConVar n => #2 (E.lookupConstructor env n)
|
adamc@198
|
331 | PConFfi {arg, ...} => arg
|
adamc@198
|
332
|
adamc@198
|
333 val t = case to of
|
adamc@198
|
334 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
|
adamc@198
|
335 | SOME t => t
|
adamc@198
|
336 in
|
adamc@198
|
337 case #1 t of
|
adamc@198
|
338 TDatatype _ => p_exp' par env e
|
adamc@198
|
339 | _ => box [string "({",
|
adamc@198
|
340 newline,
|
adamc@198
|
341 p_typ env t,
|
adamc@198
|
342 space,
|
adamc@198
|
343 string "*tmp",
|
adamc@198
|
344 space,
|
adamc@198
|
345 string "=",
|
adamc@198
|
346 space,
|
adamc@198
|
347 string "lw_malloc(ctx, sizeof(",
|
adamc@198
|
348 p_typ env t,
|
adamc@198
|
349 string "));",
|
adamc@198
|
350 newline,
|
adamc@198
|
351 string "*tmp",
|
adamc@198
|
352 space,
|
adamc@198
|
353 string "=",
|
adamc@198
|
354 p_exp' par env e,
|
adamc@198
|
355 string ";",
|
adamc@198
|
356 newline,
|
adamc@198
|
357 string "tmp;",
|
adamc@198
|
358 newline,
|
adamc@198
|
359 string "})"]
|
adamc@198
|
360 end
|
adamc@188
|
361 | ECon (Default, pc, eo) =>
|
adamc@181
|
362 let
|
adamc@196
|
363 val (xd, xc, xn) = patConInfo env pc
|
adamc@181
|
364 in
|
adamc@182
|
365 box [string "({",
|
adamc@181
|
366 newline,
|
adamc@181
|
367 string "struct",
|
adamc@181
|
368 space,
|
adamc@185
|
369 string xd,
|
adamc@181
|
370 space,
|
adamc@181
|
371 string "*tmp",
|
adamc@181
|
372 space,
|
adamc@181
|
373 string "=",
|
adamc@181
|
374 space,
|
adamc@185
|
375 string "lw_malloc(ctx, sizeof(struct ",
|
adamc@185
|
376 string xd,
|
adamc@181
|
377 string "));",
|
adamc@181
|
378 newline,
|
adamc@181
|
379 string "tmp->tag",
|
adamc@181
|
380 space,
|
adamc@181
|
381 string "=",
|
adamc@181
|
382 space,
|
adamc@185
|
383 string xc,
|
adamc@181
|
384 string ";",
|
adamc@181
|
385 newline,
|
adamc@181
|
386 case eo of
|
adamc@181
|
387 NONE => box []
|
adamc@185
|
388 | SOME e => box [string "tmp->data.",
|
adamc@196
|
389 string xn,
|
adamc@181
|
390 space,
|
adamc@181
|
391 string "=",
|
adamc@181
|
392 space,
|
adamc@181
|
393 p_exp env e,
|
adamc@181
|
394 string ";",
|
adamc@181
|
395 newline],
|
adamc@181
|
396 string "tmp;",
|
adamc@181
|
397 newline,
|
adamc@181
|
398 string "})"]
|
adamc@181
|
399 end
|
adamc@109
|
400
|
adamc@53
|
401 | EFfi (m, x) => box [string "lw_", string m, string "_", string x]
|
adamc@53
|
402 | EFfiApp (m, x, es) => box [string "lw_",
|
adamc@53
|
403 string m,
|
adamc@53
|
404 string "_",
|
adamc@53
|
405 string x,
|
adamc@117
|
406 string "(ctx, ",
|
adamc@53
|
407 p_list (p_exp env) es,
|
adamc@53
|
408 string ")"]
|
adamc@129
|
409 | EApp (e1, e2) =>
|
adamc@129
|
410 let
|
adamc@129
|
411 fun unravel (f, acc) =
|
adamc@129
|
412 case #1 f of
|
adamc@129
|
413 EApp (f', arg) => unravel (f', arg :: acc)
|
adamc@129
|
414 | _ => (f, acc)
|
adamc@129
|
415
|
adamc@129
|
416 val (f, args) = unravel (e1, [e2])
|
adamc@129
|
417 in
|
adamc@129
|
418 parenIf par (box [p_exp' true env e1,
|
adamc@129
|
419 string "(ctx,",
|
adamc@129
|
420 space,
|
adamc@129
|
421 p_list_sep (box [string ",", space]) (p_exp env) args,
|
adamc@129
|
422 string ")"])
|
adamc@129
|
423 end
|
adamc@29
|
424
|
adamc@29
|
425 | ERecord (i, xes) => box [string "({",
|
adamc@29
|
426 space,
|
adamc@29
|
427 string "struct",
|
adamc@29
|
428 space,
|
adamc@29
|
429 string ("__lws_" ^ Int.toString i),
|
adamc@29
|
430 space,
|
adamc@181
|
431 string "tmp",
|
adamc@29
|
432 space,
|
adamc@29
|
433 string "=",
|
adamc@29
|
434 space,
|
adamc@29
|
435 string "{",
|
adamc@29
|
436 p_list (fn (_, e) =>
|
adamc@29
|
437 p_exp env e) xes,
|
adamc@29
|
438 string "};",
|
adamc@29
|
439 space,
|
adamc@181
|
440 string "tmp;",
|
adamc@29
|
441 space,
|
adamc@29
|
442 string "})" ]
|
adamc@29
|
443 | EField (e, x) =>
|
adamc@29
|
444 box [p_exp' true env e,
|
adamc@182
|
445 string ".__lwf_",
|
adamc@29
|
446 string x]
|
adamc@29
|
447
|
adamc@182
|
448 | ECase (e, pes, {disc, result}) =>
|
adamc@182
|
449 let
|
adamc@182
|
450 val final = newGoto ()
|
adamc@182
|
451
|
adamc@182
|
452 val body = foldl (fn ((p, e), body) =>
|
adamc@182
|
453 let
|
adamc@182
|
454 val exit = newGoto ()
|
adamc@182
|
455 val (pr, _) = p_pat_preamble env p
|
adamc@182
|
456 val (p, env) = p_pat (env,
|
adamc@182
|
457 box [string "goto",
|
adamc@182
|
458 space,
|
adamc@182
|
459 exit,
|
adamc@182
|
460 string ";"],
|
adamc@182
|
461 0) p
|
adamc@182
|
462 in
|
adamc@182
|
463 box [body,
|
adamc@182
|
464 box [string "{",
|
adamc@182
|
465 newline,
|
adamc@182
|
466 pr,
|
adamc@182
|
467 newline,
|
adamc@182
|
468 p,
|
adamc@182
|
469 newline,
|
adamc@182
|
470 string "result",
|
adamc@182
|
471 space,
|
adamc@182
|
472 string "=",
|
adamc@182
|
473 space,
|
adamc@182
|
474 p_exp env e,
|
adamc@182
|
475 string ";",
|
adamc@182
|
476 newline,
|
adamc@182
|
477 string "goto",
|
adamc@182
|
478 space,
|
adamc@182
|
479 final,
|
adamc@182
|
480 string ";",
|
adamc@182
|
481 newline,
|
adamc@182
|
482 string "}"],
|
adamc@182
|
483 newline,
|
adamc@182
|
484 exit,
|
adamc@182
|
485 string ":",
|
adamc@182
|
486 newline]
|
adamc@182
|
487 end) (box []) pes
|
adamc@182
|
488 in
|
adamc@182
|
489 box [string "({",
|
adamc@182
|
490 newline,
|
adamc@182
|
491 p_typ env disc,
|
adamc@182
|
492 space,
|
adamc@182
|
493 string "disc0",
|
adamc@182
|
494 space,
|
adamc@182
|
495 string "=",
|
adamc@182
|
496 space,
|
adamc@182
|
497 p_exp env e,
|
adamc@182
|
498 string ";",
|
adamc@182
|
499 newline,
|
adamc@182
|
500 p_typ env result,
|
adamc@182
|
501 space,
|
adamc@182
|
502 string "result;",
|
adamc@182
|
503 newline,
|
adamc@182
|
504 body,
|
adamc@182
|
505 string "lw_error(ctx, FATAL, \"",
|
adamc@182
|
506 string (ErrorMsg.spanToString loc),
|
adamc@182
|
507 string ": pattern match failure\");",
|
adamc@182
|
508 newline,
|
adamc@182
|
509 final,
|
adamc@182
|
510 string ":",
|
adamc@182
|
511 space,
|
adamc@182
|
512 string "result;",
|
adamc@182
|
513 newline,
|
adamc@182
|
514 string "})"]
|
adamc@182
|
515 end
|
adamc@181
|
516
|
adamc@117
|
517 | EWrite e => box [string "(lw_write(ctx, ",
|
adamc@102
|
518 p_exp env e,
|
adamc@102
|
519 string "), lw_unit_v)"]
|
adamc@102
|
520
|
adamc@106
|
521 | ESeq (e1, e2) => box [string "(",
|
adamc@106
|
522 p_exp env e1,
|
adamc@106
|
523 string ",",
|
adamc@106
|
524 space,
|
adamc@106
|
525 p_exp env e2,
|
adamc@106
|
526 string ")"]
|
adamc@106
|
527
|
adamc@29
|
528 and p_exp env = p_exp' false env
|
adamc@29
|
529
|
adamc@129
|
530 fun p_fun env (fx, n, args, ran, e) =
|
adamc@129
|
531 let
|
adamc@129
|
532 val nargs = length args
|
adamc@129
|
533 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
|
adamc@129
|
534 in
|
adamc@129
|
535 box [string "static",
|
adamc@129
|
536 space,
|
adamc@129
|
537 p_typ env ran,
|
adamc@129
|
538 space,
|
adamc@129
|
539 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
|
adamc@129
|
540 string "(",
|
adamc@129
|
541 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@129
|
542 (string "lw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
|
adamc@129
|
543 box [p_typ env dom,
|
adamc@129
|
544 space,
|
adamc@129
|
545 p_rel env' (nargs - i - 1)]) args),
|
adamc@129
|
546 string ")",
|
adamc@129
|
547 space,
|
adamc@129
|
548 string "{",
|
adamc@129
|
549 newline,
|
adamc@129
|
550 box[string "return(",
|
adamc@129
|
551 p_exp env' e,
|
adamc@129
|
552 string ");"],
|
adamc@129
|
553 newline,
|
adamc@129
|
554 string "}"]
|
adamc@129
|
555 end
|
adamc@129
|
556
|
adamc@129
|
557 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@29
|
558 case d of
|
adamc@29
|
559 DStruct (n, xts) =>
|
adamc@196
|
560 let
|
adamc@196
|
561 val env = E.declBinds env dAll
|
adamc@196
|
562 in
|
adamc@196
|
563 box [string "struct",
|
adamc@196
|
564 space,
|
adamc@196
|
565 string ("__lws_" ^ Int.toString n),
|
adamc@196
|
566 space,
|
adamc@196
|
567 string "{",
|
adamc@196
|
568 newline,
|
adamc@196
|
569 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
|
adamc@196
|
570 space,
|
adamc@196
|
571 string "__lwf_",
|
adamc@196
|
572 string x,
|
adamc@196
|
573 string ";",
|
adamc@196
|
574 newline]) xts,
|
adamc@196
|
575 string "};"]
|
adamc@196
|
576 end
|
adamc@188
|
577 | DDatatype (Enum, x, n, xncs) =>
|
adamc@188
|
578 box [string "enum",
|
adamc@188
|
579 space,
|
adamc@188
|
580 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
|
adamc@188
|
581 space,
|
adamc@188
|
582 string "{",
|
adamc@188
|
583 space,
|
adamc@188
|
584 p_list_sep (box [string ",", space]) (fn (x, n, _) => string ("__lwc_" ^ x ^ "_" ^ Int.toString n)) xncs,
|
adamc@188
|
585 space,
|
adamc@188
|
586 string "};"]
|
adamc@198
|
587 | DDatatype (Option, _, _, _) => box []
|
adamc@188
|
588 | DDatatype (Default, x, n, xncs) =>
|
adamc@165
|
589 let
|
adamc@165
|
590 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
|
adamc@165
|
591 | (x, n, SOME t) => SOME (x, n, t)) xncs
|
adamc@165
|
592 in
|
adamc@165
|
593 box [string "enum",
|
adamc@165
|
594 space,
|
adamc@165
|
595 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
|
adamc@165
|
596 space,
|
adamc@165
|
597 string "{",
|
adamc@165
|
598 space,
|
adamc@165
|
599 p_list_sep (box [string ",", space]) (fn (x, n, _) => string ("__lwc_" ^ x ^ "_" ^ Int.toString n)) xncs,
|
adamc@165
|
600 space,
|
adamc@165
|
601 string "};",
|
adamc@165
|
602 newline,
|
adamc@165
|
603 newline,
|
adamc@165
|
604 string "struct",
|
adamc@165
|
605 space,
|
adamc@167
|
606 string ("__lwd_" ^ x ^ "_" ^ Int.toString n),
|
adamc@165
|
607 space,
|
adamc@165
|
608 string "{",
|
adamc@165
|
609 newline,
|
adamc@165
|
610 string "enum",
|
adamc@165
|
611 space,
|
adamc@165
|
612 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
|
adamc@165
|
613 space,
|
adamc@165
|
614 string "tag;",
|
adamc@165
|
615 newline,
|
adamc@165
|
616 box (case xncsArgs of
|
adamc@165
|
617 [] => []
|
adamc@165
|
618 | _ => [string "union",
|
adamc@165
|
619 space,
|
adamc@165
|
620 string "{",
|
adamc@165
|
621 newline,
|
adamc@165
|
622 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
|
adamc@165
|
623 space,
|
adamc@196
|
624 string ("lw_" ^ x),
|
adamc@165
|
625 string ";"]) xncsArgs,
|
adamc@165
|
626 newline,
|
adamc@165
|
627 string "}",
|
adamc@165
|
628 space,
|
adamc@165
|
629 string "data;",
|
adamc@165
|
630 newline]),
|
adamc@165
|
631 string "};"]
|
adamc@188
|
632 end
|
adamc@29
|
633
|
adamc@196
|
634 | DDatatypeForward _ => box []
|
adamc@196
|
635
|
adamc@29
|
636 | DVal (x, n, t, e) =>
|
adamc@29
|
637 box [p_typ env t,
|
adamc@29
|
638 space,
|
adamc@29
|
639 string ("__lwn_" ^ x ^ "_" ^ Int.toString n),
|
adamc@29
|
640 space,
|
adamc@29
|
641 string "=",
|
adamc@29
|
642 space,
|
adamc@29
|
643 p_exp env e,
|
adamc@29
|
644 string ";"]
|
adamc@129
|
645 | DFun vi => p_fun env vi
|
adamc@129
|
646 | DFunRec vis =>
|
adamc@29
|
647 let
|
adamc@129
|
648 val env = E.declBinds env dAll
|
adamc@29
|
649 in
|
adamc@129
|
650 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
|
adamc@129
|
651 box [string "static",
|
adamc@129
|
652 space,
|
adamc@129
|
653 p_typ env ran,
|
adamc@129
|
654 space,
|
adamc@129
|
655 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
|
adamc@129
|
656 string "(lw_context,",
|
adamc@129
|
657 space,
|
adamc@129
|
658 p_list_sep (box [string ",", space])
|
adamc@129
|
659 (fn (_, dom) => p_typ env dom) args,
|
adamc@129
|
660 string ");"]) vis,
|
adamc@29
|
661 newline,
|
adamc@129
|
662 p_list_sep newline (p_fun env) vis,
|
adamc@129
|
663 newline]
|
adamc@29
|
664 end
|
adamc@29
|
665
|
adamc@144
|
666 datatype 'a search =
|
adamc@144
|
667 Found of 'a
|
adamc@144
|
668 | NotFound
|
adamc@144
|
669 | Error
|
adamc@120
|
670
|
adamc@101
|
671
|
adamc@101
|
672 fun p_file env (ds, ps) =
|
adamc@29
|
673 let
|
adamc@101
|
674 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
675 (p_decl env d,
|
adamc@31
|
676 E.declBinds env d))
|
adamc@101
|
677 env ds
|
adamc@144
|
678
|
adamc@144
|
679 val fields = foldl (fn ((ek, _, _, ts), fields) =>
|
adamc@144
|
680 case ek of
|
adamc@144
|
681 Core.Link => fields
|
adamc@144
|
682 | Core.Action =>
|
adamc@144
|
683 case List.last ts of
|
adamc@144
|
684 (TRecord i, _) =>
|
adamc@144
|
685 let
|
adamc@144
|
686 val xts = E.lookupStruct env i
|
adamc@144
|
687 val xtsSet = SS.addList (SS.empty, map #1 xts)
|
adamc@144
|
688 in
|
adamc@144
|
689 foldl (fn ((x, _), fields) =>
|
adamc@144
|
690 let
|
adamc@144
|
691 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
|
adamc@144
|
692 in
|
adamc@144
|
693 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
|
adamc@144
|
694 xtsSet'))
|
adamc@144
|
695 end) fields xts
|
adamc@144
|
696 end
|
adamc@144
|
697 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
|
adamc@144
|
698 SM.empty ps
|
adamc@144
|
699
|
adamc@144
|
700 val fnums = SM.foldli (fn (x, xs, fnums) =>
|
adamc@144
|
701 let
|
adamc@144
|
702 val unusable = SS.foldl (fn (x', unusable) =>
|
adamc@144
|
703 case SM.find (fnums, x') of
|
adamc@144
|
704 NONE => unusable
|
adamc@144
|
705 | SOME n => IS.add (unusable, n))
|
adamc@144
|
706 IS.empty xs
|
adamc@144
|
707
|
adamc@144
|
708 fun findAvailable n =
|
adamc@144
|
709 if IS.member (unusable, n) then
|
adamc@144
|
710 findAvailable (n + 1)
|
adamc@144
|
711 else
|
adamc@144
|
712 n
|
adamc@144
|
713 in
|
adamc@144
|
714 SM.insert (fnums, x, findAvailable 0)
|
adamc@144
|
715 end)
|
adamc@144
|
716 SM.empty fields
|
adamc@144
|
717
|
adamc@144
|
718 fun makeSwitch (fnums, i) =
|
adamc@144
|
719 case SM.foldl (fn (n, NotFound) => Found n
|
adamc@144
|
720 | (n, Error) => Error
|
adamc@144
|
721 | (n, Found n') => if n = n' then
|
adamc@144
|
722 Found n'
|
adamc@144
|
723 else
|
adamc@144
|
724 Error) NotFound fnums of
|
adamc@144
|
725 NotFound => box [string "return",
|
adamc@144
|
726 space,
|
adamc@144
|
727 string "-1;"]
|
adamc@144
|
728 | Found n => box [string "return",
|
adamc@144
|
729 space,
|
adamc@144
|
730 string (Int.toString n),
|
adamc@144
|
731 string ";"]
|
adamc@144
|
732 | Error =>
|
adamc@144
|
733 let
|
adamc@144
|
734 val cmap = SM.foldli (fn (x, n, cmap) =>
|
adamc@144
|
735 let
|
adamc@144
|
736 val ch = if i < size x then
|
adamc@144
|
737 String.sub (x, i)
|
adamc@144
|
738 else
|
adamc@144
|
739 chr 0
|
adamc@144
|
740
|
adamc@144
|
741 val fnums = case CM.find (cmap, ch) of
|
adamc@144
|
742 NONE => SM.empty
|
adamc@144
|
743 | SOME fnums => fnums
|
adamc@144
|
744 val fnums = SM.insert (fnums, x, n)
|
adamc@144
|
745 in
|
adamc@144
|
746 CM.insert (cmap, ch, fnums)
|
adamc@144
|
747 end)
|
adamc@144
|
748 CM.empty fnums
|
adamc@144
|
749
|
adamc@144
|
750 val cmap = CM.listItemsi cmap
|
adamc@144
|
751 in
|
adamc@144
|
752 case cmap of
|
adamc@144
|
753 [(_, fnums)] =>
|
adamc@144
|
754 box [string "if",
|
adamc@144
|
755 space,
|
adamc@144
|
756 string "(name[",
|
adamc@144
|
757 string (Int.toString i),
|
adamc@144
|
758 string "]",
|
adamc@144
|
759 space,
|
adamc@144
|
760 string "==",
|
adamc@144
|
761 space,
|
adamc@144
|
762 string "0)",
|
adamc@144
|
763 space,
|
adamc@144
|
764 string "return",
|
adamc@144
|
765 space,
|
adamc@144
|
766 string "-1;",
|
adamc@144
|
767 newline,
|
adamc@144
|
768 makeSwitch (fnums, i+1)]
|
adamc@144
|
769 | _ =>
|
adamc@144
|
770 box [string "switch",
|
adamc@144
|
771 space,
|
adamc@144
|
772 string "(name[",
|
adamc@144
|
773 string (Int.toString i),
|
adamc@144
|
774 string "])",
|
adamc@144
|
775 space,
|
adamc@144
|
776 string "{",
|
adamc@144
|
777 newline,
|
adamc@144
|
778 box (map (fn (ch, fnums) =>
|
adamc@144
|
779 box [string "case",
|
adamc@144
|
780 space,
|
adamc@144
|
781 if ch = chr 0 then
|
adamc@144
|
782 string "0:"
|
adamc@144
|
783 else
|
adamc@144
|
784 box [string "'",
|
adamc@144
|
785 string (Char.toString ch),
|
adamc@144
|
786 string "':"],
|
adamc@144
|
787 newline,
|
adamc@144
|
788 makeSwitch (fnums, i+1),
|
adamc@144
|
789 newline]) cmap),
|
adamc@144
|
790 string "default:",
|
adamc@144
|
791 newline,
|
adamc@144
|
792 string "return",
|
adamc@144
|
793 space,
|
adamc@144
|
794 string "-1;",
|
adamc@144
|
795 newline,
|
adamc@144
|
796 string "}"]
|
adamc@144
|
797 end
|
adamc@144
|
798
|
adamc@186
|
799 fun capitalize s =
|
adamc@186
|
800 if s = "" then
|
adamc@186
|
801 ""
|
adamc@186
|
802 else
|
adamc@186
|
803 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@186
|
804
|
adamc@144
|
805 fun unurlify (t, loc) =
|
adamc@144
|
806 case t of
|
adamc@186
|
807 TFfi (m, t) => string ("lw_" ^ m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
|
adamc@144
|
808
|
adamc@144
|
809 | TRecord 0 => string "lw_unit_v"
|
adamc@144
|
810 | TRecord i =>
|
adamc@144
|
811 let
|
adamc@144
|
812 val xts = E.lookupStruct env i
|
adamc@144
|
813 in
|
adamc@144
|
814 box [string "({",
|
adamc@144
|
815 newline,
|
adamc@144
|
816 box (map (fn (x, t) =>
|
adamc@144
|
817 box [p_typ env t,
|
adamc@144
|
818 space,
|
adamc@144
|
819 string x,
|
adamc@144
|
820 space,
|
adamc@144
|
821 string "=",
|
adamc@144
|
822 space,
|
adamc@144
|
823 unurlify t,
|
adamc@144
|
824 string ";",
|
adamc@144
|
825 newline]) xts),
|
adamc@144
|
826 string "struct",
|
adamc@144
|
827 space,
|
adamc@144
|
828 string "__lws_",
|
adamc@144
|
829 string (Int.toString i),
|
adamc@144
|
830 space,
|
adamc@181
|
831 string "tmp",
|
adamc@144
|
832 space,
|
adamc@144
|
833 string "=",
|
adamc@144
|
834 space,
|
adamc@144
|
835 string "{",
|
adamc@144
|
836 space,
|
adamc@144
|
837 p_list_sep (box [string ",", space]) (fn (x, _) => string x) xts,
|
adamc@144
|
838 space,
|
adamc@144
|
839 string "};",
|
adamc@144
|
840 newline,
|
adamc@181
|
841 string "tmp;",
|
adamc@144
|
842 newline,
|
adamc@144
|
843 string "})"]
|
adamc@144
|
844 end
|
adamc@144
|
845
|
adamc@188
|
846 | TDatatype (Enum, i, _) =>
|
adamc@188
|
847 let
|
adamc@188
|
848 val (x, xncs) = E.lookupDatatype env i
|
adamc@188
|
849
|
adamc@188
|
850 fun doEm xncs =
|
adamc@188
|
851 case xncs of
|
adamc@188
|
852 [] => string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), (enum __lwe_"
|
adamc@188
|
853 ^ x ^ "_" ^ Int.toString i ^ ")0)")
|
adamc@188
|
854 | (x', n, to) :: rest =>
|
adamc@188
|
855 box [string "((!strncmp(request, \"",
|
adamc@188
|
856 string x',
|
adamc@188
|
857 string "\", ",
|
adamc@188
|
858 string (Int.toString (size x')),
|
adamc@188
|
859 string ") && (request[",
|
adamc@188
|
860 string (Int.toString (size x')),
|
adamc@188
|
861 string "] == 0 || request[",
|
adamc@188
|
862 string (Int.toString (size x')),
|
adamc@188
|
863 string ("] == '/')) ? __lwc_" ^ x' ^ "_" ^ Int.toString n),
|
adamc@188
|
864 space,
|
adamc@188
|
865 string ":",
|
adamc@188
|
866 space,
|
adamc@188
|
867 doEm rest,
|
adamc@188
|
868 string ")"]
|
adamc@188
|
869 in
|
adamc@188
|
870 doEm xncs
|
adamc@188
|
871 end
|
adamc@188
|
872
|
adamc@198
|
873 | TDatatype (Option, i, xncs) =>
|
adamc@198
|
874 let
|
adamc@198
|
875 val (x, _) = E.lookupDatatype env i
|
adamc@198
|
876
|
adamc@198
|
877 val (no_arg, has_arg, t) =
|
adamc@198
|
878 case !xncs of
|
adamc@198
|
879 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@198
|
880 (no_arg, has_arg, t)
|
adamc@198
|
881 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@198
|
882 (no_arg, has_arg, t)
|
adamc@198
|
883 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
|
adamc@198
|
884 in
|
adamc@198
|
885 box [string "(request[0] == '/' ? ++request : request,",
|
adamc@198
|
886 newline,
|
adamc@198
|
887 string "((!strncmp(request, \"",
|
adamc@198
|
888 string no_arg,
|
adamc@198
|
889 string "\", ",
|
adamc@198
|
890 string (Int.toString (size no_arg)),
|
adamc@198
|
891 string ") && (request[",
|
adamc@198
|
892 string (Int.toString (size no_arg)),
|
adamc@198
|
893 string "] == 0 || request[",
|
adamc@198
|
894 string (Int.toString (size no_arg)),
|
adamc@198
|
895 string "] == '/')) ? (request",
|
adamc@198
|
896 space,
|
adamc@198
|
897 string "+=",
|
adamc@198
|
898 space,
|
adamc@198
|
899 string (Int.toString (size no_arg)),
|
adamc@198
|
900 string ", NULL) : ((!strncmp(request, \"",
|
adamc@198
|
901 string has_arg,
|
adamc@198
|
902 string "\", ",
|
adamc@198
|
903 string (Int.toString (size has_arg)),
|
adamc@198
|
904 string ") && (request[",
|
adamc@198
|
905 string (Int.toString (size has_arg)),
|
adamc@198
|
906 string "] == 0 || request[",
|
adamc@198
|
907 string (Int.toString (size has_arg)),
|
adamc@198
|
908 string "] == '/')) ? (request",
|
adamc@198
|
909 space,
|
adamc@198
|
910 string "+=",
|
adamc@198
|
911 space,
|
adamc@198
|
912 string (Int.toString (size has_arg)),
|
adamc@198
|
913 string ", ",
|
adamc@198
|
914
|
adamc@198
|
915 case #1 t of
|
adamc@198
|
916 TDatatype _ => unurlify t
|
adamc@198
|
917 | _ => box [string "({",
|
adamc@198
|
918 newline,
|
adamc@198
|
919 p_typ env t,
|
adamc@198
|
920 space,
|
adamc@198
|
921 string "*tmp",
|
adamc@198
|
922 space,
|
adamc@198
|
923 string "=",
|
adamc@198
|
924 space,
|
adamc@198
|
925 string "lw_malloc(ctx, sizeof(",
|
adamc@198
|
926 p_typ env t,
|
adamc@198
|
927 string "));",
|
adamc@198
|
928 newline,
|
adamc@198
|
929 string "*tmp",
|
adamc@198
|
930 space,
|
adamc@198
|
931 string "=",
|
adamc@198
|
932 space,
|
adamc@198
|
933 unurlify t,
|
adamc@198
|
934 string ";",
|
adamc@198
|
935 newline,
|
adamc@198
|
936 string "tmp;",
|
adamc@198
|
937 newline,
|
adamc@198
|
938 string "})"],
|
adamc@198
|
939 string ")",
|
adamc@198
|
940 newline,
|
adamc@198
|
941 string ":",
|
adamc@198
|
942 space,
|
adamc@198
|
943 string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL))))")]
|
adamc@198
|
944 end
|
adamc@198
|
945
|
adamc@188
|
946 | TDatatype (Default, i, _) =>
|
adamc@166
|
947 let
|
adamc@166
|
948 val (x, xncs) = E.lookupDatatype env i
|
adamc@166
|
949
|
adamc@166
|
950 fun doEm xncs =
|
adamc@166
|
951 case xncs of
|
adamc@167
|
952 [] => string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL)")
|
adamc@167
|
953 | (x', n, to) :: rest =>
|
adamc@167
|
954 box [string "((!strncmp(request, \"",
|
adamc@167
|
955 string x',
|
adamc@167
|
956 string "\", ",
|
adamc@167
|
957 string (Int.toString (size x')),
|
adamc@167
|
958 string ") && (request[",
|
adamc@167
|
959 string (Int.toString (size x')),
|
adamc@167
|
960 string "] == 0 || request[",
|
adamc@167
|
961 string (Int.toString (size x')),
|
adamc@167
|
962 string "] == '/')) ? ({",
|
adamc@166
|
963 newline,
|
adamc@167
|
964 string "struct",
|
adamc@167
|
965 space,
|
adamc@166
|
966 string ("__lwd_" ^ x ^ "_" ^ Int.toString i),
|
adamc@166
|
967 space,
|
adamc@181
|
968 string "*tmp = lw_malloc(ctx, sizeof(struct __lwd_",
|
adamc@167
|
969 string x,
|
adamc@167
|
970 string "_",
|
adamc@167
|
971 string (Int.toString i),
|
adamc@167
|
972 string "));",
|
adamc@166
|
973 newline,
|
adamc@181
|
974 string "tmp->tag",
|
adamc@166
|
975 space,
|
adamc@166
|
976 string "=",
|
adamc@166
|
977 space,
|
adamc@167
|
978 string ("__lwc_" ^ x' ^ "_" ^ Int.toString n),
|
adamc@166
|
979 string ";",
|
adamc@166
|
980 newline,
|
adamc@166
|
981 string "request",
|
adamc@166
|
982 space,
|
adamc@166
|
983 string "+=",
|
adamc@166
|
984 space,
|
adamc@167
|
985 string (Int.toString (size x')),
|
adamc@166
|
986 string ";",
|
adamc@166
|
987 newline,
|
adamc@167
|
988 string "if (request[0] == '/') ++request;",
|
adamc@167
|
989 newline,
|
adamc@166
|
990 case to of
|
adamc@166
|
991 NONE => box []
|
adamc@197
|
992 | SOME t => box [string "tmp->data.lw_",
|
adamc@167
|
993 string x',
|
adamc@166
|
994 space,
|
adamc@166
|
995 string "=",
|
adamc@166
|
996 space,
|
adamc@166
|
997 unurlify t,
|
adamc@166
|
998 string ";",
|
adamc@166
|
999 newline],
|
adamc@181
|
1000 string "tmp;",
|
adamc@166
|
1001 newline,
|
adamc@166
|
1002 string "})",
|
adamc@166
|
1003 space,
|
adamc@166
|
1004 string ":",
|
adamc@166
|
1005 space,
|
adamc@166
|
1006 doEm rest,
|
adamc@166
|
1007 string ")"]
|
adamc@166
|
1008 in
|
adamc@166
|
1009 doEm xncs
|
adamc@166
|
1010 end
|
adamc@166
|
1011
|
adamc@144
|
1012 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
|
adamc@144
|
1013 space)
|
adamc@144
|
1014
|
adamc@144
|
1015
|
adamc@144
|
1016 fun p_page (ek, s, n, ts) =
|
adamc@144
|
1017 let
|
adamc@144
|
1018 val (ts, defInputs, inputsVar) =
|
adamc@144
|
1019 case ek of
|
adamc@144
|
1020 Core.Link => (ts, string "", string "")
|
adamc@144
|
1021 | Core.Action =>
|
adamc@144
|
1022 case List.last ts of
|
adamc@144
|
1023 (TRecord i, _) =>
|
adamc@144
|
1024 let
|
adamc@144
|
1025 val xts = E.lookupStruct env i
|
adamc@144
|
1026 in
|
adamc@144
|
1027 (List.drop (ts, 1),
|
adamc@144
|
1028 box [box (map (fn (x, t) => box [p_typ env t,
|
adamc@144
|
1029 space,
|
adamc@144
|
1030 string "lw_input_",
|
adamc@144
|
1031 string x,
|
adamc@144
|
1032 string ";",
|
adamc@144
|
1033 newline]) xts),
|
adamc@144
|
1034 newline,
|
adamc@144
|
1035 box (map (fn (x, t) =>
|
adamc@144
|
1036 let
|
adamc@144
|
1037 val n = case SM.find (fnums, x) of
|
adamc@144
|
1038 NONE => raise Fail "CjrPrint: Can't find in fnums"
|
adamc@144
|
1039 | SOME n => n
|
adamc@190
|
1040
|
adamc@190
|
1041 val f = case t of
|
adamc@190
|
1042 (TFfi ("Basis", "bool"), _) => "optional_"
|
adamc@190
|
1043 | _ => ""
|
adamc@144
|
1044 in
|
adamc@190
|
1045 box [string "request = lw_get_",
|
adamc@190
|
1046 string f,
|
adamc@190
|
1047 string "input(ctx, ",
|
adamc@144
|
1048 string (Int.toString n),
|
adamc@144
|
1049 string ");",
|
adamc@144
|
1050 newline,
|
adamc@144
|
1051 string "if (request == NULL) {",
|
adamc@144
|
1052 newline,
|
adamc@144
|
1053 box [string "printf(\"Missing input ",
|
adamc@144
|
1054 string x,
|
adamc@144
|
1055 string "\\n\");",
|
adamc@144
|
1056 newline,
|
adamc@144
|
1057 string "exit(1);"],
|
adamc@144
|
1058 newline,
|
adamc@144
|
1059 string "}",
|
adamc@144
|
1060 newline,
|
adamc@144
|
1061 string "lw_input_",
|
adamc@144
|
1062 string x,
|
adamc@144
|
1063 space,
|
adamc@144
|
1064 string "=",
|
adamc@144
|
1065 space,
|
adamc@144
|
1066 unurlify t,
|
adamc@144
|
1067 string ";",
|
adamc@144
|
1068 newline]
|
adamc@144
|
1069 end) xts),
|
adamc@144
|
1070 string "struct __lws_",
|
adamc@144
|
1071 string (Int.toString i),
|
adamc@144
|
1072 space,
|
adamc@144
|
1073 string "lw_inputs",
|
adamc@144
|
1074 space,
|
adamc@144
|
1075 string "= {",
|
adamc@144
|
1076 newline,
|
adamc@144
|
1077 box (map (fn (x, _) => box [string "lw_input_",
|
adamc@144
|
1078 string x,
|
adamc@144
|
1079 string ",",
|
adamc@144
|
1080 newline]) xts),
|
adamc@144
|
1081 string "};",
|
adamc@144
|
1082 newline],
|
adamc@144
|
1083 box [string ",",
|
adamc@144
|
1084 space,
|
adamc@144
|
1085 string "lw_inputs"])
|
adamc@144
|
1086 end
|
adamc@144
|
1087
|
adamc@144
|
1088 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
|
adamc@144
|
1089 in
|
adamc@144
|
1090 box [string "if (!strncmp(request, \"",
|
adamc@144
|
1091 string (String.toString s),
|
adamc@144
|
1092 string "\", ",
|
adamc@144
|
1093 string (Int.toString (size s)),
|
adamc@198
|
1094 string ") && (request[",
|
adamc@198
|
1095 string (Int.toString (size s)),
|
adamc@198
|
1096 string "] == 0 || request[",
|
adamc@198
|
1097 string (Int.toString (size s)),
|
adamc@198
|
1098 string "] == '/')) {",
|
adamc@144
|
1099 newline,
|
adamc@144
|
1100 string "request += ",
|
adamc@144
|
1101 string (Int.toString (size s)),
|
adamc@144
|
1102 string ";",
|
adamc@144
|
1103 newline,
|
adamc@144
|
1104 string "if (*request == '/') ++request;",
|
adamc@144
|
1105 newline,
|
adamc@144
|
1106 box [string "{",
|
adamc@144
|
1107 newline,
|
adamc@144
|
1108 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
|
adamc@144
|
1109 space,
|
adamc@144
|
1110 string "arg",
|
adamc@144
|
1111 string (Int.toString i),
|
adamc@144
|
1112 space,
|
adamc@144
|
1113 string "=",
|
adamc@144
|
1114 space,
|
adamc@144
|
1115 unurlify t,
|
adamc@144
|
1116 string ";",
|
adamc@144
|
1117 newline]) ts),
|
adamc@144
|
1118 defInputs,
|
adamc@144
|
1119 p_enamed env n,
|
adamc@144
|
1120 string "(",
|
adamc@144
|
1121 p_list_sep (box [string ",", space])
|
adamc@144
|
1122 (fn x => x)
|
adamc@144
|
1123 (string "ctx" :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
|
adamc@144
|
1124 inputsVar,
|
adamc@144
|
1125 string ");",
|
adamc@144
|
1126 newline,
|
adamc@144
|
1127 string "return;",
|
adamc@144
|
1128 newline,
|
adamc@144
|
1129 string "}",
|
adamc@144
|
1130 newline,
|
adamc@144
|
1131 string "}"]
|
adamc@144
|
1132 ]
|
adamc@144
|
1133 end
|
adamc@144
|
1134
|
adamc@144
|
1135 val pds' = map p_page ps
|
adamc@29
|
1136 in
|
adamc@144
|
1137 box [string "#include <stdio.h>",
|
adamc@144
|
1138 newline,
|
adamc@144
|
1139 string "#include <stdlib.h>",
|
adamc@144
|
1140 newline,
|
adamc@144
|
1141 newline,
|
adamc@144
|
1142 string "#include \"lacweb.h\"",
|
adamc@101
|
1143 newline,
|
adamc@101
|
1144 newline,
|
adamc@101
|
1145 p_list_sep newline (fn x => x) pds,
|
adamc@101
|
1146 newline,
|
adamc@144
|
1147 string "int lw_inputs_len = ",
|
adamc@144
|
1148 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
|
adamc@144
|
1149 string ";",
|
adamc@144
|
1150 newline,
|
adamc@144
|
1151 newline,
|
adamc@144
|
1152 string "int lw_input_num(char *name) {",
|
adamc@144
|
1153 newline,
|
adamc@144
|
1154 makeSwitch (fnums, 0),
|
adamc@144
|
1155 string "}",
|
adamc@144
|
1156 newline,
|
adamc@144
|
1157 newline,
|
adamc@117
|
1158 string "void lw_handle(lw_context ctx, char *request) {",
|
adamc@101
|
1159 newline,
|
adamc@101
|
1160 p_list_sep newline (fn x => x) pds',
|
adamc@101
|
1161 newline,
|
adamc@101
|
1162 string "}",
|
adamc@101
|
1163 newline]
|
adamc@29
|
1164 end
|
adamc@29
|
1165
|
adamc@29
|
1166 end
|