//helper functions for the gen monad
genMap :: Gen GenMap
genMap = gets fst
+changeGenMap :: (GenMap -> GenMap) -> Gen GenMap
+changeGenMap f = modify (appFst f) >>| genMap
+
+extend :: String LoadPlace GenMap -> GenMap
+extend k pl g = 'Map'.put k pl g
fresh :: Gen Label
fresh = gets snd >>= \vars->
g (EmptyListExpr _) = abort "Shit, empty list expr"
g (TupleExpr p (e1,e2)) = abort "How to deal with tuples?"
g (FunExpr _ k es fs) = mapM g es >>| jump "bsr" k >>= \instr-> tell [instr]
- //bra is probably not right, figure out function call way
instance g Stmt where
g (IfStmt cond th el) =
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 (ReturnStmt Nothing) = tell [Instr "ret" [] ""]
g (ReturnStmt (Just e)) =
tell [Instr "str" [Raw "RR"] ""] >>|
g (ReturnStmt Nothing)
+instance g VarDecl where
+ g _ = abort "How will we store vars? use the locals thing?"
+
+instance g FunDecl where
+ g (FunDecl _ k args mt vds stms) =
+ fresh >>= \l-> let lbl = l+++"_"+++k in
+ tell [Lab lbl] >>|
+ changeGenMap (extend k (FUNC lbl)) >>|
+ tell [Instr "link" [Lit $ length vds] ""] //reserve room for local variables
+ //Todo: actual code generation. Probably using 'RWST'.local to scope
+ //the VarDecl in this function
+
+
+
op2ins :: Op2 -> String
jump :: String String -> Gen Instr
jump instr k = genMap >>= \g-> case 'Map'.member k g of
False = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
- True = dec ('Map'.find k g) >>= \lbl-> pure $ Instr instr [lbl] k
+ True = dec ('Map'.find k g) >>= \lbl-> pure $ Instr instr [lbl] (k +++"()")
where
dec (FUNC l) = pure (L l)
dec _ = liftT (Left $ Error "PANIC: trying to jump to non label")