From: pimjager Date: Sun, 8 May 2016 22:25:19 +0000 (+0200) Subject: CodeGen for statements, no variables yet X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=6f22285a7309dce751de352f92632ce2b40742ae;p=cc1516.git CodeGen for statements, no variables yet --- diff --git a/gen.icl b/gen.icl index b24830e..be2c3ce 100644 --- a/gen.icl +++ b/gen.icl @@ -17,6 +17,7 @@ import Data.Either import Data.Tuple import Data.Functor import Data.Monoid +import Data.Maybe import Control.Applicative import Control.Monad import Control.Monad.Trans @@ -32,10 +33,11 @@ FALSE :== 0 :: Instr = Instr String [Arg] String | Lab Label :: Label :== String -:: Arg = L Label | Lit Int +:: Arg = L Label | Lit Int | Raw String :: SSMProgram :== [Instr] :: GenError = Error String :: GenMap :== 'Map'.Map String LoadPlace +//completely change to either Stack, Heap, Register? :: LoadPlace = LDA Int | LDC Int | LDH Int | LDL Int | LDR Int | LDS Int | FUNC Label @@ -88,12 +90,31 @@ instance g Stmt where fresh >>= \elseLabel-> fresh >>= \endLabel-> g cond >>| - tell [Instr "brf" [L elseLabel] "branch false"] >>| + tell [Instr "brf" [L elseLabel] "branch else"] >>| mapM_ g th >>| tell [Instr "bra" [L endLabel] "branch end if"] >>| tell [Lab elseLabel] >>| mapM_ g el >>| tell [Lab endLabel] + g (WhileStmt cond th) = + fresh >>= \startLabel-> + fresh >>= \endLabel -> + tell [Lab startLabel] >>| + g cond >>| + tell [Instr "brf" [L endLabel] "branch end while"] >>| + mapM_ g th >>| + tell [Instr "bra" [L startLabel] "branch start while"] >>| + tell [Lab endLabel] + g (AssStmt (VarDef k fs) e) = + g e >>| + abort "Shit, an assignment, figure out something with storing vars or something" + g (FunStmt _ _) = abort "CodeGen FunStmt unused" //not used + g (ReturnStmt Nothing) = tell [Instr "ret" [] ""] + g (ReturnStmt (Just e)) = + g e >>| + tell [Instr "str" [Raw "RR"] ""] >>| + g (ReturnStmt Nothing) + op2ins :: Op2 -> String