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

SCM Repository

[smlnj] View of /sml/trunk/src/MLRISC/library/annotations.sml
ViewVC logotype

View of /sml/trunk/src/MLRISC/library/annotations.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 651 - (download) (annotate)
Thu Jun 1 18:34:03 2000 UTC (19 years, 3 months ago) by monnier
File size: 2719 byte(s)
bring revisions from the vendor branch to the trunk
(*
 *  User definable annotations.
 *
 *  Note: annotations will now be used extensively in all part of
 *  the optimizer.
 *
 *  Idea is stolen from Stephen Weeks
 * 
 *  -- Allen
 *)

structure Annotations : ANNOTATIONS =
struct

   type annotation = exn
   type annotations = annotation list
   type propList = annotations
   exception NoProperty
   type 'a property = 
         { get      : annotations -> 'a option,
           peek     : annotation -> 'a option,
           lookup   : annotations -> 'a,
           contains : annotations -> bool,
           set      : 'a * annotations -> annotations,
           rmv      : annotations -> annotations,
           create   : 'a -> annotation
         }
   type flag = unit property

   val prettyPrinters = ref [] : (annotation -> string) list ref 

   fun attachPrettyPrinter p = prettyPrinters := p :: !prettyPrinters

   fun toString a =
   let fun pr([]) = ""
         | pr(p::ps) = (p a handle _ => pr ps)
   in  pr(!prettyPrinters) end

   (*
    * Look ma, a real use of generative exceptions!
    *)
   fun 'a new(toString) =
   let exception Annotation of 'a
       fun get [] = NONE
         | get (Annotation x::_) = SOME x
         | get (_::l) = get l
       fun peek(Annotation x) = SOME x
         | peek _ = NONE
       fun lookup [] = raise NoProperty
         | lookup (Annotation x::_) = x
         | lookup (_::l) = lookup l
       fun contains [] = false
         | contains (Annotation _::_) = true
         | contains (_::l) = contains l
       fun set(x,[]) = [Annotation x]
         | set(x,Annotation _::l) = Annotation x::l
         | set(x,y::l) = y::set(x,l)
       fun rmv [] = []
         | rmv (Annotation _::l) = rmv l
         | rmv (x::l) = x::rmv l
   in  case toString of
         NONE   => ()
       | SOME f => attachPrettyPrinter(fn Annotation x => f x | e => raise e);
       { get=get, peek=peek, lookup=lookup, contains=contains,
         set=set, rmv=rmv, create=Annotation
       }
   end

   fun 'a new'{create, toString, get=get'} = 
   let fun get [] = NONE
         | get (x::l) = SOME(get' x) handle _ => get l
       fun peek x = SOME(get' x) handle _ => NONE
       fun lookup [] = raise NoProperty
         | lookup (x::l) = get' x handle _ => lookup l
       fun contains [] = false
         | contains (x::l) = (get' x; true) handle _ => contains l
       fun set(x,[]) = [create x]
         | set(x,a::l) = (get' a; create x::l) handle _ => a::set(x,l)
       fun rmv [] = []
         | rmv (x::l) = (get' x; rmv l) handle _ => x::rmv l
   in  attachPrettyPrinter(toString o get');
       { get=get, peek=peek, lookup=lookup, contains=contains,
         set=set, rmv=rmv, create=create
       }
   end


end


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