Skip to content

Commit

Permalink
added function getConstructor that reifies an actual ConstructorType …
Browse files Browse the repository at this point in the history
…to a Production representation
  • Loading branch information
jurgenvinju committed Nov 30, 2023
1 parent 61a9d63 commit c34ec59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/org/rascalmpl/library/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -24,7 +25,10 @@
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.IMap;
import io.usethesource.vallang.INode;
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.ITuple;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.exceptions.FactTypeUseException;
Expand All @@ -48,6 +52,23 @@ public IValue typeOf(IValue v) {
).get("symbol");
}

public IValue getConstructor(INode v) {
assert v.getType().isAbstractData();
io.usethesource.vallang.type.Type constructor = ((IConstructor) v).getUninstantiatedConstructorType();

TypeStore store = new TypeStore();
store.declareAbstractDataType(v.getType());
store.declareConstructor(constructor);

ISetWriter grammar = vf.setWriter();

new TypeReifier(vf).typeToValue(constructor, store, vf.map());

constructor.asProductions(vf, store, grammar, new HashSet<>());

return grammar.done().stream().map(t -> ((ITuple) t).get(1)).findFirst().get();
}

public IBool eq(IValue x, IValue y) {
return vf.bool(x.equals(y));
}
Expand Down
3 changes: 3 additions & 0 deletions src/org/rascalmpl/library/Type.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,9 @@ reify operator `#` does, since values may escape the scope in which they've been
@javaClass{org.rascalmpl.library.Type}
public java Symbol typeOf(value v);

@javaClass{org.rascalmpl.library.Type}
public java Production getConstructor(&A <: node constructor);

@synopsis{Determine if the given type is an int.}
public bool isIntType(Symbol::\alias(_,_,Symbol at)) = isIntType(at);
public bool isIntType(Symbol::\parameter(_,Symbol tvb)) = isIntType(tvb);
Expand Down

0 comments on commit c34ec59

Please sign in to comment.