+instance g VarDecl where
+ g (VarDecl _ Nothing _ _) = liftT (Left $ Error "PANIC: untyped vardecl")
+ g (VarDecl _ (Just t) k e) =
+ (\l->k+++"_"+++l) <$> fresh >>= \lbl->
+ changeGenMap (extend k (FUNC lbl)) >>|
+ tell [Lab lbl] >>|
+ g e >>|
+ tell [Instr "str" [Raw "RR"] ""] >>|
+ tell [Instr "ret" [] ""]
+
+
+instance g FunDecl where
+ g (FunDecl _ k _ _ vds stms) =
+ //varDecls can call the enclosing function, so first reserve a label for it
+ (\l-> if (k=="main") "main" (l+++"_"+++k)) <$> fresh >>= \lbl->
+ changeGenMap (extend k (FUNC lbl)) >>|
+ //then generate functions for the VarDecls
+ genMap >>= \oldMap ->
+ mapM_ g vds >>|
+ //then the main function
+ tell [Lab lbl] >>|
+ mapM_ g stms >>|
+ changeGenMap (const oldMap) >>| pure ()