-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.hs
61 lines (47 loc) · 1.75 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
-- automatically generated by BNF Converter
module Main where
import System.IO
import System.Environment ( getArgs, getProgName )
import System.Exit ( exitFailure, exitSuccess )
import Lexmyjs
import Parmyjs
import Skelmyjs
import Printmyjs
import Absmyjs
import JSInterpreter
import ErrM
type ParseFun a = [Token] -> Err a
myLLexer = myLexer
type Verbosity = Int
putStrV :: Verbosity -> String -> IO ()
putStrV v s = if v > 1 then hPutStrLn stderr s else return ()
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 hPutStrLn stderr "\nParse Failed...\n"
putStrV v "Tokens:"
putStrV v $ show ts
hPutStrLn stderr s
exitFailure
Ok tree -> do --putStrLn "\nParse Successful!"
--showTree v tree
printOutputAndExit $ exec tree
printOutputAndExit (Left err, _) = do
hPutStrLn stderr $ "Unhandled error: " ++ (show err)
exitFailure
printOutputAndExit (Right output, _) = do
putStr $ show output
exitSuccess
showTree :: (Show a, Print a) => Int -> a -> IO ()
showTree v tree
= do
putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree
main :: IO ()
main = do args <- getArgs
case args of
[] -> hGetContents stdin >>= run 2 pProgram
"-s":fs -> mapM_ (runFile 0 pProgram) fs
fs -> mapM_ (runFile 2 pProgram) fs
runs = run 2 pProgram