From 7c020fec68d5904d351cc85986f8095e83196708 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Wed, 11 Oct 2023 17:07:42 +0300 Subject: [PATCH] Refactor YARPTranslator and add helper method to translate array of nodes --- .../truffleruby/parser/YARPTranslator.java | 59 +++++++------------ 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/truffleruby/parser/YARPTranslator.java b/src/main/java/org/truffleruby/parser/YARPTranslator.java index 1f89ba29f0e6..bd12b3a35c79 100644 --- a/src/main/java/org/truffleruby/parser/YARPTranslator.java +++ b/src/main/java/org/truffleruby/parser/YARPTranslator.java @@ -224,12 +224,7 @@ 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); @@ -237,11 +232,7 @@ public RubyNode visitArgumentsNode(Nodes.ArgumentsNode node) { } 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; @@ -388,11 +379,7 @@ private RescueNode translateExceptionNodes(ArrayList 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( @@ -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; @@ -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) { @@ -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; @@ -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); } @@ -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; + } }