]> ruin.nu Git - proglang.git/commitdiff
CompInt, integrated compiler and interpreter for the new century
authorMichael Andreen <harv@ruin.nu>
Tue, 14 Mar 2006 17:29:07 +0000 (17:29 +0000)
committerMichael Andreen <harv@ruin.nu>
Tue, 14 Mar 2006 17:29:07 +0000 (17:29 +0000)
Compile.hs
Interpret.hs
Interpreter.hs
Makefile
Typecheck.hs

index 0e1cf22a1c0b0239f43af889962496e9ce2c37f7..2252fc539064b6666fead76092317d1a16f31593 100644 (file)
@@ -1,8 +1,15 @@
-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";
index c5ef5e0589a302c44e7ac2436cea97660312c366..0728d5181c967cf6d109f3327f2d8905e6738b1e 100644 (file)
@@ -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)
index bccc53d0fdb4871b1469011ee041adf59b196cd8..694ce2367ea328c1e4b2ebb270ab743a99f5e87a 100644 (file)
@@ -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
index 24f8a623b51038738dda35bb8bebd26fcb382b52..b6815c2f1a4339c40837cb76e73659ee345d3bf5 100644 (file)
--- 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
 
index 70aad706a88cfc5b38a1863744ccce563772e490..1b0baa00ed5d48dc760f029f9759e8bb821eed9c 100644 (file)
@@ -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