--- /dev/null
+module Compile (compileExp, compileStm) where
+
+import Abssyntax
+import Prelude hiding (lookup)
+
+compileExp :: Exp -> String
+compileExp (EBool True) = "1";
+compileExp (EBool False) = "0";
+compileExp (EInt n) = show n
+compileExp (EVar (Ident i)) = i
+compileExp (EAss (Ident i) e) = i++"="++compileExp e
+compileExp EDefault = error "EDefault called from an illegal place"
+compileExp (BiOpExp e o e') = "("++compileExp e++")"++op o++"("++compileExp e'++")"
+compileExp (ENeg e) = "-("++compileExp e++")"
+compileExp (ENot e) ="!("++compileExp e++")"
+compileExp (EPost (Ident i) Plus) = i++"++"
+compileExp (EPost (Ident i) Minus) = i++"--"
+compileExp EReadI = "read()"
+compileExp EReadB = "read()"
+
+op :: Op -> String
+op Eq = "=="
+op NEq = "!="
+op Plus = "+"
+op Minus = "-"
+op Times = "*"
+op Div = "/"
+op Lt = "<"
+op ELt = "<="
+op Gt = ">"
+op EGt = ">="
+
+compileStm :: Stm -> String
+compileStm (SNoop) = ";\n"
+compileStm (SExp e) = compileExp e++";\n"
+compileStm (SIf b s s') = "if("++compileExp b++")"++compileStm s++" \nelse "++compileStm s'
+compileStm (SPrint e) = "printf(\"%d\\n\","++compileExp e++");\n"
+compileStm (SBlock ss) = "{\n"++concat (map (("\t"++).compileStm) ss)++"\n}\n"
+compileStm (SWhile e s) = "while("++compileExp e++")"++compileStm s
+compileStm (SDecl t (Ident i) EDefault) = "int "++i++"=0;\n"
+compileStm (SDecl t (Ident i) e) = "int "++i++"="++compileExp e++";\n"
--- /dev/null
+-- automatically generated by BNF Converter
+module Main where
+
+
+import IO ( stdin, hGetContents )
+import System ( getArgs, getProgName )
+
+import Lexsyntax
+import Parsyntax
+import Skelsyntax
+import Printsyntax
+import Abssyntax
+
+import Typecheck
+import Compile
+import Control.Monad.State
+import Data.Map as Map hiding (showTree,map)
+
+import ErrM
+
+type ParseFun a = [Token] -> Err a
+
+myLLexer = myLexer
+
+type Verbosity = Int
+
+putStrV :: Verbosity -> String -> IO ()
+putStrV v s = if v > 1 then putStrLn s else return ()
+
+runFile :: Verbosity -> ParseFun Stms -> FilePath -> IO ()
+runFile v p f = readFile f >>= run v p
+
+run :: Verbosity -> ParseFun Stms -> String -> IO ()
+run v p s = let ts = myLLexer s in case p ts of
+ Bad s -> do
+ putStrLn "\nParse Failed...\n"
+ putStrV v "Tokens:"
+ putStrV v $ show ts
+ putStrLn s
+ Ok (Program s) -> do
+ runStateT (mapM typeCheckStm s) [empty]
+ putStr $ concat (map compileStm s)
+ return ()
+
+showTree :: (Show a, Print a) => Int -> a -> IO ()
+showTree v tree
+ = do
+ putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
+ putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree
+
+main :: IO ()
+main = do args <- getArgs
+ case args of
+ [] -> hGetContents stdin >>= run 2 pStms
+ "-s":fs -> mapM_ (runFile 0 pStms) fs
+ fs -> mapM_ (runFile 2 pStms) fs
+
+
+
+
+
-all: Typechecker Interpreter
+all: Typechecker Interpreter Compiler
doc: Docsyntax.dvi
Interpreter: Interpreter.hs Interpret.hs Typecheck.hs Parsyntax.hs Lexsyntax.hs Abssyntax.hs
ghc -fglasgow-exts --make Interpreter.hs -o Interpreter
+Compiler: Compiler.hs Compile.hs Typecheck.hs Parsyntax.hs Lexsyntax.hs Abssyntax.hs
+ ghc -fglasgow-exts --make Compiler.hs -o Compiler
+
Parsyntax.hs: Parsyntax.y
happy -gca -idebug Parsyntax.y