aboutsummaryrefslogtreecommitdiff
path: root/main/Main.hs
blob: 3216833bb2477efd7a6f73d63a68d976b1c01f0a (plain)
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
module Main where

import System.Exit

import qualified CC.Parser
import qualified CC.Typecheck

import CC.Pretty
import CC.Types


-- Put the passes in a type-level list to be able to run subsequences of
-- passes.


type Pass a b = Context -> a -> Either String b

pass :: (Read a, Show b, Pretty e) => (Context -> a -> Either e b) -> Pass a b
pass f ctx a = either (Left . pretty) Right (f ctx a)

passJoin :: (Read a, Show c) => Pass a b -> Pass b c -> Pass a c
passJoin f1 f2 ctx a = f1 ctx a >>= f2 ctx

main :: IO ()
main = do
    let parse = pass CC.Parser.runPass
        typecheck = pass CC.Typecheck.runPass

    let combined = parse `passJoin` typecheck

    source <- getContents
    let context = Context "<stdin>"
    case combined context (read source) of
        Right prog -> pprint prog
        Left err -> die err