summaryrefslogtreecommitdiff
path: root/main.hs
blob: 9c33f837033fe029584c91f3b5510b20754fab34 (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
module Main where

import Control.Monad
import Data.Char
import System.Environment
import System.Exit
import System.Process

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>"
    let fname = head args

    prog <- readFile fname >>= either die return . parseProgram

    -- putStrLn $ astSuccinct prog
    -- print prog
    let opt = optimise prog
    writeFile (fname ++ ".succinct") $ astSuccinct opt
    writeFile (fname ++ ".ast") $ show opt

    case executionMode of
        EMInterpret -> do
            input <- getContents
            interpret opt (map (fromIntegral . ord) input) >>= (putStr . map (chr . fromIntegral))
        EMCompile -> do
            writeFile (fname ++ ".asm") $ compile opt
            callProcess "yasm" ["-f", "macho64", fname ++ ".asm", "-o", fname ++ ".o"]
            callProcess "gcc" [fname ++ ".o", "-o", fname ++ ".exe"]