-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GR-34365] Implement Pattern Matching in TruffleRuby (GSoC) (#2683)
PullRequest: truffleruby/3780
- Loading branch information
Showing
33 changed files
with
8,356 additions
and
5,854 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/org/truffleruby/core/array/ArrayPatternLengthCheckNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2022, 2023 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 2.0, or | ||
* GNU General Public License version 2, or | ||
* GNU Lesser General Public License version 2.1. | ||
*/ | ||
package org.truffleruby.core.array; | ||
|
||
import com.oracle.truffle.api.profiles.ConditionProfile; | ||
import org.truffleruby.language.RubyContextSourceNode; | ||
import org.truffleruby.language.RubyNode; | ||
|
||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
|
||
public class ArrayPatternLengthCheckNode extends RubyContextSourceNode { | ||
|
||
@Child RubyNode currentValueToMatch; | ||
final int patternLength; | ||
final boolean hasRest; | ||
|
||
final ConditionProfile isArrayProfile = ConditionProfile.create(); | ||
|
||
public ArrayPatternLengthCheckNode(int patternLength, RubyNode currentValueToMatch, boolean hasRest) { | ||
this.currentValueToMatch = currentValueToMatch; | ||
this.patternLength = patternLength; | ||
this.hasRest = hasRest; | ||
} | ||
|
||
@Override | ||
public Object execute(VirtualFrame frame) { | ||
Object matchArray = currentValueToMatch.execute(frame); | ||
if (isArrayProfile.profile(matchArray instanceof RubyArray)) { | ||
long size = ((RubyArray) matchArray).getArraySize(); | ||
if (hasRest) { | ||
return patternLength <= size; | ||
} else { | ||
return patternLength == size; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public RubyNode cloneUninitialized() { | ||
return new ArrayPatternLengthCheckNode(patternLength, currentValueToMatch.cloneUninitialized(), hasRest) | ||
.copyFlags(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/org/truffleruby/language/control/ExecuteAndReturnTrueNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2022, 2023 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 2.0, or | ||
* GNU General Public License version 2, or | ||
* GNU Lesser General Public License version 2.1. | ||
*/ | ||
package org.truffleruby.language.control; | ||
|
||
import org.truffleruby.language.RubyContextSourceNode; | ||
import org.truffleruby.language.RubyNode; | ||
|
||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
|
||
public final class ExecuteAndReturnTrueNode extends RubyContextSourceNode { | ||
|
||
@Child RubyNode child; | ||
|
||
public ExecuteAndReturnTrueNode(RubyNode child) { | ||
this.child = child; | ||
} | ||
|
||
@Override | ||
public Object execute(VirtualFrame frame) { | ||
child.doExecuteVoid(frame); | ||
return true; | ||
} | ||
|
||
@Override | ||
public RubyNode cloneUninitialized() { | ||
return new ExecuteAndReturnTrueNode(child.cloneUninitialized()).copyFlags(this); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/truffleruby/language/control/NoMatchingPatternNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2022, 2023 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 2.0, or | ||
* GNU General Public License version 2, or | ||
* GNU Lesser General Public License version 2.1. | ||
*/ | ||
package org.truffleruby.language.control; | ||
|
||
import com.oracle.truffle.api.dsl.Cached; | ||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import org.truffleruby.language.RubyContextSourceNode; | ||
|
||
import org.truffleruby.language.RubyNode; | ||
import org.truffleruby.language.dispatch.DispatchNode; | ||
|
||
@NodeChild(value = "expressionNode", type = RubyNode.class) | ||
public abstract class NoMatchingPatternNode extends RubyContextSourceNode { | ||
|
||
protected abstract RubyNode getExpressionNode(); | ||
|
||
@Specialization | ||
protected Object noMatchingPattern(Object expression, | ||
@Cached DispatchNode inspectNode) { | ||
Object inspected = inspectNode.call(coreLibrary().truffleTypeModule, "rb_inspect", expression); | ||
throw new RaiseException(getContext(), coreExceptions().noMatchingPatternError(inspected, this)); | ||
} | ||
|
||
@Override | ||
public RubyNode cloneUninitialized() { | ||
return NoMatchingPatternNodeGen.create(getExpressionNode().cloneUninitialized()).copyFlags(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 2.0, or | ||
* GNU General Public License version 2, or | ||
* GNU Lesser General Public License version 2.1. | ||
*/ | ||
package org.truffleruby.parser; | ||
|
||
import com.oracle.truffle.api.TruffleSafepoint; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.source.Source; | ||
import org.truffleruby.RubyLanguage; | ||
import org.truffleruby.core.DummyNode; | ||
import org.truffleruby.language.RubyNode; | ||
import org.truffleruby.language.SourceIndexLength; | ||
import org.truffleruby.parser.ast.ParseNode; | ||
|
||
public abstract class BaseTranslator extends Translator { | ||
|
||
protected final TranslatorEnvironment environment; | ||
|
||
public BaseTranslator( | ||
RubyLanguage language, | ||
Source source, | ||
ParserContext parserContext, | ||
Node currentNode, | ||
TranslatorEnvironment environment) { | ||
super(language, source, parserContext, currentNode); | ||
this.environment = environment; | ||
} | ||
|
||
protected RubyNode addNewlineIfNeeded(ParseNode jrubyNode, RubyNode node) { | ||
if (jrubyNode.isNewline()) { | ||
TruffleSafepoint.poll(DummyNode.INSTANCE); | ||
|
||
final SourceIndexLength current = node.getEncapsulatingSourceIndexLength(); | ||
|
||
if (current == null) { | ||
return node; | ||
} | ||
|
||
if (environment.getParseEnvironment().isCoverageEnabled()) { | ||
node.unsafeSetIsCoverageLine(); | ||
language.coverageManager.setLineHasCode(source, current.toSourceSection(source).getStartLine()); | ||
} | ||
node.unsafeSetIsNewLine(); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
} |
Oops, something went wrong.