1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
module ICurry.Pretty where
import ICurry.Types
import Text.Pretty
ppIProg :: IProg -> Doc
ppIProg (IProg name imports types funcs) = vsepBlank
[ ppHeader name
, ppImports imports
, ppTypes types
, ppFunctions funcs
]
ppHeader :: String -> Doc
name = text "module" <+> text name <+> text "where"
ppImports :: [String] -> Doc
ppImports = vsep . map ppImport
ppImport :: String -> Doc
ppImport name = text "import" <+> text name
ppTypes :: [IDataType] -> Doc
ppTypes = vsepBlank . map ppType
ppType :: IDataType -> Doc
ppType (name, _, tvs, cs) = nest 1 $
text "data" <+> ppQName name <+> ppTVars tvs $$ ppConstructors cs
ppConstructors :: [IConstructor] -> Doc
ppConstructors cs = vsep $ zipWith (<+>) (equals : repeat bar)
(map ppConstructor cs)
ppConstructor :: IConstructor -> Doc
ppConstructor (IConstructor name _ es) = hsep $
ppQName name : map (ppTExpr 'a') es
ppFunctions :: [IFunction] -> Doc
ppFunctions = vsepBlank . map ppFunction
ppFunction :: IFunction -> Doc
ppFunction (IFunction name _ body) = ppQName name <+> ppFuncBody body
ppQName :: IQName -> Doc
ppQName (modname, localname) = text $ modname ++ '.' : localname
ppArity :: IArity -> Doc
ppArity arity = hsep $ map ppVar [1 .. arity]
ppFuncBody :: IFuncBody -> Doc
ppFuncBody (IExternal arity name) = text ("external \"" ++ name ++ "\",")
<+> int arity
<+> text "args"
ppFuncBody (IFuncBody vars block) = ppVars vars <+> equals <+> ppBlock block
ppVars :: [IVarIndex] -> Doc
ppVars = hsep . map ppVar
ppVar :: IVarIndex -> Doc
ppVar = text . ('x':) . show
ppTVars :: [ITVarIndex] -> Doc
ppTVars = hsep . map (ppTVar 'a')
ppTVar :: Char -> ITVarIndex -> Doc
ppTVar c = text . (c:) . show
ppTExpr :: Char -> ITExpr -> Doc
ppTExpr c (ITVar v) = ppTVar c v
ppTExpr c (ITFunc e1 e2) = parens (ppTExpr c e1)
<+> rarrow <+>
parens (ppTExpr c e2)
ppTExpr c (ITCons n es) = parens $ ppQName n <+> hsep (map (ppTExpr c) es)
ppBlock :: IBlock -> Doc
ppBlock (ISimpleBlock a e) = nest 2 (
lbrace $$
ppLocals (map fst a) $$
ppAssignments a $$
text "return" <+>
ppExpr e) $$ rbrace
ppBlock (ICaseLitBlock a v bs) = nest 2 (
lbrace $$
ppLocals (map fst a) $$
ppAssignments a $$
ppLitBranches bs $$
text "case" <+> ppVar v <+> text "of") $$
rbrace
ppBlock (ICaseConsBlock a v bs) = nest 2 (
lbrace $$
ppLocals (map fst a) $$
ppAssignments a $$
nest 2 (text "case" <+> ppVar v <+> text "of" $$
ppConsBranches bs)) $$
rbrace
ppLocals :: [IVarIndex] -> Doc
ppLocals ls = if null ls
then empty
else text "local"
<+> (hsep $ punctuate comma $ map ppVar ls)
ppAssignments :: [IAssign] -> Doc
ppAssignments = vcat . map ppAssignment
ppAssignment :: IAssign -> Doc
ppAssignment (v, e) = ppVar v <+> equals <+> ppExpr e
ppExprs :: [IExpr] -> Doc
ppExprs = hsep . punctuate comma . map ppExpr
ppExpr :: IExpr -> Doc
ppExpr (IVar v) = ppVar v
ppExpr (ILit l) = ppLit l
ppExpr (IFCall n es) = ppQName n <+> lparen <+> ppExprs es <+> rparen
ppExpr (ICCall n es) = text "<constructor>" <+> ppExpr (IFCall n es)
ppExpr (IOr es) = char '?' <+> lparen <+> ppExprs es <+> rparen
ppLit :: ILiteral -> Doc
ppLit (IInt v) = int v
ppLit (IFloat v) = float v
ppLit (IChar v) = char v
ppLitBranches :: [ILitBranch] -> Doc
ppLitBranches = hsep . map ppLitBranch
ppLitBranch :: ILitBranch -> Doc
ppLitBranch (ILitBranch l block) = ppLit l <+> rarrow
ppConsBranches :: [IConsBranch] -> Doc
ppConsBranches = vcat . map ppConsBranch
ppConsBranch :: IConsBranch -> Doc
ppConsBranch (IConsBranch c vs block) =
ppQName c <+> hsep (map ppVar vs) <+> rarrow <+> ppBlock block
|