Skip to content

Visualize to_son (sea of nodes) using mermaid.js already added #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h1>Syntax Tree</h1>
<option value="prettyPrint">AST</option>
<option value="disasm">ISEQ</option>
<option value="mermaid">GRAPH</option>
<option value="seaOfNodes">SEA OF NODES</option>
</select>
</div>
</nav>
Expand Down
23 changes: 17 additions & 6 deletions src/createRuby.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ export default async function createRuby() {

return vm.eval(rubySource).toString();
},
mermaid(source) {
const jsonSource = JSON.stringify(JSON.stringify(source));
const rubySource = `SyntaxTree.parse(JSON.parse(${jsonSource})).to_mermaid`;

return vm.eval(rubySource).toString();
},
// A function that calls through to the SyntaxTree.format function to get
// the pretty-printed version of the source.
format(source) {
Expand All @@ -39,12 +33,29 @@ export default async function createRuby() {

return vm.eval(rubySource).toString();
},
mermaid(source) {
const jsonSource = JSON.stringify(JSON.stringify(source));
const rubySource = `SyntaxTree.parse(JSON.parse(${jsonSource})).to_mermaid`;

return vm.eval(rubySource).toString();
},
// A function that calls through to PP to get the pretty-printed version of
// the syntax tree.
prettyPrint(source) {
const jsonSource = JSON.stringify(JSON.stringify(source));
const rubySource = `PP.pp(SyntaxTree.parse(JSON.parse(${jsonSource})), +"", 80)`;

return vm.eval(rubySource).toString();
},
// A function to print the current sea of nodes
seaOfNodes(source) {
const jsonSource = JSON.stringify(JSON.stringify(source));
const rubySource = `
iseq = RubyVM::InstructionSequence.compile(JSON.parse(${jsonSource}))
iseq = SyntaxTree::YARV::InstructionSequence.from(iseq.to_a)
iseq.to_son.to_mermaid
`;

return vm.eval(rubySource).toString();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Promise.all([
try {
let source = displayFunction(editor.getValue());

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