-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunInterpreter.hs
More file actions
74 lines (61 loc) · 1.89 KB
/
Copy pathRunInterpreter.hs
File metadata and controls
74 lines (61 loc) · 1.89 KB
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
70
71
72
73
module Main where
import Prelude
( ($), (.)
, Either(..)
, Int, (>)
, String, (++), concat, unlines
, Show, show
, IO, (>>), (>>=), mapM_, putStrLn, putStr
, FilePath
, getContents, readFile
)
import System.IO (hPutStrLn, stderr)
import System.Environment ( getArgs )
import System.Exit ( exitFailure )
import Control.Monad ( when, void )
import AbsGramar ()
import LexGramar ( Token, mkPosToken )
import ParGramar ( pProgram, myLexer )
import PrintGramar ( Print, printTree )
import SkelGramar ()
import Interpreter.Interpreter
import TypeChecker.TypeChecker
type Err = Either String
type ParseFun a = [Token] -> Err a
type Verbosity = Int
putStrV :: Verbosity -> String -> IO ()
putStrV v s = when (v > 1) $ putStrLn s
--runFile :: (Print a, Show a) => Verbosity -> ParseFun a -> FilePath -> IO ()
runFile v p f = readFile f >>= run v p
--run :: (Print a, Show a) => Verbosity -> ParseFun a -> String -> IO ()
run v p s =
case p ts of
Left err -> do
putStrLn err
exitFailure
Right tree -> do
case runTypeChecker tree of
Left error -> hPutStrLn stderr $ "TypeChecker error: " ++ error
Right _ -> do
interpreter_res <- runInterpreter tree
case interpreter_res of
Left e -> hPutStrLn stderr $ "runtime error: " ++ e
Right f -> putStr("")
where
ts = myLexer s
showPosToken ((l,c),t) = concat [ show l, ":", show c, "\t", show t ]
usage :: IO ()
usage = do
putStrLn $ unlines
[ "usage: Call with one of the following argument combinations:"
, " --help Display this help message."
, " (no arguments) Interpret stdin."
, " (file) Interpret content of file."
]
main :: IO ()
main = do
args <- getArgs
case args of
["--help"] -> usage
[] -> getContents >>= run 2 pProgram
fs -> mapM_ (runFile 2 pProgram) fs