]> ruin.nu Git - proglang.git/blobdiff - Compile.hs
minor stuff
[proglang.git] / Compile.hs
index 69edecd879be433bab4f32259f8f0f79d2af0d41..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";
@@ -36,4 +43,8 @@ compileStm (SIf b s s') = "if("++compileExp b++")"++compileStm s++" \nelse "++co
 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 (SDeclD t i) = compileStm (SDecl t i $ case t of
+       TInt -> EInt 0
+       TBool -> EBool False
+       )
 compileStm (SDecl t (Ident i) e) = "int "++i++"="++compileExp e++";\n"