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 /MLRISC/releases/release-110.60/library/annotations.sml
ViewVC logotype

Annotation of /MLRISC/releases/release-110.60/library/annotations.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 498 - (view) (download)
Original Path: sml/branches/SMLNJ/src/MLRISC/library/annotations.sml

1 : monnier 411 (*
2 :     * User definable annotations.
3 :     *
4 :     * Note: annotations will now be used extensively in all part of
5 :     * the optimizer.
6 :     *
7 :     * Idea is stolen from Stephen Weeks
8 :     *
9 :     * -- Allen
10 :     *)
11 :    
12 : monnier 469 structure Annotations :> ANNOTATIONS =
13 : monnier 245 struct
14 :    
15 :     type annotation = exn
16 :     type annotations = annotation list
17 : monnier 469 type propList = annotations
18 :     exception NoProperty
19 :     type 'a property =
20 :     { get : annotations -> 'a option,
21 :     peek : annotation -> 'a option,
22 :     lookup : annotations -> 'a,
23 :     contains : annotations -> bool,
24 :     set : 'a * annotations -> annotations,
25 :     rmv : annotations -> annotations,
26 :     create : 'a -> annotation
27 :     }
28 :     type flag = unit property
29 : monnier 245
30 : monnier 411 val prettyPrinters = ref [] : (annotation -> string) list ref
31 : monnier 245
32 : monnier 411 fun attachPrettyPrinter p = prettyPrinters := p :: !prettyPrinters
33 : monnier 245
34 : monnier 411 fun toString a =
35 : monnier 498 let fun pr([]) = ""
36 : monnier 411 | pr(p::ps) = (p a handle _ => pr ps)
37 :     in pr(!prettyPrinters) end
38 :    
39 : monnier 245 (*
40 :     * Look ma, a real use of generative exceptions!
41 :     *)
42 : monnier 469 fun 'a new(prettyPrinter) =
43 : monnier 245 let exception Annotation of 'a
44 :     fun get [] = NONE
45 :     | get (Annotation x::_) = SOME x
46 :     | get (_::l) = get l
47 : monnier 469 fun peek(Annotation x) = SOME x
48 :     | peek _ = NONE
49 :     fun lookup [] = raise NoProperty
50 :     | lookup (Annotation x::_) = x
51 :     | lookup (_::l) = lookup l
52 :     fun contains [] = false
53 :     | contains (Annotation _::_) = true
54 :     | contains (_::l) = contains l
55 :     fun set(x,[]) = [Annotation x]
56 :     | set(x,Annotation _::l) = Annotation x::l
57 :     | set(x,y::l) = y::set(x,l)
58 : monnier 245 fun rmv [] = []
59 :     | rmv (Annotation _::l) = rmv l
60 :     | rmv (x::l) = x::rmv l
61 : monnier 469 in case prettyPrinter of
62 :     SOME f => attachPrettyPrinter(fn Annotation x => f x | e => raise e)
63 :     | NONE => ();
64 :     { get=get, peek=peek, lookup=lookup, contains=contains,
65 :     set=set, rmv=rmv, create=Annotation
66 :     }
67 : monnier 245 end
68 : monnier 469
69 :     fun newFlag "" = new NONE
70 :     | newFlag name = new(SOME(fn _ => name))
71 : monnier 245
72 :     end
73 :    

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