From 2bf3890bfc3f87569b61e0037f2ee0c00ee2ea44 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Thu, 11 Apr 2024 11:29:39 +0200 Subject: [PATCH] simplified make --- src/org/rascalmpl/library/Type.java | 52 ++++++++++++----------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/org/rascalmpl/library/Type.java b/src/org/rascalmpl/library/Type.java index 3cd470b715a..3ad86926a25 100644 --- a/src/org/rascalmpl/library/Type.java +++ b/src/org/rascalmpl/library/Type.java @@ -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; @@ -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(); } @@ -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 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 kwmap; - - if(keywordParameters.size() == 0){ - kwmap = Collections.emptyMap(); - } else { - Iterator> iter = keywordParameters.entryIterator(); - kwmap = new HashMap(); - while(iter.hasNext()){ - Entry 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