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