Skip to content

Commit

Permalink
added IValue.getPatternMatchFingerprint() that simulates rascal-core:…
Browse files Browse the repository at this point in the history
…TopLevelType.getFingerprint(v) exactly for the value kinds we have in vallang
  • Loading branch information
jurgenvinju committed Oct 6, 2023
1 parent 5c5c684 commit 069e234
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 0 deletions.
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();

// TODO: this would distinguish more constructors based on incomparable argument types:
// 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()
}

/**
* @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();
}

/**
* 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

1 comment on commit 069e234

@PaulKlint
Copy link
Member

Choose a reason for hiding this comment

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

Overall, looks good!

Please sign in to comment.