Class: Crokus::PrettyPrinter
Constant Summary
Constants included
from Indent
Indent::INDENT
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#initialize ⇒ PrettyPrinter
constructor
A new instance of PrettyPrinter.
-
#visit(ast) ⇒ Object
-
#visitAddressOf(ao, args = nil) ⇒ Object
-
#visitArrayOf(aof, args = nil) ⇒ Object
-
#visitArrayOrStructInit(init, args = nil) ⇒ Object
-
#visitArrow(arrow, args = nil) ⇒ Object
-
#visitAssign(assign, args = nil) ⇒ Object
-
#visitBinary(expr, args = nil) ⇒ Object
-
#visitBody(body, args = nil) ⇒ Object
-
#visitBreak(brk, args = nil) ⇒ Object
-
#visitCase(case_, args = nil) ⇒ Object
-
#visitCastedExpr(cexpr, args = nil) ⇒ Object
-
#visitCasting(cast, args = nil) ⇒ Object
-
#visitCharLit(lit, args = nil) ⇒ Object
-
#visitCommaStmt(comma, args = nil) ⇒ Object
-
#visitCondExpr(ternary, args = nil) ⇒ Object
-
#visitContinue(cont, args = nil) ⇒ Object
-
#visitDecl(decl, args = nil) ⇒ Object
-
#visitDefine(define, args = nil) ⇒ Object
-
#visitDeref(deref, args = nil) ⇒ Object
-
#visitDesignUnit(du, args = nil) ⇒ Object
-
#visitDotted(dotted, args = nil) ⇒ Object
-
#visitDoWhile(while_, args = nil) ⇒ Object
-
#visitElse(else_, args = nil) ⇒ Object
-
#visitFor(for_, args = nil) ⇒ Object
-
#visitFormalArg(formalArg, args = nil) ⇒ Object
-
#visitFunCall(fcall, as_procedure = nil) ⇒ Object
-
#visitFunction(func, args = nil) ⇒ Object
-
#visitFunctionProto(func, args = nil) ⇒ Object
-
#visitGoto(goto, args = nil) ⇒ Object
-
#visitIdent(ident, args = nil) ⇒ Object
-
#visitIf(if_, args = nil) ⇒ Object
-
#visitInclude(include, args = nil) ⇒ Object
-
#visitIndexed(index, args = nil) ⇒ Object
-
#visitIntLit(lit, args = nil) ⇒ Object
-
#visitITE(ite, args = nil) ⇒ Object
IR ============.
-
#visitLabeledStmt(lstmt, args = nil) ⇒ Object
-
#visitParenth(par, args = nil) ⇒ Object
-
#visitPointerTo(pto, args = nil) ⇒ Object
-
#visitPostFixAccu(accu, args = nil) ⇒ Object
-
#visitPreFixAccu(accu, args = nil) ⇒ Object
-
#visitReturn(ret, args = nil) ⇒ Object
-
#visitSemicolonStmt(stmt, args = nil) ⇒ Object
-
#visitSizeof(sizeof, args = nil) ⇒ Object
-
#visitStrLit(lit, args = nil) ⇒ Object
-
#visitStruct(struct, args = nil) ⇒ Object
-
#visitSwitch(sw_, args = nil) ⇒ Object
-
#visitToken(tok, args = nil) ⇒ Object
-
#visitType(type, args = nil) ⇒ Object
-
#visitTypedef(typdef, args = nil) ⇒ Object
-
#visitUnary(unary, args = nil) ⇒ Object
-
#visitWhile(while_, args = nil) ⇒ Object
Methods included from Indent
#dedent, #indent, #say
Methods inherited from Visitor
#visitFloatLit, #visitLabelledStmt, #visitPointed
Constructor Details
Returns a new instance of PrettyPrinter.
13
14
15
16
|
# File 'lib/crokus/pretty_printer.rb', line 13
def initialize
@ind=-2
@verbose=false
end
|
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
11
12
13
|
# File 'lib/crokus/pretty_printer.rb', line 11
def code
@code
end
|
Instance Method Details
#visit(ast) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/crokus/pretty_printer.rb', line 18
def visit ast
@code=Code.new
ast.accept(self)
c_code=@code.finalize
c_code=Cleaner.new.clean(c_code)
return c_code
end
|
#visitAddressOf(ao, args = nil) ⇒ Object
416
417
418
419
|
# File 'lib/crokus/pretty_printer.rb', line 416
def visitAddressOf ao,args=nil
e=ao.expr.accept(self)
return " &#{e} "
end
|
#visitArrayOf(aof, args = nil) ⇒ Object
112
113
114
115
116
117
|
# File 'lib/crokus/pretty_printer.rb', line 112
def visitArrayOf aof,args=nil
type=aof.type.accept(self)
size=aof.size.accept(self) if aof.size
aof
"#{type}" end
|
#visitArrayOrStructInit(init, args = nil) ⇒ Object
407
408
409
410
411
412
413
414
|
# File 'lib/crokus/pretty_printer.rb', line 407
def visitArrayOrStructInit init,args=nil
inits=init.elements.collect{|e| e.accept(self)}
code=Code.new
code << "{"+inits.join(",")+"}"
return code.finalize
end
|
#visitArrow(arrow, args = nil) ⇒ Object
395
396
397
398
399
|
# File 'lib/crokus/pretty_printer.rb', line 395
def visitArrow arrow,args=nil
lhs=arrow.lhs.accept(self)
rhs=arrow.rhs.accept(self)
return "#{lhs}->#{rhs}"
end
|
#visitAssign(assign, args = nil) ⇒ Object
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/crokus/pretty_printer.rb', line 195
def visitAssign assign,args=nil
lhs=assign.lhs.accept(self)
op =assign.op.accept(self)
rhs=assign.rhs.accept(self)
if assign.rhs.is_a? Parenth
rhs=assign.rhs.expr.accept(self)
end
ret="#{lhs} #{op} #{rhs};"
ret
end
|
#visitBinary(expr, args = nil) ⇒ Object
365
366
367
368
369
370
371
372
373
374
375
|
# File 'lib/crokus/pretty_printer.rb', line 365
def visitBinary expr,args=nil
lhs=expr.lhs.accept(self)
op =expr.op.accept(self)
rhs=expr.rhs.accept(self)
case op
when "+","-","*","/"
else
op=" "+op+" "
end
return "#{lhs}#{op}#{rhs}"
end
|
#visitBody(body, args = nil) ⇒ Object
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/crokus/pretty_printer.rb', line 437
def visitBody body,args=nil
code=Code.new
code << "{"
code.indent=2
body.each do |stmt|
kode_stmt = stmt.accept(self,true)
kode_stmt << ";"
code << kode_stmt
end
code.indent=0
code << "}"
return code
end
|
#visitBreak(brk, args = nil) ⇒ Object
329
330
331
|
# File 'lib/crokus/pretty_printer.rb', line 329
def visitBreak brk,args=nil
return "break;"
end
|
#visitCase(case_, args = nil) ⇒ Object
297
298
299
300
301
302
303
304
305
|
# File 'lib/crokus/pretty_printer.rb', line 297
def visitCase case_,args=nil
e=case_.expr.accept(self)
code=Code.new
code << "case #{e}:"
code.indent=2
code << case_.body.accept(self)
code.indent=0
return code
end
|
#visitCastedExpr(cexpr, args = nil) ⇒ Object
137
138
139
140
141
|
# File 'lib/crokus/pretty_printer.rb', line 137
def visitCastedExpr cexpr, args=nil
type=cexpr.type.accept(self)
e=cexpr.expr.accept(self)
return "(#{type}) #{e}"
end
|
#visitCasting(cast, args = nil) ⇒ Object
132
133
134
135
|
# File 'lib/crokus/pretty_printer.rb', line 132
def visitCasting cast,args=nil
type=cast.type.accept(self)
return "#{type} #{cast.modifier}"
end
|
#visitCharLit(lit, args = nil) ⇒ Object
361
362
363
|
# File 'lib/crokus/pretty_printer.rb', line 361
def visitCharLit lit,args=nil
return lit.to_s
end
|
#visitCommaStmt(comma, args = nil) ⇒ Object
183
184
185
186
187
188
|
# File 'lib/crokus/pretty_printer.rb', line 183
def visitCommaStmt comma,args=nil
lhs=comma.lhs.accept(self)
rhs=comma.rhs.accept(self)
ret="#{lhs},#{rhs}"
ret
end
|
#visitCondExpr(ternary, args = nil) ⇒ Object
383
384
385
386
387
388
|
# File 'lib/crokus/pretty_printer.rb', line 383
def visitCondExpr ternary,args=nil
cond=ternary.cond.accept(self)
lhs=ternary.lhs.accept(self)
rhs=ternary.rhs.accept(self)
"#{cond} ? #{lhs} : #{rhs}"
end
|
#visitContinue(cont, args = nil) ⇒ Object
333
334
335
|
# File 'lib/crokus/pretty_printer.rb', line 333
def visitContinue cont,args=nil
return "continue;"
end
|
#visitDecl(decl, args = nil) ⇒ Object
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
|
# File 'lib/crokus/pretty_printer.rb', line 37
def visitDecl decl,args=nil
code=Code.new
type=decl.type.accept(self)
array_size=""
t=decl.type
while t.is_a? ArrayOf
type=t.name.accept(self)
size=t.size.accept(self)
array_size="[#{size}]"+array_size
t=t.type
end
t=nil
vname=decl.var.accept(self)
init=decl.init.accept(self) if decl.init
if init.is_a? Code
init=" = "+init.finalize
else
init=" = "+init
end if decl.init
if type.is_a? Code
last=type.lines.pop
type.lines.each do |line|
code << line
end
last_str=last+" #{vname}#{array_size}#{init}"
code << last_str
else
code << "#{type} #{vname}#{array_size}#{init}"
end
code << ";"
return code
end
|
#visitDefine(define, args = nil) ⇒ Object
87
88
89
90
91
|
# File 'lib/crokus/pretty_printer.rb', line 87
def visitDefine define,args=nil
name=define.name.accept(self)
e=define.expr.accept(self)
return "#define #{name} #{e}"
end
|
#visitDeref(deref, args = nil) ⇒ Object
432
433
434
435
|
# File 'lib/crokus/pretty_printer.rb', line 432
def visitDeref deref,args=nil
e=deref.expr.accept(self)
return "*#{e}"
end
|
#visitDesignUnit(du, args = nil) ⇒ Object
30
31
32
33
34
|
# File 'lib/crokus/pretty_printer.rb', line 30
def visitDesignUnit du,args=nil
indent "DesignUnit"
du.list.each{|e| code << e.accept(self,:body)}
dedent
end
|
#visitDotted(dotted, args = nil) ⇒ Object
421
422
423
424
425
|
# File 'lib/crokus/pretty_printer.rb', line 421
def visitDotted dotted,args=nil
lhs=dotted.lhs.accept(self)
rhs=dotted.rhs.accept(self)
return "#{lhs}.#{rhs}"
end
|
#visitDoWhile(while_, args = nil) ⇒ Object
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/crokus/pretty_printer.rb', line 318
def visitDoWhile while_,args=nil
cond=while_.cond.accept(self)
code=Code.new
code << "do"
code.indent=2
code << while_.body.accept(self)
code.indent=0
code << "while #{cond};"
return code
end
|
#visitElse(else_, args = nil) ⇒ Object
266
267
268
269
270
271
272
273
|
# File 'lib/crokus/pretty_printer.rb', line 266
def visitElse else_,args=nil
code=Code.new
code << "else"
code.indent=2
code << else_.body.accept(self)
code.indent=0
return code
end
|
#visitFor(for_, args = nil) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/crokus/pretty_printer.rb', line 229
def visitFor for_,args=nil
code=Code.new
init=for_.init.collect do |stmt|
stmt_init=stmt.accept(self)
case stmt_init
when Code
stmt_init.finalize
else
stmt_init
end
end
init=init.join(";")
cond=for_.cond.accept(self)
incr=for_.increment.accept(self)
code << "for(#{init};#{cond};#{incr})"
code.indent=2
code << for_.body.accept(self)
code.indent=0
return code
end
|
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/crokus/pretty_printer.rb', line 166
def visitFormalArg formalArg,args=nil
tname=formalArg.type.accept(self)
array_size=""
t=formalArg.type
while t.is_a? ArrayOf
type=t.name.accept(self)
size=t.size.accept(self) if t.size
array_size+="[#{size}]"+array_size if tname.size
t=t.type
end
vname=formalArg.name.accept(self) if formalArg.name tname+=" " if formalArg.name
return "#{tname}#{vname}#{array_size}"
end
|
#visitFunCall(fcall, as_procedure = nil) ⇒ Object
220
221
222
223
224
225
226
227
|
# File 'lib/crokus/pretty_printer.rb', line 220
def visitFunCall fcall,as_procedure=nil
fname=fcall.name.accept(self)
argus=fcall.args.collect{|argu| argu.accept(self)}
argus=argus.join(',')
ret="#{fname}(#{argus})"
ret+=";" if as_procedure
ret
end
|
#visitFunction(func, args = nil) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/crokus/pretty_printer.rb', line 144
def visitFunction func,args=nil
code=Code.new
tname=func.type.accept(self)
fname=func.name.accept(self)
args=func.args.collect{|arg| arg.accept(self)}
args=args.join(",")
code << "\n#{tname} #{fname}(#{args})"
code.indent=2
code << func.body.accept(self)
code.indent=0
return code
end
|
#visitFunctionProto(func, args = nil) ⇒ Object
157
158
159
160
161
162
163
164
|
# File 'lib/crokus/pretty_printer.rb', line 157
def visitFunctionProto func,args=nil
tname=func.type.accept(self)
fname=func.name.accept(self)
args =func.args.collect{|arg| arg.accept(self)}
args=args.join(",")
code = "\n#{tname} #{fname}(#{args});"
return code
end
|
#visitGoto(goto, args = nil) ⇒ Object
343
344
345
346
|
# File 'lib/crokus/pretty_printer.rb', line 343
def visitGoto goto,args=nil
label=goto.label.accept(self)
return "goto #{label};"
end
|
#visitIdent(ident, args = nil) ⇒ Object
349
350
351
|
# File 'lib/crokus/pretty_printer.rb', line 349
def visitIdent ident,args=nil
return ident.to_s
end
|
#visitIf(if_, args = nil) ⇒ Object
255
256
257
258
259
260
261
262
263
264
|
# File 'lib/crokus/pretty_printer.rb', line 255
def visitIf if_,args=nil
cond=if_.cond.accept(self)
code=Code.new
code << "if (#{cond})"
code.indent=2
code << if_.body.accept(self)
code.indent=0
code << if_.else.accept(self,:body) if if_.else
return code
end
|
#visitInclude(include, args = nil) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/crokus/pretty_printer.rb', line 77
def visitInclude include,args=nil
name=include.name.accept(self)
case include.env
when :env
name="<#{name}>"
when :local
end
return "#include #{name}"
end
|
#visitIndexed(index, args = nil) ⇒ Object
401
402
403
404
405
|
# File 'lib/crokus/pretty_printer.rb', line 401
def visitIndexed index,args=nil
lhs=index.lhs.accept(self)
rhs=index.rhs.accept(self)
return "#{lhs}[#{rhs}]"
end
|
#visitIntLit(lit, args = nil) ⇒ Object
353
354
355
|
# File 'lib/crokus/pretty_printer.rb', line 353
def visitIntLit lit,args=nil
return lit.to_s
end
|
#visitITE(ite, args = nil) ⇒ Object
452
453
454
455
456
457
|
# File 'lib/crokus/pretty_printer.rb', line 452
def visitITE ite,args=nil
cond=ite.cond.accept(self)
label1=ite.trueBranch.label
label2=ite.falseBranch.label
"ite #{cond},#{label1},#{label2}"
end
|
#visitLabeledStmt(lstmt, args = nil) ⇒ Object
337
338
339
340
341
|
# File 'lib/crokus/pretty_printer.rb', line 337
def visitLabeledStmt lstmt,args=nil
label=lstmt.label.accept(self)
stmt =lstmt.stmt.accept(self)
ret="#{label} : #{stmt.to_s}"
end
|
#visitParenth(par, args = nil) ⇒ Object
390
391
392
393
|
# File 'lib/crokus/pretty_printer.rb', line 390
def visitParenth par,args=nil
e=par.expr.accept(self)
return "(#{e})"
end
|
#visitPointerTo(pto, args = nil) ⇒ Object
107
108
109
110
|
# File 'lib/crokus/pretty_printer.rb', line 107
def visitPointerTo pto,args=nil
tname=pto.type.accept(self)
return "#{tname} *"
end
|
#visitPostFixAccu(accu, args = nil) ⇒ Object
206
207
208
209
210
211
|
# File 'lib/crokus/pretty_printer.rb', line 206
def visitPostFixAccu accu,args=nil
lhs=accu.lhs.accept(self) if accu.lhs op =accu.op.accept(self)
ret="#{lhs}#{op}"
ret
end
|
#visitPreFixAccu(accu, args = nil) ⇒ Object
213
214
215
216
217
218
|
# File 'lib/crokus/pretty_printer.rb', line 213
def visitPreFixAccu accu,args=nil
lhs=accu.lhs.accept(self) if accu.lhs op =accu.op.accept(self)
ret="#{lhs}#{op}"
ret
end
|
#visitReturn(ret, args = nil) ⇒ Object
250
251
252
253
|
# File 'lib/crokus/pretty_printer.rb', line 250
def visitReturn ret,args=nil
e=ret.expr.accept(self) if ret.expr
return "return #{e};"
end
|
#visitSemicolonStmt(stmt, args = nil) ⇒ Object
190
191
192
193
|
# File 'lib/crokus/pretty_printer.rb', line 190
def visitSemicolonStmt stmt,args=nil
ret=";"
ret
end
|
#visitSizeof(sizeof, args = nil) ⇒ Object
427
428
429
430
|
# File 'lib/crokus/pretty_printer.rb', line 427
def visitSizeof sizeof,args=nil
tname=sizeof.type.accept(self)
return "sizeof(#{tname})"
end
|
#visitStrLit(lit, args = nil) ⇒ Object
357
358
359
|
# File 'lib/crokus/pretty_printer.rb', line 357
def visitStrLit lit,args=nil
return lit.to_s
end
|
#visitStruct(struct, args = nil) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/crokus/pretty_printer.rb', line 119
def visitStruct struct,args=nil
name=struct.name.accept(self) if struct.name
code=Code.new
code << "struct #{name} {"
code.indent=2
struct.decls.each do |decl|
code << decl.accept(self)
end
code.indent=0
code << "}"
return code.finalize
end
|
#visitSwitch(sw_, args = nil) ⇒ Object
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/crokus/pretty_printer.rb', line 275
def visitSwitch sw_,args=nil
e=sw_.expr.accept(self)
sw_.cases.each{|case_| case_.accept(self)}
code=Code.new
code << "switch(#{e}){"
code.indent=2
sw_.cases.each{|case_|
code << case_.accept(self)
}
code.indent=0
if sw_.default
code.indent=2
code << "default:"
code.indent=4
code << sw_.default.accept(self)
code.indent=0
end
code << "}"
return code
end
|
#visitToken(tok, args = nil) ⇒ Object
26
27
28
|
# File 'lib/crokus/pretty_printer.rb', line 26
def visitToken tok, args=nil
tok.to_s
end
|
#visitType(type, args = nil) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/crokus/pretty_printer.rb', line 99
def visitType type,args=nil
precisions=type.precisions.collect{|spec| spec.accept(self)}
precisions=precisions.join(" ")
precisions+=" " if precisions.size>0
name=type.name.accept(self)
return "#{precisions}#{name}"
end
|
#visitTypedef(typdef, args = nil) ⇒ Object
93
94
95
96
97
|
# File 'lib/crokus/pretty_printer.rb', line 93
def visitTypedef typdef,args=nil
type=typdef.type.accept(self)
name=typdef.name.accept(self)
return "typedef #{type} #{name};"
end
|
#visitUnary(unary, args = nil) ⇒ Object
377
378
379
380
381
|
# File 'lib/crokus/pretty_printer.rb', line 377
def visitUnary unary,args=nil
op=unary.op.accept(self)
e =unary.rhs.accept(self)
return unary.postfix ? "#{e}#{op}" : "#{op}#{e}"
end
|
#visitWhile(while_, args = nil) ⇒ Object
307
308
309
310
311
312
313
314
315
316
|
# File 'lib/crokus/pretty_printer.rb', line 307
def visitWhile while_,args=nil
cond=while_.cond.accept(self)
body=while_.body.accept(self)
code=Code.new
code << "while (#{cond})"
code.indent=2
code << body
code.indent=0
return code
end
|