From d015d797fe8d152864cdd5f1ce284bd5ff467f9e Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 13 Jul 2017 23:24:04 +0200 Subject: Initial --- main.hs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 main.hs (limited to 'main.hs') diff --git a/main.hs b/main.hs new file mode 100644 index 0000000..c46e5da --- /dev/null +++ b/main.hs @@ -0,0 +1,46 @@ +module Main where + +import Control.DeepSeq +import Control.Monad +import Data.Char +import System.Environment +import System.Exit + +import AST +import Compiler +import Interpreter +import Parser +import Optimiser + + +data ExecutionMode = EMInterpret | EMCompile + +executionMode :: ExecutionMode +executionMode = EMCompile + + +main :: IO () +main = do + args <- getArgs + when (length args == 0 || length args > 2) + $ die "Usage: bfcomphs [source.bf.asm]" + let fname = head args + destfname = if length args == 2 then args !! 1 else fname ++ ".asm" + + prog <- readFile fname >>= either die return . parseProgram + + -- putStrLn $ astSuccinct prog + -- print prog + let opt = optimise prog + showopt = force $ show opt + when False $ putStrLn $ astSuccinct opt + when False $ putStrLn showopt + + case executionMode of + EMInterpret -> do + -- input <- getContents + -- putStr $ map (chr . fromIntegral) $ interpret opt (map (fromIntegral . ord) input) + input <- getContents + interpret opt (map (fromIntegral . ord) input) >>= (putStr . map (chr . fromIntegral)) + EMCompile -> do + writeFile destfname $ compile opt -- cgit v1.2.3-54-g00ecf