Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/debug/RubyASTPrinter.java @ 13563:fb846424299f
Truffle/Ruby: extend Instrumentation framework with language-agnostic interfaces for access to AST printing utilities and a Ruby implementation
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Tue, 07 Jan 2014 18:09:42 -0800 |
parents | |
children | e076c87ab175 |
comparison
equal
deleted
inserted
replaced
13562:58ca96949f2e | 13563:fb846424299f |
---|---|
1 /* | |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. This | |
3 * code is released under a tri EPL/GPL/LGPL license. You can use it, | |
4 * redistribute it and/or modify it under the terms of the: | |
5 * | |
6 * Eclipse Public License version 1.0 | |
7 * GNU General Public License version 2 | |
8 * GNU Lesser General Public License version 2.1 | |
9 */ | |
10 package com.oracle.truffle.ruby.nodes.debug; | |
11 | |
12 import java.io.*; | |
13 import java.util.*; | |
14 | |
15 import com.oracle.truffle.api.*; | |
16 import com.oracle.truffle.api.nodes.*; | |
17 import com.oracle.truffle.api.nodes.NodeUtil.NodeClass; | |
18 import com.oracle.truffle.api.nodes.NodeUtil.NodeField; | |
19 import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind; | |
20 import com.oracle.truffle.api.nodes.instrument.*; | |
21 import com.oracle.truffle.ruby.nodes.*; | |
22 import com.oracle.truffle.ruby.nodes.call.*; | |
23 import com.oracle.truffle.ruby.nodes.literal.*; | |
24 import com.oracle.truffle.ruby.nodes.methods.*; | |
25 | |
26 /** | |
27 * Printers for Truffle-internal AST information. | |
28 */ | |
29 public final class RubyASTPrinter implements ASTPrinter { | |
30 | |
31 public RubyASTPrinter() { | |
32 } | |
33 | |
34 public void printTree(PrintWriter p, Node node, int maxDepth, Node markNode) { | |
35 printTree(p, node, maxDepth, markNode, 1); | |
36 p.println(); | |
37 p.flush(); | |
38 } | |
39 | |
40 public String printTreeToString(Node node, int maxDepth, Node markNode) { | |
41 StringWriter out = new StringWriter(); | |
42 printTree(new PrintWriter(out), node, maxDepth, markNode); | |
43 return out.toString(); | |
44 } | |
45 | |
46 public String printTreeToString(Node node, int maxDepth) { | |
47 return printTreeToString(node, maxDepth, null); | |
48 } | |
49 | |
50 private static void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, int level) { | |
51 if (node == null) { | |
52 p.print("null"); | |
53 return; | |
54 } | |
55 | |
56 p.print(nodeName(node)); | |
57 | |
58 String sep = ""; | |
59 p.print("("); | |
60 | |
61 final SourceSection src = node.getSourceSection(); | |
62 if (src != null) { | |
63 if (!(src instanceof NullSourceSection)) { | |
64 p.print(src.getSource().getName() + ":" + src.getStartLine()); | |
65 } else if (src instanceof CoreSourceSection) { | |
66 final CoreSourceSection coreSection = (CoreSourceSection) src; | |
67 p.print("core=\"" + (coreSection == null ? "?" : coreSection.toString()) + "\""); | |
68 } | |
69 } | |
70 if (node instanceof PhylumMarked) { | |
71 final PhylumMarked markedNode = (PhylumMarked) node; | |
72 String prefix = ""; | |
73 for (NodePhylum phylum : markedNode.getPhylumMarks()) { | |
74 p.print(prefix); | |
75 prefix = ","; | |
76 p.print(phylum.toString()); | |
77 } | |
78 | |
79 } | |
80 | |
81 ArrayList<NodeField> childFields = new ArrayList<>(); | |
82 | |
83 for (NodeField field : NodeClass.get(node.getClass()).getFields()) { | |
84 if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) { | |
85 childFields.add(field); | |
86 } else if (field.getKind() == NodeFieldKind.DATA) { | |
87 // p.print(sep); | |
88 // sep = ", "; | |
89 // | |
90 // final String fieldName = field.getName(); | |
91 // switch (fieldName) { | |
92 // | |
93 // } | |
94 // p.print(fieldName); | |
95 // p.print(" = "); | |
96 // p.print(field.loadValue(node)); | |
97 } | |
98 } | |
99 p.print(")"); | |
100 | |
101 if (level <= maxDepth) { | |
102 | |
103 if (childFields.size() != 0) { | |
104 p.print(" {"); | |
105 for (NodeField field : childFields) { | |
106 | |
107 Object value = field.loadValue(node); | |
108 if (value == null) { | |
109 printNewLine(p, level); | |
110 p.print(field.getName()); | |
111 p.print(" = null "); | |
112 } else if (field.getKind() == NodeFieldKind.CHILD) { | |
113 final Node valueNode = (Node) value; | |
114 printNewLine(p, level, valueNode == markNode); | |
115 p.print(field.getName()); | |
116 p.print(" = "); | |
117 printTree(p, valueNode, maxDepth, markNode, level + 1); | |
118 } else if (field.getKind() == NodeFieldKind.CHILDREN) { | |
119 printNewLine(p, level); | |
120 p.print(field.getName()); | |
121 Node[] children = (Node[]) value; | |
122 p.print(" = ["); | |
123 sep = ""; | |
124 for (Node child : children) { | |
125 p.print(sep); | |
126 sep = ", "; | |
127 printTree(p, child, maxDepth, markNode, level + 1); | |
128 } | |
129 p.print("]"); | |
130 } else { | |
131 printNewLine(p, level); | |
132 p.print(field.getName()); | |
133 } | |
134 } | |
135 printNewLine(p, level - 1); | |
136 p.print("}"); | |
137 } | |
138 } | |
139 } | |
140 | |
141 private static void printNewLine(PrintWriter p, int level, boolean mark) { | |
142 p.println(); | |
143 for (int i = 0; i < level; i++) { | |
144 if (mark && i == 0) { | |
145 p.print(" -->"); | |
146 } else { | |
147 p.print(" "); | |
148 } | |
149 } | |
150 } | |
151 | |
152 private static void printNewLine(PrintWriter p, int level) { | |
153 printNewLine(p, level, false); | |
154 } | |
155 | |
156 private static String nodeName(Node node) { | |
157 String nodeVal = null; | |
158 if (node instanceof CallNode) { | |
159 final CallNode callNode = (CallNode) node; | |
160 nodeVal = callNode.getName(); | |
161 | |
162 } else if (node instanceof FixnumLiteralNode) { | |
163 final FixnumLiteralNode fixnum = (FixnumLiteralNode) node; | |
164 nodeVal = Integer.toString(fixnum.getValue()); | |
165 } else if (node instanceof MethodDefinitionNode) { | |
166 final MethodDefinitionNode defNode = (MethodDefinitionNode) node; | |
167 nodeVal = defNode.getName(); | |
168 } | |
169 String result = node.getClass().getSimpleName(); | |
170 if (nodeVal != null) { | |
171 result = result + "[\"" + nodeVal + "\"]"; | |
172 } | |
173 return result; | |
174 } | |
175 | |
176 } |