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 public class ListGenerator extends ListQualifier {
18 public Expression pattern;
19 public Expression value;
21 public ListGenerator(Expression pattern, Expression value) {
22 this.pattern = pattern;
27 public void checkType(TypingContext context) {
28 TMetaVar componentType = Types.metaVar(Kinds.STAR);
29 value.checkType(context, Types.apply(Types.LIST, componentType));
30 pattern.checkTypeAsPattern(context, componentType);
34 public CompiledQualifier compile(SimplificationContext context) {
35 if(pattern instanceof EVariable)
36 return new CompiledQualifier(value, pattern);
38 Set<Variable> variables = pattern.getFreeVariables();
39 Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
40 Expression[] variableExps = new Expression[variableArray.length];
41 for(int i=0;i<variableArray.length;++i)
42 variableExps[i] = new EVariable(variableArray[i]);
43 Expression newPattern = context.tuple(variableExps);
45 EVariable blank = context.blank();
46 blank.getVariable().setType(pattern.getType());
48 return new CompiledQualifier(
51 new Case(pattern, context.singletonList(newPattern.copy())),
52 new Case(blank, context.emptyList(newPattern.getType()))
61 public void resolve(TranslationContext context) {
62 value = value.resolve(context);
63 pattern = pattern.resolveAsPattern(context);
67 public void setLocationDeep(long loc) {
68 if(location == Locations.NO_LOCATION) {
70 pattern.setLocationDeep(loc);
71 value.setLocationDeep(loc);
76 public void accept(ListQualifierVisitor visitor) {
81 public ListQualifier accept(ListQualifierTransformer transformer) {
82 return transformer.transform(this);