Skip to content

Commit 90bba8d

Browse files
authored
Merge pull request #9 from wenderjean/feature/visualize-sea-of-nodes-with-mermaid-js
Visualize to_son (sea of nodes) using mermaid.js already added
2 parents da5f1f3 + 62ba5d9 commit 90bba8d

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

docs/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ <h1>Syntax Tree</h1>
2020
<option value="prettyPrint">AST</option>
2121
<option value="disasm">ISEQ</option>
2222
<option value="mermaid">GRAPH</option>
23+
<option value="seaOfNodes">SEA OF NODES</option>
2324
</select>
2425
</div>
2526
</nav>

src/createRuby.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ export default async function createRuby() {
2525

2626
return vm.eval(rubySource).toString();
2727
},
28-
mermaid(source) {
29-
const jsonSource = JSON.stringify(JSON.stringify(source));
30-
const rubySource = `SyntaxTree.parse(JSON.parse(${jsonSource})).to_mermaid`;
31-
32-
return vm.eval(rubySource).toString();
33-
},
3428
// A function that calls through to the SyntaxTree.format function to get
3529
// the pretty-printed version of the source.
3630
format(source) {
@@ -39,12 +33,29 @@ export default async function createRuby() {
3933

4034
return vm.eval(rubySource).toString();
4135
},
36+
mermaid(source) {
37+
const jsonSource = JSON.stringify(JSON.stringify(source));
38+
const rubySource = `SyntaxTree.parse(JSON.parse(${jsonSource})).to_mermaid`;
39+
40+
return vm.eval(rubySource).toString();
41+
},
4242
// A function that calls through to PP to get the pretty-printed version of
4343
// the syntax tree.
4444
prettyPrint(source) {
4545
const jsonSource = JSON.stringify(JSON.stringify(source));
4646
const rubySource = `PP.pp(SyntaxTree.parse(JSON.parse(${jsonSource})), +"", 80)`;
4747

48+
return vm.eval(rubySource).toString();
49+
},
50+
// A function to print the current sea of nodes
51+
seaOfNodes(source) {
52+
const jsonSource = JSON.stringify(JSON.stringify(source));
53+
const rubySource = `
54+
iseq = RubyVM::InstructionSequence.compile(JSON.parse(${jsonSource}))
55+
iseq = SyntaxTree::YARV::InstructionSequence.from(iseq.to_a)
56+
iseq.to_son.to_mermaid
57+
`;
58+
4859
return vm.eval(rubySource).toString();
4960
}
5061
};

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Promise.all([
5050
try {
5151
let source = displayFunction(editor.getValue());
5252

53-
if (event.detail.kind === "mermaid") {
53+
if (event.detail.kind === "mermaid" || event.detail.kind === "seaOfNodes") {
5454
output.setAttribute("style", "display: none;");
5555
graph.setAttribute("style", "text-align: left;")
5656
graph.innerHTML = "Loading..."

0 commit comments

Comments
 (0)