Skip to content

Commit

Permalink
simplified make
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Apr 11, 2024
1 parent c4cc92c commit 2bf3890
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions src/org/rascalmpl/library/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.types.TypeReifier;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.RascalValueFactory;

import io.usethesource.vallang.IBool;
import io.usethesource.vallang.IConstructor;
Expand All @@ -36,10 +39,10 @@
import io.usethesource.vallang.type.TypeStore;

public class Type {
private final IValueFactory vf;
private final IRascalValueFactory vf;
private final IMap emptyMap;

public Type(IValueFactory vf) {
public Type(IRascalValueFactory vf) {
this.vf = vf;
emptyMap = vf.mapWriter().done();
}
Expand Down Expand Up @@ -122,38 +125,25 @@ public IValue make(IValue type, IString name, IList args, IMap keywordParameters
}

public IValue make(IConstructor cons, IList args, IMap keywordParameters) {
io.usethesource.vallang.type.Type constructor = new TypeReifier(vf).productionToConstructorType(cons);

assert constructor != null;

IValue[] children = new IValue[args.length()];
io.usethesource.vallang.type.Type[] argsTypes = new io.usethesource.vallang.type.Type[args.length()];

for (int i = 0; i < args.length(); i++) {
children[i] = args.get(i);
argsTypes[i] = children[i].getType();
Map<String, IValue> kwMap = keywordParameters
.stream()
.map(v -> (ITuple) v)
.collect(Collectors.toMap(t -> ((IString) t.get(0)).getValue(), t -> t.get(1)));

if (cons.getConstructorType() == RascalValueFactory.Production_Default) {
// this is a parse tree production. We can just return the tree, and add the keyword parameters
return vf.appl(cons, args).asWithKeywordParameters().setParameters(kwMap);
}

Map<String, IValue> kwmap;

if(keywordParameters.size() == 0){
kwmap = Collections.emptyMap();
} else {
Iterator<Entry<IValue, IValue>> iter = keywordParameters.entryIterator();
kwmap = new HashMap<String, IValue>();
while(iter.hasNext()){
Entry<IValue, IValue> entry = iter.next();
kwmap.put(((IString) entry.getKey()).getValue(), entry.getValue());
}

if (cons.getConstructorType() != RascalValueFactory.Production_Cons) {
throw RuntimeExceptionFactory.illegalArgument(cons, "must be a cons or a prod rule.");
}

try {
if (constructor == null) {
// TODO: improve error messaging, using specialized exception
throw RuntimeExceptionFactory.illegalArgument(cons, null, null);
}
return vf.constructor(constructor, children, kwmap);

io.usethesource.vallang.type.Type constructor = new TypeReifier(vf).productionToConstructorType(cons);

try {
IValue[] children = args.stream().toArray(IValue[]::new);
return vf.constructor(constructor, children, kwMap);
}
catch (FactTypeUseException e) {
// TODO: improve error messaging, using specialized exception
Expand Down

0 comments on commit 2bf3890

Please sign in to comment.