Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.ruby.parser/src/com/oracle/truffle/ruby/parser/Translator.java @ 13857:a9604b40f5e7
On HotSpot, debug_id should be an int, not a short
author | Gilles Duboscq <duboscq@ssw.jku.at> |
---|---|
date | Mon, 03 Feb 2014 14:47:11 +0100 |
parents | 2c1c805153e6 |
children | 22bf5a8ba9eb 3f27e57439ed |
rev | line source |
---|---|
13514 | 1 /* |
13569
1894412de0ed
Ruby: major upgrade in debugging support, mainly for navigation: step, next (passing over calls), return (from enclosing function), etc. Also a few bug fixes.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13514
diff
changeset
|
2 * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This |
13514 | 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.parser; | |
11 | |
12 import java.math.*; | |
13 import java.util.*; | |
14 import java.util.regex.*; | |
15 | |
16 import com.oracle.truffle.api.*; | |
17 import com.oracle.truffle.api.frame.*; | |
18 import com.oracle.truffle.api.impl.*; | |
19 import com.oracle.truffle.ruby.nodes.*; | |
20 import com.oracle.truffle.ruby.nodes.call.*; | |
21 import com.oracle.truffle.ruby.nodes.cast.*; | |
22 import com.oracle.truffle.ruby.nodes.constants.*; | |
23 import com.oracle.truffle.ruby.nodes.control.*; | |
24 import com.oracle.truffle.ruby.nodes.core.*; | |
25 import com.oracle.truffle.ruby.nodes.literal.*; | |
26 import com.oracle.truffle.ruby.nodes.literal.array.*; | |
27 import com.oracle.truffle.ruby.nodes.methods.*; | |
28 import com.oracle.truffle.ruby.nodes.methods.locals.*; | |
29 import com.oracle.truffle.ruby.nodes.objects.*; | |
30 import com.oracle.truffle.ruby.nodes.objects.instancevariables.*; | |
31 import com.oracle.truffle.ruby.nodes.yield.*; | |
32 import com.oracle.truffle.ruby.runtime.*; | |
33 import com.oracle.truffle.ruby.runtime.core.*; | |
34 import com.oracle.truffle.ruby.runtime.core.range.*; | |
35 import com.oracle.truffle.ruby.runtime.methods.*; | |
36 | |
37 /** | |
38 * A JRuby parser node visitor which translates JRuby AST nodes into our Ruby nodes, implementing a | |
39 * Ruby parser. Therefore there is some namespace contention here! We make all references to JRuby | |
40 * explicit. This is the only place though - it doesn't leak out elsewhere. | |
41 */ | |
42 public class Translator implements org.jrubyparser.NodeVisitor { | |
43 | |
44 protected final Translator parent; | |
45 | |
46 protected final RubyContext context; | |
47 protected final TranslatorEnvironment environment; | |
48 protected final Source source; | |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
49 protected final RubyNodeInstrumenter instrumenter; |
13514 | 50 |
51 private boolean translatingForStatement = false; | |
52 | |
53 private static final Map<Class, String> nodeDefinedNames = new HashMap<>(); | |
54 | |
55 static { | |
56 nodeDefinedNames.put(org.jrubyparser.ast.SelfNode.class, "self"); | |
57 nodeDefinedNames.put(org.jrubyparser.ast.NilNode.class, "nil"); | |
58 nodeDefinedNames.put(org.jrubyparser.ast.TrueNode.class, "true"); | |
59 nodeDefinedNames.put(org.jrubyparser.ast.FalseNode.class, "false"); | |
60 nodeDefinedNames.put(org.jrubyparser.ast.LocalAsgnNode.class, "assignment"); | |
61 nodeDefinedNames.put(org.jrubyparser.ast.DAsgnNode.class, "assignment"); | |
62 nodeDefinedNames.put(org.jrubyparser.ast.GlobalAsgnNode.class, "assignment"); | |
63 nodeDefinedNames.put(org.jrubyparser.ast.InstAsgnNode.class, "assignment"); | |
64 nodeDefinedNames.put(org.jrubyparser.ast.ClassVarAsgnNode.class, "assignment"); | |
65 nodeDefinedNames.put(org.jrubyparser.ast.OpAsgnAndNode.class, "assignment"); | |
66 nodeDefinedNames.put(org.jrubyparser.ast.OpAsgnOrNode.class, "assignment"); | |
67 nodeDefinedNames.put(org.jrubyparser.ast.OpAsgnNode.class, "assignment"); | |
68 nodeDefinedNames.put(org.jrubyparser.ast.OpElementAsgnNode.class, "assignment"); | |
69 nodeDefinedNames.put(org.jrubyparser.ast.MultipleAsgnNode.class, "assignment"); | |
70 nodeDefinedNames.put(org.jrubyparser.ast.GlobalVarNode.class, "global-variable"); | |
71 nodeDefinedNames.put(org.jrubyparser.ast.StrNode.class, "expression"); | |
72 nodeDefinedNames.put(org.jrubyparser.ast.DStrNode.class, "expression"); | |
73 nodeDefinedNames.put(org.jrubyparser.ast.FixnumNode.class, "expression"); | |
74 nodeDefinedNames.put(org.jrubyparser.ast.BignumNode.class, "expression"); | |
75 nodeDefinedNames.put(org.jrubyparser.ast.FloatNode.class, "expression"); | |
76 nodeDefinedNames.put(org.jrubyparser.ast.RegexpNode.class, "expression"); | |
77 nodeDefinedNames.put(org.jrubyparser.ast.DRegexpNode.class, "expression"); | |
78 nodeDefinedNames.put(org.jrubyparser.ast.ArrayNode.class, "expression"); | |
79 nodeDefinedNames.put(org.jrubyparser.ast.HashNode.class, "expression"); | |
80 nodeDefinedNames.put(org.jrubyparser.ast.SymbolNode.class, "expression"); | |
81 nodeDefinedNames.put(org.jrubyparser.ast.DotNode.class, "expression"); | |
82 nodeDefinedNames.put(org.jrubyparser.ast.NotNode.class, "expression"); | |
83 nodeDefinedNames.put(org.jrubyparser.ast.AndNode.class, "expression"); | |
84 nodeDefinedNames.put(org.jrubyparser.ast.OrNode.class, "expression"); | |
85 nodeDefinedNames.put(org.jrubyparser.ast.LocalVarNode.class, "local-variable"); | |
86 nodeDefinedNames.put(org.jrubyparser.ast.DVarNode.class, "local-variable"); | |
87 } | |
88 | |
89 /** | |
90 * Global variables which in common usage have frame local semantics. | |
91 */ | |
92 public static final Set<String> FRAME_LOCAL_GLOBAL_VARIABLES = new HashSet<>(Arrays.asList("$_")); | |
93 | |
94 public Translator(RubyContext context, Translator parent, TranslatorEnvironment environment, Source source) { | |
95 this.context = context; | |
96 this.parent = parent; | |
97 this.environment = environment; | |
98 this.source = source; | |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
99 this.instrumenter = environment.getNodeInstrumenter(); |
13514 | 100 } |
101 | |
102 @Override | |
103 public Object visitAliasNode(org.jrubyparser.ast.AliasNode node) { | |
104 final SourceSection sourceSection = translate(node.getPosition()); | |
105 | |
106 final org.jrubyparser.ast.LiteralNode oldName = (org.jrubyparser.ast.LiteralNode) node.getOldName(); | |
107 final org.jrubyparser.ast.LiteralNode newName = (org.jrubyparser.ast.LiteralNode) node.getNewName(); | |
108 | |
109 final ClassNode classNode = new ClassNode(context, sourceSection, new SelfNode(context, sourceSection)); | |
110 | |
111 return new AliasNode(context, sourceSection, classNode, newName.getName(), oldName.getName()); | |
112 } | |
113 | |
114 @Override | |
115 public Object visitAndNode(org.jrubyparser.ast.AndNode node) { | |
116 final SourceSection sourceSection = translate(node.getPosition()); | |
117 | |
118 RubyNode x; | |
119 | |
120 if (node.getFirst() == null) { | |
121 x = new NilNode(context, sourceSection); | |
122 } else { | |
123 x = (RubyNode) node.getFirst().accept(this); | |
124 } | |
125 | |
126 RubyNode y; | |
127 | |
128 if (node.getSecond() == null) { | |
129 y = new NilNode(context, sourceSection); | |
130 } else { | |
131 y = (RubyNode) node.getSecond().accept(this); | |
132 } | |
133 | |
134 return AndNodeFactory.create(context, sourceSection, x, y); | |
135 } | |
136 | |
137 @Override | |
138 public Object visitArgsCatNode(org.jrubyparser.ast.ArgsCatNode node) { | |
139 final List<org.jrubyparser.ast.Node> nodes = new ArrayList<>(); | |
140 collectArgsCatNodes(nodes, node); | |
141 | |
142 final List<RubyNode> translatedNodes = new ArrayList<>(); | |
143 | |
144 for (org.jrubyparser.ast.Node catNode : nodes) { | |
145 translatedNodes.add((RubyNode) catNode.accept(this)); | |
146 } | |
147 | |
148 return new ArrayConcatNode(context, translate(node.getPosition()), translatedNodes.toArray(new RubyNode[translatedNodes.size()])); | |
149 } | |
150 | |
151 // ArgsCatNodes can be nested - this collects them into a flat list of children | |
152 private void collectArgsCatNodes(List<org.jrubyparser.ast.Node> nodes, org.jrubyparser.ast.ArgsCatNode node) { | |
153 if (node.getFirst() instanceof org.jrubyparser.ast.ArgsCatNode) { | |
154 collectArgsCatNodes(nodes, (org.jrubyparser.ast.ArgsCatNode) node.getFirst()); | |
155 } else { | |
156 nodes.add(node.getFirst()); | |
157 } | |
158 | |
159 if (node.getSecond() instanceof org.jrubyparser.ast.ArgsCatNode) { | |
160 collectArgsCatNodes(nodes, (org.jrubyparser.ast.ArgsCatNode) node.getSecond()); | |
161 } else { | |
162 nodes.add(node.getSecond()); | |
163 } | |
164 } | |
165 | |
166 @Override | |
167 public Object visitArgsNode(org.jrubyparser.ast.ArgsNode node) { | |
168 return unimplemented(node); | |
169 } | |
170 | |
171 @Override | |
172 public Object visitArgsPushNode(org.jrubyparser.ast.ArgsPushNode node) { | |
173 return new ArrayPushNode(context, translate(node.getPosition()), (RubyNode) node.getFirstNode().accept(this), (RubyNode) node.getSecondNode().accept(this)); | |
174 } | |
175 | |
176 @Override | |
177 public Object visitArrayNode(org.jrubyparser.ast.ArrayNode node) { | |
178 final List<org.jrubyparser.ast.Node> values = node.childNodes(); | |
179 | |
180 final RubyNode[] translatedValues = new RubyNode[values.size()]; | |
181 | |
182 for (int n = 0; n < values.size(); n++) { | |
183 translatedValues[n] = (RubyNode) values.get(n).accept(this); | |
184 } | |
185 | |
186 return new UninitialisedArrayLiteralNode(context, translate(node.getPosition()), translatedValues); | |
187 } | |
188 | |
189 @Override | |
190 public Object visitAttrAssignNode(org.jrubyparser.ast.AttrAssignNode node) { | |
191 return visitAttrAssignNodeExtraArgument(node, null); | |
192 } | |
193 | |
194 /** | |
195 * See translateDummyAssignment to understand what this is for. | |
196 */ | |
197 public RubyNode visitAttrAssignNodeExtraArgument(org.jrubyparser.ast.AttrAssignNode node, RubyNode extraArgument) { | |
198 final org.jrubyparser.ast.CallNode callNode = new org.jrubyparser.ast.CallNode(node.getPosition(), node.getReceiver(), node.getName(), node.getArgs()); | |
199 return visitCallNodeExtraArgument(callNode, extraArgument); | |
200 } | |
201 | |
202 @Override | |
203 public Object visitBackRefNode(org.jrubyparser.ast.BackRefNode node) { | |
204 return unimplemented(node); | |
205 } | |
206 | |
207 @Override | |
208 public Object visitBeginNode(org.jrubyparser.ast.BeginNode node) { | |
209 return node.getBody().accept(this); | |
210 } | |
211 | |
212 @Override | |
213 public Object visitBignumNode(org.jrubyparser.ast.BignumNode node) { | |
214 return new BignumLiteralNode(context, translate(node.getPosition()), node.getValue()); | |
215 } | |
216 | |
217 @Override | |
218 public Object visitBlockArg18Node(org.jrubyparser.ast.BlockArg18Node node) { | |
219 return unimplemented(node); | |
220 } | |
221 | |
222 @Override | |
223 public Object visitBlockArgNode(org.jrubyparser.ast.BlockArgNode node) { | |
224 return unimplemented(node); | |
225 } | |
226 | |
227 @Override | |
228 public Object visitBlockNode(org.jrubyparser.ast.BlockNode node) { | |
229 final List<org.jrubyparser.ast.Node> children = node.childNodes(); | |
230 | |
231 final List<RubyNode> translatedChildren = new ArrayList<>(); | |
232 | |
233 for (int n = 0; n < children.size(); n++) { | |
234 final RubyNode translatedChild = (RubyNode) children.get(n).accept(this); | |
235 | |
236 if (!(translatedChild instanceof DeadNode)) { | |
237 translatedChildren.add(translatedChild); | |
238 } | |
239 } | |
240 | |
241 if (translatedChildren.size() == 1) { | |
242 return translatedChildren.get(0); | |
243 } else { | |
244 return new SequenceNode(context, translate(node.getPosition()), translatedChildren.toArray(new RubyNode[translatedChildren.size()])); | |
245 } | |
246 } | |
247 | |
248 @Override | |
249 public Object visitBlockPassNode(org.jrubyparser.ast.BlockPassNode node) { | |
250 return unimplemented(node); | |
251 } | |
252 | |
253 @Override | |
254 public Object visitBreakNode(org.jrubyparser.ast.BreakNode node) { | |
255 final SourceSection sourceSection = translate(node.getPosition()); | |
256 | |
257 RubyNode resultNode; | |
258 | |
259 if (node.getValueNode() == null) { | |
260 resultNode = new NilNode(context, sourceSection); | |
261 } else { | |
262 resultNode = (RubyNode) node.getValueNode().accept(this); | |
263 } | |
264 | |
265 return new BreakNode(context, sourceSection, resultNode); | |
266 } | |
267 | |
268 @Override | |
269 public Object visitCallNode(org.jrubyparser.ast.CallNode node) { | |
270 return visitCallNodeExtraArgument(node, null); | |
271 } | |
272 | |
273 /** | |
274 * See translateDummyAssignment to understand what this is for. | |
275 */ | |
276 public RubyNode visitCallNodeExtraArgument(org.jrubyparser.ast.CallNode node, RubyNode extraArgument) { | |
277 final SourceSection sourceSection = translate(node.getPosition()); | |
278 | |
279 final RubyNode receiverTranslated = (RubyNode) node.getReceiver().accept(this); | |
280 | |
281 org.jrubyparser.ast.Node args = node.getArgs(); | |
282 org.jrubyparser.ast.Node block = node.getIter(); | |
283 | |
284 if (block == null && args instanceof org.jrubyparser.ast.IterNode) { | |
285 final org.jrubyparser.ast.Node temp = args; | |
286 args = block; | |
287 block = temp; | |
288 } | |
289 | |
290 final ArgumentsAndBlockTranslation argumentsAndBlock = translateArgumentsAndBlock(sourceSection, block, args, extraArgument); | |
291 | |
13569
1894412de0ed
Ruby: major upgrade in debugging support, mainly for navigation: step, next (passing over calls), return (from enclosing function), etc. Also a few bug fixes.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13514
diff
changeset
|
292 RubyNode translated = new CallNode(context, sourceSection, node.getName(), receiverTranslated, argumentsAndBlock.getBlock(), argumentsAndBlock.isSplatted(), argumentsAndBlock.getArguments()); |
1894412de0ed
Ruby: major upgrade in debugging support, mainly for navigation: step, next (passing over calls), return (from enclosing function), etc. Also a few bug fixes.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13514
diff
changeset
|
293 |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
294 return instrumenter.instrumentAsCall(translated, node.getName()); |
13514 | 295 } |
296 | |
297 protected class ArgumentsAndBlockTranslation { | |
298 | |
299 private final RubyNode block; | |
300 private final RubyNode[] arguments; | |
301 private final boolean isSplatted; | |
302 | |
303 public ArgumentsAndBlockTranslation(RubyNode block, RubyNode[] arguments, boolean isSplatted) { | |
304 super(); | |
305 this.block = block; | |
306 this.arguments = arguments; | |
307 this.isSplatted = isSplatted; | |
308 } | |
309 | |
310 public RubyNode getBlock() { | |
311 return block; | |
312 } | |
313 | |
314 public RubyNode[] getArguments() { | |
315 return arguments; | |
316 } | |
317 | |
318 public boolean isSplatted() { | |
319 return isSplatted; | |
320 } | |
321 | |
322 } | |
323 | |
324 protected ArgumentsAndBlockTranslation translateArgumentsAndBlock(SourceSection sourceSection, org.jrubyparser.ast.Node iterNode, org.jrubyparser.ast.Node argsNode, RubyNode extraArgument) { | |
325 assert !(argsNode instanceof org.jrubyparser.ast.IterNode); | |
326 | |
327 final List<org.jrubyparser.ast.Node> arguments = new ArrayList<>(); | |
328 org.jrubyparser.ast.Node blockPassNode = null; | |
329 | |
330 boolean isSplatted = false; | |
331 | |
332 if (argsNode instanceof org.jrubyparser.ast.ListNode) { | |
333 arguments.addAll(((org.jrubyparser.ast.ListNode) argsNode).childNodes()); | |
334 } else if (argsNode instanceof org.jrubyparser.ast.BlockPassNode) { | |
335 final org.jrubyparser.ast.BlockPassNode blockPass = (org.jrubyparser.ast.BlockPassNode) argsNode; | |
336 | |
337 final org.jrubyparser.ast.Node blockPassArgs = blockPass.getArgs(); | |
338 | |
339 if (blockPassArgs instanceof org.jrubyparser.ast.ListNode) { | |
340 arguments.addAll(((org.jrubyparser.ast.ListNode) blockPassArgs).childNodes()); | |
341 } else if (blockPassArgs instanceof org.jrubyparser.ast.ArgsCatNode) { | |
342 arguments.add(blockPassArgs); | |
343 } else if (blockPassArgs != null) { | |
344 throw new UnsupportedOperationException("Don't know how to block pass " + blockPassArgs); | |
345 } | |
346 | |
347 blockPassNode = blockPass.getBody(); | |
348 } else if (argsNode instanceof org.jrubyparser.ast.SplatNode) { | |
349 isSplatted = true; | |
350 arguments.add(argsNode); | |
351 } else if (argsNode instanceof org.jrubyparser.ast.ArgsCatNode) { | |
352 isSplatted = true; | |
353 arguments.add(argsNode); | |
354 } else if (argsNode != null) { | |
355 isSplatted = true; | |
356 arguments.add(argsNode); | |
357 } | |
358 | |
359 RubyNode blockTranslated; | |
360 | |
361 if (blockPassNode != null && iterNode != null) { | |
362 throw new UnsupportedOperationException("Don't know how to pass both an block and a block-pass argument"); | |
363 } else if (iterNode != null) { | |
364 blockTranslated = (BlockDefinitionNode) iterNode.accept(this); | |
365 } else if (blockPassNode != null) { | |
366 blockTranslated = ProcCastNodeFactory.create(context, sourceSection, (RubyNode) blockPassNode.accept(this)); | |
367 } else { | |
368 blockTranslated = null; | |
369 } | |
370 | |
371 final List<RubyNode> argumentsTranslated = new ArrayList<>(); | |
372 | |
373 for (org.jrubyparser.ast.Node argument : arguments) { | |
374 argumentsTranslated.add((RubyNode) argument.accept(this)); | |
375 } | |
376 | |
377 if (extraArgument != null) { | |
378 argumentsTranslated.add(extraArgument); | |
379 } | |
380 | |
381 final RubyNode[] argumentsTranslatedArray = argumentsTranslated.toArray(new RubyNode[argumentsTranslated.size()]); | |
382 | |
383 return new ArgumentsAndBlockTranslation(blockTranslated, argumentsTranslatedArray, isSplatted); | |
384 } | |
385 | |
386 @Override | |
387 public Object visitCaseNode(org.jrubyparser.ast.CaseNode node) { | |
388 final SourceSection sourceSection = translate(node.getPosition()); | |
389 | |
390 RubyNode elseNode; | |
391 | |
392 if (node.getElse() != null) { | |
393 elseNode = (RubyNode) node.getElse().accept(this); | |
394 } else { | |
395 elseNode = new NilNode(context, sourceSection); | |
396 } | |
397 | |
398 /* | |
399 * There are two sorts of case - one compares a list of expressions against a value, the | |
400 * other just checks a list of expressions for truth. | |
401 */ | |
402 | |
403 if (node.getCase() != null) { | |
404 // Evaluate the case expression and store it in a local | |
405 | |
406 final String tempName = environment.allocateLocalTemp(); | |
407 | |
408 final RubyNode readTemp = environment.findLocalVarNode(tempName, sourceSection); | |
409 | |
410 final RubyNode assignTemp = ((ReadNode) readTemp).makeWriteNode((RubyNode) node.getCase().accept(this)); | |
411 | |
412 /* | |
413 * Build an if expression from the whens and else. Work backwards because the first if | |
414 * contains all the others in its else clause. | |
415 */ | |
416 | |
417 for (int n = node.getCases().size() - 1; n >= 0; n--) { | |
418 final org.jrubyparser.ast.WhenNode when = (org.jrubyparser.ast.WhenNode) node.getCases().get(n); | |
419 | |
420 // Make a condition from the one or more expressions combined in an or expression | |
421 | |
422 final List<org.jrubyparser.ast.Node> expressions; | |
423 | |
424 if (when.getExpression() instanceof org.jrubyparser.ast.ListNode) { | |
425 expressions = ((org.jrubyparser.ast.ListNode) when.getExpression()).childNodes(); | |
426 } else { | |
427 expressions = Arrays.asList(when.getExpression()); | |
428 } | |
429 | |
430 final List<RubyNode> comparisons = new ArrayList<>(); | |
431 | |
432 for (org.jrubyparser.ast.Node expressionNode : expressions) { | |
433 final RubyNode rubyExpression = (RubyNode) expressionNode.accept(this); | |
434 | |
435 final CallNode comparison = new CallNode(context, sourceSection, "===", rubyExpression, null, false, new RubyNode[]{environment.findLocalVarNode(tempName, sourceSection)}); | |
436 | |
437 comparisons.add(comparison); | |
438 } | |
439 | |
440 RubyNode conditionNode = comparisons.get(comparisons.size() - 1); | |
441 | |
442 // As with the if nodes, we work backwards to make it left associative | |
443 | |
444 for (int i = comparisons.size() - 2; i >= 0; i--) { | |
445 conditionNode = OrNodeFactory.create(context, sourceSection, comparisons.get(i), conditionNode); | |
446 } | |
447 | |
448 // Create the if node | |
449 | |
450 final BooleanCastNode conditionCastNode = BooleanCastNodeFactory.create(context, sourceSection, conditionNode); | |
451 | |
452 RubyNode thenNode; | |
453 | |
454 if (when.getBody() == null) { | |
455 thenNode = new NilNode(context, sourceSection); | |
456 } else { | |
457 thenNode = (RubyNode) when.getBody().accept(this); | |
458 } | |
459 | |
460 final IfNode ifNode = new IfNode(context, sourceSection, conditionCastNode, thenNode, elseNode); | |
461 | |
462 // This if becomes the else for the next if | |
463 | |
464 elseNode = ifNode; | |
465 } | |
466 | |
467 final RubyNode ifNode = elseNode; | |
468 | |
469 // A top-level block assigns the temp then runs the if | |
470 | |
471 return new SequenceNode(context, sourceSection, assignTemp, ifNode); | |
472 } else { | |
473 for (int n = node.getCases().size() - 1; n >= 0; n--) { | |
474 final org.jrubyparser.ast.WhenNode when = (org.jrubyparser.ast.WhenNode) node.getCases().get(n); | |
475 | |
476 // Make a condition from the one or more expressions combined in an or expression | |
477 | |
478 final List<org.jrubyparser.ast.Node> expressions; | |
479 | |
480 if (when.getExpression() instanceof org.jrubyparser.ast.ListNode) { | |
481 expressions = ((org.jrubyparser.ast.ListNode) when.getExpression()).childNodes(); | |
482 } else { | |
483 expressions = Arrays.asList(when.getExpression()); | |
484 } | |
485 | |
486 final List<RubyNode> tests = new ArrayList<>(); | |
487 | |
488 for (org.jrubyparser.ast.Node expressionNode : expressions) { | |
489 final RubyNode rubyExpression = (RubyNode) expressionNode.accept(this); | |
490 tests.add(rubyExpression); | |
491 } | |
492 | |
493 RubyNode conditionNode = tests.get(tests.size() - 1); | |
494 | |
495 // As with the if nodes, we work backwards to make it left associative | |
496 | |
497 for (int i = tests.size() - 2; i >= 0; i--) { | |
498 conditionNode = OrNodeFactory.create(context, sourceSection, tests.get(i), conditionNode); | |
499 } | |
500 | |
501 // Create the if node | |
502 | |
503 final BooleanCastNode conditionCastNode = BooleanCastNodeFactory.create(context, sourceSection, conditionNode); | |
504 | |
505 final RubyNode thenNode = (RubyNode) when.getBody().accept(this); | |
506 | |
507 final IfNode ifNode = new IfNode(context, sourceSection, conditionCastNode, thenNode, elseNode); | |
508 | |
509 // This if becomes the else for the next if | |
510 | |
511 elseNode = ifNode; | |
512 } | |
513 | |
514 return elseNode; | |
515 } | |
516 } | |
517 | |
518 @Override | |
519 public Object visitClassNode(org.jrubyparser.ast.ClassNode node) { | |
520 final SourceSection sourceSection = translate(node.getPosition()); | |
521 | |
522 final String name = node.getCPath().getName(); | |
523 | |
524 final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true, | |
525 new UniqueMethodIdentifier()); | |
526 final ModuleTranslator classTranslator = new ModuleTranslator(context, this, newEnvironment, source); | |
527 | |
528 final MethodDefinitionNode definitionMethod = classTranslator.compileClassNode(node.getPosition(), node.getCPath().getName(), node.getBody()); | |
529 | |
530 /* | |
531 * See my note in visitDefnNode about where the class gets defined - the same applies here. | |
532 */ | |
533 | |
534 RubyNode superClass; | |
535 | |
536 if (node.getSuper() != null) { | |
537 superClass = (RubyNode) node.getSuper().accept(this); | |
538 } else { | |
539 superClass = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getObjectClass()); | |
540 } | |
541 | |
542 final DefineOrGetClassNode defineOrGetClass = new DefineOrGetClassNode(context, sourceSection, name, getModuleToDefineModulesIn(sourceSection), superClass); | |
543 | |
544 return new OpenModuleNode(context, sourceSection, defineOrGetClass, definitionMethod); | |
545 } | |
546 | |
547 protected RubyNode getModuleToDefineModulesIn(SourceSection sourceSection) { | |
548 return new ClassNode(context, sourceSection, new SelfNode(context, sourceSection)); | |
549 } | |
550 | |
551 @Override | |
552 public Object visitClassVarAsgnNode(org.jrubyparser.ast.ClassVarAsgnNode node) { | |
553 final SourceSection sourceSection = translate(node.getPosition()); | |
554 | |
555 final RubyNode receiver = new ClassNode(context, sourceSection, new SelfNode(context, sourceSection)); | |
556 | |
557 final RubyNode rhs = (RubyNode) node.getValue().accept(this); | |
558 | |
559 return new WriteClassVariableNode(context, sourceSection, node.getName(), receiver, rhs); | |
560 } | |
561 | |
562 @Override | |
563 public Object visitClassVarDeclNode(org.jrubyparser.ast.ClassVarDeclNode node) { | |
564 return unimplemented(node); | |
565 } | |
566 | |
567 @Override | |
568 public Object visitClassVarNode(org.jrubyparser.ast.ClassVarNode node) { | |
569 final SourceSection sourceSection = translate(node.getPosition()); | |
570 return new ReadClassVariableNode(context, sourceSection, node.getName(), new SelfNode(context, sourceSection)); | |
571 } | |
572 | |
573 @Override | |
574 public Object visitColon2Node(org.jrubyparser.ast.Colon2Node node) { | |
575 final RubyNode lhs = (RubyNode) node.getLeftNode().accept(this); | |
576 | |
577 return new UninitializedReadConstantNode(context, translate(node.getPosition()), node.getName(), lhs); | |
578 } | |
579 | |
580 @Override | |
581 public Object visitColon3Node(org.jrubyparser.ast.Colon3Node node) { | |
582 // Colon3 means the root namespace, as in ::Foo | |
583 | |
584 final SourceSection sourceSection = translate(node.getPosition()); | |
585 | |
586 final ObjectLiteralNode root = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getMainObject()); | |
587 | |
588 return new UninitializedReadConstantNode(context, sourceSection, node.getName(), root); | |
589 } | |
590 | |
591 @Override | |
592 public Object visitConstDeclNode(org.jrubyparser.ast.ConstDeclNode node) { | |
593 final SourceSection sourceSection = translate(node.getPosition()); | |
594 | |
595 final ClassNode classNode = new ClassNode(context, sourceSection, new SelfNode(context, sourceSection)); | |
596 | |
597 return new WriteConstantNode(context, sourceSection, node.getName(), classNode, (RubyNode) node.getValue().accept(this)); | |
598 } | |
599 | |
600 @Override | |
601 public Object visitConstNode(org.jrubyparser.ast.ConstNode node) { | |
602 final SourceSection sourceSection = translate(node.getPosition()); | |
603 | |
604 return new UninitializedReadConstantNode(context, sourceSection, node.getName(), new SelfNode(context, sourceSection)); | |
605 } | |
606 | |
607 @Override | |
608 public Object visitDAsgnNode(org.jrubyparser.ast.DAsgnNode node) { | |
609 return new org.jrubyparser.ast.LocalAsgnNode(node.getPosition(), node.getName(), node.getDepth(), node.getValue()).accept(this); | |
610 } | |
611 | |
612 @Override | |
613 public Object visitDRegxNode(org.jrubyparser.ast.DRegexpNode node) { | |
614 SourceSection sourceSection = translate(node.getPosition()); | |
615 | |
616 final RubyNode stringNode = translateInterpolatedString(sourceSection, node.childNodes()); | |
617 | |
618 return StringToRegexpNodeFactory.create(context, sourceSection, stringNode); | |
619 } | |
620 | |
621 @Override | |
622 public Object visitDStrNode(org.jrubyparser.ast.DStrNode node) { | |
623 return translateInterpolatedString(translate(node.getPosition()), node.childNodes()); | |
624 } | |
625 | |
626 @Override | |
627 public Object visitDSymbolNode(org.jrubyparser.ast.DSymbolNode node) { | |
628 SourceSection sourceSection = translate(node.getPosition()); | |
629 | |
630 final RubyNode stringNode = translateInterpolatedString(sourceSection, node.childNodes()); | |
631 | |
632 return StringToSymbolNodeFactory.create(context, sourceSection, stringNode); | |
633 } | |
634 | |
635 private RubyNode translateInterpolatedString(SourceSection sourceSection, List<org.jrubyparser.ast.Node> childNodes) { | |
636 final List<RubyNode> children = new ArrayList<>(); | |
637 | |
638 for (org.jrubyparser.ast.Node child : childNodes) { | |
639 children.add((RubyNode) child.accept(this)); | |
640 } | |
641 | |
642 return new InterpolatedStringNode(context, sourceSection, children.toArray(new RubyNode[children.size()])); | |
643 } | |
644 | |
645 @Override | |
646 public Object visitDVarNode(org.jrubyparser.ast.DVarNode node) { | |
647 RubyNode readNode = environment.findLocalVarNode(node.getName(), translate(node.getPosition())); | |
648 | |
649 if (readNode == null) { | |
650 context.implementationMessage("can't find variable %s at %s, using noop", node.getName(), node.getPosition()); | |
651 readNode = new NilNode(context, translate(node.getPosition())); | |
652 } | |
653 | |
654 return readNode; | |
655 } | |
656 | |
657 @Override | |
658 public Object visitDXStrNode(org.jrubyparser.ast.DXStrNode node) { | |
659 SourceSection sourceSection = translate(node.getPosition()); | |
660 | |
661 final RubyNode string = translateInterpolatedString(sourceSection, node.childNodes()); | |
662 | |
663 return new SystemNode(context, sourceSection, string); | |
664 } | |
665 | |
666 @Override | |
667 public Object visitDefinedNode(org.jrubyparser.ast.DefinedNode node) { | |
668 final SourceSection sourceSection = translate(node.getPosition()); | |
669 | |
670 org.jrubyparser.ast.Node expressionNode = node.getExpression(); | |
671 | |
672 while (expressionNode instanceof org.jrubyparser.ast.NewlineNode) { | |
673 expressionNode = ((org.jrubyparser.ast.NewlineNode) expressionNode).getNextNode(); | |
674 } | |
675 | |
676 final String name = nodeDefinedNames.get(expressionNode.getClass()); | |
677 | |
678 if (name != null) { | |
679 final StringLiteralNode literal = new StringLiteralNode(context, sourceSection, name); | |
680 return literal; | |
681 } | |
682 | |
683 return new DefinedNode(context, sourceSection, (RubyNode) node.getExpression().accept(this)); | |
684 } | |
685 | |
686 @Override | |
687 public Object visitDefnNode(org.jrubyparser.ast.DefnNode node) { | |
688 final SourceSection sourceSection = translate(node.getPosition()); | |
689 final ClassNode classNode = new ClassNode(context, sourceSection, new SelfNode(context, sourceSection)); | |
690 return translateMethodDefinition(sourceSection, classNode, node.getName(), node.getArgs(), node.getBody()); | |
691 } | |
692 | |
693 @Override | |
694 public Object visitDefsNode(org.jrubyparser.ast.DefsNode node) { | |
695 final SourceSection sourceSection = translate(node.getPosition()); | |
696 | |
697 final RubyNode objectNode = (RubyNode) node.getReceiver().accept(this); | |
698 | |
699 final SingletonClassNode singletonClassNode = new SingletonClassNode(context, sourceSection, objectNode); | |
700 | |
701 return translateMethodDefinition(sourceSection, singletonClassNode, node.getName(), node.getArgs(), node.getBody()); | |
702 } | |
703 | |
704 private RubyNode translateMethodDefinition(SourceSection sourceSection, RubyNode classNode, String methodName, org.jrubyparser.ast.ArgsNode argsNode, org.jrubyparser.ast.Node bodyNode) { | |
705 final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true, | |
706 new UniqueMethodIdentifier()); | |
707 | |
708 // ownScopeForAssignments is the same for the defined method as the current one. | |
709 | |
710 final MethodTranslator methodCompiler = new MethodTranslator(context, this, newEnvironment, false, source); | |
711 | |
712 final MethodDefinitionNode functionExprNode = methodCompiler.compileFunctionNode(sourceSection, methodName, argsNode, bodyNode); | |
713 | |
714 /* | |
715 * In the top-level, methods are defined in the class of the main object. This is | |
716 * counter-intuitive - I would have expected them to be defined in the singleton class. | |
717 * Apparently this is a design decision to make top-level methods sort of global. | |
718 * | |
719 * http://stackoverflow.com/questions/1761148/where-are-methods-defined-at-the-ruby-top-level | |
720 */ | |
721 | |
722 return new AddMethodNode(context, sourceSection, classNode, functionExprNode); | |
723 } | |
724 | |
725 @Override | |
726 public Object visitDotNode(org.jrubyparser.ast.DotNode node) { | |
727 final RubyNode begin = (RubyNode) node.getBegin().accept(this); | |
728 final RubyNode end = (RubyNode) node.getEnd().accept(this); | |
729 SourceSection sourceSection = translate(node.getPosition()); | |
730 | |
731 if (begin instanceof FixnumLiteralNode && end instanceof FixnumLiteralNode) { | |
732 final int beginValue = ((FixnumLiteralNode) begin).getValue(); | |
733 final int endValue = ((FixnumLiteralNode) end).getValue(); | |
734 | |
735 return new ObjectLiteralNode(context, sourceSection, new FixnumRange(context.getCoreLibrary().getRangeClass(), beginValue, endValue, node.isExclusive())); | |
736 } | |
737 // See RangeNode for why there is a node specifically for creating this one type | |
738 return RangeLiteralNodeFactory.create(context, sourceSection, node.isExclusive(), begin, end); | |
739 } | |
740 | |
741 @Override | |
742 public Object visitEncodingNode(org.jrubyparser.ast.EncodingNode node) { | |
743 return unimplemented(node); | |
744 } | |
745 | |
746 @Override | |
747 public Object visitEnsureNode(org.jrubyparser.ast.EnsureNode node) { | |
748 final RubyNode tryPart = (RubyNode) node.getBody().accept(this); | |
749 final RubyNode ensurePart = (RubyNode) node.getEnsure().accept(this); | |
750 return new EnsureNode(context, translate(node.getPosition()), tryPart, ensurePart); | |
751 } | |
752 | |
753 @Override | |
754 public Object visitEvStrNode(org.jrubyparser.ast.EvStrNode node) { | |
755 return node.getBody().accept(this); | |
756 } | |
757 | |
758 @Override | |
759 public Object visitFCallNode(org.jrubyparser.ast.FCallNode node) { | |
760 final org.jrubyparser.ast.Node receiver = new org.jrubyparser.ast.SelfNode(node.getPosition()); | |
761 final org.jrubyparser.ast.Node callNode = new org.jrubyparser.ast.CallNode(node.getPosition(), receiver, node.getName(), node.getArgs(), node.getIter()); | |
762 | |
763 return callNode.accept(this); | |
764 } | |
765 | |
766 @Override | |
767 public Object visitFalseNode(org.jrubyparser.ast.FalseNode node) { | |
768 return new BooleanLiteralNode(context, translate(node.getPosition()), false); | |
769 } | |
770 | |
771 @Override | |
772 public Object visitFixnumNode(org.jrubyparser.ast.FixnumNode node) { | |
773 final long value = node.getValue(); | |
774 | |
775 if (value >= RubyFixnum.MIN_VALUE && value <= RubyFixnum.MAX_VALUE) { | |
776 return new FixnumLiteralNode(context, translate(node.getPosition()), (int) value); | |
777 } | |
778 return new BignumLiteralNode(context, translate(node.getPosition()), BigInteger.valueOf(value)); | |
779 } | |
780 | |
781 @Override | |
782 public Object visitFlipNode(org.jrubyparser.ast.FlipNode node) { | |
783 final SourceSection sourceSection = translate(node.getPosition()); | |
784 | |
785 final RubyNode begin = (RubyNode) node.getBegin().accept(this); | |
786 final RubyNode end = (RubyNode) node.getEnd().accept(this); | |
787 | |
788 final BooleanCastNode beginCast = BooleanCastNodeFactory.create(context, sourceSection, begin); | |
789 final BooleanCastNode endCast = BooleanCastNodeFactory.create(context, sourceSection, end); | |
790 final FlipFlopStateNode stateNode = createFlipFlopState(sourceSection, 0); | |
791 | |
792 return new FlipFlopNode(context, sourceSection, beginCast, endCast, stateNode, node.isExclusive()); | |
793 } | |
794 | |
795 protected FlipFlopStateNode createFlipFlopState(SourceSection sourceSection, int depth) { | |
796 final FrameSlot frameSlot = environment.declareVar(environment.allocateLocalTemp()); | |
797 environment.getFlipFlopStates().add(frameSlot); | |
798 | |
799 if (depth == 0) { | |
800 return new LocalFlipFlopStateNode(sourceSection, frameSlot); | |
801 } else { | |
802 return new LevelFlipFlopStateNode(sourceSection, depth, frameSlot); | |
803 } | |
804 } | |
805 | |
806 @Override | |
807 public Object visitFloatNode(org.jrubyparser.ast.FloatNode node) { | |
808 return new FloatLiteralNode(context, translate(node.getPosition()), node.getValue()); | |
809 } | |
810 | |
811 @Override | |
812 public Object visitForNode(org.jrubyparser.ast.ForNode node) { | |
813 /** | |
814 * A Ruby for-loop, such as: | |
815 * | |
816 * <pre> | |
817 * for x in y | |
818 * z = x | |
819 * puts z | |
820 * end | |
821 * </pre> | |
822 * | |
823 * naively desugars to: | |
824 * | |
825 * <pre> | |
826 * y.each do |x| | |
827 * z = x | |
828 * puts z | |
829 * end | |
830 * </pre> | |
831 * | |
832 * The main difference is that z is always going to be local to the scope outside the block, | |
833 * so it's a bit more like: | |
834 * | |
835 * <pre> | |
836 * z = nil unless z is already defined | |
837 * y.each do |x| | |
838 * z = x | |
839 * puts x | |
840 * end | |
841 * </pre> | |
842 * | |
843 * Which forces z to be defined in the correct scope. The parser already correctly calls z a | |
844 * local, but then that causes us a problem as if we're going to translate to a block we | |
845 * need a formal parameter - not a local variable. My solution to this is to add a | |
846 * temporary: | |
847 * | |
848 * <pre> | |
849 * z = nil unless z is already defined | |
850 * y.each do |temp| | |
851 * x = temp | |
852 * z = x | |
853 * puts x | |
854 * end | |
855 * </pre> | |
856 * | |
857 * We also need that temp because the expression assigned in the for could be index | |
858 * assignment, multiple assignment, or whatever: | |
859 * | |
860 * <pre> | |
861 * for x[0] in y | |
862 * z = x[0] | |
863 * puts z | |
864 * end | |
865 * </pre> | |
866 * | |
867 * http://blog.grayproductions.net/articles/the_evils_of_the_for_loop | |
868 * http://stackoverflow.com/questions/3294509/for-vs-each-in-ruby | |
869 * | |
870 * The other complication is that normal locals should be defined in the enclosing scope, | |
871 * unlike a normal block. We do that by setting a flag on this translator object when we | |
872 * visit the new iter, translatingForStatement, which we recognise when visiting an iter | |
873 * node. | |
874 * | |
875 * Finally, note that JRuby's terminology is strange here. Normally 'iter' is a different | |
876 * term for a block. Here, JRuby calls the object being iterated over the 'iter'. | |
877 */ | |
878 | |
879 final String temp = environment.allocateLocalTemp(); | |
880 | |
881 final org.jrubyparser.ast.Node receiver = node.getIter(); | |
882 | |
883 /* | |
884 * The x in for x in ... is like the nodes in multiple assignment - it has a dummy RHS which | |
885 * we need to replace with our temp. Just like in multiple assignment this is really awkward | |
886 * with the JRuby AST. | |
887 */ | |
888 | |
889 final org.jrubyparser.ast.LocalVarNode readTemp = new org.jrubyparser.ast.LocalVarNode(node.getPosition(), 0, temp); | |
890 final org.jrubyparser.ast.Node forVar = node.getVar(); | |
891 final org.jrubyparser.ast.Node assignTemp = setRHS(forVar, readTemp); | |
892 | |
893 final org.jrubyparser.ast.BlockNode bodyWithTempAssign = new org.jrubyparser.ast.BlockNode(node.getPosition()); | |
894 bodyWithTempAssign.add(assignTemp); | |
895 bodyWithTempAssign.add(node.getBody()); | |
896 | |
897 final org.jrubyparser.ast.ArgumentNode blockVar = new org.jrubyparser.ast.ArgumentNode(node.getPosition(), temp); | |
898 final org.jrubyparser.ast.ListNode blockArgsPre = new org.jrubyparser.ast.ListNode(node.getPosition(), blockVar); | |
899 final org.jrubyparser.ast.ArgsNode blockArgs = new org.jrubyparser.ast.ArgsNode(node.getPosition(), blockArgsPre, null, null, null, null, null, null); | |
900 final org.jrubyparser.ast.IterNode block = new org.jrubyparser.ast.IterNode(node.getPosition(), blockArgs, node.getScope(), bodyWithTempAssign); | |
901 | |
902 final org.jrubyparser.ast.CallNode callNode = new org.jrubyparser.ast.CallNode(node.getPosition(), receiver, "each", null, block); | |
903 | |
904 translatingForStatement = true; | |
905 final RubyNode translated = (RubyNode) callNode.accept(this); | |
906 translatingForStatement = false; | |
907 | |
908 return translated; | |
909 } | |
910 | |
911 private static org.jrubyparser.ast.Node setRHS(org.jrubyparser.ast.Node node, org.jrubyparser.ast.Node rhs) { | |
912 if (node instanceof org.jrubyparser.ast.LocalAsgnNode) { | |
913 final org.jrubyparser.ast.LocalAsgnNode localAsgnNode = (org.jrubyparser.ast.LocalAsgnNode) node; | |
914 return new org.jrubyparser.ast.LocalAsgnNode(node.getPosition(), localAsgnNode.getName(), 0, rhs); | |
915 } else if (node instanceof org.jrubyparser.ast.DAsgnNode) { | |
916 final org.jrubyparser.ast.DAsgnNode dAsgnNode = (org.jrubyparser.ast.DAsgnNode) node; | |
917 return new org.jrubyparser.ast.DAsgnNode(node.getPosition(), dAsgnNode.getName(), 0, rhs); | |
918 } else if (node instanceof org.jrubyparser.ast.MultipleAsgnNode) { | |
919 final org.jrubyparser.ast.MultipleAsgnNode multAsgnNode = (org.jrubyparser.ast.MultipleAsgnNode) node; | |
920 return new org.jrubyparser.ast.MultipleAsgnNode(node.getPosition(), multAsgnNode.getPre(), multAsgnNode.getRest(), multAsgnNode.getPost()); | |
921 } else if (node instanceof org.jrubyparser.ast.InstAsgnNode) { | |
922 final org.jrubyparser.ast.InstAsgnNode instAsgnNode = (org.jrubyparser.ast.InstAsgnNode) node; | |
923 return new org.jrubyparser.ast.InstAsgnNode(node.getPosition(), instAsgnNode.getName(), rhs); | |
924 } else if (node instanceof org.jrubyparser.ast.ClassVarAsgnNode) { | |
925 final org.jrubyparser.ast.ClassVarAsgnNode instAsgnNode = (org.jrubyparser.ast.ClassVarAsgnNode) node; | |
926 return new org.jrubyparser.ast.ClassVarAsgnNode(node.getPosition(), instAsgnNode.getName(), rhs); | |
927 } else if (node instanceof org.jrubyparser.ast.ConstDeclNode) { | |
928 final org.jrubyparser.ast.ConstDeclNode constDeclNode = (org.jrubyparser.ast.ConstDeclNode) node; | |
929 return new org.jrubyparser.ast.ConstDeclNode(node.getPosition(), constDeclNode.getName(), (org.jrubyparser.ast.INameNode) constDeclNode.getConstNode(), rhs); | |
930 } else { | |
931 throw new UnsupportedOperationException("Don't know how to set the RHS of a " + node.getClass().getName()); | |
932 } | |
933 } | |
934 | |
935 @Override | |
936 public Object visitGlobalAsgnNode(org.jrubyparser.ast.GlobalAsgnNode node) { | |
937 final SourceSection sourceSection = translate(node.getPosition()); | |
938 | |
939 final String name = "$" + node.getName(); | |
940 final RubyNode rhs = (RubyNode) node.getValue().accept(this); | |
941 | |
942 if (FRAME_LOCAL_GLOBAL_VARIABLES.contains(name)) { | |
943 context.implementationMessage("Assigning to frame local global variables not implemented at %s", node.getPosition()); | |
944 | |
945 return rhs; | |
946 } else { | |
947 final ObjectLiteralNode globalVariablesObjectNode = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getGlobalVariablesObject()); | |
948 | |
949 return new UninitializedWriteInstanceVariableNode(context, sourceSection, name, globalVariablesObjectNode, rhs); | |
950 } | |
951 } | |
952 | |
953 @Override | |
954 public Object visitGlobalVarNode(org.jrubyparser.ast.GlobalVarNode node) { | |
955 final String name = "$" + node.getName(); | |
956 final SourceSection sourceSection = translate(node.getPosition()); | |
957 | |
958 if (FRAME_LOCAL_GLOBAL_VARIABLES.contains(name)) { | |
959 // Assignment is implicit for many of these, so we need to declare when we use | |
960 | |
961 environment.declareVar(name); | |
962 | |
963 final RubyNode readNode = environment.findLocalVarNode(name, sourceSection); | |
964 | |
965 return readNode; | |
966 } else { | |
967 final ObjectLiteralNode globalVariablesObjectNode = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getGlobalVariablesObject()); | |
968 | |
969 return new UninitializedReadInstanceVariableNode(context, sourceSection, name, globalVariablesObjectNode); | |
970 } | |
971 } | |
972 | |
973 @Override | |
974 public Object visitHashNode(org.jrubyparser.ast.HashNode node) { | |
975 final SourceSection sourceSection = translate(node.getPosition()); | |
976 | |
977 final List<RubyNode> keys = new ArrayList<>(); | |
978 final List<RubyNode> values = new ArrayList<>(); | |
979 | |
980 final org.jrubyparser.ast.ListNode entries = node.getListNode(); | |
981 | |
982 assert entries.size() % 2 == 0; | |
983 | |
984 for (int n = 0; n < entries.size(); n += 2) { | |
985 if (entries.get(n) == null) { | |
986 final NilNode nilNode = new NilNode(context, sourceSection); | |
987 keys.add(nilNode); | |
988 } else { | |
989 keys.add((RubyNode) entries.get(n).accept(this)); | |
990 } | |
991 | |
992 if (entries.get(n + 1) == null) { | |
993 final NilNode nilNode = new NilNode(context, sourceSection); | |
994 values.add(nilNode); | |
995 } else { | |
996 values.add((RubyNode) entries.get(n + 1).accept(this)); | |
997 } | |
998 } | |
999 | |
1000 return new HashLiteralNode(translate(node.getPosition()), keys.toArray(new RubyNode[keys.size()]), values.toArray(new RubyNode[values.size()]), context); | |
1001 } | |
1002 | |
1003 @Override | |
1004 public Object visitIfNode(org.jrubyparser.ast.IfNode node) { | |
1005 final SourceSection sourceSection = translate(node.getPosition()); | |
1006 | |
1007 org.jrubyparser.ast.Node thenBody = node.getThenBody(); | |
1008 | |
1009 if (thenBody == null) { | |
1010 thenBody = new org.jrubyparser.ast.NilNode(node.getPosition()); | |
1011 } | |
1012 | |
1013 org.jrubyparser.ast.Node elseBody = node.getElseBody(); | |
1014 | |
1015 if (elseBody == null) { | |
1016 elseBody = new org.jrubyparser.ast.NilNode(node.getPosition()); | |
1017 } | |
1018 | |
1019 RubyNode condition; | |
1020 | |
1021 if (node.getCondition() == null) { | |
1022 condition = new NilNode(context, sourceSection); | |
1023 } else { | |
1024 condition = (RubyNode) node.getCondition().accept(this); | |
1025 } | |
1026 | |
1027 final BooleanCastNode conditionCast = BooleanCastNodeFactory.create(context, sourceSection, condition); | |
1028 | |
1029 final RubyNode thenBodyTranslated = (RubyNode) thenBody.accept(this); | |
1030 final RubyNode elseBodyTranslated = (RubyNode) elseBody.accept(this); | |
1031 | |
1032 return new IfNode(context, sourceSection, conditionCast, thenBodyTranslated, elseBodyTranslated); | |
1033 } | |
1034 | |
1035 @Override | |
1036 public Object visitInstAsgnNode(org.jrubyparser.ast.InstAsgnNode node) { | |
1037 final SourceSection sourceSection = translate(node.getPosition()); | |
1038 final String nameWithoutSigil = node.getName(); | |
1039 | |
1040 final RubyNode receiver = new SelfNode(context, sourceSection); | |
1041 | |
1042 RubyNode rhs; | |
1043 | |
1044 if (node.getValue() == null) { | |
1045 rhs = new DeadNode(context, sourceSection); | |
1046 } else { | |
1047 rhs = (RubyNode) node.getValue().accept(this); | |
1048 } | |
1049 | |
1050 return new UninitializedWriteInstanceVariableNode(context, sourceSection, nameWithoutSigil, receiver, rhs); | |
1051 } | |
1052 | |
1053 @Override | |
1054 public Object visitInstVarNode(org.jrubyparser.ast.InstVarNode node) { | |
1055 final SourceSection sourceSection = translate(node.getPosition()); | |
1056 final String nameWithoutSigil = node.getName(); | |
1057 | |
1058 final RubyNode receiver = new SelfNode(context, sourceSection); | |
1059 | |
1060 return new UninitializedReadInstanceVariableNode(context, sourceSection, nameWithoutSigil, receiver); | |
1061 } | |
1062 | |
1063 @Override | |
1064 public Object visitIterNode(org.jrubyparser.ast.IterNode node) { | |
1065 /* | |
1066 * In a block we do NOT allocate a new return ID - returns will return from the method, not | |
1067 * the block (in the general case, see Proc and the difference between Proc and Lambda for | |
1068 * specifics). | |
1069 */ | |
1070 | |
1071 final boolean hasOwnScope = !translatingForStatement; | |
1072 | |
1073 // Unset this flag for any for any blocks within the for statement's body | |
1074 | |
1075 translatingForStatement = false; | |
1076 | |
1077 final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(), environment.getReturnID(), hasOwnScope, false, | |
1078 new UniqueMethodIdentifier()); | |
1079 final MethodTranslator methodCompiler = new MethodTranslator(context, this, newEnvironment, true, source); | |
1080 | |
1081 org.jrubyparser.ast.ArgsNode argsNode; | |
1082 | |
1083 if (node.getVar() instanceof org.jrubyparser.ast.ArgsNode) { | |
1084 argsNode = (org.jrubyparser.ast.ArgsNode) node.getVar(); | |
1085 } else if (node.getVar() instanceof org.jrubyparser.ast.DAsgnNode) { | |
1086 final org.jrubyparser.ast.ArgumentNode arg = new org.jrubyparser.ast.ArgumentNode(node.getPosition(), ((org.jrubyparser.ast.DAsgnNode) node.getVar()).getName()); | |
1087 final org.jrubyparser.ast.ListNode preArgs = new org.jrubyparser.ast.ArrayNode(node.getPosition(), arg); | |
1088 argsNode = new org.jrubyparser.ast.ArgsNode(node.getPosition(), preArgs, null, null, null, null, null, null); | |
1089 } else if (node.getVar() == null) { | |
1090 argsNode = null; | |
1091 } else { | |
1092 throw new UnsupportedOperationException(); | |
1093 } | |
1094 | |
1095 return methodCompiler.compileFunctionNode(translate(node.getPosition()), "(block)", argsNode, node.getBody()); | |
1096 } | |
1097 | |
1098 @Override | |
1099 public Object visitLiteralNode(org.jrubyparser.ast.LiteralNode node) { | |
1100 return unimplemented(node); | |
1101 } | |
1102 | |
1103 @Override | |
1104 public Object visitLocalAsgnNode(org.jrubyparser.ast.LocalAsgnNode node) { | |
1105 | |
1106 final SourceSection sourceSection = translate(node.getPosition()); | |
1107 | |
1108 if (environment.getNeverAssignInParentScope()) { | |
1109 environment.declareVar(node.getName()); | |
1110 } | |
1111 | |
1112 RubyNode lhs = environment.findLocalVarNode(node.getName(), sourceSection); | |
1113 | |
1114 if (lhs == null) { | |
1115 if (environment.hasOwnScopeForAssignments()) { | |
1116 environment.declareVar(node.getName()); | |
1117 } | |
1118 | |
1119 TranslatorEnvironment environmentToDeclareIn = environment; | |
1120 | |
1121 while (!environmentToDeclareIn.hasOwnScopeForAssignments()) { | |
1122 environmentToDeclareIn = environmentToDeclareIn.getParent(); | |
1123 } | |
1124 | |
1125 environmentToDeclareIn.declareVar(node.getName()); | |
1126 lhs = environment.findLocalVarNode(node.getName(), sourceSection); | |
1127 | |
1128 if (lhs == null) { | |
1129 throw new RuntimeException("shoudln't be here"); | |
1130 } | |
1131 } | |
1132 | |
1133 RubyNode rhs; | |
1134 | |
1135 if (node.getValue() == null) { | |
1136 rhs = new DeadNode(context, sourceSection); | |
1137 } else { | |
1138 rhs = (RubyNode) node.getValue().accept(this); | |
1139 } | |
1140 | |
1141 RubyNode translated = ((ReadNode) lhs).makeWriteNode(rhs); | |
1142 | |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
1143 final UniqueMethodIdentifier methodIdentifier = environment.findMethodForLocalVar(node.getName()); |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
1144 |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
1145 return instrumenter.instrumentAsLocalAssignment(translated, methodIdentifier, node.getName()); |
13514 | 1146 } |
1147 | |
1148 @Override | |
1149 public Object visitLocalVarNode(org.jrubyparser.ast.LocalVarNode node) { | |
1150 final SourceSection sourceSection = translate(node.getPosition()); | |
1151 | |
1152 final String name = node.getName(); | |
1153 | |
1154 RubyNode readNode = environment.findLocalVarNode(name, sourceSection); | |
1155 | |
1156 if (readNode == null) { | |
1157 context.implementationMessage("Local variable found by parser but not by translator - " + name + " at " + node.getPosition()); | |
1158 readNode = environment.findLocalVarNode(environment.allocateLocalTemp(), sourceSection); | |
1159 } | |
1160 | |
1161 return readNode; | |
1162 } | |
1163 | |
1164 @Override | |
1165 public Object visitMatch2Node(org.jrubyparser.ast.Match2Node node) { | |
1166 final org.jrubyparser.ast.Node argsNode = buildArrayNode(node.getPosition(), node.getValue()); | |
1167 final org.jrubyparser.ast.Node callNode = new org.jrubyparser.ast.CallNode(node.getPosition(), node.getReceiver(), "=~", argsNode, null); | |
1168 return callNode.accept(this); | |
1169 } | |
1170 | |
1171 @Override | |
1172 public Object visitMatch3Node(org.jrubyparser.ast.Match3Node node) { | |
1173 final org.jrubyparser.ast.Node argsNode = buildArrayNode(node.getPosition(), node.getValue()); | |
1174 final org.jrubyparser.ast.Node callNode = new org.jrubyparser.ast.CallNode(node.getPosition(), node.getReceiver(), "=~", argsNode, null); | |
1175 return callNode.accept(this); | |
1176 } | |
1177 | |
1178 @Override | |
1179 public Object visitMatchNode(org.jrubyparser.ast.MatchNode node) { | |
1180 return unimplemented(node); | |
1181 } | |
1182 | |
1183 @Override | |
1184 public Object visitModuleNode(org.jrubyparser.ast.ModuleNode node) { | |
1185 // See visitClassNode | |
1186 | |
1187 final SourceSection sourceSection = translate(node.getPosition()); | |
1188 | |
1189 final String name = node.getCPath().getName(); | |
1190 | |
1191 final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true, | |
1192 new UniqueMethodIdentifier()); | |
1193 final ModuleTranslator classTranslator = new ModuleTranslator(context, this, newEnvironment, source); | |
1194 | |
1195 final MethodDefinitionNode definitionMethod = classTranslator.compileClassNode(node.getPosition(), node.getCPath().getName(), node.getBody()); | |
1196 | |
1197 final DefineOrGetModuleNode defineModuleNode = new DefineOrGetModuleNode(context, sourceSection, name, getModuleToDefineModulesIn(sourceSection)); | |
1198 | |
1199 return new OpenModuleNode(context, sourceSection, defineModuleNode, definitionMethod); | |
1200 } | |
1201 | |
1202 @Override | |
1203 public Object visitMultipleAsgnNode(org.jrubyparser.ast.MultipleAsgnNode node) { | |
1204 final SourceSection sourceSection = translate(node.getPosition()); | |
1205 | |
1206 final org.jrubyparser.ast.ArrayNode preArray = (org.jrubyparser.ast.ArrayNode) node.getPre(); | |
1207 final org.jrubyparser.ast.Node rhs = node.getValue(); | |
1208 | |
1209 RubyNode rhsTranslated; | |
1210 | |
1211 if (rhs == null) { | |
1212 context.implementationMessage("warning: no RHS for multiple assignment - using noop"); | |
1213 rhsTranslated = new NilNode(context, sourceSection); | |
1214 } else { | |
1215 rhsTranslated = (RubyNode) rhs.accept(this); | |
1216 } | |
1217 | |
1218 /* | |
1219 * One very common case is to do | |
1220 * | |
1221 * a, b = c, d | |
1222 */ | |
1223 | |
1224 if (preArray != null && node.getPost() == null && node.getRest() == null && rhsTranslated instanceof UninitialisedArrayLiteralNode && | |
1225 ((UninitialisedArrayLiteralNode) rhsTranslated).getValues().length == preArray.size()) { | |
1226 /* | |
1227 * We can deal with this common case be rewriting as | |
1228 * | |
1229 * temp1 = c; temp2 = d; a = temp1; b = temp2 | |
1230 * | |
1231 * We can't just do | |
1232 * | |
1233 * a = c; b = d | |
1234 * | |
1235 * As we don't know if d depends on the original value of a. | |
1236 * | |
1237 * We also need to return an array [c, d], but we make that result elidable so it isn't | |
1238 * executed if it isn't actually demanded. | |
1239 */ | |
1240 | |
1241 final RubyNode[] rhsValues = ((UninitialisedArrayLiteralNode) rhsTranslated).getValues(); | |
1242 final int assignedValuesCount = preArray.size(); | |
1243 | |
1244 final RubyNode[] sequence = new RubyNode[assignedValuesCount * 2]; | |
1245 | |
1246 final RubyNode[] tempValues = new RubyNode[assignedValuesCount]; | |
1247 | |
1248 for (int n = 0; n < assignedValuesCount; n++) { | |
1249 final String tempName = environment.allocateLocalTemp(); | |
1250 final RubyNode readTemp = environment.findLocalVarNode(tempName, sourceSection); | |
1251 final RubyNode assignTemp = ((ReadNode) readTemp).makeWriteNode(rhsValues[n]); | |
1252 final RubyNode assignFinalValue = translateDummyAssignment(preArray.get(n), readTemp); | |
1253 | |
1254 sequence[n] = assignTemp; | |
1255 sequence[assignedValuesCount + n] = assignFinalValue; | |
1256 | |
1257 tempValues[n] = readTemp; | |
1258 } | |
1259 | |
1260 final RubyNode blockNode = new SequenceNode(context, sourceSection, sequence); | |
1261 | |
1262 final UninitialisedArrayLiteralNode arrayNode = new UninitialisedArrayLiteralNode(context, sourceSection, tempValues); | |
1263 | |
1264 final ElidableResultNode elidableResult = new ElidableResultNode(context, sourceSection, blockNode, arrayNode); | |
1265 | |
1266 return elidableResult; | |
1267 } else if (preArray != null) { | |
1268 /* | |
1269 * The other simple case is | |
1270 * | |
1271 * a, b, c = x | |
1272 * | |
1273 * If x is an array, then it's | |
1274 * | |
1275 * a[0] = x[0] etc | |
1276 * | |
1277 * If x isn't an array then it's | |
1278 * | |
1279 * a, b, c = [x, nil, nil] | |
1280 * | |
1281 * Which I believe is the same effect as | |
1282 * | |
1283 * a, b, c, = *x | |
1284 * | |
1285 * So we insert the splat cast node, even though it isn't there. | |
1286 */ | |
1287 | |
1288 /* | |
1289 * Create a temp for the array. | |
1290 */ | |
1291 | |
1292 final String tempName = environment.allocateLocalTemp(); | |
1293 | |
1294 /* | |
1295 * Create a sequence of instructions, with the first being the literal array assigned to | |
1296 * the temp. | |
1297 */ | |
1298 | |
1299 final List<RubyNode> sequence = new ArrayList<>(); | |
1300 | |
1301 final RubyNode splatCastNode = SplatCastNodeFactory.create(context, sourceSection, rhsTranslated); | |
1302 | |
1303 final RubyNode writeTemp = ((ReadNode) environment.findLocalVarNode(tempName, sourceSection)).makeWriteNode(splatCastNode); | |
1304 | |
1305 sequence.add(writeTemp); | |
1306 | |
1307 /* | |
1308 * Then index the temp array for each assignment on the LHS. | |
1309 */ | |
1310 | |
1311 for (int n = 0; n < preArray.size(); n++) { | |
1312 final ArrayIndexNode assignedValue = ArrayIndexNodeFactory.create(context, sourceSection, n, environment.findLocalVarNode(tempName, sourceSection)); | |
1313 | |
1314 sequence.add(translateDummyAssignment(preArray.get(n), assignedValue)); | |
1315 } | |
1316 | |
1317 if (node.getRest() != null) { | |
1318 final ArrayRestNode assignedValue = new ArrayRestNode(context, sourceSection, preArray.size(), environment.findLocalVarNode(tempName, sourceSection)); | |
1319 | |
1320 sequence.add(translateDummyAssignment(node.getRest(), assignedValue)); | |
1321 } | |
1322 | |
1323 return new SequenceNode(context, sourceSection, sequence.toArray(new RubyNode[sequence.size()])); | |
1324 } else if (node.getPre() == null && node.getPost() == null && node.getRest() instanceof org.jrubyparser.ast.StarNode) { | |
1325 return rhsTranslated; | |
1326 } else if (node.getPre() == null && node.getPost() == null && node.getRest() != null && rhs != null && !(rhs instanceof org.jrubyparser.ast.ArrayNode)) { | |
1327 /* | |
1328 * *a = b | |
1329 * | |
1330 * >= 1.8, this seems to be the same as: | |
1331 * | |
1332 * a = *b | |
1333 */ | |
1334 | |
1335 final RubyNode restTranslated = ((RubyNode) node.getRest().accept(this)).getNonProxyNode(); | |
1336 | |
1337 /* | |
1338 * Sometimes rest is a corrupt write with no RHS, like in other multiple assignments, | |
1339 * and sometimes it is already a read. | |
1340 */ | |
1341 | |
1342 ReadNode restRead; | |
1343 | |
1344 if (restTranslated instanceof ReadNode) { | |
1345 restRead = (ReadNode) restTranslated; | |
1346 } else if (restTranslated instanceof WriteNode) { | |
1347 restRead = (ReadNode) ((WriteNode) restTranslated).makeReadNode(); | |
1348 } else { | |
1349 throw new RuntimeException("Unknown form of multiple assignment " + node + " at " + node.getPosition()); | |
1350 } | |
1351 | |
1352 final SplatCastNode rhsSplatCast = SplatCastNodeFactory.create(context, sourceSection, rhsTranslated); | |
1353 | |
1354 return restRead.makeWriteNode(rhsSplatCast); | |
1355 } else if (node.getPre() == null && node.getPost() == null && node.getRest() != null && rhs != null && rhs instanceof org.jrubyparser.ast.ArrayNode) { | |
1356 /* | |
1357 * *a = [b, c] | |
1358 * | |
1359 * This seems to be the same as: | |
1360 * | |
1361 * a = [b, c] | |
1362 */ | |
1363 | |
1364 final RubyNode restTranslated = ((RubyNode) node.getRest().accept(this)).getNonProxyNode(); | |
1365 | |
1366 /* | |
1367 * Sometimes rest is a corrupt write with no RHS, like in other multiple assignments, | |
1368 * and sometimes it is already a read. | |
1369 */ | |
1370 | |
1371 ReadNode restRead; | |
1372 | |
1373 if (restTranslated instanceof ReadNode) { | |
1374 restRead = (ReadNode) restTranslated; | |
1375 } else if (restTranslated instanceof WriteNode) { | |
1376 restRead = (ReadNode) ((WriteNode) restTranslated).makeReadNode(); | |
1377 } else { | |
1378 throw new RuntimeException("Unknown form of multiple assignment " + node + " at " + node.getPosition()); | |
1379 } | |
1380 | |
1381 return restRead.makeWriteNode(rhsTranslated); | |
1382 } else { | |
1383 throw new RuntimeException("Unknown form of multiple assignment " + node + " at " + node.getPosition()); | |
1384 } | |
1385 } | |
1386 | |
1387 private RubyNode translateDummyAssignment(org.jrubyparser.ast.Node dummyAssignment, RubyNode rhs) { | |
1388 final SourceSection sourceSection = translate(dummyAssignment.getPosition()); | |
1389 | |
1390 /* | |
1391 * This is tricky. To represent the RHS of a multiple assignment they use corrupt assignment | |
1392 * values, in some cases with no value to be assigned, and in other cases with a dummy | |
1393 * value. We can't visit them normally, as they're corrupt. We can't just modify them to | |
1394 * have our RHS, as that's a node in our AST, not theirs. We can't use a dummy value in | |
1395 * their AST because I can't add new visitors to this interface. | |
1396 */ | |
1397 | |
1398 RubyNode translated; | |
1399 | |
1400 if (dummyAssignment instanceof org.jrubyparser.ast.LocalAsgnNode) { | |
1401 /* | |
1402 * They have a dummy NilImplicitNode as the RHS. Translate, convert to read, convert to | |
1403 * write which allows us to set the RHS. | |
1404 */ | |
1405 | |
1406 final WriteNode dummyTranslated = (WriteNode) ((RubyNode) dummyAssignment.accept(this)).getNonProxyNode(); | |
1407 translated = ((ReadNode) dummyTranslated.makeReadNode()).makeWriteNode(rhs); | |
1408 } else if (dummyAssignment instanceof org.jrubyparser.ast.InstAsgnNode) { | |
1409 /* | |
1410 * Same as before, just a different type of assignment. | |
1411 */ | |
1412 | |
1413 final WriteInstanceVariableNode dummyTranslated = (WriteInstanceVariableNode) dummyAssignment.accept(this); | |
1414 translated = dummyTranslated.makeReadNode().makeWriteNode(rhs); | |
1415 } else if (dummyAssignment instanceof org.jrubyparser.ast.AttrAssignNode) { | |
1416 /* | |
1417 * They've given us an AttrAssignNode with the final argument, the assigned value, | |
1418 * missing. If we translate that we'll get foo.[]=(index), so missing the value. To | |
1419 * solve we have a special version of the visitCallNode that allows us to pass another | |
1420 * already translated argument, visitCallNodeExtraArgument. However, we initially have | |
1421 * an AttrAssignNode, so we also need a special version of that. | |
1422 */ | |
1423 | |
1424 final org.jrubyparser.ast.AttrAssignNode dummyAttrAssignment = (org.jrubyparser.ast.AttrAssignNode) dummyAssignment; | |
1425 translated = visitAttrAssignNodeExtraArgument(dummyAttrAssignment, rhs); | |
1426 } else if (dummyAssignment instanceof org.jrubyparser.ast.DAsgnNode) { | |
1427 final RubyNode dummyTranslated = (RubyNode) dummyAssignment.accept(this); | |
1428 | |
1429 if (dummyTranslated.getNonProxyNode() instanceof WriteLevelVariableNode) { | |
1430 translated = ((ReadNode) ((WriteLevelVariableNode) dummyTranslated.getNonProxyNode()).makeReadNode()).makeWriteNode(rhs); | |
1431 } else { | |
1432 translated = ((ReadNode) ((WriteLocalVariableNode) dummyTranslated.getNonProxyNode()).makeReadNode()).makeWriteNode(rhs); | |
1433 } | |
1434 } else { | |
1435 translated = ((ReadNode) environment.findLocalVarNode(environment.allocateLocalTemp(), sourceSection)).makeWriteNode(rhs); | |
1436 } | |
1437 | |
1438 return translated; | |
1439 } | |
1440 | |
1441 @Override | |
1442 public Object visitNewlineNode(org.jrubyparser.ast.NewlineNode node) { | |
1443 RubyNode translated = (RubyNode) node.getNextNode().accept(this); | |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
1444 return instrumenter.instrumentAsStatement(translated); |
13514 | 1445 } |
1446 | |
1447 @Override | |
1448 public Object visitNextNode(org.jrubyparser.ast.NextNode node) { | |
13556
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1449 final SourceSection sourceSection = translate(node.getPosition()); |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1450 |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1451 RubyNode resultNode; |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1452 |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1453 if (node.getValueNode() == null) { |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1454 resultNode = new NilNode(context, sourceSection); |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1455 } else { |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1456 resultNode = (RubyNode) node.getValueNode().accept(this); |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1457 } |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1458 |
44288fe54352
Ruby: fix some ?break? semantics.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
1459 return new NextNode(context, sourceSection, resultNode); |
13514 | 1460 } |
1461 | |
1462 @Override | |
1463 public Object visitNilNode(org.jrubyparser.ast.NilNode node) { | |
1464 return new NilNode(context, translate(node.getPosition())); | |
1465 } | |
1466 | |
1467 @Override | |
1468 public Object visitNotNode(org.jrubyparser.ast.NotNode node) { | |
1469 final SourceSection sourceSection = translate(node.getPosition()); | |
1470 | |
1471 final BooleanCastNode booleanCastNode = BooleanCastNodeFactory.create(context, sourceSection, (RubyNode) node.getCondition().accept(this)); | |
1472 | |
1473 return new NotNode(context, sourceSection, booleanCastNode); | |
1474 } | |
1475 | |
1476 @Override | |
1477 public Object visitNthRefNode(org.jrubyparser.ast.NthRefNode node) { | |
1478 final SourceSection sourceSection = translate(node.getPosition()); | |
1479 | |
1480 final String name = "$" + node.getMatchNumber(); | |
1481 | |
1482 RubyNode readLocal = environment.findLocalVarNode(name, sourceSection); | |
1483 | |
1484 if (readLocal == null) { | |
1485 environment.declareVar(name); | |
1486 readLocal = environment.findLocalVarNode(name, sourceSection); | |
1487 } | |
1488 | |
1489 return readLocal; | |
1490 } | |
1491 | |
1492 @Override | |
1493 public Object visitOpAsgnAndNode(org.jrubyparser.ast.OpAsgnAndNode node) { | |
1494 final org.jrubyparser.ast.Node lhs = node.getFirst(); | |
1495 final org.jrubyparser.ast.Node rhs = node.getSecond(); | |
1496 | |
1497 return AndNodeFactory.create(context, translate(node.getPosition()), (RubyNode) lhs.accept(this), (RubyNode) rhs.accept(this)); | |
1498 } | |
1499 | |
1500 @Override | |
1501 public Object visitOpAsgnNode(org.jrubyparser.ast.OpAsgnNode node) { | |
1502 /* | |
1503 * We're going to de-sugar a.foo += c into a.foo = a.foo + c. Note that we can't evaluate a | |
1504 * more than once, so we put it into a temporary, and we're doing something more like: | |
1505 * | |
1506 * temp = a; temp.foo = temp.foo + c | |
1507 */ | |
1508 | |
1509 final String temp = environment.allocateLocalTemp(); | |
1510 final org.jrubyparser.ast.Node writeReceiverToTemp = new org.jrubyparser.ast.LocalAsgnNode(node.getPosition(), temp, 0, node.getReceiver()); | |
1511 final org.jrubyparser.ast.Node readReceiverFromTemp = new org.jrubyparser.ast.LocalVarNode(node.getPosition(), 0, temp); | |
1512 | |
1513 final org.jrubyparser.ast.Node readMethod = new org.jrubyparser.ast.CallNode(node.getPosition(), readReceiverFromTemp, node.getVariableName(), null); | |
1514 final org.jrubyparser.ast.Node operation = new org.jrubyparser.ast.CallNode(node.getPosition(), readMethod, node.getOperatorName(), buildArrayNode(node.getPosition(), node.getValue())); | |
1515 final org.jrubyparser.ast.Node writeMethod = new org.jrubyparser.ast.CallNode(node.getPosition(), readReceiverFromTemp, node.getVariableName() + "=", buildArrayNode(node.getPosition(), | |
1516 operation)); | |
1517 | |
1518 final org.jrubyparser.ast.BlockNode block = new org.jrubyparser.ast.BlockNode(node.getPosition()); | |
1519 block.add(writeReceiverToTemp); | |
1520 block.add(writeMethod); | |
1521 | |
1522 return block.accept(this); | |
1523 } | |
1524 | |
1525 @Override | |
1526 public Object visitOpAsgnOrNode(org.jrubyparser.ast.OpAsgnOrNode node) { | |
1527 /* | |
1528 * De-sugar x ||= y into x || x = y. No repeated evaluations there so it's easy. It's also | |
1529 * basically how jruby-parser represents it already. We'll do it directly, rather than via | |
1530 * another JRuby AST node. | |
1531 */ | |
1532 | |
1533 final org.jrubyparser.ast.Node lhs = node.getFirst(); | |
1534 final org.jrubyparser.ast.Node rhs = node.getSecond(); | |
1535 | |
1536 return OrNodeFactory.create(context, translate(node.getPosition()), (RubyNode) lhs.accept(this), (RubyNode) rhs.accept(this)); | |
1537 } | |
1538 | |
1539 @Override | |
1540 public Object visitOpElementAsgnNode(org.jrubyparser.ast.OpElementAsgnNode node) { | |
1541 /* | |
1542 * We're going to de-sugar a[b] += c into a[b] = a[b] + c. See discussion in | |
1543 * visitOpAsgnNode. | |
1544 */ | |
1545 | |
1546 org.jrubyparser.ast.Node index; | |
1547 | |
1548 if (node.getArgs() == null) { | |
1549 index = null; | |
1550 } else { | |
1551 index = node.getArgs().childNodes().get(0); | |
1552 } | |
1553 | |
1554 final org.jrubyparser.ast.Node operand = node.getValue(); | |
1555 | |
1556 final String temp = environment.allocateLocalTemp(); | |
1557 final org.jrubyparser.ast.Node writeArrayToTemp = new org.jrubyparser.ast.LocalAsgnNode(node.getPosition(), temp, 0, node.getReceiver()); | |
1558 final org.jrubyparser.ast.Node readArrayFromTemp = new org.jrubyparser.ast.LocalVarNode(node.getPosition(), 0, temp); | |
1559 | |
1560 final org.jrubyparser.ast.Node arrayRead = new org.jrubyparser.ast.CallNode(node.getPosition(), readArrayFromTemp, "[]", buildArrayNode(node.getPosition(), index)); | |
1561 | |
1562 final String op = node.getOperatorName(); | |
1563 | |
1564 org.jrubyparser.ast.Node operation = null; | |
1565 | |
1566 if (op.equals("||")) { | |
1567 operation = new org.jrubyparser.ast.OrNode(node.getPosition(), arrayRead, operand); | |
1568 } else if (op.equals("&&")) { | |
1569 operation = new org.jrubyparser.ast.AndNode(node.getPosition(), arrayRead, operand); | |
1570 } else { | |
1571 operation = new org.jrubyparser.ast.CallNode(node.getPosition(), arrayRead, node.getOperatorName(), buildArrayNode(node.getPosition(), operand)); | |
1572 } | |
1573 | |
1574 final org.jrubyparser.ast.Node arrayWrite = new org.jrubyparser.ast.CallNode(node.getPosition(), readArrayFromTemp, "[]=", buildArrayNode(node.getPosition(), index, operation)); | |
1575 | |
1576 final org.jrubyparser.ast.BlockNode block = new org.jrubyparser.ast.BlockNode(node.getPosition()); | |
1577 block.add(writeArrayToTemp); | |
1578 block.add(arrayWrite); | |
1579 | |
1580 return block.accept(this); | |
1581 } | |
1582 | |
1583 private static org.jrubyparser.ast.ArrayNode buildArrayNode(org.jrubyparser.SourcePosition sourcePosition, org.jrubyparser.ast.Node first, org.jrubyparser.ast.Node... rest) { | |
1584 if (first == null) { | |
1585 return new org.jrubyparser.ast.ArrayNode(sourcePosition); | |
1586 } | |
1587 | |
1588 final org.jrubyparser.ast.ArrayNode array = new org.jrubyparser.ast.ArrayNode(sourcePosition, first); | |
1589 | |
1590 for (org.jrubyparser.ast.Node node : rest) { | |
1591 array.add(node); | |
1592 } | |
1593 | |
1594 return array; | |
1595 } | |
1596 | |
1597 @Override | |
1598 public Object visitOrNode(org.jrubyparser.ast.OrNode node) { | |
1599 final SourceSection sourceSection = translate(node.getPosition()); | |
1600 | |
1601 RubyNode x; | |
1602 | |
1603 if (node.getFirst() == null) { | |
1604 x = new NilNode(context, sourceSection); | |
1605 } else { | |
1606 x = (RubyNode) node.getFirst().accept(this); | |
1607 } | |
1608 | |
1609 RubyNode y; | |
1610 | |
1611 if (node.getSecond() == null) { | |
1612 y = new NilNode(context, sourceSection); | |
1613 } else { | |
1614 y = (RubyNode) node.getSecond().accept(this); | |
1615 } | |
1616 | |
1617 return OrNodeFactory.create(context, sourceSection, x, y); | |
1618 } | |
1619 | |
1620 @Override | |
1621 public Object visitPostExeNode(org.jrubyparser.ast.PostExeNode node) { | |
1622 return unimplemented(node); | |
1623 } | |
1624 | |
1625 @Override | |
1626 public Object visitPreExeNode(org.jrubyparser.ast.PreExeNode node) { | |
1627 return unimplemented(node); | |
1628 } | |
1629 | |
1630 @Override | |
1631 public Object visitRedoNode(org.jrubyparser.ast.RedoNode node) { | |
1632 return new RedoNode(context, translate(node.getPosition())); | |
1633 } | |
1634 | |
1635 @Override | |
1636 public Object visitRegexpNode(org.jrubyparser.ast.RegexpNode node) { | |
1637 RubyRegexp regexp; | |
1638 | |
1639 try { | |
1640 final String patternText = node.getValue(); | |
1641 | |
1642 int flags = Pattern.MULTILINE | Pattern.UNIX_LINES; | |
1643 | |
1644 final org.jrubyparser.RegexpOptions options = node.getOptions(); | |
1645 | |
1646 if (options.isIgnorecase()) { | |
1647 flags |= Pattern.CASE_INSENSITIVE; | |
1648 } | |
1649 | |
1650 if (options.isMultiline()) { | |
1651 // TODO(cs): isn't this the default? | |
1652 flags |= Pattern.MULTILINE; | |
1653 } | |
1654 | |
1655 final Pattern pattern = Pattern.compile(patternText, flags); | |
1656 | |
1657 regexp = new RubyRegexp(context.getCoreLibrary().getRegexpClass(), pattern); | |
1658 } catch (PatternSyntaxException e) { | |
1659 context.implementationMessage("failed to parse Ruby regexp " + node.getValue() + " as Java regexp - replacing with ."); | |
1660 regexp = new RubyRegexp(context.getCoreLibrary().getRegexpClass(), "."); | |
1661 } | |
1662 | |
1663 final ObjectLiteralNode literalNode = new ObjectLiteralNode(context, translate(node.getPosition()), regexp); | |
1664 return literalNode; | |
1665 } | |
1666 | |
1667 @Override | |
1668 public Object visitRescueBodyNode(org.jrubyparser.ast.RescueBodyNode node) { | |
1669 return unimplemented(node); | |
1670 } | |
1671 | |
1672 @Override | |
1673 public Object visitRescueNode(org.jrubyparser.ast.RescueNode node) { | |
1674 final SourceSection sourceSection = translate(node.getPosition()); | |
1675 | |
1676 RubyNode tryPart; | |
1677 | |
1678 if (node.getBody() != null) { | |
1679 tryPart = (RubyNode) node.getBody().accept(this); | |
1680 } else { | |
1681 tryPart = new NilNode(context, sourceSection); | |
1682 } | |
1683 | |
1684 final List<RescueNode> rescueNodes = new ArrayList<>(); | |
1685 | |
1686 org.jrubyparser.ast.RescueBodyNode rescueBody = node.getRescue(); | |
1687 | |
1688 while (rescueBody != null) { | |
1689 if (rescueBody.getExceptions() != null) { | |
1690 if (rescueBody.getExceptions() instanceof org.jrubyparser.ast.ArrayNode) { | |
1691 final List<org.jrubyparser.ast.Node> exceptionNodes = ((org.jrubyparser.ast.ArrayNode) rescueBody.getExceptions()).childNodes(); | |
1692 | |
1693 final RubyNode[] handlingClasses = new RubyNode[exceptionNodes.size()]; | |
1694 | |
1695 for (int n = 0; n < handlingClasses.length; n++) { | |
1696 handlingClasses[n] = (RubyNode) exceptionNodes.get(n).accept(this); | |
1697 } | |
1698 | |
1699 RubyNode translatedBody; | |
1700 | |
1701 if (rescueBody.getBody() == null) { | |
1702 translatedBody = new NilNode(context, sourceSection); | |
1703 } else { | |
1704 translatedBody = (RubyNode) rescueBody.getBody().accept(this); | |
1705 } | |
1706 | |
1707 final RescueClassesNode rescueNode = new RescueClassesNode(context, sourceSection, handlingClasses, translatedBody); | |
1708 rescueNodes.add(rescueNode); | |
1709 } else if (rescueBody.getExceptions() instanceof org.jrubyparser.ast.SplatNode) { | |
1710 final org.jrubyparser.ast.SplatNode splat = (org.jrubyparser.ast.SplatNode) rescueBody.getExceptions(); | |
1711 | |
1712 RubyNode splatTranslated; | |
1713 | |
1714 if (splat.getValue() == null) { | |
1715 splatTranslated = new NilNode(context, sourceSection); | |
1716 } else { | |
1717 splatTranslated = (RubyNode) splat.getValue().accept(this); | |
1718 } | |
1719 | |
1720 RubyNode bodyTranslated; | |
1721 | |
1722 if (rescueBody.getBody() == null) { | |
1723 bodyTranslated = new NilNode(context, sourceSection); | |
1724 } else { | |
1725 bodyTranslated = (RubyNode) rescueBody.getBody().accept(this); | |
1726 } | |
1727 | |
1728 final RescueSplatNode rescueNode = new RescueSplatNode(context, sourceSection, splatTranslated, bodyTranslated); | |
1729 rescueNodes.add(rescueNode); | |
1730 } else { | |
1731 unimplemented(node); | |
1732 } | |
1733 } else { | |
1734 RubyNode bodyNode; | |
1735 | |
1736 if (rescueBody.getBody() == null) { | |
1737 bodyNode = new NilNode(context, sourceSection); | |
1738 } else { | |
1739 bodyNode = (RubyNode) rescueBody.getBody().accept(this); | |
1740 } | |
1741 | |
1742 final RescueAnyNode rescueNode = new RescueAnyNode(context, sourceSection, bodyNode); | |
1743 rescueNodes.add(rescueNode); | |
1744 } | |
1745 | |
1746 rescueBody = rescueBody.getOptRescue(); | |
1747 } | |
1748 | |
1749 RubyNode elsePart; | |
1750 | |
1751 if (node.getElse() != null) { | |
1752 elsePart = (RubyNode) node.getElse().accept(this); | |
1753 } else { | |
1754 elsePart = new NilNode(context, sourceSection); | |
1755 } | |
1756 | |
1757 return new TryNode(context, sourceSection, tryPart, rescueNodes.toArray(new RescueNode[rescueNodes.size()]), elsePart); | |
1758 } | |
1759 | |
1760 @Override | |
1761 public Object visitRestArgNode(org.jrubyparser.ast.RestArgNode node) { | |
1762 return unimplemented(node); | |
1763 } | |
1764 | |
1765 @Override | |
1766 public Object visitRetryNode(org.jrubyparser.ast.RetryNode node) { | |
1767 return new RetryNode(context, translate(node.getPosition())); | |
1768 } | |
1769 | |
1770 @Override | |
1771 public Object visitReturnNode(org.jrubyparser.ast.ReturnNode node) { | |
1772 final SourceSection sourceSection = translate(node.getPosition()); | |
1773 | |
1774 RubyNode translatedChild; | |
1775 | |
1776 if (node.getValue() == null) { | |
1777 translatedChild = new NilNode(context, sourceSection); | |
1778 } else { | |
1779 translatedChild = (RubyNode) node.getValue().accept(this); | |
1780 } | |
1781 | |
1782 return new ReturnNode(context, sourceSection, environment.getReturnID(), translatedChild); | |
1783 } | |
1784 | |
1785 @Override | |
1786 public Object visitRootNode(org.jrubyparser.ast.RootNode node) { | |
1787 return unimplemented(node); | |
1788 } | |
1789 | |
1790 @Override | |
1791 public Object visitSClassNode(org.jrubyparser.ast.SClassNode node) { | |
1792 final SourceSection sourceSection = translate(node.getPosition()); | |
1793 | |
1794 final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true, | |
1795 new UniqueMethodIdentifier()); | |
1796 final ModuleTranslator classTranslator = new ModuleTranslator(context, this, newEnvironment, source); | |
1797 | |
1798 final MethodDefinitionNode definitionMethod = classTranslator.compileClassNode(node.getPosition(), "singleton", node.getBody()); | |
1799 | |
1800 final RubyNode receiverNode = (RubyNode) node.getReceiver().accept(this); | |
1801 | |
1802 final SingletonClassNode singletonClassNode = new SingletonClassNode(context, sourceSection, receiverNode); | |
1803 | |
1804 return new OpenModuleNode(context, sourceSection, singletonClassNode, definitionMethod); | |
1805 } | |
1806 | |
1807 @Override | |
1808 public Object visitSValueNode(org.jrubyparser.ast.SValueNode node) { | |
1809 return node.getValue().accept(this); | |
1810 } | |
1811 | |
1812 @Override | |
1813 public Object visitSelfNode(org.jrubyparser.ast.SelfNode node) { | |
1814 return new SelfNode(context, translate(node.getPosition())); | |
1815 } | |
1816 | |
1817 @Override | |
1818 public Object visitSplatNode(org.jrubyparser.ast.SplatNode node) { | |
1819 final SourceSection sourceSection = translate(node.getPosition()); | |
1820 | |
1821 RubyNode value; | |
1822 | |
1823 if (node.getValue() == null) { | |
1824 value = new NilNode(context, sourceSection); | |
1825 } else { | |
1826 value = (RubyNode) node.getValue().accept(this); | |
1827 } | |
1828 | |
1829 return SplatCastNodeFactory.create(context, sourceSection, value); | |
1830 } | |
1831 | |
1832 @Override | |
1833 public Object visitStrNode(org.jrubyparser.ast.StrNode node) { | |
1834 return new StringLiteralNode(context, translate(node.getPosition()), node.getValue()); | |
1835 } | |
1836 | |
1837 @Override | |
1838 public Object visitSuperNode(org.jrubyparser.ast.SuperNode node) { | |
1839 return unimplemented(node); | |
1840 } | |
1841 | |
1842 @Override | |
1843 public Object visitSymbolNode(org.jrubyparser.ast.SymbolNode node) { | |
1844 return new ObjectLiteralNode(context, translate(node.getPosition()), new RubySymbol(context.getCoreLibrary().getSymbolClass(), node.getName())); | |
1845 } | |
1846 | |
1847 @Override | |
1848 public Object visitToAryNode(org.jrubyparser.ast.ToAryNode node) { | |
1849 return unimplemented(node); | |
1850 } | |
1851 | |
1852 @Override | |
1853 public Object visitTrueNode(org.jrubyparser.ast.TrueNode node) { | |
1854 return new BooleanLiteralNode(context, translate(node.getPosition()), true); | |
1855 } | |
1856 | |
1857 @Override | |
1858 public Object visitUndefNode(org.jrubyparser.ast.UndefNode node) { | |
1859 return unimplemented(node); | |
1860 } | |
1861 | |
1862 @Override | |
1863 public Object visitUntilNode(org.jrubyparser.ast.UntilNode node) { | |
1864 final SourceSection sourceSection = translate(node.getPosition()); | |
1865 | |
1866 RubyNode condition; | |
1867 | |
1868 if (node.getCondition() == null) { | |
1869 condition = new NilNode(context, sourceSection); | |
1870 } else { | |
1871 condition = (RubyNode) node.getCondition().accept(this); | |
1872 } | |
1873 | |
1874 final BooleanCastNode conditionCast = BooleanCastNodeFactory.create(context, sourceSection, condition); | |
1875 final NotNode conditionCastNot = new NotNode(context, sourceSection, conditionCast); | |
1876 final BooleanCastNode conditionCastNotCast = BooleanCastNodeFactory.create(context, sourceSection, conditionCastNot); | |
1877 | |
1878 final RubyNode body = (RubyNode) node.getBody().accept(this); | |
1879 | |
1880 return new WhileNode(context, sourceSection, conditionCastNotCast, body); | |
1881 } | |
1882 | |
1883 @Override | |
1884 public Object visitVAliasNode(org.jrubyparser.ast.VAliasNode node) { | |
1885 return unimplemented(node); | |
1886 } | |
1887 | |
1888 @Override | |
1889 public Object visitVCallNode(org.jrubyparser.ast.VCallNode node) { | |
1890 final org.jrubyparser.ast.Node receiver = new org.jrubyparser.ast.SelfNode(node.getPosition()); | |
1891 final org.jrubyparser.ast.Node args = null; | |
1892 final org.jrubyparser.ast.Node callNode = new org.jrubyparser.ast.CallNode(node.getPosition(), receiver, node.getName(), args); | |
1893 | |
1894 return callNode.accept(this); | |
1895 } | |
1896 | |
1897 @Override | |
1898 public Object visitWhenNode(org.jrubyparser.ast.WhenNode node) { | |
1899 return unimplemented(node); | |
1900 } | |
1901 | |
1902 @Override | |
1903 public Object visitWhileNode(org.jrubyparser.ast.WhileNode node) { | |
1904 final SourceSection sourceSection = translate(node.getPosition()); | |
1905 | |
1906 RubyNode condition; | |
1907 | |
1908 if (node.getCondition() == null) { | |
1909 condition = new NilNode(context, sourceSection); | |
1910 } else { | |
1911 condition = (RubyNode) node.getCondition().accept(this); | |
1912 } | |
1913 | |
1914 final BooleanCastNode conditionCast = BooleanCastNodeFactory.create(context, sourceSection, condition); | |
1915 | |
1916 final RubyNode body = (RubyNode) node.getBody().accept(this); | |
1917 | |
1918 return new WhileNode(context, sourceSection, conditionCast, body); | |
1919 } | |
1920 | |
1921 @Override | |
1922 public Object visitXStrNode(org.jrubyparser.ast.XStrNode node) { | |
1923 SourceSection sourceSection = translate(node.getPosition()); | |
1924 | |
1925 final StringLiteralNode literal = new StringLiteralNode(context, sourceSection, node.getValue()); | |
1926 | |
1927 return new SystemNode(context, sourceSection, literal); | |
1928 } | |
1929 | |
1930 @Override | |
1931 public Object visitYieldNode(org.jrubyparser.ast.YieldNode node) { | |
1932 final List<org.jrubyparser.ast.Node> arguments = new ArrayList<>(); | |
1933 | |
1934 final org.jrubyparser.ast.Node argsNode = node.getArgs(); | |
1935 | |
1936 if (argsNode != null) { | |
1937 if (argsNode instanceof org.jrubyparser.ast.ListNode) { | |
1938 arguments.addAll(((org.jrubyparser.ast.ListNode) node.getArgs()).childNodes()); | |
1939 } else { | |
1940 arguments.add(node.getArgs()); | |
1941 } | |
1942 } | |
1943 | |
1944 final List<RubyNode> argumentsTranslated = new ArrayList<>(); | |
1945 | |
1946 for (org.jrubyparser.ast.Node argument : arguments) { | |
1947 argumentsTranslated.add((RubyNode) argument.accept(this)); | |
1948 } | |
1949 | |
1950 final RubyNode[] argumentsTranslatedArray = argumentsTranslated.toArray(new RubyNode[argumentsTranslated.size()]); | |
1951 | |
1952 return new YieldNode(context, translate(node.getPosition()), argumentsTranslatedArray); | |
1953 } | |
1954 | |
1955 @Override | |
1956 public Object visitZArrayNode(org.jrubyparser.ast.ZArrayNode node) { | |
1957 final RubyNode[] values = new RubyNode[0]; | |
1958 | |
1959 return new UninitialisedArrayLiteralNode(context, translate(node.getPosition()), values); | |
1960 } | |
1961 | |
1962 @Override | |
1963 public Object visitZSuperNode(org.jrubyparser.ast.ZSuperNode node) { | |
1964 return unimplemented(node); | |
1965 } | |
1966 | |
1967 public Object visitArgumentNode(org.jrubyparser.ast.ArgumentNode node) { | |
1968 return unimplemented(node); | |
1969 } | |
1970 | |
1971 public Object visitCommentNode(org.jrubyparser.ast.CommentNode node) { | |
1972 return unimplemented(node); | |
1973 } | |
1974 | |
1975 public Object visitKeywordArgNode(org.jrubyparser.ast.KeywordArgNode node) { | |
1976 return unimplemented(node); | |
1977 } | |
1978 | |
1979 public Object visitKeywordRestArgNode(org.jrubyparser.ast.KeywordRestArgNode node) { | |
1980 return unimplemented(node); | |
1981 } | |
1982 | |
1983 public Object visitListNode(org.jrubyparser.ast.ListNode node) { | |
1984 return unimplemented(node); | |
1985 } | |
1986 | |
1987 public Object visitMethodNameNode(org.jrubyparser.ast.MethodNameNode node) { | |
1988 return unimplemented(node); | |
1989 } | |
1990 | |
1991 public Object visitOptArgNode(org.jrubyparser.ast.OptArgNode node) { | |
1992 return unimplemented(node); | |
1993 } | |
1994 | |
1995 public Object visitSyntaxNode(org.jrubyparser.ast.SyntaxNode node) { | |
1996 return unimplemented(node); | |
1997 } | |
1998 | |
1999 public Object visitImplicitNilNode(org.jrubyparser.ast.ImplicitNilNode node) { | |
2000 return new NilNode(context, translate(node.getPosition())); | |
2001 } | |
2002 | |
2003 public Object visitLambdaNode(org.jrubyparser.ast.LambdaNode node) { | |
2004 // TODO(cs): code copied and modified from visitIterNode - extract common | |
2005 | |
2006 final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(), environment.getReturnID(), false, false, new UniqueMethodIdentifier()); | |
2007 final MethodTranslator methodCompiler = new MethodTranslator(context, this, newEnvironment, false, source); | |
2008 | |
2009 org.jrubyparser.ast.ArgsNode argsNode; | |
2010 | |
2011 if (node.getVar() instanceof org.jrubyparser.ast.ArgsNode) { | |
2012 argsNode = (org.jrubyparser.ast.ArgsNode) node.getVar(); | |
2013 } else if (node.getVar() instanceof org.jrubyparser.ast.DAsgnNode) { | |
2014 final org.jrubyparser.ast.ArgumentNode arg = new org.jrubyparser.ast.ArgumentNode(node.getPosition(), ((org.jrubyparser.ast.DAsgnNode) node.getVar()).getName()); | |
2015 final org.jrubyparser.ast.ListNode preArgs = new org.jrubyparser.ast.ArrayNode(node.getPosition(), arg); | |
2016 argsNode = new org.jrubyparser.ast.ArgsNode(node.getPosition(), preArgs, null, null, null, null, null, null); | |
2017 } else if (node.getVar() == null) { | |
2018 argsNode = null; | |
2019 } else { | |
2020 throw new UnsupportedOperationException(); | |
2021 } | |
2022 | |
2023 final MethodDefinitionNode definitionNode = methodCompiler.compileFunctionNode(translate(node.getPosition()), "(lambda)", argsNode, node.getBody()); | |
2024 | |
2025 return new LambdaNode(context, translate(node.getPosition()), definitionNode); | |
2026 } | |
2027 | |
2028 public Object visitUnaryCallNode(org.jrubyparser.ast.UnaryCallNode node) { | |
2029 final org.jrubyparser.ast.Node callNode = new org.jrubyparser.ast.CallNode(node.getPosition(), node.getReceiver(), node.getName(), null, null); | |
2030 return callNode.accept(this); | |
2031 } | |
2032 | |
2033 protected Object unimplemented(org.jrubyparser.ast.Node node) { | |
2034 context.implementationMessage("warning: %s at %s does nothing", node, node.getPosition()); | |
2035 return new NilNode(context, translate(node.getPosition())); | |
2036 } | |
2037 | |
2038 protected SourceSection translate(final org.jrubyparser.SourcePosition sourcePosition) { | |
2039 try { | |
2040 // TODO(cs): get an identifier | |
2041 final String identifier = "(identifier)"; | |
2042 | |
2043 // TODO(cs): work out the start column | |
2044 final int startColumn = -1; | |
2045 | |
2046 final int charLength = sourcePosition.getEndOffset() - sourcePosition.getStartOffset(); | |
2047 | |
2048 return new DefaultSourceSection(source, identifier, sourcePosition.getStartLine() + 1, startColumn, sourcePosition.getStartOffset(), charLength); | |
2049 } catch (UnsupportedOperationException e) { | |
2050 // In some circumstances JRuby can't tell you what the position is | |
2051 return translate(new org.jrubyparser.SourcePosition("(unknown)", 0, 0)); | |
2052 } | |
2053 } | |
2054 | |
2055 protected SequenceNode initFlipFlopStates(SourceSection sourceSection) { | |
2056 final RubyNode[] initNodes = new RubyNode[environment.getFlipFlopStates().size()]; | |
2057 | |
2058 for (int n = 0; n < initNodes.length; n++) { | |
2059 initNodes[n] = new InitFlipFlopSlotNode(context, sourceSection, environment.getFlipFlopStates().get(n)); | |
2060 } | |
2061 | |
2062 return new SequenceNode(context, sourceSection, initNodes); | |
2063 } | |
2064 | |
2065 } |