]> ruin.nu Git - proglang.git/blob - Interpreter.hs
Interpreter works
[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 import Control.Monad.State hiding (State)
17 import Control.Concurrent.MVar
18 import Data.Map as Map hiding (showTree)
19
20 import ErrM
21
22 type ParseFun a = [Token] -> Err a
23
24 myLLexer = myLexer
25
26 type Verbosity = Int
27
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)
32
33 putStrV :: Verbosity -> String -> IO ()
34 putStrV v s = if v > 1 then putStrLn s else return ()
35
36 runFile :: Verbosity -> ParseFun Program -> FilePath -> IO ()
37 runFile v p f = putStrLn f >> readFile f >>= run v p
38
39 run :: Verbosity -> ParseFun Program -> String -> IO ()
40 run v p s = let ts = myLLexer s in case p ts of
41         Bad s    -> do
42                 putStrLn "\nParse              Failed...\n"
43                 putStrV v "Tokens:"
44                 putStrV v $ show ts
45                 putStrLn s
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:"
52                 mv <- newEmptyMVar
53                 runStateT (do mapM Interpret.addFunction fun; mapM execute st) Interpret.emptyState{ret=mv}
54                 print "Done running program!"
55                 return ()
56
57 showTree :: (Show a, Print a) => Int -> a -> IO ()
58 showTree v tree
59  = do
60       putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
61       putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree
62
63 main :: IO ()
64 main = do args <- getArgs
65           case args of
66             [] -> hGetContents stdin >>= run 2 pProgram
67             "-s":fs -> mapM_ (runFile 0 pProgram) fs
68             fs -> mapM_ (runFile 2 pProgram) fs
69
70
71
72
73