class Crokus::PrettyPrinter

Attributes

code[RW]

Public Class Methods

new() click to toggle source
# File lib/crokus/pretty_printer.rb, line 13
def initialize
  @ind=-2
  @verbose=false
end

Public Instance Methods

visit(ast) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/crokus/pretty_printer.rb, line 324
def visitBreak brk,args=nil
  return "break;"
end
visitCase(case_,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/crokus/pretty_printer.rb, line 356
def visitCharLit lit,args=nil
  return lit.to_s
end
visitCommaStmt(comma,args=nil) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# File lib/crokus/pretty_printer.rb, line 328
def visitContinue cont,args=nil
  return "continue;"
end
visitDecl(decl,args=nil) click to toggle source

WTF !?#

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
visitDoWhile(while_,args=nil) click to toggle source
# 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
visitDotted(dotted,args=nil) click to toggle source
# 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
visitElse(else_,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# File lib/crokus/pretty_printer.rb, line 338
def visitGoto goto,args=nil
  label=goto.label.accept(self)
  return "goto #{label};"
end
visitITE(ite,args=nil) click to toggle source
IR ============
# 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
visitIdent(ident,args=nil) click to toggle source

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

# File lib/crokus/pretty_printer.rb, line 344
def visitIdent ident,args=nil
  return ident.to_s
end
visitIf(if_,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/crokus/pretty_printer.rb, line 348
def visitIntLit lit,args=nil
  return lit.to_s
end
visitLabeledStmt(lstmt,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/crokus/pretty_printer.rb, line 352
def visitStrLit lit,args=nil
  return lit.to_s
end
visitStruct(struct,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/crokus/pretty_printer.rb, line 26
def visitToken tok, args=nil
  tok.to_s
end
visitType(type,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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