import Data.Tuple
import Data.Functor
import Data.Monoid
+import Data.Maybe
import Control.Applicative
import Control.Monad
import Control.Monad.Trans
:: 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
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