]> ruin.nu Git - proglang.git/commitdiff
adding compiler
authorMichael Andreen <harv@ruin.nu>
Wed, 1 Mar 2006 14:18:38 +0000 (14:18 +0000)
committerMichael Andreen <harv@ruin.nu>
Wed, 1 Mar 2006 14:18:38 +0000 (14:18 +0000)
Compile.hs [new file with mode: 0644]
Compiler.hs [new file with mode: 0644]
Makefile

diff --git a/Compile.hs b/Compile.hs
new file mode 100644 (file)
index 0000000..c3e93f9
--- /dev/null
@@ -0,0 +1,41 @@
+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"
diff --git a/Compiler.hs b/Compiler.hs
new file mode 100644 (file)
index 0000000..2f1f31b
--- /dev/null
@@ -0,0 +1,61 @@
+-- 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
+
+
+
+
+
index f662112e8f88a49689dd97c0a3f07f7a54dc3a77..24f8a623b51038738dda35bb8bebd26fcb382b52 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-all: Typechecker Interpreter
+all: Typechecker Interpreter Compiler
 
 doc: Docsyntax.dvi
 
 
 doc: Docsyntax.dvi
 
@@ -14,6 +14,9 @@ Typechecker: Typechecker.hs  Typecheck.hs Parsyntax.hs Lexsyntax.hs Abssyntax.hs
 Interpreter: Interpreter.hs Interpret.hs Typecheck.hs Parsyntax.hs Lexsyntax.hs Abssyntax.hs
        ghc -fglasgow-exts --make Interpreter.hs -o Interpreter
 
 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
 
 Parsyntax.hs: Parsyntax.y
        happy -gca -idebug Parsyntax.y