-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
67 lines (62 loc) · 2.23 KB
/
Main.hs
File metadata and controls
67 lines (62 loc) · 2.23 KB
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
{-|
Module : Main
Description : Entry-point for the emperor compiler
Copyright : (c) Edward Jones, 2019
License : GPL-3
Maintainer : Edward Jones
Stability : experimental
Portability : POSIX
Language : Haskell2010
This is the entry-point for the compiler of the @emperor@ language.
A code-formatter may be invoked from the command-line.
For details on how this is done, see @man emperor@ or @emperor.json@ in the GitHub repository.
-}
module Main
( main
) where
import Args (Args, doFormat, entryPoint, input, outputFile, parseArgv, version)
import Control.Monad (when)
import Formatter.Formatter (formatFresh)
import Logger.Logger (Loggers, makeLoggers)
import Parser.EmperorParserWrapper (AST, parse)
import System.Exit (exitFailure, exitSuccess)
import Types.Types (TypeCheckResult(..), resolveTypes, writeHeader)
-- | Provides the entry-point
main :: IO ()
main = do
args <- parseArgv
(err, inf, scc, wrn) <- makeLoggers args
when (version args) (putStrLn "emperor v1.0.0" >>= const exitSuccess)
if input args == ""
then inf "No input files detected, reading from stdin"
else inf $ "Using input file " ++ input args
let sanitisedArguments =
if input args == ""
then args {input = "-"}
else args
parseResult <- parse (input sanitisedArguments)
case parseResult of
Left msg -> err msg
Right prog -> do
scc "Parsing done for input file"
when (doFormat args) (output args (formatFresh prog) >>= const exitSuccess)
typeCheck args (err, inf, scc, wrn) prog
when
(not (entryPoint args) && outputFile args /= "-")
(do inf "Outputting header..."
writeHeader (outputFile args ++ ".eh.json.gz") prog)
typeCheck :: Args -> Loggers -> AST -> IO ()
typeCheck _ (err, inf, scc, wrn) prog = do
inf "Checking types"
typeResult <- resolveTypes (err, inf, scc, wrn) prog
case typeResult of
Fail x -> do
err x
exitFailure
Pass -> scc "Type-checking passed"
output :: Args -> String -> IO ()
output args c = do
let path = outputFile args
if path == "-"
then putStrLn c
else writeFile path c