Evaluating a test with automatic inclusion of basis
[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
AC
24
25val dmy = ErrorMsg.dummyLoc
26
27val tInit = (TAction ((CRoot, dmy),
28 StringMap.empty,
29 StringMap.empty),
30 dmy)
d189ec0e
AC
31
32
234b917a 33
d189ec0e 34fun check' G fname =
a3698041 35 let
d189ec0e 36 (*val _ = print ("Check " ^ fname ^ "\n")*)
a3698041
AC
37 val prog = Parse.parse fname
38 in
39 if !ErrorMsg.anyErrors then
d189ec0e 40 G
a3698041 41 else
d189ec0e 42 Tycheck.checkFile G tInit prog
a3698041
AC
43 end
44
d189ec0e 45fun basis () =
234b917a 46 let
d189ec0e
AC
47 val dir = Posix.FileSys.opendir Config.libRoot
48
49 fun loop files =
50 case Posix.FileSys.readdir dir of
51 NONE => files
52 | SOME fname =>
53 if String.isSuffix ".dtl" fname then
54 loop (String.concatWith "/" [Config.libRoot, fname]
55 :: files)
56 else
57 loop files
58
59 val files = loop []
60 val files = Order.order files
61 in
62 foldl (fn (fname, G) => check' G fname) Env.empty files
63 end
64
65fun check fname =
66 let
67 val _ = ErrorMsg.reset ()
68
69 val b = basis ()
234b917a
AC
70 in
71 if !ErrorMsg.anyErrors then
d189ec0e 72 (b, NONE)
234b917a
AC
73 else
74 let
d189ec0e 75 val prog = Parse.parse fname
234b917a 76 in
492c1cff 77 if !ErrorMsg.anyErrors then
d189ec0e 78 (Env.empty, NONE)
492c1cff 79 else
d189ec0e
AC
80 let
81 val G' = Tycheck.checkFile b tInit prog
82 in
83 (G', #3 prog)
84 end
234b917a
AC
85 end
86 end
87
d189ec0e 88fun reduce fname =
a3698041 89 let
d189ec0e 90 val (G, body) = check fname
a3698041
AC
91 in
92 if !ErrorMsg.anyErrors then
d189ec0e 93 NONE
a3698041 94 else
d189ec0e
AC
95 case body of
96 SOME body =>
97 let
98 val body' = Reduce.reduceExp G body
99 in
100 (*printd (PD.hovBox (PD.PPS.Rel 0,
101 [PD.string "Result:",
102 PD.space 1,
103 p_exp body']))*)
104 SOME body'
105 end
106 | _ => NONE
a3698041
AC
107 end
108
d189ec0e
AC
109fun eval fname =
110 case reduce fname of
111 (SOME body') =>
112 if !ErrorMsg.anyErrors then
113 ()
114 else
115 Eval.exec StringMap.empty body'
116 | NONE => ()
117
234b917a 118end