Class: Crokus::PrettyPrinter

Inherits:
Visitor
  • Object
show all
Includes:
Indent
Defined in:
lib/crokus/pretty_printer.rb

Constant Summary

Constants included from Indent

Indent::INDENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Indent

#dedent, #indent, #say

Methods inherited from Visitor

#visitFloatLit, #visitLabelledStmt, #visitPointed

Constructor Details

#initializePrettyPrinter

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

#codeObject

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



411
412
413
414
# File 'lib/crokus/pretty_printer.rb', line 411

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}" #[size] not returned!
end

#visitArrayOrStructInit(init, args = nil) ⇒ Object



402
403
404
405
406
407
408
409
# File 'lib/crokus/pretty_printer.rb', line 402

def visitArrayOrStructInit init,args=nil
  inits=init.elements.collect{|e| e.accept(self)}
  #handle imbrications
  #inits=inits.collect{|init| (init.is_a? Code) ? init.finalize : init}
  code=Code.new
  code << "{"+inits.join(",")+"}"
  return code.finalize
end

#visitArrow(arrow, args = nil) ⇒ Object



390
391
392
393
394
# File 'lib/crokus/pretty_printer.rb', line 390

def visitArrow arrow,args=nil
  lhs=arrow.lhs.accept(self)
  rhs=arrow.rhs.accept(self)
  return "#{lhs}->#{rhs}"
end

#visitAssign(assign, args = nil) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/crokus/pretty_printer.rb', line 190

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



360
361
362
363
364
365
366
367
368
369
370
# File 'lib/crokus/pretty_printer.rb', line 360

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



432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/crokus/pretty_printer.rb', line 432

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



324
325
326
# File 'lib/crokus/pretty_printer.rb', line 324

def visitBreak brk,args=nil
  return "break;"
end

#visitCase(case_, args = nil) ⇒ Object



292
293
294
295
296
297
298
299
300
# File 'lib/crokus/pretty_printer.rb', line 292

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



356
357
358
# File 'lib/crokus/pretty_printer.rb', line 356

def visitCharLit lit,args=nil
  return lit.to_s
end

#visitCommaStmt(comma, args = nil) ⇒ Object

.….……stmts.….….……



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



378
379
380
381
382
383
# File 'lib/crokus/pretty_printer.rb', line 378

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



328
329
330
# File 'lib/crokus/pretty_printer.rb', line 328

def visitContinue cont,args=nil
  return "continue;"
end

#visitDecl(decl, args = nil) ⇒ Object

WTF !?#



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

  # handle complex declaration that use structs
  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



427
428
429
430
# File 'lib/crokus/pretty_printer.rb', line 427

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



416
417
418
419
420
# File 'lib/crokus/pretty_printer.rb', line 416

def visitDotted dotted,args=nil
  lhs=dotted.lhs.accept(self)
  rhs=dotted.rhs.accept(self)
  return "#{lhs}.#{rhs}"
end

#visitDoWhile(while_, args = nil) ⇒ Object



313
314
315
316
317
318
319
320
321
322
# File 'lib/crokus/pretty_printer.rb', line 313

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



261
262
263
264
265
266
267
268
# File 'lib/crokus/pretty_printer.rb', line 261

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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/crokus/pretty_printer.rb', line 224

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

#visitFormalArg(formalArg, args = nil) ⇒ Object



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 # e.g : main(void)
  tname+=" " if formalArg.name
  return "#{tname}#{vname}#{array_size}"
end

#visitFunCall(fcall, as_procedure = nil) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/crokus/pretty_printer.rb', line 215

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

.….…. end of types.….…..



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



338
339
340
341
# File 'lib/crokus/pretty_printer.rb', line 338

def visitGoto goto,args=nil
  label=goto.label.accept(self)
  return "goto #{label};"
end

#visitIdent(ident, args = nil) ⇒ Object

.….…..expresions.….…..



344
345
346
# File 'lib/crokus/pretty_printer.rb', line 344

def visitIdent ident,args=nil
  return ident.to_s
end

#visitIf(if_, args = nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/crokus/pretty_printer.rb', line 250

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



396
397
398
399
400
# File 'lib/crokus/pretty_printer.rb', line 396

def visitIndexed index,args=nil
  lhs=index.lhs.accept(self)
  rhs=index.rhs.accept(self)
  return "#{lhs}[#{rhs}]"
end

#visitIntLit(lit, args = nil) ⇒ Object



348
349
350
# File 'lib/crokus/pretty_printer.rb', line 348

def visitIntLit lit,args=nil
  return lit.to_s
end

#visitITE(ite, args = nil) ⇒ Object

IR ============


447
448
449
450
451
452
# File 'lib/crokus/pretty_printer.rb', line 447

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



332
333
334
335
336
# File 'lib/crokus/pretty_printer.rb', line 332

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



385
386
387
388
# File 'lib/crokus/pretty_printer.rb', line 385

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



201
202
203
204
205
206
# File 'lib/crokus/pretty_printer.rb', line 201

def visitPostFixAccu accu,args=nil
  lhs=accu.lhs.accept(self) if accu.lhs #++i
  op =accu.op.accept(self)
  ret="#{lhs}#{op}"
  ret
end

#visitPreFixAccu(accu, args = nil) ⇒ Object



208
209
210
211
212
213
# File 'lib/crokus/pretty_printer.rb', line 208

def visitPreFixAccu accu,args=nil
  lhs=accu.lhs.accept(self) if accu.lhs #++i
  op =accu.op.accept(self)
  ret="#{lhs}#{op}"
  ret
end

#visitReturn(ret, args = nil) ⇒ Object



245
246
247
248
# File 'lib/crokus/pretty_printer.rb', line 245

def visitReturn ret,args=nil
  e=ret.expr.accept(self) if ret.expr
  return "return #{e};"
end

#visitSizeof(sizeof, args = nil) ⇒ Object



422
423
424
425
# File 'lib/crokus/pretty_printer.rb', line 422

def visitSizeof sizeof,args=nil
  tname=sizeof.type.accept(self)
  return "sizeof(#{tname})"
end

#visitStrLit(lit, args = nil) ⇒ Object



352
353
354
# File 'lib/crokus/pretty_printer.rb', line 352

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



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/crokus/pretty_printer.rb', line 270

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



372
373
374
375
376
# File 'lib/crokus/pretty_printer.rb', line 372

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



302
303
304
305
306
307
308
309
310
311
# File 'lib/crokus/pretty_printer.rb', line 302

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