Class: RubyRTL::ASTPrinter
- Inherits:
-
Object
- Object
- RubyRTL::ASTPrinter
- Defined in:
- lib/ruby_rtl/ast_printer.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#nodes_cnx ⇒ Object
Returns the value of attribute nodes_cnx.
-
#nodes_decl ⇒ Object
Returns the value of attribute nodes_decl.
Instance Method Summary collapse
-
#clean(code) ⇒ Object
suppress syndrom : name=“”not correct“”.
- #process(node, level = 1) ⇒ Object
- #run(ast, suffix = nil) ⇒ Object
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
5 6 7 |
# File 'lib/ruby_rtl/ast_printer.rb', line 5 def code @code end |
#nodes_cnx ⇒ Object
Returns the value of attribute nodes_cnx.
5 6 7 |
# File 'lib/ruby_rtl/ast_printer.rb', line 5 def nodes_cnx @nodes_cnx end |
#nodes_decl ⇒ Object
Returns the value of attribute nodes_decl.
5 6 7 |
# File 'lib/ruby_rtl/ast_printer.rb', line 5 def nodes_decl @nodes_decl end |
Instance Method Details
#clean(code) ⇒ Object
suppress syndrom : name=“”not correct“”
76 77 78 79 80 81 82 83 |
# File 'lib/ruby_rtl/ast_printer.rb', line 76 def clean code #puts "=> cleaning dot code" code.lines.each_with_index do |line,idx| if line=~/\"\"(.*)\"\"/ code.lines[idx].gsub!(/\"\"(.*)\"\"/,"\"#{$1}\"") end end end |
#process(node, level = 1) ⇒ Object
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 |
# File 'lib/ruby_rtl/ast_printer.rb', line 34 def process node,level=1 kname=node.class.name.split("::")[1] || "#{node.class}" id=node.object_id indent=" "*level #puts indent+"processing #{node}" (nodes_decl << "#{id} [label=\"#{kname}\"]") unless @visited.include?(id) node.instance_variables.each{|vname| #puts indent+"-"+vname.to_s ivar=node.instance_variable_get(vname) vname=vname.to_s[1..-1] if ivar case ivar when Array ivar.each_with_index{|e,idx| sink=process(e,level+1) @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}[#{idx}]\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink } when Symbol,Integer,String val=ivar.to_s sink="#{ivar.object_id}" nodes_decl << "#{sink} [label=\"#{val}\",color=\"red\"]" @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink else sink=process(ivar,level+1) @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink end end } end @visited << id return id end |
#run(ast, suffix = nil) ⇒ Object
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 |
# File 'lib/ruby_rtl/ast_printer.rb', line 7 def run ast,suffix=nil puts "[+] printing ast in dot file" @verbose=false @nodes_decl=Code.new @nodes_cnx=Code.new @printed_cnx={} #Cosmetic ! to keep track of already printed cnx source->sink @code=Code.new code << "digraph G {" code.indent=2 code << "ordering=out;" code << "ranksep=.4;" code << "bgcolor=\"lightgrey\";" code.newline code << "node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"" code << " width=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];" code << "edge [arrowsize=.5, color=\"black\", style=\"bold\"]" @visited=[] process(ast) code << @nodes_decl code << @nodes_cnx code.indent=0 code << "}" clean(code) dot_filename="#{ast.name}_ast#{suffix}.dot" File.open(dot_filename,'w'){|f| f.puts code.finalize} end |