Class: Astrapi::DotPrinter

Inherits:
Object
  • Object
show all
Includes:
Indent
Defined in:
lib/dot_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

Instance Attribute Details

#codeObject

Returns the value of attribute code.



11
12
13
# File 'lib/dot_printer.rb', line 11

def code
  @code
end

#nodes_cnxObject

Returns the value of attribute nodes_cnx.



11
12
13
# File 'lib/dot_printer.rb', line 11

def nodes_cnx
  @nodes_cnx
end

#nodes_declObject

Returns the value of attribute nodes_decl.



11
12
13
# File 'lib/dot_printer.rb', line 11

def nodes_decl
  @nodes_decl
end

Instance Method Details

entry method



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dot_printer.rb', line 13

def print ast #entry method
  @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\"]"
  process(ast)
  code << @nodes_decl
  code << @nodes_cnx
  code.indent=0
  code << "}"
  return code
end

#process(node, level = 0) ⇒ Object



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
# File 'lib/dot_printer.rb', line 36

def process node,level=0

  kname=node.class.name.split("::")[1]
  id=node.object_id
  nodes_decl << "#{id} [label=\"#{kname}\"]"

  node.instance_variables.each{|vname|
    ivar=node.instance_variable_get(vname)
    vname=vname.to_s[1..-1]
    case ivar
    when Array
      ivar.each_with_index{|e,idx|
        sink=process(e,level+2)
        @printed_cnx[id]||=[]
        nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}[#{idx}]\"]" if not @printed_cnx[id].include? sink
        @printed_cnx[id] << sink
      }
    when Token
      val=ivar.val
      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+2)
      @printed_cnx[id]||=[]
      nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}\"]" if not @printed_cnx[id].include? sink
      @printed_cnx[id] << sink
    end

  }
  return id
end