-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMain.hs
72 lines (63 loc) · 2.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
module Main where
import Microc hiding ( Parser )
import Options.Applicative
import LLVM.Pretty
import Data.String.Conversions
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Text.Pretty.Simple
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Render.Text
data Action = Ast | Sast | LLVM | Compile FilePath | Run
data ParserType = Combinator | Generator
data Options = Options { action :: Action, infile :: FilePath, parser :: ParserType }
actionP :: Parser Action
actionP = flag' Ast (long "ast" <> short 'a' <> help "Pretty print the ast")
<|> flag' Sast (long "sast" <> short 's' <> help "Pretty print the sast")
<|> flag' LLVM (long "llvm" <> short 'l' <> help "Pretty print the generated llvm")
<|> flag' Compile (long "compile" <> short 'c' <> help "Compile to an executable")
<*> strOption (short 'o' <> value "a.out" <> metavar "FILE")
-- running the file to see the expected output is default
<|> pure Run
parserP :: Parser ParserType
parserP =
flag'
Combinator
(long "combinator" <> help "Use the megaparsec parser implementation (default).")
<|> flag'
Generator
(long "generator" <> short 'g' <> help "Use alex and happy to parse.")
<|> pure Combinator -- default to megaparsec
optionsP :: Parser Options
optionsP =
Options
<$> actionP
<*> strArgument (help "Source file" <> metavar "FILE")
<*> parserP
main :: IO ()
main = runOpts =<< execParser (optionsP `withInfo` infoString)
where
withInfo opts desc = info (helper <*> opts) $ progDesc desc
infoString
= "Run the mcc compiler on the given file. \
\Passing no flags will compile the file, execute it, and print the output."
runOpts :: Options -> IO ()
runOpts (Options action infile ptype) = do
program <- T.readFile infile
let parseTree = case ptype of
Combinator -> runParser programP infile program
Generator -> Right $ parse . alexScanTokens $ T.unpack program
case parseTree of
Left err -> putStrLn $ errorBundlePretty err
Right ast -> case action of
Ast -> putDoc $ pretty ast <> "\n"
_ -> case checkProgram ast of
Left err -> putDoc $ pretty err <> "\n"
Right sast ->
let llvm = codegenProgram sast
in case action of
Sast -> pPrint sast
LLVM -> T.putStrLn . cs . ppllvm $ llvm
Compile outfile -> compile llvm outfile
Run -> run llvm >>= T.putStr
Ast -> error "unreachable"