adamc@110
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@110
|
2 * All rights reserved.
|
adamc@110
|
3 *
|
adamc@110
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@110
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@110
|
6 *
|
adamc@110
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@110
|
8 * this list of conditions and the following disclaimer.
|
adamc@110
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@110
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@110
|
11 * and/or other materials provided with the distribution.
|
adamc@110
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@110
|
13 * derived from this software without specific prior written permission.
|
adamc@110
|
14 *
|
adamc@110
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@110
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@110
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@110
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@110
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@110
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@110
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@110
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@110
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@110
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@110
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@110
|
26 *)
|
adamc@110
|
27
|
adamc@110
|
28 structure Tag :> TAG = struct
|
adamc@110
|
29
|
adamc@110
|
30 open Core
|
adamc@110
|
31
|
adamc@110
|
32 structure U = CoreUtil
|
adamc@110
|
33 structure E = CoreEnv
|
adamc@110
|
34
|
adamc@110
|
35 structure IM = IntBinaryMap
|
adamc@112
|
36 structure SM = BinaryMapFn(struct
|
adamc@112
|
37 type ord_key = string
|
adamc@112
|
38 val compare = String.compare
|
adamc@112
|
39 end)
|
adamc@110
|
40
|
adamc@110
|
41 fun kind (k, s) = (k, s)
|
adamc@110
|
42 fun con (c, s) = (c, s)
|
adamc@110
|
43
|
adamc@112
|
44 fun exp env (e, s) =
|
adamc@110
|
45 case e of
|
adamc@110
|
46 EApp (
|
adamc@110
|
47 (EApp (
|
adamc@110
|
48 (EApp (
|
adamc@110
|
49 (ECApp (
|
adamc@110
|
50 (ECApp (
|
adamc@110
|
51 (ECApp (
|
adamc@110
|
52 (ECApp (
|
adamc@140
|
53 (ECApp (
|
adamc@140
|
54 (ECApp (
|
adamc@140
|
55 (ECApp (
|
adamc@140
|
56 (ECApp (
|
adamc@140
|
57 (EFfi ("Basis", "tag"),
|
adamc@140
|
58 loc), given), _), absent), _), outer), _), inner), _),
|
adamc@140
|
59 useOuter), _), useInner), _), bindOuter), _), bindInner), _),
|
adamc@110
|
60 attrs), _),
|
adamc@110
|
61 tag), _),
|
adamc@110
|
62 xml) =>
|
adamc@110
|
63 (case attrs of
|
adamc@110
|
64 (ERecord xets, _) =>
|
adamc@110
|
65 let
|
adamc@110
|
66 val (xets, s) =
|
adamc@112
|
67 ListUtil.foldlMap (fn ((x, e, t), (count, tags, byTag, newTags)) =>
|
adamc@143
|
68 let
|
adamc@143
|
69 fun tagIt newAttr =
|
adamc@143
|
70 let
|
adamc@143
|
71 fun unravel (e, _) =
|
adamc@143
|
72 case e of
|
adamc@143
|
73 ENamed n => (n, [])
|
adamc@143
|
74 | EApp (e1, e2) =>
|
adamc@143
|
75 let
|
adamc@143
|
76 val (n, es) = unravel e1
|
adamc@143
|
77 in
|
adamc@143
|
78 (n, es @ [e2])
|
adamc@143
|
79 end
|
adamc@143
|
80 | _ => (ErrorMsg.errorAt loc "Invalid link expression";
|
adamc@143
|
81 (0, []))
|
adamc@110
|
82
|
adamc@110
|
83
|
adamc@110
|
84
|
adamc@143
|
85 val (f, args) = unravel e
|
adamc@112
|
86
|
adamc@143
|
87 val (cn, count, tags, newTags) =
|
adamc@143
|
88 case IM.find (tags, f) of
|
adamc@143
|
89 NONE =>
|
adamc@143
|
90 (count, count + 1, IM.insert (tags, f, count),
|
adamc@143
|
91 (f, count) :: newTags)
|
adamc@143
|
92 | SOME cn => (cn, count, tags, newTags)
|
adamc@143
|
93
|
adamc@143
|
94 val (_, _, _, s) = E.lookupENamed env f
|
adamc@112
|
95
|
adamc@143
|
96 val byTag = case SM.find (byTag, s) of
|
adamc@143
|
97 NONE => SM.insert (byTag, s, f)
|
adamc@143
|
98 | SOME f' =>
|
adamc@143
|
99 (if f = f' then
|
adamc@143
|
100 ()
|
adamc@143
|
101 else
|
adamc@143
|
102 ErrorMsg.errorAt loc
|
adamc@143
|
103 ("Duplicate HTTP tag "
|
adamc@143
|
104 ^ s);
|
adamc@143
|
105 byTag)
|
adamc@143
|
106
|
adamc@143
|
107 val e = (EClosure (cn, args), loc)
|
adamc@143
|
108 val t = (CFfi ("Basis", "string"), loc)
|
adamc@143
|
109 in
|
adamc@143
|
110 (((CName newAttr, loc), e, t),
|
adamc@143
|
111 (count, tags, byTag, newTags))
|
adamc@143
|
112 end
|
adamc@143
|
113 in
|
adamc@143
|
114 case x of
|
adamc@143
|
115 (CName "Link", _) => tagIt "Href"
|
adamc@143
|
116 | (CName "Action", _) => tagIt "Action"
|
adamc@143
|
117 | _ => ((x, e, t), (count, tags, byTag, newTags))
|
adamc@143
|
118 end)
|
adamc@110
|
119 s xets
|
adamc@110
|
120 in
|
adamc@110
|
121 (EApp (
|
adamc@110
|
122 (EApp (
|
adamc@110
|
123 (EApp (
|
adamc@110
|
124 (ECApp (
|
adamc@110
|
125 (ECApp (
|
adamc@110
|
126 (ECApp (
|
adamc@110
|
127 (ECApp (
|
adamc@140
|
128 (ECApp (
|
adamc@140
|
129 (ECApp (
|
adamc@140
|
130 (ECApp (
|
adamc@140
|
131 (ECApp (
|
adamc@140
|
132 (EFfi ("Basis", "tag"),
|
adamc@140
|
133 loc), given), loc), absent), loc), outer), loc), inner), loc),
|
adamc@140
|
134 useOuter), loc), useInner), loc), bindOuter), loc), bindInner), loc),
|
adamc@110
|
135 (ERecord xets, loc)), loc),
|
adamc@110
|
136 tag), loc),
|
adamc@110
|
137 xml), s)
|
adamc@110
|
138 end
|
adamc@110
|
139 | _ => (ErrorMsg.errorAt loc "Attribute record is too complex";
|
adamc@110
|
140 (e, s)))
|
adamc@110
|
141
|
adamc@110
|
142 | _ => (e, s)
|
adamc@110
|
143
|
adamc@110
|
144 fun decl (d, s) = (d, s)
|
adamc@110
|
145
|
adamc@110
|
146 fun tag file =
|
adamc@110
|
147 let
|
adamc@110
|
148 val count = foldl (fn ((d, _), count) =>
|
adamc@110
|
149 case d of
|
adamc@110
|
150 DCon (_, n, _, _) => Int.max (n, count)
|
adamc@110
|
151 | DVal (_, n, _, _, _) => Int.max (n, count)
|
adamc@125
|
152 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
|
adamc@110
|
153 | DExport _ => count) 0 file
|
adamc@110
|
154
|
adamc@112
|
155 fun doDecl (d as (d', loc), (env, count, tags, byTag)) =
|
adamc@112
|
156 case d' of
|
adamc@112
|
157 DExport n =>
|
adamc@112
|
158 let
|
adamc@112
|
159 val (_, _, _, s) = E.lookupENamed env n
|
adamc@112
|
160 in
|
adamc@112
|
161 case SM.find (byTag, s) of
|
adamc@112
|
162 NONE => ([d], (env, count, tags, byTag))
|
adamc@112
|
163 | SOME n' => ([], (env, count, tags, byTag))
|
adamc@112
|
164 end
|
adamc@112
|
165 | _ =>
|
adamc@112
|
166 let
|
adamc@126
|
167 val env' = E.declBinds env d
|
adamc@126
|
168 val env'' = case d' of
|
adamc@126
|
169 DValRec _ => env'
|
adamc@126
|
170 | _ => env
|
adamc@126
|
171
|
adamc@112
|
172 val (d, (count, tags, byTag, newTags)) =
|
adamc@112
|
173 U.Decl.foldMap {kind = kind,
|
adamc@112
|
174 con = con,
|
adamc@126
|
175 exp = exp env'',
|
adamc@112
|
176 decl = decl}
|
adamc@112
|
177 (count, tags, byTag, []) d
|
adamc@110
|
178
|
adamc@126
|
179 val env = env'
|
adamc@110
|
180
|
adamc@126
|
181 val newDs = map
|
adamc@112
|
182 (fn (f, cn) =>
|
adamc@112
|
183 let
|
adamc@112
|
184 fun unravel (all as (t, _)) =
|
adamc@112
|
185 case t of
|
adamc@112
|
186 TFun (dom, ran) =>
|
adamc@112
|
187 let
|
adamc@112
|
188 val (args, result) = unravel ran
|
adamc@112
|
189 in
|
adamc@112
|
190 (dom :: args, result)
|
adamc@112
|
191 end
|
adamc@112
|
192 | _ => ([], all)
|
adamc@110
|
193
|
adamc@112
|
194 val (fnam, t, _, tag) = E.lookupENamed env f
|
adamc@112
|
195 val (args, result) = unravel t
|
adamc@110
|
196
|
adamc@112
|
197 val unit = (TRecord (CRecord ((KType, loc), []), loc), loc)
|
adamc@119
|
198
|
adamc@119
|
199 val (abs, t) =
|
adamc@119
|
200 case args of
|
adamc@119
|
201 [] =>
|
adamc@119
|
202 let
|
adamc@119
|
203 val body = (EWrite (ENamed f, loc), loc)
|
adamc@119
|
204 in
|
adamc@119
|
205 ((EAbs ("x", unit, unit, body), loc),
|
adamc@119
|
206 (TFun (unit, unit), loc))
|
adamc@119
|
207 end
|
adamc@119
|
208 | _ =>
|
adamc@119
|
209 let
|
adamc@119
|
210 val (app, _) = foldl (fn (t, (app, n)) =>
|
adamc@119
|
211 ((EApp (app, (ERel n, loc)), loc),
|
adamc@119
|
212 n - 1))
|
adamc@119
|
213 ((ENamed f, loc), length args - 1) args
|
adamc@119
|
214 val body = (EWrite app, loc)
|
adamc@119
|
215 val (abs, _, t) = foldr (fn (t, (abs, n, rest)) =>
|
adamc@119
|
216 ((EAbs ("x" ^ Int.toString n,
|
adamc@119
|
217 t,
|
adamc@119
|
218 rest,
|
adamc@119
|
219 abs), loc),
|
adamc@119
|
220 n + 1,
|
adamc@119
|
221 (TFun (t, rest), loc)))
|
adamc@119
|
222 (body, 0, unit) args
|
adamc@119
|
223 in
|
adamc@119
|
224 (abs, t)
|
adamc@119
|
225 end
|
adamc@112
|
226 in
|
adamc@126
|
227 (("wrap_" ^ fnam, cn, t, abs, tag),
|
adamc@126
|
228 (DExport cn, loc))
|
adamc@112
|
229 end) newTags
|
adamc@126
|
230
|
adamc@126
|
231 val (newVals, newExports) = ListPair.unzip newDs
|
adamc@126
|
232
|
adamc@126
|
233 val ds = case d of
|
adamc@126
|
234 (DValRec vis, _) => [(DValRec (vis @ newVals), loc)]
|
adamc@126
|
235 | _ => map (fn vi => (DVal vi, loc)) newVals @ [d]
|
adamc@112
|
236 in
|
adamc@126
|
237 (ds @ newExports, (env, count, tags, byTag))
|
adamc@112
|
238 end
|
adamc@110
|
239
|
adamc@112
|
240 val (file, _) = ListUtil.foldlMapConcat doDecl (CoreEnv.empty, count+1, IM.empty, SM.empty) file
|
adamc@110
|
241 in
|
adamc@110
|
242 file
|
adamc@110
|
243 end
|
adamc@110
|
244
|
adamc@110
|
245 end
|