summaryrefslogtreecommitdiff
path: root/main.hs
blob: c46e5da0c00201de8a56ca76ce9276a4db8bf0fd (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
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> [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