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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2152 - (view) (download)

1 : jhr 71 (* env.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 71 * All rights reserved.
5 :     *)
6 :    
7 :     structure Env : sig
8 :    
9 :     type env
10 :    
11 : jhr 78 val new : unit -> env
12 :    
13 : jhr 2152 (* functions are either user-defined or pre-defined *)
14 :     datatype fun_def
15 :     = PrimFun of AST.var list (* possibly overloaded builtin function *)
16 :     | UserFun of AST.var (* user-defined function *)
17 : jhr 169
18 : jhr 2152 val findFunc : env * Atom.atom -> fun_def
19 :    
20 : jhr 71 val findVar : env * Atom.atom -> AST.var option
21 :    
22 : jhr 169 (* insert a function binding *)
23 : jhr 2152 val insertFunc : env * Atom.atom * fun_def -> env
24 : jhr 169
25 :     (* insert variable bindings *)
26 : jhr 78 val insertGlobal : env * Atom.atom * AST.var -> env
27 :     val insertLocal : env * Atom.atom * AST.var -> env
28 :    
29 : jhr 71 end = struct
30 :    
31 :     structure ATbl = AtomTable
32 :     structure AMap = AtomMap
33 :    
34 : jhr 2152 (* functions are either user-defined or pre-defined *)
35 :     datatype fun_def
36 :     = PrimFun of AST.var list (* possibly overloaded builtin function *)
37 :     | UserFun of AST.var (* user-defined function *)
38 :    
39 : jhr 511 (* global environment holds global variables and strands *)
40 : jhr 71 datatype global_env = GE of {
41 : jhr 2152 fEnv : fun_def ATbl.hash_table, (* functions, which may be overloaded *)
42 : jhr 169 vEnv : AST.var ATbl.hash_table (* global variable bindings *)
43 : jhr 71 }
44 :    
45 :     datatype env = E of {
46 :     g : global_env,
47 :     vEnv : AST.var AMap.map
48 :     }
49 :    
50 : jhr 78 fun new () = E{
51 : jhr 169 g = GE{
52 :     fEnv = ATbl.mkTable(128, Fail "global function env"),
53 :     vEnv = ATbl.mkTable(128, Fail "global variable env")
54 :     },
55 : jhr 78 vEnv = AMap.empty
56 :     }
57 :    
58 : jhr 1116 fun findFunc (E{g=GE{fEnv, ...}, ...}, x) = (case ATbl.find fEnv x
59 : jhr 2152 of NONE => PrimFun[]
60 :     | SOME fdef => fdef
61 : jhr 1116 (* end case *))
62 : jhr 169
63 : jhr 71 fun findVar (E{g=GE{vEnv=gvEnv, ...}, vEnv}, x) = (case AMap.find(vEnv, x)
64 :     of NONE => ATbl.find gvEnv x
65 :     | someVar => someVar
66 :     (* end case *))
67 :    
68 : jhr 169 fun insertFunc (env as E{g=GE{fEnv, ...}, ...}, f, f') = (
69 :     ATbl.insert fEnv (f, f');
70 :     env)
71 :    
72 : jhr 78 fun insertGlobal (env as E{g=GE{vEnv, ...}, ...}, x, x') = (
73 :     ATbl.insert vEnv (x, x');
74 :     env)
75 : jhr 71
76 : jhr 72 fun insertLocal (env as E{vEnv, g}, x, x') =
77 : jhr 75 E{vEnv = AMap.insert(vEnv, x, x'), g = g}
78 : jhr 72
79 : jhr 71 end

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