Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cm/semant/semant.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/semant/semant.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 643 - (view) (download)

1 : blume 267 (*
2 :     * semantic actions to go with the grammar for CM description files
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 : blume 265 signature CM_SEMANT = sig
9 :    
10 : blume 354 type context = SrcPath.context
11 :     type pathname = SrcPath.t
12 : blume 297 type region = GenericVC.SourceMap.region
13 : blume 265 type ml_symbol
14 :     type cm_symbol
15 : blume 380 type cm_class
16 : blume 632 type cm_version = Version.t
17 : blume 265
18 : blume 294 type group = GroupGraph.group
19 : blume 265
20 : blume 305 type privilegespec
21 : blume 265 type aexp
22 :     type exp
23 :     type members (* still conditional *)
24 :     type exports (* still conditional *)
25 :    
26 : blume 588 type toolopt
27 :    
28 : blume 266 type complainer = string -> unit
29 :    
30 : blume 267 (* getting elements of primitive types (pathnames and symbols) *)
31 : blume 270 val file_native : string * context -> pathname
32 : blume 632 val file_standard :
33 :     GeneralParams.info -> string * context * complainer -> pathname
34 : blume 265 val cm_symbol : string -> cm_symbol
35 : blume 632 val cm_version : string * complainer -> cm_version
36 : blume 265 val ml_structure : string -> ml_symbol
37 :     val ml_signature : string -> ml_symbol
38 :     val ml_functor : string -> ml_symbol
39 :     val ml_funsig : string -> ml_symbol
40 : blume 380 val class : cm_symbol -> cm_class
41 : blume 265
42 : blume 270 (* getting the full analysis for a group/library *)
43 : blume 632 val group : { path: pathname,
44 :     privileges: privilegespec,
45 :     exports: exports option,
46 :     members: members,
47 :     gp: GeneralParams.info,
48 :     curlib: pathname option,
49 :     owner: pathname option,
50 :     error: complainer,
51 :     initgroup: group } -> group
52 :     val library : { path: pathname,
53 :     privileges: privilegespec,
54 :     exports: exports,
55 :     version : cm_version option,
56 :     members: members,
57 :     gp: GeneralParams.info,
58 :     initgroup: group } -> group
59 : blume 265
60 : blume 283 (* assembling privilege lists *)
61 :     val initialPrivilegeSpec : privilegespec
62 :     val require : privilegespec * cm_symbol * complainer -> privilegespec
63 : blume 348 val wrap : privilegespec * cm_symbol * complainer -> privilegespec
64 : blume 265
65 : blume 267 (* constructing member collections *)
66 : blume 265 val emptyMembers : members
67 : blume 297 val member :
68 : blume 632 { gp: GeneralParams.info,
69 :     rparse: pathname option -> pathname * Version.t option -> group,
70 :     load_plugin: SrcPath.context -> string -> bool }
71 : blume 587 -> { name: string,
72 :     mkpath: string -> pathname,
73 :     group: pathname * region,
74 :     class: cm_class option,
75 : blume 588 tooloptions: toolopt list option,
76 : blume 493 context: SrcPath.context }
77 : blume 270 -> members
78 : blume 265 val members : members * members -> members
79 : blume 443 val guarded_members :
80 :     exp * (members * members) * (string -> unit) -> members
81 : blume 275 val error_member : (unit -> unit) -> members
82 : blume 265
83 : blume 267 (* constructing export lists *)
84 : blume 265 val emptyExports : exports
85 : blume 356 val export : ml_symbol * complainer -> exports
86 : blume 265 val exports : exports * exports -> exports
87 : blume 275 val guarded_exports :
88 :     exp * (exports * exports) * (string -> unit) -> exports
89 :     val error_export : (unit -> unit) -> exports
90 : blume 265
91 : blume 632 (* groups of operator symbols (to make grammar smaller) *)
92 :     type addsym
93 :     val PLUS : addsym
94 :     val MINUS : addsym
95 :    
96 :     type mulsym
97 :     val TIMES : mulsym
98 :     val DIV : mulsym
99 :     val MOD : mulsym
100 :    
101 :     type eqsym
102 :     val EQ : eqsym
103 :     val NE : eqsym
104 :    
105 :     type ineqsym
106 :     val GT : ineqsym
107 :     val GE : ineqsym
108 :     val LT : ineqsym
109 :     val LE : ineqsym
110 :    
111 : blume 267 (* arithmetic (number-valued) expression *)
112 : blume 265 val number : int -> aexp
113 : blume 336 val variable : GeneralParams.info -> cm_symbol -> aexp
114 : blume 632 val add : aexp * addsym * aexp -> aexp
115 :     val mul : aexp * mulsym * aexp -> aexp
116 :     val sign : addsym * aexp -> aexp
117 : blume 265 val negate : aexp -> aexp
118 :    
119 : blume 267 (* (bool-valued) expressions *)
120 : blume 265 val ml_defined : ml_symbol -> exp
121 : blume 336 val cm_defined : GeneralParams.info -> cm_symbol -> exp
122 : blume 265 val conj : exp * exp -> exp
123 :     val disj : exp * exp -> exp
124 : blume 632 val beq : exp * eqsym * exp -> exp
125 : blume 265 val not : exp -> exp
126 : blume 632 val ineq : aexp * ineqsym * aexp -> exp
127 :     val eq : aexp * eqsym * aexp -> exp
128 : blume 588
129 :     (* tool options *)
130 :     val string : { name: string, mkpath: string -> pathname } -> toolopt
131 :     val subopts : { name: string, opts: toolopt list } -> toolopt
132 : blume 265 end
133 :    
134 :     structure CMSemant :> CM_SEMANT = struct
135 :    
136 : blume 267 structure SymPath = GenericVC.SymPath
137 : blume 294 structure EM = GenericVC.ErrorMsg
138 :     structure GG = GroupGraph
139 : blume 267
140 : blume 354 type pathname = SrcPath.t
141 :     type context = SrcPath.context
142 : blume 297 type region = GenericVC.SourceMap.region
143 : blume 267 type ml_symbol = Symbol.symbol
144 : blume 265 type cm_symbol = string
145 : blume 380 type cm_class = string
146 : blume 632 type cm_version = Version.t
147 : blume 265
148 : blume 294 type group = GG.group
149 : blume 348 type privilegespec = { required: GG.privileges, wrapped: GG.privileges }
150 : blume 265
151 : blume 268 type environment = MemberCollection.collection
152 : blume 265
153 :     type aexp = environment -> int
154 :     type exp = environment -> bool
155 : blume 367 type members = environment * pathname option -> MemberCollection.collection
156 : blume 267 type exports = environment -> SymbolSet.set
157 : blume 265
158 : blume 588 type toolopt = PrivateTools.toolopt
159 :    
160 : blume 266 type complainer = string -> unit
161 :    
162 : blume 275 fun saveEval (exp, env, error) =
163 : blume 267 exp env
164 :     handle exn =>
165 : blume 275 (error ("expression raises exception: " ^ General.exnMessage exn);
166 :     false)
167 : blume 267
168 : blume 354 fun file_native (s, d) = SrcPath.native { context = d, spec = s }
169 : blume 632 fun file_standard (gp: GeneralParams.info) (s, d, err) =
170 : blume 643 SrcPath.standard (#pcmode (#param gp))
171 :     { context = d, spec = s, err = err }
172 : blume 265 fun cm_symbol s = s
173 : blume 632 fun cm_version (s, error) =
174 :     case Version.fromString s of
175 :     SOME v => v
176 :     | NONE => (error "ill-formed version specification"; Version.zero)
177 : blume 267 val ml_structure = Symbol.strSymbol
178 :     val ml_signature = Symbol.sigSymbol
179 :     val ml_functor = Symbol.fctSymbol
180 :     val ml_funsig = Symbol.fsigSymbol
181 : blume 265
182 : blume 380 fun class s = String.map Char.toLower s
183 :    
184 : blume 282 fun applyTo mc e = e mc
185 :    
186 : blume 340 fun sgl2sll subgroups = let
187 : blume 444 fun sameSL (p, g) (p', g') = SrcPath.compare (p, p') = EQUAL
188 : blume 340 fun add (x, l) =
189 :     if List.exists (sameSL x) l then l else x :: l
190 : blume 444 fun oneSG (x as (_, GG.GROUP { kind, sublibs, ... }), l) =
191 : blume 587 (case kind of
192 :     GG.NOLIB _ => foldl add l sublibs
193 :     | _ => add (x, l))
194 :     | oneSG (_, l) = l
195 : blume 340 in
196 :     foldl oneSG [] subgroups
197 :     end
198 :    
199 : blume 632 fun group arg = let
200 :     val { path = g, privileges = p, exports = e, members = m,
201 :     gp, curlib, owner, error, initgroup } = arg
202 : blume 642 val mc = applyTo (MemberCollection.implicit gp initgroup, curlib) m
203 : blume 283 val filter = Option.map (applyTo mc) e
204 : blume 537 val pfsbn = let
205 : blume 587 val { exports, ... } =
206 : blume 632 case initgroup of
207 : blume 587 GG.GROUP x => x
208 :     | GG.ERRORGROUP =>
209 : blume 632 EM.impossible "semant.sml: group: bad init group"
210 : blume 537 in
211 : blume 592 #1 (valOf (SymbolMap.find (exports, PervAccess.pervStrSym)))
212 : blume 537 end
213 :     val (exports, rp) = MemberCollection.build (mc, filter, gp, pfsbn)
214 : blume 294 val subgroups = MemberCollection.subgroups mc
215 : blume 348 val { required = rp', wrapped = wr } = p
216 :     val rp'' = StringSet.union (rp', StringSet.union (rp, wr))
217 : blume 280 in
218 : blume 632 if StringSet.isEmpty wr then ()
219 :     else EM.impossible "group with wrapped privileges";
220 : blume 348 GG.GROUP { exports = exports,
221 : blume 632 kind = GG.NOLIB { subgroups = subgroups, owner = owner },
222 : blume 348 required = rp'',
223 : blume 305 grouppath = g,
224 : blume 642 sources = MemberCollection.sources mc,
225 : blume 348 sublibs = sgl2sll subgroups }
226 : blume 280 end
227 :    
228 : blume 632 fun library arg = let
229 :     val { path = g, privileges = p, exports = e, members = m,
230 :     version, gp, initgroup } = arg
231 : blume 642 val mc = applyTo (MemberCollection.implicit gp initgroup, SOME g) m
232 : blume 632 val filter = SOME (applyTo mc e)
233 :     val pfsbn = let
234 :     val { exports, ... } =
235 :     case initgroup of
236 :     GG.GROUP x => x
237 :     | GG.ERRORGROUP =>
238 :     EM.impossible "semant.sml: lib: bad init group"
239 :     in
240 :     #1 (valOf (SymbolMap.find (exports, PervAccess.pervStrSym)))
241 :     end
242 :     val (exports, rp) = MemberCollection.build (mc, filter, gp, pfsbn)
243 :     val subgroups = MemberCollection.subgroups mc
244 :     val { required = rp', wrapped = wr } = p
245 :     val rp'' = StringSet.union (rp', StringSet.union (rp, wr))
246 :     in
247 :     GG.GROUP { exports = exports,
248 :     kind = GG.LIB { version = version,
249 :     kind = GG.DEVELOPED { subgroups = subgroups,
250 :     wrapped = wr } },
251 :     required = rp'',
252 :     grouppath = g,
253 : blume 642 sources = MemberCollection.sources mc,
254 : blume 632 sublibs = sgl2sll subgroups }
255 :     end
256 : blume 265
257 : blume 266 local
258 : blume 267 val isMember = StringSet.member
259 : blume 348 fun sanity ({ required, wrapped }, s, error) =
260 :     if isMember (required, s) orelse isMember (wrapped, s) then
261 : blume 283 error ("duplicate privilege name: " ^ s)
262 : blume 266 else ()
263 :     in
264 : blume 283 val initialPrivilegeSpec = { required = StringSet.empty,
265 : blume 348 wrapped = StringSet.empty }
266 :     fun require (a as ({ required, wrapped }, s, _)) =
267 : blume 266 (sanity a;
268 : blume 348 { required = StringSet.add (required, s), wrapped = wrapped })
269 :     fun wrap (a as ({ required, wrapped }, s, _)) =
270 : blume 266 (sanity a;
271 : blume 348 { required = required, wrapped = StringSet.add (wrapped, s) })
272 : blume 266 end
273 : blume 265
274 : blume 367 fun emptyMembers (env, _) = env
275 : blume 632 fun member { gp, rparse, load_plugin } arg (env, curlib) = let
276 :     val coll = MemberCollection.expandOne
277 :     { gp = gp, rparse = rparse curlib,
278 :     load_plugin = load_plugin }
279 :     arg
280 : blume 297 val group = #group arg
281 : blume 299 val error = GroupReg.error (#groupreg gp) group
282 : blume 294 fun e0 s = error EM.COMPLAIN s EM.nullErrorBody
283 : blume 573 fun checkowner (_, GG.GROUP { kind = GG.NOLIB { owner, ... }, ...}) =
284 :     let fun libname NONE = "<toplevel>"
285 :     | libname (SOME p) = SrcPath.descr p
286 :     fun eq (NONE, NONE) = true
287 :     | eq (SOME p, SOME p') = SrcPath.compare (p, p') = EQUAL
288 :     | eq _ = false
289 :     in
290 :     if eq (curlib, owner) then ()
291 : blume 587 else e0 (concat ["owner of subgroup (", libname owner,
292 : blume 573 ") does not match current library (",
293 : monnier 581 libname curlib, ")"])
294 : blume 573 end
295 :     | checkowner _ = ()
296 : blume 270 in
297 : blume 573 app checkowner (MemberCollection.subgroups coll);
298 : blume 277 MemberCollection.sequential (env, coll, e0)
299 : blume 270 end
300 : blume 367 fun members (m1, m2) (env, curlib) = m2 (m1 (env, curlib), curlib)
301 :     fun guarded_members (c, (m1, m2), error) (env, curlib) =
302 :     if saveEval (c, env, error) then m1 (env, curlib) else m2 (env, curlib)
303 :     fun error_member thunk (env, _) = (thunk (); env)
304 : blume 265
305 : blume 267 fun emptyExports env = SymbolSet.empty
306 : blume 356 fun export (s, error) env =
307 :     if MemberCollection.ml_look env s then SymbolSet.singleton s
308 :     else (error (concat ["exported ",
309 :     Symbol.nameSpaceToString (Symbol.nameSpace s),
310 :     " not defined: ", Symbol.name s]);
311 :     SymbolSet.empty)
312 : blume 267 fun exports (e1, e2) env = SymbolSet.union (e1 env, e2 env)
313 : blume 275 fun guarded_exports (c, (e1, e2), error) env =
314 :     if saveEval (c, env, error) then e1 env else e2 env
315 :     fun error_export thunk env = (thunk (); SymbolSet.empty)
316 : blume 265
317 : blume 632 datatype addsym = PLUS | MINUS
318 :     datatype mulsym = TIMES | DIV | MOD
319 :     datatype eqsym = EQ | NE
320 :     datatype ineqsym = GT | GE | LT | LE
321 :    
322 : blume 265 fun number i _ = i
323 : blume 336 fun variable gp v e = MemberCollection.num_look gp e v
324 : blume 632 fun add (e1, PLUS, e2) e = e1 e + e2 e
325 :     | add (e1, MINUS, e2) e = e1 e - e2 e
326 :     fun mul (e1, TIMES, e2) e = e1 e * e2 e
327 :     | mul (e1, DIV, e2) e = e1 e div e2 e
328 :     | mul (e1, MOD, e2) e = e1 e mod e2 e
329 :     fun sign (PLUS, ex) e = ex e
330 :     | sign (MINUS, ex) e = ~(ex e)
331 : blume 265 fun negate ex e = ~(ex e)
332 :    
333 : blume 267 fun ml_defined s e = MemberCollection.ml_look e s
334 : blume 336 fun cm_defined gp s e = MemberCollection.cm_look gp e s
335 : blume 265 fun conj (e1, e2) e = e1 e andalso e2 e
336 :     fun disj (e1, e2) e = e1 e orelse e2 e
337 : blume 632 fun beq (e1: exp, EQ, e2) e = e1 e = e2 e
338 :     | beq (e1, NE, e2) e = e1 e <> e2 e
339 : blume 265 fun not ex e = Bool.not (ex e)
340 : blume 632 fun ineq (e1, LT, e2) e = e1 e < e2 e
341 :     | ineq (e1, LE, e2) e = e1 e <= e2 e
342 :     | ineq (e1, GT, e2) e = e1 e > e2 e
343 :     | ineq (e1, GE, e2) e = e1 e >= e2 e
344 :     fun eq (e1: aexp, EQ, e2) e = e1 e = e2 e
345 :     | eq (e1, NE, e2) e = e1 e <> e2 e
346 : blume 588
347 :     val string = PrivateTools.STRING
348 :     val subopts = PrivateTools.SUBOPTS
349 : blume 265 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0