Skip to content

Commit

Permalink
Combine similar switch statements in helper method
Browse files Browse the repository at this point in the history
We’re switching twice on the node type, so we should just do it once
instead.
  • Loading branch information
Lillian Zhang authored and tomstuart committed Dec 11, 2020
1 parent 35571cc commit 2c753ea
Showing 1 changed file with 46 additions and 52 deletions.
98 changes: 46 additions & 52 deletions src/main/java/org/truffleruby/parser/BodyTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,8 @@ public RubyNode visitCase3Node(Case3ParseNode node) {
private RubyNode case3PatternMatch(ParseNode patternNode, ParseNode expressionNode, RubyNode expressionValue,
SourceIndexLength sourceSection) {
final RubyCallNodeParameters deconstructCallParameters;
final RubyCallNodeParameters matcherCallParameters;
final RubyNode receiver;
final RubyNode deconstructed;

switch (patternNode.getNodeType()) {
Expand All @@ -887,7 +889,19 @@ private RubyNode case3PatternMatch(ParseNode patternNode, ParseNode expressionNo
true);
deconstructed = language.coreMethodAssumptions
.createCallNode(deconstructCallParameters, environment);
break;

receiver = new TruffleInternalModuleLiteralNode();
receiver.unsafeSetSourceSection(sourceSection);

matcherCallParameters = new RubyCallNodeParameters(
receiver,
"array_pattern_matches?",
null,
new RubyNode[]{ patternNode.accept(this), NodeUtil.cloneNode(deconstructed) },
false,
true);
return language.coreMethodAssumptions
.createCallNode(matcherCallParameters, environment);
case HASHNODE:
deconstructCallParameters = new RubyCallNodeParameters(
expressionValue,
Expand All @@ -898,58 +912,38 @@ private RubyNode case3PatternMatch(ParseNode patternNode, ParseNode expressionNo
true);
deconstructed = language.coreMethodAssumptions
.createCallNode(deconstructCallParameters, environment);
break;
default:
deconstructed = expressionValue;
}

final RubyCallNodeParameters matcherCallParameters;
final RubyNode receiver;
if (patternNode instanceof LocalVarParseNode) {
// Assigns the value of an existing variable pattern as the value of the expression.
// May need to add a case with same/similar logic for new variables.
final RubyNode assignmentNode = new LocalAsgnParseNode(
patternNode.getPosition(),
((LocalVarParseNode) patternNode).getName(),
((LocalVarParseNode) patternNode).getDepth(),
expressionNode).accept(this);
return new OrNode(assignmentNode, new BooleanLiteralNode(true));
} else if (patternNode instanceof ArrayParseNode) {
receiver = new TruffleInternalModuleLiteralNode();
receiver.unsafeSetSourceSection(sourceSection);

matcherCallParameters = new RubyCallNodeParameters(
receiver,
"array_pattern_matches?",
null,
new RubyNode[]{ patternNode.accept(this), NodeUtil.cloneNode(deconstructed) },
false,
true);
return language.coreMethodAssumptions
.createCallNode(matcherCallParameters, environment);
} else if (patternNode instanceof HashParseNode) {
receiver = new TruffleInternalModuleLiteralNode();
receiver.unsafeSetSourceSection(sourceSection);

matcherCallParameters = new RubyCallNodeParameters(
receiver,
"hash_pattern_matches?",
null,
new RubyNode[]{ patternNode.accept(this), NodeUtil.cloneNode(deconstructed) },
false,
true);
return language.coreMethodAssumptions
.createCallNode(matcherCallParameters, environment);
} else {
matcherCallParameters = new RubyCallNodeParameters(
patternNode.accept(this),
"===",
null,
new RubyNode[]{ NodeUtil.cloneNode(deconstructed) },
false,
true);
return language.coreMethodAssumptions
.createCallNode(matcherCallParameters, environment);
receiver = new TruffleInternalModuleLiteralNode();
receiver.unsafeSetSourceSection(sourceSection);

matcherCallParameters = new RubyCallNodeParameters(
receiver,
"hash_pattern_matches?",
null,
new RubyNode[]{ patternNode.accept(this), NodeUtil.cloneNode(deconstructed) },
false,
true);
return language.coreMethodAssumptions
.createCallNode(matcherCallParameters, environment);
case LOCALVARNODE:
// Assigns the value of an existing variable pattern as the value of the expression.
// May need to add a case with same/similar logic for new variables.
final RubyNode assignmentNode = new LocalAsgnParseNode(
patternNode.getPosition(),
((LocalVarParseNode) patternNode).getName(),
((LocalVarParseNode) patternNode).getDepth(),
expressionNode).accept(this);
return new OrNode(assignmentNode, new BooleanLiteralNode(true));
default:
matcherCallParameters = new RubyCallNodeParameters(
patternNode.accept(this),
"===",
null,
new RubyNode[]{ NodeUtil.cloneNode(expressionValue) },
false,
true);
return language.coreMethodAssumptions
.createCallNode(matcherCallParameters, environment);
}
}

Expand Down

0 comments on commit 2c753ea

Please sign in to comment.