Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/ast/var.sml
ViewVC logotype

View of /branches/vis15/src/compiler/ast/var.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3430 - (download) (annotate)
Fri Nov 13 22:24:24 2015 UTC (3 years, 11 months ago) by jhr
File size: 6297 byte(s)
  working on merge
(* var.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)

structure Var : sig

    type t

  (* AST variable kinds *)
    datatype kind
      = BasisVar		(* predefined basis variable (see basis-vars.sml) *)
      | ConstVar		(* 'const' variable *)
      | InputVar		(* 'input' variable *)
      | GlobalVar		(* variable defined at global scope *)
      | FunVar                  (* user-defined function *)
      | FunParam		(* parameter to function *)
      | StrandParam             (* parameter to strand definition *)
      | StrandStateVar          (* strand state variable *)
      | StrandOutputVar         (* strand output variable *)
      | LocalVar                (* local variable in method *)
      | IterVar                 (* iteration variable *)

  (* define a new monomorphic variable *)
    val new : Atom.atom * Error.location * kind * Types.ty -> t
    
  (* define a new polymorphic variable; this function is used to define Basis variables,
   * but not user-variables, so we omit the location and kind information.
   *)
    val newBasis : Atom.atom * Types.scheme -> t

  (* make a copy of a variable (same name and type) *)
    val copy : t -> t

  (* return the variable's name (as it appears in the source) *)
    val nameOf : t -> string

  (* return the variable's type scheme *)
    val typeOf : t -> Types.scheme

  (* return the variable's type; this function raise Fail if the variable is polymorphic *)
    val monoTypeOf : t -> Types.ty

  (* return the variable's kind *)
    val kindOf : t -> kind

  (* return a unique string representation of the variable *)
    val uniqueNameOf : t -> string

  (* return the variable's binding location; this function returns UNKNOWN for basis variables *)
    val locationOf : t -> Error.location

  (* return true if the variable is a primitive variable (i.e., defined in the Basis) *)
    val isPrim : t -> bool

  (* return true if the variable is a compile-time constant *)
    val isConst : t -> bool

  (* return true if the variable is an output variable *)
    val isOutput : t -> bool

  (* define a property for variables *)
    val newProp : (t -> 'a) -> {
	    clrFn  : t -> unit,
	    getFn  : t -> 'a,
	    peekFn : t -> 'a option,
	    setFn  : t * 'a -> unit
	  }
  (* define a boolean property for variables *)
    val newFlag : unit -> { getFn : t -> bool, setFn : t * bool -> unit }

  (* are two variables the same? *)
    val same : t * t -> bool

  (* compare two variables *)
    val compare : t * t -> order

  (* return a hash code for a variable *)
    val hash : t -> word

  (* return the string representation of the variable's kind *)
    val kindToString : t -> string

    structure Map : ORD_MAP where type Key.ord_key = t
    structure Set : ORD_SET where type Key.ord_key = t
    structure Tbl : MONO_HASH_TABLE where type Key.hash_key = t

  end = struct

  (* AST variable kinds *)
    datatype kind
      = BasisVar		(* predefined basis variable (see basis-vars.sml) *)
      | ConstVar		(* 'const' variable *)
      | InputVar		(* 'input' variable *)
      | GlobalVar		(* variable defined at global scope *)
      | FunVar                  (* user-defined function *)
      | FunParam		(* parameter to function *)
      | StrandParam             (* parameter to strand definition *)
      | StrandStateVar          (* strand state variable *)
      | StrandOutputVar         (* strand output variable *)
      | LocalVar                (* local variable in method *)
      | IterVar                 (* iteration variable *)

    datatype t = V of {
        name : string,          (* print name of variable *)
        id : Stamp.stamp,       (* unique ID *)
	loc : Error.location,	(* the location where this variable is bound *)
        kind : kind,            (* variable kind *)
        ty : Types.scheme,      (* type scheme *)
        props : PropList.holder (* property list *)
      }

    fun nameOf (V{name, ...}) = name
    fun typeOf (V{ty, ...}) = ty
    fun monoTypeOf (V{ty=([], ty), ...}) = ty
      | monoTypeOf (V{name, ...}) = raise Fail(name ^ " is not monomorphic")
    fun kindOf (V{kind, ...}) = kind
    fun uniqueNameOf (V{name, id, ...}) = name ^ Stamp.toString id
    fun locationOf (V{loc, ...}) = loc

    fun newPoly (name, loc, kind, scheme) = let
	  val id = Stamp.new()
	  in
	    V{name=Atom.toString name, id=id, loc=loc, kind=kind, ty=scheme, props=PropList.newHolder()}
	  end

    fun newBasis (name, scheme) = newPoly (name, Error.UNKNOWN, BasisVar, scheme)

    fun new (name, loc, kind, ty) = newPoly (name, loc, kind, ([], ty))

    fun copy (V{name, kind, ty, ...}) =
	  V{name=name, id=Stamp.new(), loc=Error.UNKNOWN, kind=kind, ty=ty, props=PropList.newHolder()}

    fun isPrim (V{kind = BasisVar, ...}) = true
      | isPrim _ = false

  (* return true if the variable is a compile-time constant *)
    fun isConst (V{kind=ConstVar, ...}) = true
      | isConst _ = false

  (* return true if the variable is an output variable *)
    fun isOutput (V{kind=StrandOutputVar, ...}) = true
      | isOutput _ = false

    fun newProp initFn = PropList.newProp (fn (V{props, ...}) => props, initFn)
    fun newFlag () = PropList.newFlag (fn (V{props, ...}) => props)

    fun compare (V{id=a, ...}, V{id=b, ...}) = Stamp.compare(a, b)
    fun same (V{id=a, ...}, V{id=b, ...}) = Stamp.same(a, b)
    fun hash (V{id, ...}) = Stamp.hash id

    fun kindToString (V{kind, ...}) = (case kind
	  of BasisVar => "basis variable"
	   | ConstVar => "constant"
	   | InputVar => "input variable"
	   | GlobalVar => "global variable"
	   | FunVar => "function"
	   | FunParam => "function parameter"
	   | StrandParam => "strand parameter"
	   | StrandStateVar => "state variable"
	   | StrandOutputVar => "strand output variable"
	   | LocalVar => "local variable"
	   | IterVar => "iteration variable"
	  (* end case *))

    local
      structure V =
	struct
	  type ord_key = t
	  val compare = compare
	end
    in
    structure Map = RedBlackMapFn (V)
    structure Set = RedBlackSetFn (V)
    end

    structure Tbl = HashTableFn (
      struct
	type hash_key = t
	val hashVal = hash
	val sameKey = same
      end)

  end

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