X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Compile.hs;h=2252fc539064b6666fead76092317d1a16f31593;hb=b127b854d31e2936f86d4aabdcebe9cb298b23e9;hp=c3e93f94850a720839196f010a8b6fe6294b60ce;hpb=98e238b8dd8c07705ab9681ff07c85e10eda9397;p=proglang.git diff --git a/Compile.hs b/Compile.hs index c3e93f9..2252fc5 100644 --- a/Compile.hs +++ b/Compile.hs @@ -1,15 +1,21 @@ -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"; 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++")" @@ -37,5 +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 (SDecl t (Ident i) EDefault) = "int "++i++"=0;\n" +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"