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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/global-env/global-env.sml
ViewVC logotype

View of /branches/vis15/src/compiler/global-env/global-env.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3431 - (download) (annotate)
Sat Nov 14 14:03:58 2015 UTC (3 years, 11 months ago) by jhr
File size: 3440 byte(s)
  Working on merge; some bug fixing and code cleanup
(* global-env.sml
 *
 * The global environment is an imperative environment that tracks global variables,
 * program properties, and the name of the strand.
 *
 * 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 GlobalEnv : sig

    type t

  (* create a new (empty) global environment *)
    val new : unit -> t

  (* functions are either user-defined or pre-defined *)
    datatype fun_def
      = PrimFun of AST.var list                 (* possibly overloaded builtin function *)
      | UserFun of AST.var                      (* user-defined function *)

  (* lookup a strand *)
    val findStrand : t * Atom.atom -> StrandEnv.t option

  (* lookup a function definition *)
    val findFunc : t * Atom.atom -> fun_def

  (* lookup a global variable *)
    val findVar : t * Atom.atom -> AST.var option

  (* add the strand to the environment; this function returns the strand
   * environment that we use to track strand state variables.
   *)
    val insertStrand : t *  StrandEnv.t -> unit

  (* insert a function binding *)
    val insertFunc : t * Atom.atom * fun_def -> unit

  (* insert a global variable binding *)
    val insertVar : t * Atom.atom * AST.var -> unit

  (* tracking program features *)
    val recordProp : t * Properties.t -> unit
    val properties : t -> Properties.t list

  end = struct

    structure ATbl = AtomTable
    structure AMap = AtomMap

  (* functions are either user-defined or pre-defined *)
    datatype fun_def
      = PrimFun of AST.var list                 (* possibly overloaded builtin function *)
      | UserFun of AST.var                      (* user-defined function *)

  (* global environment holds global variables and strands *)
    datatype t = GE of {
        strand : StrandEnv.t option ref,	(* the strand *)
        fEnv : fun_def ATbl.hash_table,         (* functions, which may be overloaded *)
        vEnv : AST.var ATbl.hash_table,         (* global variable bindings *)
        props : Properties.t list ref		(* record program properties *)
      }

    fun new () = GE{
	    strand = ref NONE,
	    fEnv = ATbl.mkTable(128, Fail "global function env"),
	    vEnv = ATbl.mkTable(128, Fail "global variable env"),
	    props = ref[]
	  }

  (* add the strand to the environment; this function returns the strand
   * environment that we use to track strand state variables.
   *)
    fun insertStrand (GE{strand, ...}, sEnv) = (case !strand
	   of NONE => strand := SOME sEnv
	    | _ => raise Fail "impossible: redefinition of strand"
	  (* end case *))

    fun insertFunc (GE{fEnv, ...}, f, f') = ATbl.insert fEnv (f, f')

    fun insertVar (GE{vEnv, ...}, x, x') = ATbl.insert vEnv (x, x')

  (* lookup a strand *)
    fun findStrand (GE{strand, ...}, name) = (case !strand
	   of NONE => NONE
	    | someEnv as SOME sEnv => if Atom.same(name, StrandEnv.strandName sEnv)
		then someEnv
		else NONE
	  (* end case *))

    fun findFunc (GE{fEnv, ...}, x) = (case ATbl.find fEnv x
           of NONE => PrimFun[]
            | SOME fdef => fdef
          (* end case *))

    fun findVar (GE{vEnv, ...}, x) = ATbl.find vEnv x

  (* tracking program features *)
    fun recordProp (GE{props, ...}, p) = if (Properties.hasProp p (!props))
          then ()
          else props := p :: !props

    fun properties (GE{props, ...}) = !props

  end

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