aboutsummaryrefslogtreecommitdiff
path: root/Main.hs
blob: b8c50e67731f073f2dad5dd10795f9d9583761e3 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Main where

import System.Exit
import System.IO
import System.Process
import Debug.Trace

import BuildIR
import CodeGen
import Defs
import Optimiser
import Pretty
import ProgramParser
import TypeCheck
import Verify


infix 2 <?>
(<?>) :: (a -> Error b) -> String -> a -> Error b
f <?> pre = \a -> case f a of
    Left e -> Left $ pre ++ ": " ++ e
    Right x -> Right x


tracePrettyId :: Pretty a => a -> a
tracePrettyId x = trace (pretty x) x

eitherToIO :: Either String a -> IO a
eitherToIO = either die return

performCompile :: String -> IO ()
performCompile source = do
    let eres = return source
                 >>= parseProgram <?> "Parse error"
                 >>= typeCheck <?> "Type error"
                 >>= buildIR <?> "IR building error"
                 >>= optimise <?> "Error while optimising"
                 >>= return . traceShowId
                 >>= verify <?> "Verify error"
                 >>= return . tracePrettyId
                 >>= codegen <?> "Codegen error"

    asm <- eitherToIO eres
    -- hPutStr stderr asm

    writeFile "z_output.asm" asm

    hPutStrLn stderr "Assembling with yasm..."
    callCommand "yasm -w+all -fmacho64 z_output.asm -o z_output.o"

    hPutStrLn stderr "Linking with ld..."
    callCommand "ld z_output.o liblang.o -o z_output"

main :: IO ()
main = getContents >>= performCompile