X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=CompInt.hs;h=d5aa2ca052ada77068d47afd86bb901eeb053a98;hp=1188466da980b4d1886b4d3deff9d5e199fa08fc;hb=04f0a9566794cf761b7bcf83190051a400ec3653;hpb=cfe5796efb06251047f77bf9d2295d7093910292 diff --git a/CompInt.hs b/CompInt.hs index 1188466..d5aa2ca 100644 --- a/CompInt.hs +++ b/CompInt.hs @@ -14,7 +14,6 @@ import Abssyntax import Typecheck import Interpret import Compile -import Data.Map as Map hiding (showTree) import ErrM @@ -22,33 +21,33 @@ type ParseFun a = [Token] -> Err a myLLexer = myLexer - -cHeader = "#include \nint read(){\nint n;\nscanf(\"%d\",&n);\nreturn n;\n}\nint main(void){\n" - -cFooter = "return 0;}" +splitFunStm :: [FuncStm] -> ([Func],[Stm]) +splitFunStm [] = ([],[]) +splitFunStm ((F f):fss) = let (fs,ss) = splitFunStm fss in (f:fs,ss) +splitFunStm ((S s):fss) = let (fs,ss) = splitFunStm fss in (fs,s:ss) putStrV :: Int -> String -> IO () putStrV v s = if v > 1 then putStrLn s else return () -runFile :: ([Stm] -> IO()) -> ParseFun Stms -> FilePath -> IO () +runFile :: ([Func] -> [Stm] -> IO()) -> ParseFun Program -> FilePath -> IO () runFile e p f = readFile f >>= run e p -run :: ([Stm] -> IO()) -> ParseFun Stms -> String -> IO () +run :: ([Func] -> [Stm] -> IO()) -> ParseFun Program -> String -> IO () run e p s = let ts = myLLexer s in case p ts of Bad s -> do putStrLn "\nParse Failed...\n" putStrLn "Tokens:" putStrLn $ show ts putStrLn s - Ok (Program s) -> do - typeCheck s - e s + Ok (Program s) -> let (fun,st) = splitFunStm (s) in do + typeCheck fun st + e fun st main :: IO () main = do args <- getArgs case args of - [] -> hGetContents stdin >>= run interpret pStms - "-c":f:[] -> runFile (writeFile (f++".c") . compile) pStms f - f:[] -> runFile interpret pStms f + [] -> hGetContents stdin >>= run interpret pProgram + "-c":f:[] -> runFile (\fun st -> writeFile (f++".c") $ compile fun st) pProgram f + f:[] -> runFile interpret pProgram f _ -> print "Too many arguments"