1 implementation module gen
7 from StdFunc import id, const
12 import qualified Data.Map as Map
19 import Control.Applicative
21 import Control.Monad.Trans
22 from Text import class Text(concat), instance Text String
29 :: Instr = Instr String [Arg] String
32 :: Arg = L Label | Lit Int | Raw String
33 :: SSMProgram :== [Instr]
34 :: GenError = Error String
35 :: Addressbook :== 'Map'.Map String Address
36 :: Address = LAB String | ADDR Int
37 :: Gen a :== RWST () SSMProgram (Addressbook, [Label]) (Either GenError) a
39 labelStream :: [Label]
40 labelStream = ["lbl_" +++ toString i\\i<-[1..]]
42 defaultAddressBook :: Addressbook
43 defaultAddressBook = extend "print" (LAB "print")
44 $ extend "read" (LAB "read")
45 $ extend "isEmpty" (LAB "isEmpty")
48 gen :: AST -> Either String String
49 gen (AST fds) = case evalRWST prog () (defaultAddressBook, labelStream) of
50 Left (Error e) = Left e
51 Right (_, p) = Right $ toString p
54 Instr "bsr" [L "main"] "",
56 ] >>| tell programContext
59 programContext :: SSMProgram
60 programContext = [Lab "print" //there is no actual IO in SSM
61 ,Instr "link" [Lit 0] ""
64 ,Lab "read" //there is no actual IO in SSM
65 ,Instr "link" [Lit 0] ""
66 ,Instr "ldc" [Lit 0] ""
68 ,Instr "str" [Raw "RR"] ""
72 ,Instr "link" [Lit 0] ""
73 ,Instr "ldl" [Lit -2] "load prt to list"
74 ,Instr "lda" [Lit 0] "derefrence ptr"
75 ,Instr "ldc" [Lit 0] ""
76 ,Instr "eq" [] "test for null pointer"
77 ,Instr "str" [Raw "RR"] ""
82 //helper functions for the gen monad
83 getAdressbook :: Gen Addressbook
84 getAdressbook = gets fst
86 updateAdressbook :: (Addressbook -> Addressbook) -> Gen Addressbook
87 updateAdressbook f = modify (appFst f) >>| getAdressbook
89 extend :: String Address Addressbook -> Addressbook
90 extend k pl g = 'Map'.put k pl g
93 fresh = gets snd >>= \vars->
94 modify (appSnd $ const $ tail vars) >>|
97 class g a :: a -> Gen ()
100 g UnNegation = tell [Instr "not" [] ""]
101 g UnMinus = tell [Instr "neg" [] ""]
104 g o = tell [Instr s [] ""]
120 BiCons = abort "Shit, Cons, how to deal with this?"
122 instance g Expr where
123 g (IntExpr _ i) = tell [Instr "ldc" [Lit i] ""]
124 g (CharExpr _ c) = tell [Instr "ldc" [Lit (toInt c)] ""]
125 g (BoolExpr _ b) = tell [Instr "ldc" [Lit (if b TRUE FALSE)] ""]
126 g (EmptyListExpr _) = tell [Instr "ldc" [Lit 0] ""]
127 >>| tell [Instr "sth" [] ""]
128 g (Op1Expr _ o e) = g e >>| g o
129 g (Op2Expr _ e1 BiCons e2) = g e2 >>| g e1
130 >>| tell [Instr "sth" [] ""]
131 >>| tell [Instr "ajs" [Lit -1] ""]
132 >>| tell [Instr "sth" [] ""]
133 g (Op2Expr _ e1 op e2) = g e1 >>| g e2 >>| g op
134 g (TupleExpr _ (e1,e2)) = g e1
135 >>| tell [Instr "sth" [] ""]
137 >>| tell [Instr "sth" [] ""]
138 >>| tell [Instr "ajs" [Lit -1] ""]
139 g (VarExpr _ (VarDef k fs)) = getAdressbook >>= \ab->case 'Map'.get k ab of
140 Nothing = liftT (Left $ Error "PANIC: undefined variable")
141 Just (ADDR t) = tell [Instr "ldl" [Lit t] ""]
142 Just (LAB t) = liftT (Left $ Error "PANIC: variable and function name clash")
143 //load k >>= \instr-> tell [instr] //note: pure is pure for list, i.e. []
144 g (FunExpr _ k es fs) =
147 >>| mapM_ (const $ tell [Instr "ajs" [Lit -1] ""]) es //clean up args
148 >>| tell [Instr "ldr" [Raw "RR"] ""]
150 jump :: String String -> Gen ()
151 jump instr k = getAdressbook >>= \ab->case 'Map'.get k ab of
152 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
153 Just (LAB t) = tell [Instr instr [L t] (k +++"()")]
154 Just (ADDR t) = liftT (Left $ Error $ "PANIC: jump should go to label")
156 instance g Stmt where
157 g (IfStmt cond th el) =
158 fresh >>= \elseLabel->
159 fresh >>= \endLabel->
161 tell [Instr "brf" [L elseLabel] "branch else"] >>|
163 tell [Instr "bra" [L endLabel] "branch end if"] >>|
164 tell [Lab elseLabel] >>|
167 g (WhileStmt cond th) =
168 fresh >>= \startLabel->
169 fresh >>= \endLabel ->
170 tell [Lab startLabel] >>|
172 tell [Instr "brf" [L endLabel] "branch end while"] >>|
174 tell [Instr "bra" [L startLabel] "branch start while"] >>|
176 g (AssStmt (VarDef k fs) e) =
177 g e >>| getAdressbook >>= \ab->case 'Map'.get k ab of
178 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as var"])
179 Just (LAB t) = liftT (Left $ Error $ "PANIC: cannot assign to function")
180 Just (ADDR t) = tell [Instr "stl" [Lit t] ""]
181 g (FunStmt k es) = mapM_ g es
183 >>| mapM_ (const $ tell [Instr "ajs" [Lit -1] ""]) es //clean up args
185 g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""]
186 >>| tell [Instr "ret" [] ""]
187 g (ReturnStmt (Just e)) = g e
188 >>| tell [Instr "str" [Raw "RR"] ""]
189 >>| g (ReturnStmt Nothing)
191 foldVarDecl :: Int VarDecl -> Gen Int
192 foldVarDecl x (VarDecl _ _ k e) = g e
194 >>| updateAdressbook (extend k (ADDR x)) >>| pure (x + 1)
196 addVars :: [String] -> (Addressbook -> Addressbook)
198 addVars [x:xs] = \ab->extend x (ADDR (-2 - (length xs))) (addVars xs ab)
200 instance g FunDecl where
201 g (FunDecl _ k args _ vds stms) =
202 //varDecls can call the enclosing function, so first reserve a label for it
203 updateAdressbook (extend k (LAB k)) >>|
204 getAdressbook >>= \oldMap ->
205 updateAdressbook (addVars args) >>|
207 tell [Instr "link" [Lit 0] ""] >>|
209 foldM foldVarDecl 1 vds >>|
212 updateAdressbook (const oldMap) >>| pure ()
214 annote :: Int String -> Gen ()
216 tell [Instr "annote" [Raw "MP", Lit pos, Lit pos, Raw "green", Raw key] ""]
218 class print a :: a -> [String]
220 instance print Instr where
221 print (Lab l) = [l, ":", "\n"]
222 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
224 instance print [Arg] where
225 print args = (map toString args)
227 instance toString Arg where
229 toString (Lit int) = toString int
232 instance toString SSMProgram where
233 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p