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

Type.match and Type.instantiate for NonTerminalType #1844

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 35 additions & 1 deletion src/org/rascalmpl/types/NonTerminalType.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeFactory;
import io.usethesource.vallang.type.TypeFactory.RandomTypesConfig;
Expand Down Expand Up @@ -315,7 +316,6 @@ protected Type glbWithAbstractData(Type type) {
protected Type glbWithConstructor(Type type) {
return TF.voidType();
}


@Override
protected boolean isSupertypeOf(Type type) {
Expand All @@ -325,6 +325,7 @@ protected boolean isSupertypeOf(Type type) {

return super.isSupertypeOf(type);
}


@Override
protected boolean isSupertypeOf(RascalType type) {
Expand Down Expand Up @@ -532,4 +533,37 @@ public IValue randomValue(Random random, IValueFactory vf, TypeStore store, Map<
// TODO: this generates an on-the-fly nullable production and returns a tree for that rule
return rvf.appl(vf.constructor(RascalValueFactory.Production_Default, symbol, vf.list(), vf.set()), vf.list());
}

@Override
public Type instantiate(Map<Type, Type> bindings) {
return RTF.nonTerminalType(SymbolAdapter.instantiate(symbol, bindings));
}

@Override
public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException {
// return matched.isSubtypeOf(this);
if (matched.isSubtypeOf(TypeFactory.getInstance().voidType())) {
// this is a common fast path otherwise handled by the else case down here
return true;

}
else if (matched instanceof NonTerminalType) {
// we have to implement this on the symbol level since we do not have separate types
// for every different kind of non-terminal (regular symbols, literals, etc)
return SymbolAdapter.match(symbol, ((NonTerminalType) matched).symbol, bindings);
}
else {
IRascalValueFactory vf = IRascalValueFactory.getInstance();
// here we lift the other types to symbols, such that they can be compared.
// this is mainly necessary for the different kinds of parametrized sorts and
// how they match against the ADT names and parameters.

return SymbolAdapter.match(symbol, matched.asSymbol(vf, new TypeStore(), vf.setWriter(), new HashSet<>()), bindings);
}
}

@Override
public boolean isOpen() {
return SymbolAdapter.isOpen(symbol);
}
}
3 changes: 3 additions & 0 deletions src/org/rascalmpl/types/RascalType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.rascalmpl.types;

import java.util.Map;

import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.type.ExternalType;
import io.usethesource.vallang.type.Type;

Expand Down
Loading