1 -- automatically generated by BNF Converter
5 import IO ( stdin, hGetContents )
6 import System ( getArgs, getProgName )
14 import Control.Monad.State hiding (State)
15 import Data.Map as Map hiding (showTree)
20 type ParseFun a = [Token] -> Err a
27 splitFunStm :: [FuncStm] -> ([Func],[Stm])
28 splitFunStm [] = ([],[])
29 splitFunStm ((F f):fss) = let (fs,ss) = splitFunStm fss in (f:fs,ss)
30 splitFunStm ((S s):fss) = let (fs,ss) = splitFunStm fss in (fs,s:ss)
32 putStrV :: Verbosity -> String -> IO ()
33 putStrV v s = if v > 1 then putStrLn s else return ()
35 runFile :: Verbosity -> ParseFun Program -> FilePath -> IO ()
36 runFile v p f = putStrLn f >> readFile f >>= run v p
38 run :: Verbosity -> ParseFun Program -> String -> IO ()
39 run v p s = let ts = myLLexer s in case p ts of
41 putStrLn "\nParse Failed...\n"
45 Ok (Program s) -> let (fun,st) = splitFunStm (s) in do
46 putStrLn "\nParse Successful!"
47 showTree v (Program s)
48 runStateT (do mapM addFunction fun; mapM typeCheckFunction fun; mapM typeCheckStm st) emptyState
49 print "The program is type-correct!!"
52 showTree :: (Show a, Print a) => Int -> a -> IO ()
55 putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
56 putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree
59 main = do args <- getArgs
61 [] -> hGetContents stdin >>= run 2 pProgram
62 "-s":fs -> mapM_ (runFile 0 pProgram) fs
63 fs -> mapM_ (runFile 2 pProgram) fs