From 9625a8a7eb7aebeb161ca15cf66cff5699f89103 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 14 Mar 2006 17:29:07 +0000 Subject: [PATCH] CompInt, integrated compiler and interpreter for the new century --- Compile.hs | 9 ++++++++- Interpret.hs | 5 ++++- Interpreter.hs | 15 +++++++-------- Makefile | 5 ++++- Typecheck.hs | 5 ++++- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Compile.hs b/Compile.hs index 0e1cf22..2252fc5 100644 --- a/Compile.hs +++ b/Compile.hs @@ -1,8 +1,15 @@ -module Compile (compileExp, compileStm) where +module Compile (compile,compileExp, compileStm) where import Abssyntax import Prelude hiding (lookup) +cHeader = "#include \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"; diff --git a/Interpret.hs b/Interpret.hs index c5ef5e0..0728d51 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -1,4 +1,4 @@ -module Interpret (eval, execute, Value(VInt, VBool)) where +module Interpret (interpret, eval, execute, Value(VInt, VBool)) where import Abssyntax import Control.Monad.State @@ -14,6 +14,9 @@ instance Show Value where 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) diff --git a/Interpreter.hs b/Interpreter.hs index bccc53d..694ce23 100644 --- a/Interpreter.hs +++ b/Interpreter.hs @@ -38,14 +38,13 @@ run v p s = let ts = myLLexer s in case p ts of 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 diff --git a/Makefile b/Makefile index 24f8a62..b6815c2 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: Typechecker Interpreter Compiler +all: CompInt doc: Docsyntax.dvi @@ -17,6 +17,9 @@ Interpreter: Interpreter.hs Interpret.hs Typecheck.hs Parsyntax.hs Lexsyntax.hs 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 diff --git a/Typecheck.hs b/Typecheck.hs index 70aad70..1b0baa0 100644 --- a/Typecheck.hs +++ b/Typecheck.hs @@ -1,4 +1,4 @@ -module Typecheck (typeCheckExp, typeCheckStm, typeCheckVar) where +module Typecheck (typeCheck,typeCheckExp, typeCheckStm, typeCheckVar) where import Abssyntax @@ -16,6 +16,9 @@ assert :: Monad m => Bool -> String -> m () 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 -- 2.39.2