]> ruin.nu Git - proglang.git/commitdiff
compiler seems to be working
authorMichael Andreen <harv@ruin.nu>
Tue, 14 Mar 2006 20:23:09 +0000 (20:23 +0000)
committerMichael Andreen <harv@ruin.nu>
Tue, 14 Mar 2006 20:23:09 +0000 (20:23 +0000)
1  2 
CompInt.hs
Compile.hs

diff --combined CompInt.hs
index d5aa2ca052ada77068d47afd86bb901eeb053a98,298c9f49c8ae3a50fcf220e393bdf99ed994c1bd..1934447c5e1110079dc94f93cf2867a9cfb52c47
@@@ -14,6 -14,7 +14,6 @@@ import Abssynta
  import Typecheck
  import Interpret
  import Compile
 -import Data.Map as Map hiding (showTree)
  
  import ErrM
  
@@@ -21,33 -22,37 +21,37 @@@ type ParseFun a = [Token] -> Err 
  
  myLLexer = myLexer
  
 -
 -cHeader = "#include <stdio.h>\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
 +              [] -> hGetContents stdin >>= run interpret pProgram
-               "-c":f:[] -> runFile (\fun st -> writeFile (f++".c") $ compile fun st)  pProgram f
+               "-c":f:[] -> let file = (f++".c") in do
+                       putStrLn $ "Compiling "++f++" to the C99-compatible file:"++file
 -                      runFile (writeFile file . compile)  pStms f
 -              f:[] -> runFile interpret pStms f
++                      runFile (\fun st -> writeFile file $ compile fun st)  pProgram f
 +              f:[] -> runFile interpret pProgram f
-               _ -> print "Too many arguments"
+               _ -> do
+                       putStrLn "Usage: ./CompInt [-c] <file>"
+                       putStrLn "-c : compile <file> to C99-compatible file"
diff --combined Compile.hs
index eb70d0c8223e9e71ec356cce274f850ad454239b,2252fc539064b6666fead76092317d1a16f31593..f2de7ecdac8228f805218815320e1e2fb5a3ab2f
@@@ -3,12 -3,12 +3,14 @@@ module Compile (compile,compileExp, com
  import Abssyntax
  import Prelude hiding (lookup)
  
--cHeader = "#include <stdio.h>\nint read(){\nint n;\nscanf(\"%d\",&n);\nreturn n;\n}\nint main(void){\n"
++cHeader = "#include <stdio.h>\nint read(){\nint n;\nscanf(\"%d\",&n);\nreturn n;\n}\n"
  
--cFooter = "return 0;}"
++cMiddle = "\nint main(void){\n"
 -compile :: [Stm] -> String
 -compile s = cHeader++concat (map compileStm s)++cFooter
++cFooter = "return 0;}\n"
 +
 +compile :: [Func] -> [Stm] -> String
- compile f s = cHeader++concat (map compileStm s)++cFooter
++compile f s = cHeader++concat (map compileFuncDecl f)++concat (map compileFunc f)++cMiddle++concat (map compileStm s)++cFooter
  
  compileExp :: Exp -> String
  compileExp (EBool True) = "1";
@@@ -23,6 -23,6 +25,7 @@@ compileExp (EPost (Ident i) Plus) = i++
  compileExp (EPost (Ident i) Minus) = i++"--"
  compileExp EReadI = "read()"
  compileExp EReadB = "read()"
++compileExp (EFunc (Ident i) as) = i++"("++(foldl1 (\a b -> a++","++b) (map compileExp as))++")"
  
  op :: Op -> String
  op Eq = "=="
@@@ -48,3 -48,3 +51,10 @@@ compileStm (SDeclD t i) = compileStm (S
        TBool -> EBool False
        )
  compileStm (SDecl t (Ident i) e) = "int "++i++"="++compileExp e++";\n"
++compileStm (SReturn e) = "return "++compileExp e++";"
++
++compileFunc :: Func -> String
++compileFunc (Func _ (Ident i) d ss) = "\nint "++i++"("++(foldl1 (\a b -> a++","++b) (map (\(Decl _ (Ident i)) -> "int "++i) d))++"){\n"++concat (map compileStm ss)++"\n}\n"
++
++compileFuncDecl :: Func -> String
++compileFuncDecl (Func _ (Ident i) d ss) = "\nint "++i++"("++(foldl1 (\a b -> a++","++b) (map (\(Decl _ (Ident i)) -> "int "++i) d))++");\n"