File tree Expand file tree Collapse file tree 4 files changed +76
-4
lines changed Expand file tree Collapse file tree 4 files changed +76
-4
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,9 @@ Style/CaseLikeIf:
9090Style/ClassVars :
9191 Enabled : false
9292
93+ Style/CombinableLoops :
94+ Enabled : false
95+
9396Style/DocumentDynamicEvalDefinition :
9497 Enabled : false
9598
Original file line number Diff line number Diff line change @@ -134,12 +134,12 @@ def to_json(*opts)
134134 accept ( Visitor ::JSONVisitor . new ) . to_json ( *opts )
135135 end
136136
137- def construct_keys
138- PrettierPrint . format ( + "" ) { | q | accept ( Visitor ::MatchVisitor . new ( q ) ) }
137+ def to_mermaid
138+ accept ( Visitor ::MermaidVisitor . new )
139139 end
140140
141- def mermaid
142- accept ( Visitor ::MermaidVisitor . new )
141+ def construct_keys
142+ PrettierPrint . format ( + "" ) { | q | accept ( Visitor ::MatchVisitor . new ( q ) ) }
143143 end
144144 end
145145
Original file line number Diff line number Diff line change @@ -55,6 +55,40 @@ def disasm
5555 fmt . string
5656 end
5757
58+ def to_mermaid
59+ output = StringIO . new
60+ output . puts ( "flowchart TD" )
61+
62+ fmt = Disassembler ::Mermaid . new
63+ blocks . each do |block |
64+ output . puts ( " subgraph #{ block . id } " )
65+ previous = nil
66+
67+ block . each_with_length do |insn , length |
68+ node_id = "node_#{ length } "
69+ label = "%04d %s" % [ length , insn . disasm ( fmt ) ]
70+
71+ output . puts ( " #{ node_id } (\" #{ CGI . escapeHTML ( label ) } \" )" )
72+ output . puts ( " #{ previous } --> #{ node_id } " ) if previous
73+
74+ previous = node_id
75+ end
76+
77+ output . puts ( " end" )
78+ end
79+
80+ blocks . each do |block |
81+ block . outgoing_blocks . each do |outgoing |
82+ offset =
83+ block . block_start + block . insns . sum ( &:length ) -
84+ block . insns . last . length
85+ output . puts ( " node_#{ offset } --> node_#{ outgoing . block_start } " )
86+ end
87+ end
88+
89+ output . string
90+ end
91+
5892 # This method is used to verify that the control flow graph is well
5993 # formed. It does this by checking that each basic block is itself well
6094 # formed.
Original file line number Diff line number Diff line change 33module SyntaxTree
44 module YARV
55 class Disassembler
6+ # This class is another object that handles disassembling a YARV
7+ # instruction sequence but it does so in order to provide a label for a
8+ # mermaid diagram.
9+ class Mermaid
10+ def calldata ( value )
11+ value . inspect
12+ end
13+
14+ def enqueue ( iseq )
15+ end
16+
17+ def event ( name )
18+ end
19+
20+ def inline_storage ( cache )
21+ "<is:#{ cache } >"
22+ end
23+
24+ def instruction ( name , operands = [ ] )
25+ operands . empty? ? name : "#{ name } #{ operands . join ( ", " ) } "
26+ end
27+
28+ def label ( value )
29+ "%04d" % value . name [ "label_" . length ..]
30+ end
31+
32+ def local ( index , **)
33+ index . inspect
34+ end
35+
36+ def object ( value )
37+ value . inspect
38+ end
39+ end
40+
641 attr_reader :output , :queue
742
843 attr_reader :current_prefix
You can’t perform that action at this time.
0 commit comments