1 package org.simantics.scl.compiler.elaboration.expressions.list;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.elaboration.expressions.Case;
9 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
10 import org.simantics.scl.compiler.elaboration.expressions.Expression;
11 import org.simantics.scl.compiler.elaboration.expressions.Variable;
12 import org.simantics.scl.compiler.errors.Locations;
13 import org.simantics.scl.compiler.types.TMetaVar;
14 import org.simantics.scl.compiler.types.Types;
15 import org.simantics.scl.compiler.types.kinds.Kinds;
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.TIntHashSet;
20 public class ListGenerator extends ListQualifier {
21 public Expression pattern;
22 public Expression value;
24 public ListGenerator(Expression pattern, Expression value) {
25 this.pattern = pattern;
30 public void checkType(TypingContext context) {
31 TMetaVar componentType = Types.metaVar(Kinds.STAR);
32 value.checkType(context, Types.apply(Types.LIST, componentType));
33 pattern.checkTypeAsPattern(context, componentType);
37 public void collectVars(TObjectIntHashMap<Variable> allVars,
39 value.collectVars(allVars, vars);
43 public CompiledQualifier compile(SimplificationContext context) {
44 if(pattern instanceof EVariable)
45 return new CompiledQualifier(value, pattern);
47 Set<Variable> variables = pattern.getFreeVariables();
48 Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
49 Expression[] variableExps = new Expression[variableArray.length];
50 for(int i=0;i<variableArray.length;++i)
51 variableExps[i] = new EVariable(variableArray[i]);
52 Expression newPattern = context.tuple(variableExps);
54 EVariable blank = context.blank();
55 blank.getVariable().setType(pattern.getType());
57 return new CompiledQualifier(
60 new Case(pattern, context.singletonList(newPattern.copy())),
61 new Case(blank, context.emptyList(newPattern.getType()))
70 public void resolve(TranslationContext context) {
71 value = value.resolve(context);
72 pattern = pattern.resolveAsPattern(context);
76 public void setLocationDeep(long loc) {
77 if(location == Locations.NO_LOCATION) {
79 pattern.setLocationDeep(loc);
80 value.setLocationDeep(loc);
85 public void accept(ListQualifierVisitor visitor) {
90 public ListQualifier accept(ListQualifierTransformer transformer) {
91 return transformer.transform(this);