~jojo/Carth

ref: 0827c3a92bacaccc97ec9c84e8f02c1deffe7a2a Carth/app/Main.hs -rw-r--r-- 1.4 KiB
0827c3a9JoJo Prefix debug files (like out.ll) with ".dbg." to make invisible 1 year, 6 months ago
                                                                                
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
{-# LANGUAGE LambdaCase #-}

module Main (main) where

import Data.Functor

import Misc
import qualified TypeErr
import qualified Ast
import qualified DesugaredAst
import qualified MonoAst
import Check
import Config
import Compile
import Mono
import qualified Parse
import CompiletimeVars

main :: IO ()
main = uncurry compileFile =<< getConfig

compileFile :: FilePath -> CompileConfig -> IO ()
compileFile f cfg = do
    putStrLn ("   Compiling " ++ f ++ "")
    putStrLn ("     Compiletime variables:")
    putStrLn ("       lib directory = " ++ libDir)
    putStrLn ("       mod directory = " ++ modDir)
    putStrLn ("   Parsing")
    ast <- parse f
    putStrLn ("   Typechecking")
    ann <- typecheck' f ast
    putStrLn ("   Monomorphizing")
    mon <- monomorphize' ann
    compile f cfg mon
    putStrLn ""

parse :: FilePath -> IO Ast.Program
parse f = Parse.parse f >>= \case
    Left e -> putStrLn (formatParseErr e) >> abort f
    Right p -> writeFile ".dbg.out.parsed" (pretty p) $> p
  where
    formatParseErr e =
        let ss = lines e in (unlines ((head ss ++ " Error:") : tail ss))

typecheck' :: FilePath -> Ast.Program -> IO DesugaredAst.Program
typecheck' f p = case typecheck p of
    Left e -> TypeErr.printErr e >> abort f
    Right p -> writeFile ".dbg.out.checked" (show p) $> p

monomorphize' :: DesugaredAst.Program -> IO MonoAst.Program
monomorphize' p = do
    let p' = monomorphize p
    writeFile ".dbg.out.mono" (show p')
    pure p'