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

added IValue.getPatternMatchFingerprint() that simulates rascal-core:TopLevelType.getFingerprint(v) exactly for the value kinds we have in vallang #210

Merged
merged 12 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions src/main/java/io/usethesource/vallang/IConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
*/
public interface IConstructor extends INode {

@Override
default int getPatternMatchFingerprint() {
return getName().hashCode() << 2 + arity();
DavyLandman marked this conversation as resolved.
Show resolved Hide resolved

// TODO: this would distinguish more constructors based on incomparable argument types:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost committed this, but I realize this has to be reflected in the switch code generator in the compiler as well. So we need to have a java function that produces the hashCode for a given constructor then. We can push this to the future. We only have to realize that changing these hashCodes breaks all earlier compiled Rascal code, always.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed. We already have this at the moment in util::Reflective via the functions getFingerprint (2 versions) and getFingerprintNode. Note that the former has a boolean parameter concretePatterns. In the new compiler set-up a single function getFingerprint(value v) will do. (In the compiler runtime I have a function getConcreteFingerprint that operates on concrete syntax trees.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So leave as is for now and change later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes let's do that

// return getUninstantiatedConstructorType().hashCode();
}

/**
* @return the specific ConstructorType of this constructor
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/usethesource/vallang/IExternalValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
* Note that NORMAL USE OF THE PDB DOES NOT REQUIRE IMPLEMENTING THIS INTERFACE
*/
public interface IExternalValue extends IValue {
/**
* External values must re-think their pattern match fingerprint,
* instead of returning `IValue.hashCode()` automatically.
*/
@Override
int getPatternMatchFingerprint();

/**
* @return an ExternalType
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/usethesource/vallang/IList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
import io.usethesource.vallang.visitors.IValueVisitor;

public interface IList extends ICollection<IList> {

@Override
default int getPatternMatchFingerprint() {
return 3322014; // "list".hashCode()
DavyLandman marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return the number of elements in the list
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/usethesource/vallang/IMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

public interface IMap extends ICollection<IMap> {

@Override
default int getPatternMatchFingerprint() {
return 107868; // "map".hashCode()
}

/**
* Adds a new entry to the map, mapping the key to value. If the
* key existed before, the old value will be lost.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/usethesource/vallang/INode.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
* it recursively.
*/
public interface INode extends IValue, Iterable<IValue> {

@Override
default int getPatternMatchFingerprint() {
return getName().hashCode() << 2 + arity();
}

/**
* Get a child
* @param i the zero based index of the child
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/usethesource/vallang/ISet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

public interface ISet extends ICollection<ISet> {

@Override
default int getPatternMatchFingerprint() {
return 113762; // "set".hashCode()
}

/**
* Add an element to the set.
* @param element
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/usethesource/vallang/IString.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.usethesource.vallang.visitors.IValueVisitor;

public interface IString extends IValue, Iterable<Integer> {

/**
* @return the Java string that this string represents
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/usethesource/vallang/ITuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
import io.usethesource.vallang.visitors.IValueVisitor;

public interface ITuple extends Iterable<IValue>, IValue {
@Override
default int getPatternMatchFingerprint() {
return 442900256 /* "tuple".hashCode() << 2 */ + arity();
DavyLandman marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Retrieve the given field at the given index.
*
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/usethesource/vallang/IValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public interface IValue {
* @return the {@link Type} of a value
*/
public Type getType();

/**
* This method is used exclusively by code generated by the Rascal compiler,
* or by the Rascal interpreter. The returned integer codes are opaque, although stable.
* If you need to know what kind of value you have, use the IValueVisitor or the ITypeVisitor
* interfaces and the `accept` methods on IValue and Type.
*
* @return an integer code that:
* * accurate reflects the identity of the top-level structure of this value
* * such that if pattern.match(this) ===> pattern.getPatternMatchFingerprint() == this.getPatternMatchFingerprint()
* * distinguishes maximally between different kinds of values
* * never makes the same or similar value have a different fingerprint
*/
default int getPatternMatchFingerprint() {
return hashCode();
}

/**
* Execute the {@link IValueVisitor} on the current node
Expand Down
Loading