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 3398 - (download) (annotate)
Wed Nov 11 01:17:58 2015 UTC (3 years, 10 months ago) by jhr
File size: 3165 byte(s)
working on merge
(* 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 *)

    val findStrand : t * Atom.atom -> AST.strand option
    val findFunc : t * Atom.atom -> fun_def
    val findVar : t * Atom.atom -> AST.var option

  (* add the strand to the environment *)
    val insertStrand : t * AST.strand -> 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 * StrandUtil.program_prop -> unit
    val properties : t -> StrandUtil.program_prop 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 : AST.strand 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 : StrandUtil.program_prop 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[]
	  }

(* QUESTION: how do we want to deal with the recursion in strands? *)
    fun findStrand (GE{strand, ...}, sname) = (case !strand
	   of NONE => NONE
	    | SOME(s as AST.Strand{name, ...}) => if Atom.same(name, sname)
		then SOME s
		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

    fun insertStrand (GE{strand, ...}, s) = (case !strand
	   of NONE => strand := SOME s
	    | _ => 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')

  (* tracking program features *)
    fun recordProp (GE{props, ...}, p) = if (StrandUtil.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