-module Compile (compileExp, compileStm) where
+module Compile (compile,compileExp, compileStm) where
import Abssyntax
import Prelude hiding (lookup)
+cHeader = "#include <stdio.h>\nint read(){\nint n;\nscanf(\"%d\",&n);\nreturn n;\n}\nint main(void){\n"
+
+cFooter = "return 0;}"
+
+compile :: [Stm] -> String
+compile s = cHeader++concat (map compileStm s)++cFooter
+
compileExp :: Exp -> String
compileExp (EBool True) = "1";
compileExp (EBool False) = "0";
-module Interpret (eval, execute, Value(VInt, VBool)) where
+module Interpret (interpret, eval, execute, Value(VInt, VBool)) where
import Abssyntax
import Control.Monad.State
type Variables = [Map Ident Value]
+interpret :: [Stm] -> IO ()
+interpret s = runStateT (mapM execute s) [empty] >> return ()
+
--eval :: (MonadState Variables m) => Exp -> m Value
eval :: Exp -> StateT Variables IO Value
eval (EBool b) = return (VBool b)
putStrV v $ show ts
putStrLn s
Ok (Program s) -> do
- putStrLn "\nParse Successful!"
- showTree v (Program s)
- runStateT (mapM typeCheckStm s) [empty]
- print "The program is type-correct!!"
- print "Running program:"
- runStateT (mapM execute s) [empty]
- print "Done running program!"
- return ()
+ --putStrLn "\nParse Successful!"
+ --showTree v (Program s)
+ typeCheck s
+ --print "The program is type-correct!!"
+ --print "Running program:"
+ interpret s
+ --print "Done running program!"
showTree :: (Show a, Print a) => Int -> a -> IO ()
showTree v tree
-all: Typechecker Interpreter Compiler
+all: CompInt
doc: Docsyntax.dvi
Compiler: Compiler.hs Compile.hs Typecheck.hs Parsyntax.hs Lexsyntax.hs Abssyntax.hs
ghc -fglasgow-exts --make Compiler.hs -o Compiler
+CompInt: CompInt.hs Compile.hs Interpret.hs Typecheck.hs Parsyntax.hs Lexsyntax.hs Abssyntax.hs
+ ghc -fglasgow-exts --make CompInt.hs -o CompInt
+
Parsyntax.hs: Parsyntax.y
happy -gca -idebug Parsyntax.y
-module Typecheck (typeCheckExp, typeCheckStm, typeCheckVar) where
+module Typecheck (typeCheck,typeCheckExp, typeCheckStm, typeCheckVar) where
import Abssyntax
assert True _ = return ()
assert False s = fail s
+typeCheck :: [Stm] -> IO ()
+typeCheck s = runStateT (mapM typeCheckStm s) [empty] >> return ()
+
typeCheckExp :: (MonadState Types m) => Exp -> m Type
typeCheckExp (BiOpExp e o e') = do
t1 <- typeCheckExp e