-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMain.hs
51 lines (46 loc) · 1.65 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
module Main where
import Data.Char (toLower)
import System.Environment
import System.IO
import Syntax
import qualified Lexer as L
import qualified Parser as P
import qualified Static as S
import qualified Type as T
import Compiler (programToExpr, expandCon, skiCompile)
import PatComp (compilePatternMatch)
import Optimizer (optimizeExpr)
import Builtin (expandBltin)
import PPrint (showProgram)
compile s = (prog, as ++ a, expr2, ski2, ce')
where (prog, is, ce', as) = S.analyze ce topdecls
topdecls = P.Decl (P.VarDecl ("@main", [], P.Rhs (P.Var "main") []))
: (P.parse (L.lexer "argf" s))
as' = as ++ T.preludeAssumptions
(a, prog') = T.tiProgram ce' as' prog
prog2 = ([],[is]):prog'
prog3 = compilePatternMatch prog2
expr1 = expandCon $ programToExpr prog3
expr2 = optimizeExpr expr1
ski1 = skiCompile expr2
ski2 = expandBltin ski1
Just ce = T.addCoreClasses T.initialEnv
main :: IO ()
main = do source <- argf
let (p, as, p', e, ce) = compile source in
do --hPutStrLn stderr (show p')
--mapM_ (hPutStrLn stderr . show) as
putStrLn $ insertNewline 80 $ map toLower $ show e
insertNewline :: Int -> String -> String
insertNewline n [] = []
insertNewline n s = let (line, s') = splitAt n s
in (line ++ '\n' : insertNewline n s')
argf :: IO String
argf = do argv <- getArgs
if argv == []
then getContents
else do conts <- mapM getFileContents argv
return (concat conts)
getFileContents :: String -> IO String
getFileContents fname = do handle <- openFile fname ReadMode
hGetContents handle