Better DNS slave handling
[hcoop/domtool2.git] / src / main.sml
CommitLineData
234b917a
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 *)
234b917a
AC
18
19(* Main interface *)
20
21structure Main :> MAIN = struct
22
492c1cff 23open Ast Print
234b917a 24
6ae327f8
AC
25structure SM = StringMap
26
234b917a
AC
27val dmy = ErrorMsg.dummyLoc
28
6ae327f8
AC
29val defaultT : record ref = ref SM.empty
30val defaultV : exp SM.map ref = ref SM.empty
31
32fun registerDefault (name, t, v) =
33 case SM.find (!defaultT, name) of
34 NONE => (defaultT := SM.insert (!defaultT, name, t);
35 defaultV := SM.insert (!defaultV, name, v))
36 | SOME _ => raise Fail "Duplicate default environment variable"
37
38fun tInit () = (TAction ((CRoot, dmy),
39 !defaultT,
40 StringMap.empty),
41 dmy)
d189ec0e
AC
42
43
234b917a 44
d189ec0e 45fun check' G fname =
a3698041 46 let
d189ec0e 47 (*val _ = print ("Check " ^ fname ^ "\n")*)
a3698041
AC
48 val prog = Parse.parse fname
49 in
50 if !ErrorMsg.anyErrors then
d189ec0e 51 G
a3698041 52 else
6ae327f8 53 Tycheck.checkFile G (tInit ()) prog
a3698041
AC
54 end
55
d189ec0e 56fun basis () =
234b917a 57 let
d189ec0e
AC
58 val dir = Posix.FileSys.opendir Config.libRoot
59
60 fun loop files =
61 case Posix.FileSys.readdir dir of
d612d62c
AC
62 NONE => (Posix.FileSys.closedir dir;
63 files)
d189ec0e
AC
64 | SOME fname =>
65 if String.isSuffix ".dtl" fname then
d612d62c
AC
66 loop (OS.Path.joinDirFile {dir = Config.libRoot,
67 file = fname}
d189ec0e
AC
68 :: files)
69 else
70 loop files
71
72 val files = loop []
73 val files = Order.order files
74 in
6ae327f8
AC
75 if !ErrorMsg.anyErrors then
76 Env.empty
77 else
78 foldl (fn (fname, G) => check' G fname) Env.empty files
d189ec0e
AC
79 end
80
81fun check fname =
82 let
83 val _ = ErrorMsg.reset ()
84
85 val b = basis ()
234b917a
AC
86 in
87 if !ErrorMsg.anyErrors then
d189ec0e 88 (b, NONE)
234b917a
AC
89 else
90 let
d189ec0e 91 val prog = Parse.parse fname
234b917a 92 in
492c1cff 93 if !ErrorMsg.anyErrors then
d189ec0e 94 (Env.empty, NONE)
492c1cff 95 else
d189ec0e 96 let
6ae327f8 97 val G' = Tycheck.checkFile b (tInit ()) prog
d189ec0e
AC
98 in
99 (G', #3 prog)
100 end
234b917a
AC
101 end
102 end
103
d189ec0e 104fun reduce fname =
a3698041 105 let
d189ec0e 106 val (G, body) = check fname
a3698041
AC
107 in
108 if !ErrorMsg.anyErrors then
d189ec0e 109 NONE
a3698041 110 else
d189ec0e
AC
111 case body of
112 SOME body =>
113 let
114 val body' = Reduce.reduceExp G body
115 in
116 (*printd (PD.hovBox (PD.PPS.Rel 0,
117 [PD.string "Result:",
118 PD.space 1,
119 p_exp body']))*)
120 SOME body'
121 end
122 | _ => NONE
a3698041
AC
123 end
124
d189ec0e
AC
125fun eval fname =
126 case reduce fname of
127 (SOME body') =>
128 if !ErrorMsg.anyErrors then
129 ()
130 else
6ae327f8 131 Eval.exec (!defaultV) body'
d189ec0e
AC
132 | NONE => ()
133
234b917a 134end