1 package org.simantics.scl.compiler.elaboration.expressions.list;
3 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
4 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
6 import org.simantics.scl.compiler.elaboration.expressions.Case;
7 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
8 import org.simantics.scl.compiler.elaboration.expressions.Expression;
9 import org.simantics.scl.compiler.elaboration.expressions.Variable;
10 import org.simantics.scl.compiler.errors.Locations;
11 import org.simantics.scl.compiler.types.TMetaVar;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.kinds.Kinds;
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
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 void collectFreeVariables(THashSet<Variable> vars) {
44 value.collectFreeVariables(vars);
45 pattern.collectFreeVariables(vars);
49 public CompiledQualifier compile(SimplificationContext context) {
50 if(pattern instanceof EVariable)
51 return new CompiledQualifier(value, pattern);
53 THashSet<Variable> variables = pattern.getFreeVariables();
54 Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
55 Expression[] variableExps = new Expression[variableArray.length];
56 for(int i=0;i<variableArray.length;++i)
57 variableExps[i] = new EVariable(variableArray[i]);
58 Expression newPattern = context.tuple(variableExps);
60 EVariable blank = context.blank();
61 blank.getVariable().setType(pattern.getType());
63 return new CompiledQualifier(
66 new Case(pattern, context.singletonList(newPattern.copy())),
67 new Case(blank, context.emptyList(newPattern.getType()))
76 public void resolve(TranslationContext context) {
77 value = value.resolve(context);
78 pattern = pattern.resolveAsPattern(context);
82 public void collectEffects(THashSet<Type> effects) {
83 pattern.collectEffects(effects);
84 value.collectEffects(effects);
88 public void setLocationDeep(long loc) {
89 if(location == Locations.NO_LOCATION) {
91 pattern.setLocationDeep(loc);
92 value.setLocationDeep(loc);
97 public void accept(ListQualifierVisitor visitor) {
102 public ListQualifier accept(ListQualifierTransformer transformer) {
103 return transformer.transform(this);