1 -- automatically generated by BNF Converter
5 import IO ( stdin, hGetContents )
6 import System ( getArgs, getProgName )
16 import Control.Monad.State hiding (State)
17 import Control.Concurrent.MVar
18 import Data.Map as Map hiding (showTree)
22 type ParseFun a = [Token] -> Err a
28 splitFunStm :: [FuncStm] -> ([Func],[Stm])
29 splitFunStm [] = ([],[])
30 splitFunStm ((F f):fss) = let (fs,ss) = splitFunStm fss in (f:fs,ss)
31 splitFunStm ((S s):fss) = let (fs,ss) = splitFunStm fss in (fs,s:ss)
33 putStrV :: Verbosity -> String -> IO ()
34 putStrV v s = if v > 1 then putStrLn s else return ()
36 runFile :: Verbosity -> ParseFun Program -> FilePath -> IO ()
37 runFile v p f = putStrLn f >> readFile f >>= run v p
39 run :: Verbosity -> ParseFun Program -> String -> IO ()
40 run v p s = let ts = myLLexer s in case p ts of
42 putStrLn "\nParse Failed...\n"
46 Ok (Program s) -> let (fun,st) = splitFunStm (s) in do
47 putStrLn "\nParse Successful!"
48 showTree v (Program s)
49 runStateT (do mapM Typecheck.addFunction fun; mapM typeCheckFunction fun; mapM typeCheckStm st) Typecheck.emptyState
50 print "The program is type-correct!!"
51 print "Running program:"
53 runStateT (do mapM Interpret.addFunction fun; mapM execute st) Interpret.emptyState{ret=mv}
54 print "Done running program!"
57 showTree :: (Show a, Print a) => Int -> a -> IO ()
60 putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
61 putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree
64 main = do args <- getArgs
66 [] -> hGetContents stdin >>= run 2 pProgram
67 "-s":fs -> mapM_ (runFile 0 pProgram) fs
68 fs -> mapM_ (runFile 2 pProgram) fs