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 3384 - (download) (annotate)
Mon Nov 9 20:57:32 2015 UTC (4 years ago) by jhr
File size: 3306 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 =
  struct

  (* AST variable kinds *)
    datatype var_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 *)

    datatype var = V of {
        name : string,          (* print name of variable *)
        id : Stamp.stamp,       (* unique ID *)
	loc : Error.location,	(* the location where this variable is bound *)
        kind : var_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 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()}

  (* return true if variable has global scope *)
    fun isGlobal (V{kind, ...}) = (case kind
	   of BasisVar => true
	    | ConstVar => true
	    | InputVar => true
	    | GlobalVar => true
	    | FunVar => true
	    | _ => false
	  (* end case *))

  (* return true if the variable is a compile-time constant *)
    fun isConst (V{kind=ConstVar, ...}) = true
      | isConst _ = 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"
	   | InputVar => "input variable"
	   | GlobalVar => "global variable"
	   | FunVar => "function"
	   | StrandParam => "strand parameter"
	   | StrandStateVar => "state variable"
	   | StrandOutputVar => "strand output variable"
	   | LocalVar => "variable"
	  (* end case *))

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

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

  end

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