Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding getMatchFingerPrint and getConcreteMatchFingerPrint to implement a part of the compiled-runtime with more efficiency #1873

Merged
merged 20 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
<includes>
<include>**/org/rascalmpl/test/AllSuiteParallel.java</include>
<include>**/org/rascalmpl/test/value/AllTests.java</include>
<include>**/org/rascalmpl/*Test.java</include>
</includes>
</configuration>
</plugin>
Expand Down Expand Up @@ -361,10 +362,10 @@
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<dependency>
<groupId>io.usethesource</groupId>
<artifactId>vallang</artifactId>
<version>0.15.1</version>
<version>1.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/org/rascalmpl/interpreter/result/AbstractFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.values.RascalValueFactory;
import org.rascalmpl.values.functions.IFunction;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IExternalValue;
import io.usethesource.vallang.IListWriter;
import io.usethesource.vallang.ISourceLocation;
Expand Down
4 changes: 3 additions & 1 deletion src/org/rascalmpl/library/vis/Charts.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ data ChartType
data ChartOptions
= chartOptions(
bool responsive=true,
bool animations=false,
jurgenvinju marked this conversation as resolved.
Show resolved Hide resolved
ChartPlugins plugins = chartPlugins()
);

Expand Down Expand Up @@ -395,13 +396,14 @@ A chart has a typical default layout that we can reuse for all kinds of chart ty
provides the template and immediately instantiates the client and the server to start displaying the chart
in a browser.
}
Response(Request) chartServer(ChartData theData, ChartType \type=\bar(), str title="Chart", ChartAutoColorMode colorMode=\data(), bool legend=false)
Response(Request) chartServer(ChartData theData, ChartType \type=\bar(), str title="Chart", ChartAutoColorMode colorMode=\data(), bool legend=false, bool animations=false)
= chartServer(
chart(
\type=\type,
\data=theData,
options=chartOptions(
responsive=true,
animations=animations,
plugins=chartPlugins(
legend=chartLegend(
position=top(),
Expand Down
5 changes: 5 additions & 0 deletions src/org/rascalmpl/tasks/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@
map.remove(k);
removed.add(k);
}

@Override
public int getMatchFingerprint() {
return hashCode();

Check warning on line 423 in src/org/rascalmpl/tasks/Transaction.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/tasks/Transaction.java#L423

Added line #L423 was not covered by tests
}
}

class Key {
Expand Down
99 changes: 64 additions & 35 deletions src/org/rascalmpl/values/RascalValueFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.Supplier;

import org.rascalmpl.parser.gtd.util.ArrayList;
import org.rascalmpl.types.NonTerminalType;
import org.rascalmpl.types.RascalTypeFactory;
import org.rascalmpl.types.TypeReifier;
import org.rascalmpl.values.parsetrees.ITree;
Expand All @@ -30,7 +31,6 @@

import io.usethesource.capsule.util.collection.AbstractSpecialisedImmutableMap;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IExternalValue;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.IListWriter;
Expand Down Expand Up @@ -385,8 +385,16 @@

@Override
public IConstructor reifiedType(IConstructor symbol, IMap definitions) {
// This is where the "reified type contract" is implemented.
// A few inocuous lines of code that have a lot riding on them.

// Contract: The dynamic type of a `type(symbol, definitionsMap)` constructor instance
// is `type[what the symbol value represents]`.
// So here the symbol value is "unlifted" to the {@see Type} representation (by `symbolToType`).
// Therefore you can count on that, for example, `type(int(), ())` has type `type[int]`.
java.util.Map<Type,Type> bindings =
Collections.singletonMap(RascalValueFactory.TypeParam, tr.symbolToType(symbol, definitions));

return super.constructor(RascalValueFactory.Type_Reified.instantiate(bindings), symbol, definitions);
}

Expand Down Expand Up @@ -505,13 +513,18 @@
* and {@link AbstractArgumentList} abstract classes.
*/

static class CharInt implements ITree, IExternalValue {
static class CharInt implements ITree {
final int ch;

@Override
public boolean isChar() {
return true;
}

@Override
public int getConcreteMatchFingerprint() {
return 3052374 /* "char".hashCode() */ + ch;

Check warning on line 526 in src/org/rascalmpl/values/RascalValueFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/RascalValueFactory.java#L526

Added line #L526 was not covered by tests
}

@Override
public INode setChildren(IValue[] childArray) {
Expand All @@ -526,11 +539,6 @@
public CharInt(int ch) {
this.ch = ch;
}

@Override
public IConstructor encodeAsConstructor() {
return this;
}

@Override
public IValue get(int i) throws IndexOutOfBoundsException {
Expand Down Expand Up @@ -675,12 +683,17 @@
}
}

private static class CharByte implements ITree, IExternalValue {
private static class CharByte implements ITree {
final byte ch;

public CharByte(byte ch) {
this.ch = ch;
}

@Override
public int getConcreteMatchFingerprint() {
return 3052374 /* "char".hashCode() */ + ch;
}

@Override
public boolean isChar() {
Expand All @@ -696,11 +709,6 @@
public <E extends Throwable> ITree accept(TreeVisitor<E> v) throws E {
return (ITree) v.visitTreeChar(this);
}

@Override
public IConstructor encodeAsConstructor() {
return this;
}

@Override
public IValue get(int i) throws IndexOutOfBoundsException {
Expand Down Expand Up @@ -850,14 +858,19 @@
}
}

private static class Cycle implements ITree, IExternalValue {
private static class Cycle implements ITree {
protected final IConstructor symbol;
protected final int cycleLength;

public Cycle(IConstructor symbol, int cycleLength) {
this.symbol = symbol;
this.cycleLength = cycleLength;
}

@Override
public int getConcreteMatchFingerprint() {
return 95131878 /* "cycle".hashCode() */ + 13 * symbol.hashCode();
jurgenvinju marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean isCycle() {
Expand All @@ -869,11 +882,6 @@
return (ITree) v.visitTreeCycle(this);
}

@Override
public IConstructor encodeAsConstructor() {
return this;
}

@Override
public String getName() {
return Tree_Cycle.getName();
Expand Down Expand Up @@ -1034,28 +1042,29 @@
}
}

private static class Amb implements ITree, IExternalValue {
private static class Amb implements ITree {
protected final ISet alternatives;

public Amb(ISet alts) {
assert alts.size() > 0;
this.alternatives = alts;
}

@Override
public int getConcreteMatchFingerprint() {
return 96694 /* "amb".hashCode() */ + 43 * ((NonTerminalType) alternatives.getElementType()).getSymbol().hashCode();
}

@Override
public boolean isAmb() {
return true;
}

@Override
public <E extends Throwable> ITree accept(TreeVisitor<E> v) throws E {
return (ITree) v.visitTreeAmb(this);
}

@Override
public IConstructor encodeAsConstructor() {
return this;
}

@Override
public String getName() {
return Tree_Amb.getName();
Expand Down Expand Up @@ -1114,8 +1123,8 @@
public IValue next() {
count++;
switch(count) {
case 1: return getAlternatives();
default: return null;
case 1: return getAlternatives();
default: return null;

Check warning on line 1127 in src/org/rascalmpl/values/RascalValueFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/RascalValueFactory.java#L1127

Added line #L1127 was not covered by tests
}
}
};
Expand Down Expand Up @@ -1230,6 +1239,11 @@
super(content, parameters);
}

@Override
public int getConcreteMatchFingerprint() {
return ((ITree) content).getConcreteMatchFingerprint();

Check warning on line 1244 in src/org/rascalmpl/values/RascalValueFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/RascalValueFactory.java#L1244

Added line #L1244 was not covered by tests
}

@Override
public <E extends Throwable> ITree accept(TreeVisitor<E> v) throws E {
return v.visitTreeAppl(this);
Expand Down Expand Up @@ -1281,6 +1295,11 @@
super(content, parameters);
}

@Override
public int getConcreteMatchFingerprint() {
return ((ITree) content).getConcreteMatchFingerprint();

Check warning on line 1300 in src/org/rascalmpl/values/RascalValueFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/RascalValueFactory.java#L1300

Added line #L1300 was not covered by tests
}

@Override
public <E extends Throwable> ITree accept(TreeVisitor<E> v) throws E {
return v.visitTreeAmb(this);
Expand Down Expand Up @@ -1327,6 +1346,11 @@
super(content, parameters);
}

@Override
public int getConcreteMatchFingerprint() {
return ((ITree) content).getConcreteMatchFingerprint();

Check warning on line 1351 in src/org/rascalmpl/values/RascalValueFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/RascalValueFactory.java#L1351

Added line #L1351 was not covered by tests
}

@Override
public <E extends Throwable> ITree accept(TreeVisitor<E> v) throws E {
return v.visitTreeCycle(this);
Expand Down Expand Up @@ -1369,6 +1393,11 @@
super(content, parameters);
}

@Override
public int getConcreteMatchFingerprint() {
return ((ITree) content).getConcreteMatchFingerprint();

Check warning on line 1398 in src/org/rascalmpl/values/RascalValueFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/RascalValueFactory.java#L1398

Added line #L1398 was not covered by tests
jurgenvinju marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public <E extends Throwable> ITree accept(TreeVisitor<E> v) throws E {
return v.visitTreeChar(this);
Expand Down Expand Up @@ -1410,12 +1439,17 @@
}
}

private static abstract class AbstractAppl implements ITree, IExternalValue {
private static abstract class AbstractAppl implements ITree {
protected final IConstructor production;
protected final boolean isMatchIgnorable;
protected Type type = null;


@Override
public int getConcreteMatchFingerprint() {
return 3568542 /* "tree".hashCode() */ + 41 * production.hashCode();
}

@Override
public <E extends Throwable> ITree accept(TreeVisitor<E> v) throws E {
return v.visitTreeAppl(this);
Expand All @@ -1439,11 +1473,6 @@
return true;
}

@Override
public IConstructor encodeAsConstructor() {
return this;
}

@Override
public String getName() {
return Tree_Appl.getName();
Expand Down
5 changes: 5 additions & 0 deletions src/org/rascalmpl/values/functions/IFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

public interface IFunction extends IExternalValue {

@Override
default int getMatchFingerprint() {
return 3154628 /* "func".hashCode() */ + 89 * getType().hashCode();
}

/**
* Invokes the receiver function.
*
Expand Down
36 changes: 34 additions & 2 deletions src/org/rascalmpl/values/parsetrees/ITree.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,46 @@
import org.rascalmpl.values.parsetrees.visitors.TreeVisitor;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IExternalValue;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.INode;
import io.usethesource.vallang.ISet;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.visitors.IValueVisitor;

public interface ITree extends IConstructor {
public interface ITree extends IConstructor, IExternalValue {

@Override
default IConstructor encodeAsConstructor() {
return this;

Check warning on line 18 in src/org/rascalmpl/values/parsetrees/ITree.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/parsetrees/ITree.java#L18

Added line #L18 was not covered by tests
}

@Override
default <T, E extends Throwable> T accept(IValueVisitor<T, E> v) throws E {
return v.visitExternal(this);

Check warning on line 23 in src/org/rascalmpl/values/parsetrees/ITree.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/values/parsetrees/ITree.java#L23

Added line #L23 was not covered by tests
}

@Override
default int getMatchFingerprint() {
// ITrees must simulate their constructor prints in case
// we pattern match on the abstract Tree data-type
return IConstructor.super.getMatchFingerprint();
}

/**
* Concrete patterns need another layer of fingerprinting on top
* of `getMatchFingerprint`. The reason is that _the same IValue_
* can be matched against an abstract pattern of the Tree data-type,
* and against concrete patterns.
*
* Like before, the match-fingerprint contract is:
* if pattern.match(tree) ==> pattern.fingerprint() == match.fingerprint();
*
* @return a unique code for each outermost ITree node
*/
int getConcreteMatchFingerprint();

default boolean isAppl() {
return false;
}
Expand Down Expand Up @@ -55,4 +87,4 @@

return result;
}
}
}
1 change: 1 addition & 0 deletions src/org/rascalmpl/values/parsetrees/TreeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ else if (isChar(tree)) {
return SymbolAdapter.charClass(TreeAdapter.getCharacter(tree));
}
else if (isAmb(tree)) {
// ambiguities are never empty
return getType((ITree) getAlternatives(tree).iterator().next());
}
throw new ImplementationError("ITree does not have a type");
Expand Down
Loading
Loading