-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.hs
69 lines (56 loc) · 1.52 KB
/
interpreter.hs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- automatically generated by BNF Converter
module Main where
import System.IO ( stderr, getContents, hPutStr, putStr )
import System.Environment ( getArgs, getProgName )
import System.Exit ( exitFailure, exitSuccess )
import Control.Monad
import ErrM
import LexCinnabar
import ParCinnabar
import SkelCinnabar
import PrintCinnabar
import AbsCinnabar
import Builtins
import Program
type ParseFun a = [Token] -> Err a
myLLexer = myLexer
type Verbosity = Int
putStrV :: Verbosity -> String -> IO ()
putStrV v s = when (v > 1) $ putStrLn s
runFile :: Verbosity -> ParseFun Program -> FilePath -> IO ()
runFile v p f = readFile f >>= run v p
run :: Verbosity -> ParseFun Program -> String -> IO ()
run v p s = let ts = myLLexer s in case p ts of
Bad s -> do
putStrLn "\nParse Failed...\n"
putStrV v "Tokens:"
putStrV v $ show ts
putStrLn s
exitFailure
Ok program -> do
input <- getContents
let initSt = initialState input
let (stdo, stde, waserr) = runProgram program initSt
putStr stdo
hPutStr stderr stde
if waserr
then exitFailure
else exitSuccess
usage :: IO ()
usage = do
putStrLn $ unlines
[ "Usage: ./interpreter [--help] [-v] FILE"
, "Run contents of FILE."
, "Options:"
, " --help Display this help message."
, " -v Verbose mode."
]
exitFailure
main :: IO ()
main = do
args <- getArgs
case args of
"--help":_ -> usage
["-v", fname] -> runFile 2 pProgram fname
[fname] -> runFile 0 pProgram fname
_ -> usage