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" >>= return . tracePrettyId >>= 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