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