|
| 1 | +/* |
| 2 | + * Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved. This |
| 3 | + * code is released under a tri EPL/GPL/LGPL license. You can use it, |
| 4 | + * redistribute it and/or modify it under the terms of the: |
| 5 | + * |
| 6 | + * Eclipse Public License version 2.0, or |
| 7 | + * GNU General Public License version 2, or |
| 8 | + * GNU Lesser General Public License version 2.1. |
| 9 | + */ |
| 10 | +package org.truffleruby.parser; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | + |
| 14 | +import com.oracle.truffle.api.CompilerDirectives; |
| 15 | +import org.prism.Nodes; |
| 16 | +import org.truffleruby.RubyLanguage; |
| 17 | +import org.truffleruby.core.array.ArrayIndexNodes; |
| 18 | +import org.truffleruby.core.array.ArrayPatternLengthCheckNode; |
| 19 | +import org.truffleruby.core.array.ArraySliceNodeGen; |
| 20 | +import org.truffleruby.language.RubyNode; |
| 21 | +import org.truffleruby.language.control.AndNodeGen; |
| 22 | +import org.truffleruby.language.control.ExecuteAndReturnTrueNode; |
| 23 | +import org.truffleruby.language.control.NotNodeGen; |
| 24 | +import org.truffleruby.language.literal.TruffleInternalModuleLiteralNode; |
| 25 | +import org.truffleruby.language.locals.ReadLocalNode; |
| 26 | +import org.truffleruby.language.locals.WriteLocalNode; |
| 27 | + |
| 28 | + |
| 29 | +/** Translate the pattern of pattern matching. Executing the translated node must result in true or false. Every visit |
| 30 | + * method here should return a node which is the condition of whether it matched. Visit methods can change & restore |
| 31 | + * {@code currentValueToMatch} to change which expression is being matched against. */ |
| 32 | +public final class YARPPatternMatchingTranslator extends YARPBaseTranslator { |
| 33 | + |
| 34 | + private final YARPTranslator yarpTranslator; |
| 35 | + |
| 36 | + private RubyNode currentValueToMatch; |
| 37 | + |
| 38 | + public YARPPatternMatchingTranslator( |
| 39 | + RubyLanguage language, |
| 40 | + TranslatorEnvironment environment, |
| 41 | + RubySource rubySource, |
| 42 | + YARPTranslator yarpTranslator) { |
| 43 | + super(language, environment, rubySource); |
| 44 | + this.yarpTranslator = yarpTranslator; |
| 45 | + } |
| 46 | + |
| 47 | + public RubyNode translatePatternNode(Nodes.Node patternNode, RubyNode expressionValue) { |
| 48 | + currentValueToMatch = expressionValue; |
| 49 | + return patternNode.accept(this); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public RubyNode visitIfNode(Nodes.IfNode node) { // a guard like `in [a] if a.even?` |
| 54 | + assert node.statements.body.length == 1; |
| 55 | + var pattern = node.statements.body[0].accept(this); |
| 56 | + var condition = node.predicate.accept(yarpTranslator); // translate after the pattern which might introduce new variables |
| 57 | + return AndNodeGen.create(pattern, condition); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public RubyNode visitUnlessNode(Nodes.UnlessNode node) { // a guard like `in [a] unless a.even?` |
| 62 | + assert node.statements.body.length == 1; |
| 63 | + var pattern = node.statements.body[0].accept(this); |
| 64 | + var condition = NotNodeGen.create(node.predicate.accept(yarpTranslator)); // translate after the pattern which might introduce new variables |
| 65 | + return AndNodeGen.create(pattern, condition); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public RubyNode visitArrayPatternNode(Nodes.ArrayPatternNode node) { |
| 70 | + var preNodes = node.requireds; |
| 71 | + var restNode = node.rest; |
| 72 | + var postNodes = node.posts; |
| 73 | + |
| 74 | + int preSize = preNodes.length; |
| 75 | + int postSize = postNodes.length; |
| 76 | + |
| 77 | + var deconstructed = createCallNode(new TruffleInternalModuleLiteralNode(), "deconstruct_checked", |
| 78 | + currentValueToMatch); |
| 79 | + |
| 80 | + final int deconstructedSlot = environment.declareLocalTemp("pattern_deconstruct_array"); |
| 81 | + final ReadLocalNode readTemp = environment.readNode(deconstructedSlot, node); |
| 82 | + final RubyNode assignTemp = readTemp.makeWriteNode(deconstructed); |
| 83 | + currentValueToMatch = readTemp; |
| 84 | + |
| 85 | + RubyNode condition = new ArrayPatternLengthCheckNode(preSize + postSize, |
| 86 | + currentValueToMatch, restNode != null); |
| 87 | + |
| 88 | + if (node.constant != null) { // Constant[a] |
| 89 | + condition = AndNodeGen.create(matchValue(node.constant), condition); |
| 90 | + } |
| 91 | + |
| 92 | + for (int i = 0; i < preNodes.length; i++) { |
| 93 | + var preNode = preNodes[i]; |
| 94 | + |
| 95 | + RubyNode prev = currentValueToMatch; |
| 96 | + currentValueToMatch = ArrayIndexNodes.ReadConstantIndexNode.create(currentValueToMatch, i); |
| 97 | + try { |
| 98 | + condition = AndNodeGen.create(condition, preNode.accept(this)); |
| 99 | + } finally { |
| 100 | + currentValueToMatch = prev; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (restNode != null) { |
| 105 | + if (restNode instanceof Nodes.SplatNode splatNode) { |
| 106 | + if (splatNode.expression != null) { |
| 107 | + RubyNode prev = currentValueToMatch; |
| 108 | + currentValueToMatch = ArraySliceNodeGen.create(preSize, -postSize, currentValueToMatch); |
| 109 | + try { |
| 110 | + condition = AndNodeGen.create(condition, splatNode.expression.accept(this)); |
| 111 | + } finally { |
| 112 | + currentValueToMatch = prev; |
| 113 | + } |
| 114 | + } else { // in [1, *, 2] |
| 115 | + // Nothing |
| 116 | + } |
| 117 | + } else if (restNode instanceof Nodes.ImplicitRestNode) { // in [0, 1,] |
| 118 | + // Nothing |
| 119 | + } else { |
| 120 | + throw CompilerDirectives.shouldNotReachHere(node.getClass().getName()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + for (int i = 0; i < postNodes.length; i++) { |
| 125 | + var postNode = postNodes[i]; |
| 126 | + int index = -postNodes.length + i; |
| 127 | + RubyNode prev = currentValueToMatch; |
| 128 | + currentValueToMatch = ArrayIndexNodes.ReadConstantIndexNode.create(currentValueToMatch, index); |
| 129 | + try { |
| 130 | + condition = AndNodeGen.create(condition, postNode.accept(this)); |
| 131 | + } finally { |
| 132 | + currentValueToMatch = prev; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return YARPTranslator.sequence(node, Arrays.asList(assignTemp, condition)); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public RubyNode visitHashPatternNode(Nodes.HashPatternNode node) { |
| 141 | + // var deconstructed = createCallNode(currentValueToMatch, "deconstruct_keys", new NilLiteralNode()); |
| 142 | + |
| 143 | + // Not correct, the pattern cannot always be represented as a runtime value and this overflows the stack: |
| 144 | + // return createCallNode( |
| 145 | + // new TruffleInternalModuleLiteralNode(), |
| 146 | + // "hash_pattern_matches?", |
| 147 | + // node.accept(this), |
| 148 | + // NodeUtil.cloneNode(deconstructed)); |
| 149 | + |
| 150 | + return defaultVisit(node); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public RubyNode visitLocalVariableTargetNode(Nodes.LocalVariableTargetNode node) { |
| 155 | + WriteLocalNode writeLocalNode = yarpTranslator.visitLocalVariableTargetNode(node); |
| 156 | + writeLocalNode.setValueNode(currentValueToMatch); |
| 157 | + return new ExecuteAndReturnTrueNode(writeLocalNode); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public RubyNode visitPinnedVariableNode(Nodes.PinnedVariableNode node) { |
| 162 | + return matchValue(node.variable); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public RubyNode visitPinnedExpressionNode(Nodes.PinnedExpressionNode node) { |
| 167 | + return matchValue(node.expression); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + protected RubyNode defaultVisit(Nodes.Node node) { |
| 172 | + return matchValue(node); |
| 173 | + } |
| 174 | + |
| 175 | + private RubyNode matchValue(Nodes.Node value) { |
| 176 | + RubyNode translatedValue = value.accept(yarpTranslator); |
| 177 | + return createCallNode(translatedValue, "===", currentValueToMatch); |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments