Added concept of multiple nodes
[hcoop/domtool2.git] / src / env.sml
CommitLineData
492c1cff
AC
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
dac62e84 17 *)
492c1cff
AC
18
19(* Domtool type-checking and reduction environments *)
20
21structure Env :> ENV = struct
22
23open Ast
24
25structure SS = StringSet
26structure SM = StringMap
27
a3698041
AC
28type typeRule = exp -> bool
29val typeRules : typeRule SM.map ref = ref SM.empty
6be996d4
AC
30fun registerType (name, rule) = typeRules := SM.insert (!typeRules, name, rule)
31fun typeRule name = SM.find (!typeRules, name)
32
a3698041
AC
33type env_vars = exp SM.map
34type action = env_vars * Ast.exp list -> env_vars
35val actions : action SM.map ref = ref SM.empty
36fun registerAction (name, action) = actions := SM.insert (!actions, name, action)
37fun action name = SM.find (!actions, name)
38
39val containers : (action * (unit -> unit)) SM.map ref = ref SM.empty
40fun registerContainer (name, befor, after) =
41 containers := SM.insert (!containers, name, (befor, after))
42fun container name = SM.find (!containers, name)
43
e0b0abd2
AC
44local
45 val pr = ref (fn () => ())
46in
47
48fun registerPre f =
49 let
50 val old = !pr
51 in
52 pr := (fn () => (old (); f ()))
53 end
54fun pre () = !pr ()
55
56end
57
58local
59 val pst = ref (fn () => ())
60in
61
62fun registerPost f =
63 let
64 val old = !pst
65 in
66 pst := (fn () => (old (); f ()))
67 end
68fun post () = !pst ()
69
70end
71
629a34f6
AC
72fun badArgs (name, args) =
73 (print ("Invalid arguments to " ^ name ^ "\n");
74 app (fn arg => Print.preface ("Argument: ", Print.p_exp arg)) args;
75 raise Domain)
76fun badArg (func, arg, v) =
77 (print ("Invalid " ^ arg ^ " argument to " ^ func ^ "\n");
78 Print.preface ("Argument: ", Print.p_exp v);
79 raise Domain)
80
81type 'a arg = exp -> 'a option
82
83fun int (EInt n, _) = SOME n
84 | int _ = NONE
85
86fun string (EString s, _) = SOME s
87 | string _ = NONE
88
89fun mapFail f [] = SOME []
90 | mapFail f (h :: t) =
91 case f h of
92 NONE => NONE
93 | SOME h' =>
94 case mapFail f t of
95 NONE => NONE
96 | SOME t' => SOME (h' :: t')
97
98fun list f (EList ls, _) = mapFail f ls
99 | list _ _ = NONE
100
ed9fda3a
AC
101fun none func f (_, []) = (f ();
102 SM.empty)
103 | none func _ (_, es) = badArgs (func, es)
104
629a34f6
AC
105fun one func (name, arg) f (_, [e]) =
106 (case arg e of
107 NONE => badArg (func, name, e)
108 | SOME v => (f v;
109 SM.empty))
110 | one func _ _ (_, es) = badArgs (func, es)
111
112fun two func (name1, arg1, name2, arg2) f (_, [e1, e2]) =
113 (case (arg1 e1, arg2 e2) of
114 (NONE, _) => badArg (func, name1, e1)
115 | (_, NONE) => badArg (func, name2, e2)
116 | (SOME v1, SOME v2) => (f (v1, v2);
117 SM.empty))
118 | two func _ _ (_, es) = badArgs (func, es)
119
6ae327f8
AC
120
121fun oneV func (name, arg) f (evs, [e]) =
122 (case arg e of
123 NONE => badArg (func, name, e)
124 | SOME v => (f (evs, v);
125 SM.empty))
126 | oneV func _ _ (_, es) = badArgs (func, es)
127
e0b0abd2
AC
128fun twoV func (name1, arg1, name2, arg2) f (evs, [e1, e2]) =
129 (case (arg1 e1, arg2 e2) of
130 (NONE, _) => badArg (func, name1, e1)
131 | (_, NONE) => badArg (func, name2, e2)
132 | (SOME v1, SOME v2) => (f (evs, v1, v2);
133 SM.empty))
134 | twoV func _ _ (_, es) = badArgs (func, es)
135
6ae327f8
AC
136
137fun env arg (evs, name) =
138 case SM.find (evs, name) of
139 NONE => raise Fail ("Unavailable environment variable " ^ name)
140 | SOME e =>
141 case arg e of
142 NONE => raise Fail ("Bad format for environment variable " ^ name)
143 | SOME v => v
144
629a34f6
AC
145fun type_one func arg f =
146 registerType (func, fn e =>
147 case arg e of
148 NONE => false
149 | SOME v => f v)
150
ed9fda3a 151fun action_none name f = registerAction (name, none name f)
629a34f6
AC
152fun action_one name args f = registerAction (name, one name args f)
153fun action_two name args f = registerAction (name, two name args f)
154
e0b0abd2 155fun actionV_none name f = registerAction (name, fn (env, _) => (f env; env))
6ae327f8 156fun actionV_one name args f = registerAction (name, oneV name args f)
e0b0abd2 157fun actionV_two name args f = registerAction (name, twoV name args f)
6ae327f8
AC
158
159fun container_none name (f, g) = registerContainer (name, none name f, g)
629a34f6 160fun container_one name args (f, g) = registerContainer (name, one name args f, g)
a3698041 161
6ae327f8
AC
162fun containerV_one name args (f, g) = registerContainer (name, oneV name args f, g)
163
095de39e
AC
164type env = SS.set * (typ * exp option) SM.map * SS.set
165val empty : env = (SS.empty, SM.empty, SS.empty)
492c1cff 166
095de39e
AC
167fun lookupType (ts, _, _) name = SS.member (ts, name)
168fun lookupVal (_, vs, _) name =
492c1cff
AC
169 case SM.find (vs, name) of
170 NONE => NONE
171 | SOME (t, _) => SOME t
095de39e 172fun lookupEquation (_, vs, _) name =
492c1cff
AC
173 case SM.find (vs, name) of
174 NONE => NONE
175 | SOME (_, eqo) => eqo
095de39e 176fun lookupContext (_, _, cs) name = SS.member (cs, name)
492c1cff 177
095de39e
AC
178fun bindType (ts, vs, cs) name = (SS.add (ts, name), vs, cs)
179fun bindVal (ts, vs, cs) (name, t, eqo) = (ts, SM.insert (vs, name, (t, eqo)), cs)
180fun bindContext (ts, vs, cs) name = (ts, vs, SS.add (cs, name))
181
182fun types (ts, _, _) = ts
183fun vals (_, vs, _) = SM.foldli (fn (name, _, vs) => SS.add (vs, name)) SS.empty vs
184fun contexts (_, _, cs) = cs
492c1cff
AC
185
186end