a11c0ff3 |
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. |
ae3a5b8c |
17 | *) |
a11c0ff3 |
18 | |
19 | (* Execution of Domtool programs reduced to primitive actions *) |
20 | |
21 | structure Eval :> EVAL = struct |
22 | |
23 | open Ast |
24 | |
25 | structure SM = StringMap |
26 | |
27 | fun lookup (evs, ev) = |
28 | case SM.find (evs, ev) of |
29 | NONE => raise Fail ("Couldn't find an environment variable " |
30 | ^ ev ^ " that type-checking has guaranteed") |
31 | | SOME v => v |
32 | |
d68ab27c |
33 | fun printEvs (name, evs) = |
34 | (print ("Environment " ^ name ^ "\n"); |
35 | SM.appi (fn (name, i) => Print.preface (name, Print.p_exp i)) evs; |
36 | print "\n") |
37 | |
a11c0ff3 |
38 | val conjoin : Env.env_vars * Env.env_vars -> Env.env_vars = |
39 | SM.unionWith #2 |
40 | |
41 | fun findPrimitive e = |
42 | let |
43 | fun findPrim (e, _) = |
44 | case e of |
45 | EVar name => (name, []) |
46 | | EApp (e1, e2) => |
47 | let |
48 | val (name, args) = findPrim e1 |
49 | in |
50 | (name, e2 :: args) |
51 | end |
52 | | _ => raise Fail "Non-primitive action left after reduction" |
53 | |
54 | val (name, args) = findPrim e |
55 | in |
56 | (name, rev args) |
57 | end |
58 | |
59 | fun exec evs e = |
60 | let |
61 | fun exec' evs (eAll as (e, _)) = |
62 | case e of |
63 | ESkip => SM.empty |
64 | | ESet (ev, e) => SM.insert (SM.empty, ev, e) |
65 | | EGet (x, ev, e) => exec' evs (Reduce.subst x (lookup (evs, ev)) e) |
66 | | ESeq es => |
67 | let |
68 | val (new, _) = |
69 | foldl (fn (e, (new, keep)) => |
70 | let |
71 | val new' = exec' keep e |
72 | in |
73 | (conjoin (new, new'), |
74 | conjoin (keep, new')) |
75 | end) (SM.empty, evs) es |
76 | in |
77 | new |
78 | end |
79 | | ELocal (e1, e2) => |
80 | let |
81 | val evs' = exec' evs e1 |
82 | val evs'' = exec' (conjoin (evs, evs')) e2 |
83 | in |
84 | conjoin (evs, evs'') |
85 | end |
86 | | EWith (e1, e2) => |
87 | let |
88 | val (prim, args) = findPrimitive e1 |
89 | in |
90 | case Env.container prim of |
91 | NONE => raise Fail "Unbound primitive container" |
92 | | SOME (action, cleanup) => |
93 | let |
94 | val evs' = action (evs, args) |
95 | val evs'' = exec' evs e2 |
96 | in |
97 | cleanup (); |
d68ab27c |
98 | evs' |
a11c0ff3 |
99 | end |
100 | end |
101 | |
102 | | _ => |
103 | let |
104 | val (prim, args) = findPrimitive eAll |
105 | in |
106 | case Env.action prim of |
107 | NONE => raise Fail "Unbound primitive action" |
108 | | SOME action => action (evs, args) |
109 | end |
110 | |
668e333e |
111 | val _ = Env.pre () |
a11c0ff3 |
112 | val evs' = exec' evs e |
113 | in |
668e333e |
114 | Env.post () |
a11c0ff3 |
115 | end |
116 | |
117 | end |