aboutsummaryrefslogtreecommitdiff
path: root/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Main.hs')
-rw-r--r--Main.hs56
1 files changed, 56 insertions, 0 deletions
diff --git a/Main.hs b/Main.hs
new file mode 100644
index 0000000..f1307c7
--- /dev/null
+++ b/Main.hs
@@ -0,0 +1,56 @@
+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
+
+
+main :: IO ()
+main = do
+ source <- getContents
+
+ let eres = return source
+ >>= parseProgram <?> "Parse error"
+ -- >>= return . traceShowId
+ >>= typeCheck <?> "Type error"
+ >>= buildIR <?> "IR building error"
+ -- >>= return . tracePrettyId
+ >>= optimise <?> "Error while optimising"
+ >>= 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 -o z_output"