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

SCM Repository

[diderot] View of /branches/staging/src/compiler/env/env.sml
ViewVC logotype

View of /branches/staging/src/compiler/env/env.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2152 - (download) (annotate)
Sun Feb 17 19:39:37 2013 UTC (6 years, 5 months ago) by jhr
File size: 2166 byte(s)
 Updating stage with changes from vis12 branch (mostly functions, plus some bug fixes)
(* env.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure Env : sig

    type env

    val new : unit -> env

  (* 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 findFunc : env * Atom.atom -> fun_def

    val findVar : env * Atom.atom -> AST.var option

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

  (* insert variable bindings *)
    val insertGlobal : env * Atom.atom * AST.var -> env
    val insertLocal : env * Atom.atom * AST.var -> env

  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 global_env = GE of {
	fEnv : fun_def ATbl.hash_table,	(* functions, which may be overloaded *)
	vEnv : AST.var ATbl.hash_table		(* global variable bindings *)
      }

    datatype env = E of {
	g : global_env,
	vEnv : AST.var AMap.map
      }

    fun new () = E{
	    g = GE{
		fEnv = ATbl.mkTable(128, Fail "global function env"),
		vEnv = ATbl.mkTable(128, Fail "global variable env")
	      },
	    vEnv = AMap.empty
	  }

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

    fun findVar (E{g=GE{vEnv=gvEnv, ...}, vEnv}, x) = (case AMap.find(vEnv, x)
	   of NONE => ATbl.find gvEnv x
	    | someVar => someVar
	  (* end case *))

    fun insertFunc (env as E{g=GE{fEnv, ...}, ...}, f, f') = (
	  ATbl.insert fEnv (f, f');
	  env)

    fun insertGlobal (env as E{g=GE{vEnv, ...}, ...}, x, x') = (
	  ATbl.insert vEnv (x, x');
	  env)

    fun insertLocal (env as E{vEnv, g}, x, x') =
	  E{vEnv = AMap.insert(vEnv, x, x'), g = g}

  end

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