-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.hs
48 lines (40 loc) · 1.34 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
module Main where
import Data.List (intersect)
import qualified Data.Text.Lazy.IO as TLIO
import System.IO
import System.Environment
import LLVM.Pretty (ppllvm)
import Parser (parseCode)
import Syntax (joinedPrettyAST)
import AST.Processor (processAST)
import AST.Utils (ppAST)
import AST.Errors (showE)
import Codegen.Builder (buildIR)
import StringUtils
debugFlag = ["--debug", "-d"]
emitFlag = ["--emit", "-e"]
parseArgs :: [String] -> ([String], String) -- [flags], filename
parseArgs (reverse -> (filename:args)) = (args, filename) -- TODO: parse incoming arguments
main = do
args <- getArgs
case args of
[] -> putStrLn "Provide file name!"
args -> do
code <- readFile filename
case parseCode code of
Left err -> print err
Right ast -> do
actionFor debugFlag (ppAST ast >> putStrLn "")
let maybeTAST = processAST ast
case maybeTAST of
Right errors -> putStrLn $ joinN (map showE errors)
Left tast -> do
actionFor debugFlag (ppAST tast >> putStrLn "")
let ir = buildIR tast
actionFor emitFlag (TLIO.putStrLn $ ppllvm ir)
return ()
where
(flags, filename) = parseArgs args
actionFor key action = if not (null (key `intersect` flags))
then action
else pure ()