]> ruin.nu Git - proglang.git/blob - Interpreter.hs
minor change
[proglang.git] / Interpreter.hs
1 -- automatically generated by BNF Converter
2 module Main where
3
4
5 import IO ( stdin, hGetContents )
6 import System ( getArgs, getProgName )
7
8 import Lexsyntax
9 import Parsyntax
10 import Skelsyntax
11 import Printsyntax
12 import Abssyntax
13
14 import Typecheck
15 import Interpret
16
17 import ErrM
18
19 type ParseFun a = [Token] -> Err a
20
21 myLLexer = myLexer
22
23 type Verbosity = Int
24
25 splitFunStm :: [FuncStm] -> ([Func],[Stm])
26 splitFunStm [] = ([],[])
27 splitFunStm ((F f):fss) = let (fs,ss) = splitFunStm fss in (f:fs,ss)
28 splitFunStm ((S s):fss) = let (fs,ss) = splitFunStm fss in (fs,s:ss)
29
30 putStrV :: Verbosity -> String -> IO ()
31 putStrV v s = if v > 1 then putStrLn s else return ()
32
33 runFile :: Verbosity -> ParseFun Program -> FilePath -> IO ()
34 runFile v p f = putStrLn f >> readFile f >>= run v p
35
36 run :: Verbosity -> ParseFun Program -> String -> IO ()
37 run v p s = let ts = myLLexer s in case p ts of
38         Bad s    -> do
39                 putStrLn "\nParse              Failed...\n"
40                 putStrV v "Tokens:"
41                 putStrV v $ show ts
42                 putStrLn s
43         Ok (Program s) -> let (fun,st) = splitFunStm (s) in do
44                 putStrLn "\nParse Successful!"
45                 showTree v (Program s)
46                 typeCheck fun st
47                 print "The program is type-correct!!"
48                 print "Running program:"
49                 interpret fun st
50                 print "Done running program!"
51
52 showTree :: (Show a, Print a) => Int -> a -> IO ()
53 showTree v tree
54  = do
55       putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
56       putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree
57
58 main :: IO ()
59 main = do args <- getArgs
60           case args of
61             [] -> hGetContents stdin >>= run 2 pProgram
62             "-s":fs -> mapM_ (runFile 0 pProgram) fs
63             fs -> mapM_ (runFile 2 pProgram) fs
64
65
66
67
68