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@29
|
86 | EApp (e1, e2) => parenIf par (box [p_exp' true env e1,
|
adamc@29
|
87 string "(",
|
adamc@29
|
88 p_exp env e2,
|
adamc@29
|
89 string ")"])
|
adamc@29
|
90
|
adamc@29
|
91 | ERecord (i, xes) => box [string "({",
|
adamc@29
|
92 space,
|
adamc@29
|
93 string "struct",
|
adamc@29
|
94 space,
|
adamc@29
|
95 string ("__lws_" ^ Int.toString i),
|
adamc@29
|
96 space,
|
adamc@29
|
97 string "__lw_tmp",
|
adamc@29
|
98 space,
|
adamc@29
|
99 string "=",
|
adamc@29
|
100 space,
|
adamc@29
|
101 string "{",
|
adamc@29
|
102 p_list (fn (_, e) =>
|
adamc@29
|
103 p_exp env e) xes,
|
adamc@29
|
104 string "};",
|
adamc@29
|
105 space,
|
adamc@29
|
106 string "__lw_tmp;",
|
adamc@29
|
107 space,
|
adamc@29
|
108 string "})" ]
|
adamc@29
|
109 | EField (e, x) =>
|
adamc@29
|
110 box [p_exp' true env e,
|
adamc@29
|
111 string ".",
|
adamc@29
|
112 string x]
|
adamc@29
|
113
|
adamc@117
|
114 | EWrite e => box [string "(lw_write(ctx, ",
|
adamc@102
|
115 p_exp env e,
|
adamc@102
|
116 string "), lw_unit_v)"]
|
adamc@102
|
117
|
adamc@106
|
118 | ESeq (e1, e2) => box [string "(",
|
adamc@106
|
119 p_exp env e1,
|
adamc@106
|
120 string ",",
|
adamc@106
|
121 space,
|
adamc@106
|
122 p_exp env e2,
|
adamc@106
|
123 string ")"]
|
adamc@106
|
124
|
adamc@29
|
125 and p_exp env = p_exp' false env
|
adamc@29
|
126
|
adamc@29
|
127 fun p_decl env ((d, _) : decl) =
|
adamc@29
|
128 case d of
|
adamc@29
|
129 DStruct (n, xts) =>
|
adamc@29
|
130 box [string "struct",
|
adamc@29
|
131 space,
|
adamc@29
|
132 string ("__lws_" ^ Int.toString n),
|
adamc@29
|
133 space,
|
adamc@29
|
134 string "{",
|
adamc@29
|
135 newline,
|
adamc@29
|
136 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
|
adamc@29
|
137 space,
|
adamc@29
|
138 string x,
|
adamc@29
|
139 string ";",
|
adamc@29
|
140 newline]) xts,
|
adamc@29
|
141 string "};"]
|
adamc@29
|
142
|
adamc@29
|
143 | DVal (x, n, t, e) =>
|
adamc@29
|
144 box [p_typ env t,
|
adamc@29
|
145 space,
|
adamc@29
|
146 string ("__lwn_" ^ x ^ "_" ^ Int.toString n),
|
adamc@29
|
147 space,
|
adamc@29
|
148 string "=",
|
adamc@29
|
149 space,
|
adamc@29
|
150 p_exp env e,
|
adamc@29
|
151 string ";"]
|
adamc@121
|
152 | DFun (fx, n, args, ran, e) =>
|
adamc@29
|
153 let
|
adamc@121
|
154 val nargs = length args
|
adamc@121
|
155 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
|
adamc@29
|
156 in
|
adamc@106
|
157 box [string "static",
|
adamc@106
|
158 space,
|
adamc@106
|
159 p_typ env ran,
|
adamc@29
|
160 space,
|
adamc@109
|
161 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
|
adamc@121
|
162 string "(",
|
adamc@121
|
163 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@121
|
164 (string "lw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
|
adamc@121
|
165 box [p_typ env dom,
|
adamc@121
|
166 space,
|
adamc@121
|
167 p_rel env' (nargs - i - 1)]) args),
|
adamc@29
|
168 string ")",
|
adamc@29
|
169 space,
|
adamc@29
|
170 string "{",
|
adamc@29
|
171 newline,
|
adamc@29
|
172 box[string "return(",
|
adamc@29
|
173 p_exp env' e,
|
adamc@101
|
174 string ");"],
|
adamc@29
|
175 newline,
|
adamc@29
|
176 string "}"]
|
adamc@29
|
177 end
|
adamc@29
|
178
|
adamc@120
|
179 fun unurlify (t, loc) =
|
adamc@120
|
180 case t of
|
adamc@120
|
181 TFfi ("Basis", "int") => string "lw_unurlifyInt(&request)"
|
adamc@120
|
182 | TFfi ("Basis", "float") => string "lw_unurlifyFloat(&request)"
|
adamc@120
|
183 | TFfi ("Basis", "string") => string "lw_unurlifyString(&request)"
|
adamc@120
|
184
|
adamc@120
|
185 | TRecord 0 => string "lw_unit_v"
|
adamc@120
|
186
|
adamc@120
|
187 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
|
adamc@120
|
188 space)
|
adamc@120
|
189
|
adamc@120
|
190 fun p_page env (s, n, ts) =
|
adamc@120
|
191 box [string "if (!strncmp(request, \"",
|
adamc@116
|
192 string (String.toString s),
|
adamc@120
|
193 string "\", ",
|
adamc@120
|
194 string (Int.toString (size s)),
|
adamc@120
|
195 string ")) {",
|
adamc@116
|
196 newline,
|
adamc@120
|
197 string "request += ",
|
adamc@120
|
198 string (Int.toString (size s)),
|
adamc@120
|
199 string ";",
|
adamc@116
|
200 newline,
|
adamc@120
|
201 string "if (*request == '/') ++request;",
|
adamc@119
|
202 newline,
|
adamc@120
|
203 box [string "{",
|
adamc@120
|
204 newline,
|
adamc@120
|
205 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
|
adamc@120
|
206 space,
|
adamc@120
|
207 string "arg",
|
adamc@120
|
208 string (Int.toString i),
|
adamc@120
|
209 space,
|
adamc@120
|
210 string "=",
|
adamc@120
|
211 space,
|
adamc@120
|
212 unurlify t,
|
adamc@120
|
213 string ";",
|
adamc@120
|
214 newline]) ts),
|
adamc@120
|
215 p_enamed env n,
|
adamc@120
|
216 string "(",
|
adamc@120
|
217 p_list_sep (box [string ",", space])
|
adamc@120
|
218 (fn x => x)
|
adamc@120
|
219 (string "ctx" :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
|
adamc@120
|
220 string ");",
|
adamc@120
|
221 newline,
|
adamc@120
|
222 string "return;",
|
adamc@120
|
223 newline,
|
adamc@120
|
224 string "}",
|
adamc@120
|
225 newline,
|
adamc@120
|
226 string "}"]
|
adamc@120
|
227 ]
|
adamc@101
|
228
|
adamc@101
|
229 fun p_file env (ds, ps) =
|
adamc@29
|
230 let
|
adamc@101
|
231 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
232 (p_decl env d,
|
adamc@31
|
233 E.declBinds env d))
|
adamc@101
|
234 env ds
|
adamc@101
|
235 val pds' = map (p_page env) ps
|
adamc@29
|
236 in
|
adamc@101
|
237 box [string "#include \"lacweb.h\"",
|
adamc@101
|
238 newline,
|
adamc@101
|
239 newline,
|
adamc@101
|
240 p_list_sep newline (fn x => x) pds,
|
adamc@101
|
241 newline,
|
adamc@117
|
242 string "void lw_handle(lw_context ctx, char *request) {",
|
adamc@101
|
243 newline,
|
adamc@101
|
244 p_list_sep newline (fn x => x) pds',
|
adamc@101
|
245 newline,
|
adamc@101
|
246 string "}",
|
adamc@101
|
247 newline]
|
adamc@29
|
248 end
|
adamc@29
|
249
|
adamc@29
|
250 end
|