Just (LAB t) = liftT (Left $ Error "PANIC: variable and function name clash")
//load k >>= \instr-> tell [instr] //note: pure is pure for list, i.e. []
g (FunExpr _ k es fs) =
-// tell [Instr "ldr" [Raw "MP"] ("old frame pointer")]
mapM g es
>>| jump "bsr" k
>>| tell [Instr "ldr" [Raw "RR"] ""]
Just (ADDR t) = liftT (Left $ Error $ "PANIC: jump should go to label")
instance g Stmt where
-// g (IfStmt cond th el) =
-// fresh >>= \elseLabel->
-// fresh >>= \endLabel->
-// g cond >>|
-// 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"
-// //vars will be on stack in locals (possible pointers to heap)
-// g (FunStmt _ _) = abort "CodeGen, FunStmt unused" //not used
+ g (IfStmt cond th el) =
+ fresh >>= \elseLabel->
+ fresh >>= \endLabel->
+ g cond >>|
+ 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 >>| getAdressbook >>= \ab->case 'Map'.get k ab of
+ Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as var"])
+ Just (LAB t) = liftT (Left $ Error $ "PANIC: cannot assign to function")
+ Just (ADDR t) = tell [Instr "stl" [Lit t] ""]
+ g (FunStmt k es) = mapM g es >>| jump "bsr" k
g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""]
>>| tell [Instr "ret" [] ""]
g (ReturnStmt (Just e)) = g e