Add if..then..else
[hcoop/domtool2.git] / src / eval.sml
CommitLineData
a3698041
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 *)
a3698041
AC
18
19(* Execution of Domtool programs reduced to primitive actions *)
20
21structure Eval :> EVAL = struct
22
23open Ast
24
25structure SM = StringMap
26
27fun 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
8a7c40fa
AC
33fun 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
a3698041
AC
38val conjoin : Env.env_vars * Env.env_vars -> Env.env_vars =
39 SM.unionWith #2
40
41fun 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
1824f573
AC
59fun exec' evs (eAll as (e, _)) =
60 case e of
61 ESkip => SM.empty
62 | ESet (ev, e) => SM.insert (SM.empty, ev, e)
8cbb9632 63 | EGet (x, _, ev, e) => exec' evs (Reduce.subst x (lookup (evs, ev)) e)
1824f573
AC
64 | ESeq es =>
65 let
66 val (new, _) =
67 foldl (fn (e, (new, keep)) =>
68 let
69 val new' = exec' keep e
70 in
71 (conjoin (new, new'),
72 conjoin (keep, new'))
73 end) (SM.empty, evs) es
74 in
75 new
76 end
77 | ELocal (e1, e2) =>
78 let
79 val evs' = exec' evs e1
1824f573 80 in
e47271a1 81 exec' (conjoin (evs, evs')) e2
1824f573
AC
82 end
83 | EWith (e1, e2) =>
84 let
85 val (prim, args) = findPrimitive e1
86 in
87 case Env.container prim of
88 NONE => raise Fail "Unbound primitive container"
89 | SOME (action, cleanup) =>
a3698041 90 let
1824f573
AC
91 val evs' = action (evs, args)
92 val evs'' = exec' evs e2
a3698041 93 in
1824f573
AC
94 cleanup ();
95 evs'
a3698041 96 end
1824f573 97 end
a3698041 98
1824f573
AC
99 | _ =>
100 let
101 val (prim, args) = findPrimitive eAll
102 in
103 case Env.action prim of
104 NONE => raise Fail "Unbound primitive action"
b0963032 105 | SOME action => action (evs, List.map (Reduce.reduceExp Env.empty) args)
1824f573 106 end
a3698041 107
1824f573
AC
108fun exec evs e =
109 let
e0b0abd2 110 val _ = Env.pre ()
a3698041
AC
111 val evs' = exec' evs e
112 in
e0b0abd2 113 Env.post ()
a3698041
AC
114 end
115
116end