Added some comments on codegen
[cc1516.git] / gen.icl
1 implementation module gen
2
3
4 import StdMisc
5 import StdList
6 import StdOverloaded
7 import StdString
8
9 from Data.Func import $
10 from Text import class Text(join), instance Text String
11 from Data.List import intersperse
12
13 import AST
14
15 //Instruction is an instruction, with possible arguments and a possible comment
16 //Or is a label
17 :: Instr = Instr String [Arg] String
18 | Label String
19 :: Arg = L String | Lit Int
20 :: SSMProgram :== [Instr]
21
22
23 gen :: AST -> String
24 gen _ = toString [Label "Test"
25 ,Instr "ldc" [Lit 1] "Eerste instructie"
26 ,Instr "ldc" [Lit 2] "Tweede instructie"]
27
28
29 //Scrap this, we'll need shared state when generating
30 //i.e. to figure out the positions of vars relative to the
31 //SP/MP/whatever or in which register they are
32 //and to supply with fresh labels
33 class g a :: a -> SSMProgram
34
35 instance g Expr where
36 g _ = undef
37
38
39
40 class print a :: a -> [String]
41
42 instance print Instr where
43 print (Label l) = [l, ":", "\n"]
44 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
45
46 instance print [Arg] where
47 print args = (map toString args)
48
49 instance toString Arg where
50 toString (L l) = l
51 toString (Lit int) = toString int
52
53 instance toString SSMProgram where
54 toString p = join " " $ map (\i-> join " " $ print i) p