This repository was archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
116 lines (104 loc) · 3.66 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Main(main) where
import Control.Exception
import Data.Char
import System.Directory
import System.FilePath as FP
import System.IO
import qualified Data.ByteString.Lazy as LBS
import DataConstructors
import E.Main
import E.Program
import E.Rules
import E.Type
import FrontEnd.Class
import Grin.Main(compileToGrin)
import Grin.Show(render)
import Ho.Build
import Ho.Collected
import Ho.Library
import Name.Names
import Options
import StringTable.Atom
import Support.TempDir
import Util.Gen
import Util.SetLike as S
import Util.Std
import Version.Version(versionSimple)
import qualified FlagDump as FD
import qualified Interactive
main = wrapMain $ do
hSetEncoding stdout utf8
hSetEncoding stderr utf8
o <- processOptions
when (dump FD.Atom) $ do
addAtExit dumpStringTableStats
addAtExit dumpToFile
-- set temporary directory
maybeDo $ do x <- optWorkDir o; return $ setTempDir x
let darg = progressM $ do
(argstring,_) <- getArgString
return (argstring ++ "\n" ++ versionSimple)
case optMode o of
BuildHl hl -> darg >> buildLibrary processInitialHo processDecls hl
ListLibraries -> listLibraries
ShowHo ho -> dumpHoFile ho
PurgeCache -> purgeCache
Preprocess -> forM_ (optArgs o) $ \fn -> do
lbs <- LBS.readFile fn
res <- preprocessHs options fn lbs
LBS.putStr res
_ -> darg >> processFiles (optArgs o)
-- we are very careful to only delete cache files.
purgeCache = do
Just hc <- findHoCache
ds <- getDirectoryContents hc
let cacheFile fn = case map toLower (reverse fn) of
'o':'h':'.':fs -> length fs == 26 && all isAlphaNum fs
_ -> False
forM_ ds $ \fn -> when (cacheFile fn) (removeFile (hc </> fn))
processFiles :: [String] -> IO ()
processFiles cs = f cs (optMainFunc options) where
f [] Nothing = do
int <- Interactive.isInteractive
when (not int) $ putErrDie "jhc: no input files"
g [Left mod_Prelude]
f [] (Just (b,m)) = do
m <- getModule (parseName Val m)
g [Left m]
f cs _ = g (map fileOrModule cs)
g fs = processCollectedHo . snd =<< parseFiles options [outputName] []
fs processInitialHo processDecls
fileOrModule f = case reverse f of
('s':'h':'.':_) -> Right f
('s':'h':'l':'.':_) -> Right f
('c':'s':'h':'.':_) -> Right f
_ -> Left $ toModule f
processCollectedHo cho = do
if optStop options == CompileHo then return () else do
putProgressLn "Collected Compilation..."
when (dump FD.ClassSummary) $ do
putStrLn " ---- class summary ---- "
printClassSummary (choClassHierarchy cho)
when (dump FD.Class) $ do
putStrLn " ---- class hierarchy ---- "
printClassHierarchy (choClassHierarchy cho)
let dataTable = choDataTable cho
combinators = values $ choCombinators cho
evaluate dataTable
evaluate combinators
let prog = programUpdate program {
progCombinators = combinators,
progDataTable = dataTable
}
-- dump final version of various requested things
wdump FD.Datatable $ putErrLn (render $ showDataTable dataTable)
wdump FD.DatatableBuiltin $
putErrLn (render $ showDataTable samplePrimitiveDataTable)
dumpRules (Rules $ fromList
[(combIdent x,combRules x) | x <- combinators, not $ null (combRules x)])
-- enter interactive mode
int <- Interactive.isInteractive
if int then Interactive.interact cho else do
prog <- compileWholeProgram prog
compileToGrin prog
progressM c = wdump FD.Progress $ (c >>= putErrLn) >> hFlush stderr