]> gerrit.simantics Code Review - simantics/platform.git/blob
7f480d8df889dbd7973ac160aab17f729482b70d
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions.list;
2
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.elaboration.expressions.VariableProcedure;
11 import org.simantics.scl.compiler.errors.Locations;
12 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
13 import org.simantics.scl.compiler.types.TMetaVar;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.kinds.Kinds;
17
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
21
22 public class ListGenerator extends ListQualifier {
23     public Expression pattern;
24     public Expression value;
25     
26     public ListGenerator(Expression pattern, Expression value) {
27         this.pattern = pattern;
28         this.value = value;
29     }
30     
31     @Override
32     public void checkType(TypingContext context) {
33         TMetaVar componentType = Types.metaVar(Kinds.STAR);
34         value.checkType(context, Types.apply(Types.LIST, componentType));
35         pattern.checkTypeAsPattern(context, componentType);
36     }
37
38     @Override
39     public void collectRefs(TObjectIntHashMap<Object> allRefs,
40             TIntHashSet refs) {
41         value.collectRefs(allRefs, refs);
42     }
43
44     @Override
45     public void collectVars(TObjectIntHashMap<Variable> allVars,
46             TIntHashSet vars) {
47         value.collectVars(allVars, vars);
48     }
49
50     @Override
51     public void collectFreeVariables(THashSet<Variable> vars) {
52         value.collectFreeVariables(vars);
53         pattern.collectFreeVariables(vars);
54     }
55
56     @Override
57     public CompiledQualifier compile(SimplificationContext context) {
58         if(pattern instanceof EVariable)
59             return new CompiledQualifier(value, pattern);
60         else {
61             THashSet<Variable> variables = pattern.getFreeVariables();
62             Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
63             Expression[] variableExps = new Expression[variableArray.length];
64             for(int i=0;i<variableArray.length;++i)
65                 variableExps[i] = new EVariable(variableArray[i]);
66             Expression newPattern = context.tuple(variableExps);
67             
68             EVariable blank = context.blank();
69             blank.getVariable().setType(pattern.getType());
70             
71             return new CompiledQualifier(
72                     context.concatMap(
73                             context.lambda(
74                                     new Case(pattern, context.singletonList(newPattern.copy())),
75                                     new Case(blank, context.emptyList(newPattern.getType()))
76                                     ), 
77                             value
78                             ), 
79                     newPattern);
80         }
81     }
82
83     @Override
84     public void resolve(TranslationContext context) {
85         value = value.resolve(context);
86         pattern = pattern.resolveAsPattern(context);
87     }
88     
89     @Override
90     public void decorate(ExpressionDecorator decorator) {
91         value = value.decorate(decorator);
92         pattern = pattern.decorate(decorator);
93     }
94     
95     @Override
96     public void collectEffects(THashSet<Type> effects) {
97         pattern.collectEffects(effects);
98         value.collectEffects(effects);
99     }
100     
101     @Override
102     public void setLocationDeep(long loc) {
103         if(location == Locations.NO_LOCATION) {
104             location = loc;
105             pattern.setLocationDeep(loc);
106             value.setLocationDeep(loc);
107         }
108     }
109     
110     @Override
111     public void accept(ListQualifierVisitor visitor) {
112         visitor.visit(this);
113     }
114     
115     @Override
116     public void forVariables(VariableProcedure procedure) {
117         value.forVariables(procedure);
118     }
119     
120     @Override
121     public ListQualifier accept(ListQualifierTransformer transformer) {
122         return transformer.transform(this);
123     }
124 }