X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=gen.icl;h=be2c3ce839adfe58a2b7b68e8024879fcd1eafd6;hb=6f22285a7309dce751de352f92632ce2b40742ae;hp=b24830e6dafc57289f63f84e58470476f88ad814;hpb=ac2e692f8057d6f9c41817ff964561205b856c2c;p=cc1516.git 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