Skip to content

Commit

Permalink
Refactor YARPTranslator and add helper method to translate array of n…
Browse files Browse the repository at this point in the history
…odes
  • Loading branch information
andrykonchin committed Oct 12, 2023
1 parent 33b0a61 commit 7c020fe
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions src/main/java/org/truffleruby/parser/YARPTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,15 @@ public RubyNode visitArgumentsNode(Nodes.ArgumentsNode node) {
return values[0].accept(this);
}

final RubyNode[] translatedValues = createArray(values.length);

for (int n = 0; n < values.length; n++) {
translatedValues[n] = values[n].accept(this);
}

final RubyNode[] translatedValues = translate(values);
final RubyNode rubyNode = ArrayLiteralNode.create(language, translatedValues);
assignNodePositionInSource(node, rubyNode);

return rubyNode;
}

public RubyNode visitArrayNode(Nodes.ArrayNode node) {
RubyNode[] elements = new RubyNode[node.elements.length];
for (int i = 0; i < node.elements.length; i++) {
elements[i] = node.elements[i].accept(this);
}

RubyNode[] elements = translate(node.elements);
RubyNode rubyNode = ArrayLiteralNode.create(language, elements);
assignNodePositionInSource(node, rubyNode);
return rubyNode;
Expand Down Expand Up @@ -388,11 +379,7 @@ private RescueNode translateExceptionNodes(ArrayList<Nodes.Node> exceptionNodes,
RubyNode translatedBody = translateNodeOrNil(rescueClause.statements);

final Nodes.Node[] exceptionNodesArray = exceptionNodes.toArray(Nodes.Node.EMPTY_ARRAY);
final RubyNode[] handlingClasses = new RubyNode[exceptionNodesArray.length];

for (int i = 0; i < exceptionNodesArray.length; i++) {
handlingClasses[i] = exceptionNodesArray[i].accept(this);
}
final RubyNode[] handlingClasses = translate(exceptionNodesArray);

if (rescueClause.reference != null) {
final RubyNode exceptionWriteNode = translateRescueException(
Expand Down Expand Up @@ -446,10 +433,7 @@ public RubyNode visitCallNode(Nodes.CallNode node) {
arguments = node.arguments.arguments;
}

var translatedArguments = new RubyNode[arguments.length];
for (int i = 0; i < arguments.length; i++) {
translatedArguments[i] = arguments[i].accept(this);
}
var translatedArguments = translate(arguments);

// If the receiver is explicit or implicit 'self' then we can call private methods
final boolean ignoreVisibility = node.receiver == null || node.receiver instanceof Nodes.SelfNode;
Expand Down Expand Up @@ -1409,12 +1393,8 @@ public RubyNode visitSplatNode(Nodes.SplatNode node) {
}

public RubyNode visitStatementsNode(Nodes.StatementsNode node) {
var body = node.body;
var translated = new RubyNode[body.length];
for (int i = 0; i < body.length; i++) {
translated[i] = body[i].accept(this);
}
return sequence(node, Arrays.asList(translated));
RubyNode[] rubyNodes = translate(node.body);
return sequence(node, Arrays.asList(rubyNodes));
}

public RubyNode visitStringConcatNode(Nodes.StringConcatNode node) {
Expand Down Expand Up @@ -1452,11 +1432,7 @@ public RubyNode visitTrueNode(Nodes.TrueNode node) {
}

public RubyNode visitUndefNode(Nodes.UndefNode node) {
RubyNode[] names = new RubyNode[node.names.length];
for (int i = 0; i < node.names.length; i++) {
names[i] = node.names[i].accept(this);
}

final RubyNode[] names = translate(node.names);
final RubyNode rubyNode = new ModuleNodes.UndefNode(names);
assignNodePositionInSource(node, rubyNode);
return rubyNode;
Expand Down Expand Up @@ -1543,12 +1519,7 @@ private RubyNode translateExpressionsList(Nodes.Node[] nodes) {
// fast path (no SplatNode)

if (!containSplatOperator) {
RubyNode[] rubyNodes = new RubyNode[nodes.length];

for (int i = 0; i < nodes.length; i++) {
rubyNodes[i] = nodes[i].accept(this);
}

RubyNode[] rubyNodes = translate(nodes);
return ArrayLiteralNode.create(language, rubyNodes);
}

Expand Down Expand Up @@ -1975,4 +1946,18 @@ private boolean containYARPSplatNode(Nodes.Node[] nodes) {

return false;
}

private RubyNode[] translate(Nodes.Node[] nodes) {
if (nodes.length == 0) {
return RubyNode.EMPTY_ARRAY;
}

RubyNode[] rubyNodes = new RubyNode[nodes.length];

for (int i = 0; i < nodes.length; i++) {
rubyNodes[i] = nodes[i].accept(this);
}

return rubyNodes;
}
}

0 comments on commit 7c020fe

Please sign in to comment.