1 implementation module gen
12 import qualified Data.Map as Map
17 import Control.Applicative
19 import Control.Monad.State
20 import Control.Monad.Trans
21 from Text import class Text(concat), instance Text String
25 //Instruction is an instruction, with possible arguments and a possible comment
27 :: Instr = Instr String [Arg] String
30 :: Arg = L Label | Lit Int
31 :: SSMProgram :== [Instr]
35 gen _ = toString [Lab "Test"
36 ,Instr "ldc" [Lit 1] "Eerste instructie"
37 ,Instr "ldc" [Lit 2] "Tweede instructie"]
40 //Scrap this, we'll need shared state when generating
41 //i.e. to figure out the positions of vars relative to the
42 //SP/MP/whatever or in which register they are
43 //and to supply with fresh labels
45 //The generation monad
46 :: GenError = Error String
47 :: GenMap :== 'Map'.Map String LoadPlace
48 :: LoadPlace = LDA Int | LDC Int | LDH Int | LDL Int
50 :: Gen a :== StateT (GenMap, [Label]) (Either GenError) a
55 class g a :: a -> Gen SSMProgram
58 g (VarExpr _ (VarDef k fs)) = pure <$> load k //note: pure is pure for list, i.e. []
60 load :: String -> Gen Instr
61 load k = genMap >>= \g-> case 'Map'.member k g of
62 False = liftT (Left $ Error $ concat ["PANIC: ", k, " not found in variable mapping"])
63 True = loadP $ 'Map'.find k g
65 loadP :: LoadPlace -> Gen Instr
66 loadP pl = dec pl >>= \(instr, arg)-> pure $ Instr instr [arg] ""
68 dec (LDA i) = pure ("lda", Lit i)
69 dec (LDC i) = pure ("ldc", Lit i)
70 dec (LDH i) = pure ("ldh", Lit i)
71 dec (LDL i) = pure ("ldl", Lit i)
72 dec (LDR i) = pure ("ldr", Lit i)
73 dec (LDS i) = pure ("lds", Lit i)
74 dec _ = liftT (Left $ Error "PANIC: trying to load non adres")
76 class print a :: a -> [String]
78 instance print Instr where
79 print (Lab l) = [l, ":", "\n"]
80 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
82 instance print [Arg] where
83 print args = (map toString args)
85 instance toString Arg where
87 toString (Lit int) = toString int
89 instance toString SSMProgram where
90 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p
92 instance MonadTrans (StateT (GenMap,[Label])) where
93 liftT m = StateT \s-> m >>= \a-> return (a, s)