]> ruin.nu Git - proglang.git/blob - CompInt.hs
minor change
[proglang.git] / CompInt.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 Compile
17
18 import ErrM
19
20 type ParseFun a = [Token] -> Err a
21
22 myLLexer = myLexer
23
24 splitFunStm :: [FuncStm] -> ([Func],[Stm])
25 splitFunStm [] = ([],[])
26 splitFunStm ((F f):fss) = let (fs,ss) = splitFunStm fss in (f:fs,ss)
27 splitFunStm ((S s):fss) = let (fs,ss) = splitFunStm fss in (fs,s:ss)
28
29 putStrV :: Int -> String -> IO ()
30 putStrV v s = if v > 1 then putStrLn s else return ()
31
32 runFile :: ([Func] -> [Stm] -> IO()) -> ParseFun Program -> FilePath -> IO ()
33 runFile e p f = readFile f >>= run e p
34
35 run :: ([Func] -> [Stm] -> IO()) -> ParseFun Program -> String -> IO ()
36 run e p s = let ts = myLLexer s in case p ts of
37         Bad s    -> do
38                 putStrLn "\nParse              Failed...\n"
39                 putStrLn "Tokens:"
40                 putStrLn $ show ts
41                 putStrLn s
42         Ok (Program s) -> let (fun,st) = splitFunStm (s) in do
43                 typeCheck fun st
44                 e fun st
45
46 main :: IO ()
47 main = do 
48         args <- getArgs
49         case args of
50                 [] -> hGetContents stdin >>= run interpret pProgram
51                 "-c":f:[] -> let file = (f++".c") in do
52                         putStrLn $ "Compiling "++f++" to the C99-compatible file:"++file
53                         runFile (\fun st -> writeFile file $ compile fun st)  pProgram f
54                 f:[] -> runFile interpret pProgram f
55                 _ -> do
56                         putStrLn "Usage: ./CompInt [-c] <file>"
57                         putStrLn "-c : compile <file> to C99-compatible file"