]> gerrit.simantics Code Review - simantics/platform.git/blob
a0073b889d62e6e23b50f1bd99176eb9d8006c2e
[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 collectRefs(TObjectIntHashMap<Object> allRefs,
34             TIntHashSet refs) {
35         value.collectRefs(allRefs, refs);
36     }
37
38     @Override
39     public void collectVars(TObjectIntHashMap<Variable> allVars,
40             TIntHashSet vars) {
41         value.collectVars(allVars, vars);
42     }
43
44     @Override
45     public void collectFreeVariables(THashSet<Variable> vars) {
46         value.collectFreeVariables(vars);
47         pattern.collectFreeVariables(vars);
48     }
49
50     @Override
51     public CompiledQualifier compile(SimplificationContext context) {
52         if(pattern instanceof EVariable)
53             return new CompiledQualifier(context.singletonList(value), pattern);
54         else {
55             THashSet<Variable> variables = pattern.getFreeVariables();
56             Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
57             Expression[] variableExps = new Expression[variableArray.length];
58             for(int i=0;i<variableArray.length;++i)
59                 variableExps[i] = new EVariable(variableArray[i]);
60             Expression newPattern = context.tuple(variableExps);
61             
62             EVariable blank = context.blank();
63             blank.getVariable().setType(pattern.getType());
64             
65             return new CompiledQualifier(
66                 context.match(value, 
67                         new Case(pattern, context.singletonList(newPattern.copy())),
68                         new Case(blank, context.emptyList(newPattern.getType()))
69                         ),
70                         newPattern
71                     );
72         }
73     }
74
75     @Override
76     public void resolve(TranslationContext context) {
77         value = value.resolve(context);
78         pattern = pattern.resolveAsPattern(context);
79     }
80
81     @Override
82     public void collectEffects(THashSet<Type> effects) {
83         pattern.collectEffects(effects);
84         value.collectEffects(effects);
85     }
86     
87     @Override
88     public void setLocationDeep(long loc) {
89         if(location == Locations.NO_LOCATION) {
90             location = loc;
91             pattern.setLocationDeep(loc);
92             value.setLocationDeep(loc);
93         }
94     }
95
96     @Override
97     public void accept(ListQualifierVisitor visitor) {
98         visitor.visit(this);
99     }
100
101     @Override
102     public ListQualifier accept(ListQualifierTransformer transformer) {
103         return transformer.transform(this);
104     }
105 }