]> gerrit.simantics Code Review - simantics/platform.git/blob
959e3bf3d5d71afec0f61d5817f201946eb73e00
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions.list;
2
3 import java.util.Set;
4
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;
16
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.TIntHashSet;
19
20 public class ListGenerator extends ListQualifier {
21     public Expression pattern;
22     public Expression value;
23     
24     public ListGenerator(Expression pattern, Expression value) {
25         this.pattern = pattern;
26         this.value = value;
27     }
28     
29     @Override
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);
34     }
35
36     @Override
37     public void collectVars(TObjectIntHashMap<Variable> allVars,
38             TIntHashSet vars) {
39         value.collectVars(allVars, vars);
40     }
41
42     @Override
43     public CompiledQualifier compile(SimplificationContext context) {
44         if(pattern instanceof EVariable)
45             return new CompiledQualifier(value, pattern);
46         else {
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);
53             
54             EVariable blank = context.blank();
55             blank.getVariable().setType(pattern.getType());
56             
57             return new CompiledQualifier(
58                     context.concatMap(
59                             context.lambda(
60                                     new Case(pattern, context.singletonList(newPattern.copy())),
61                                     new Case(blank, context.emptyList(newPattern.getType()))
62                                     ), 
63                             value
64                             ), 
65                     newPattern);
66         }
67     }
68
69     @Override
70     public void resolve(TranslationContext context) {
71         value = value.resolve(context);
72         pattern = pattern.resolveAsPattern(context);
73     }
74
75     @Override
76     public void setLocationDeep(long loc) {
77         if(location == Locations.NO_LOCATION) {
78             location = loc;
79             pattern.setLocationDeep(loc);
80             value.setLocationDeep(loc);
81         }
82     }
83     
84     @Override
85     public void accept(ListQualifierVisitor visitor) {
86         visitor.visit(this);
87     }
88     
89     @Override
90     public ListQualifier accept(ListQualifierTransformer transformer) {
91         return transformer.transform(this);
92     }
93 }