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