]> gerrit.simantics Code Review - simantics/platform.git/blob
47aeef1a5c40b9e05e91296257afc1c9a4875d0d
[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
14 import gnu.trove.map.hash.TObjectIntHashMap;
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 CompiledQualifier compile(SimplificationContext context) {
40         if(pattern instanceof EVariable)
41             return new CompiledQualifier(context.singletonList(value), pattern);
42         else {
43             Set<Variable> variables = pattern.getFreeVariables();
44             Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
45             Expression[] variableExps = new Expression[variableArray.length];
46             for(int i=0;i<variableArray.length;++i)
47                 variableExps[i] = new EVariable(variableArray[i]);
48             Expression newPattern = context.tuple(variableExps);
49             
50             EVariable blank = context.blank();
51             blank.getVariable().setType(pattern.getType());
52             
53             return new CompiledQualifier(
54                 context.match(value, 
55                         new Case(pattern, context.singletonList(newPattern.copy())),
56                         new Case(blank, context.emptyList(newPattern.getType()))
57                         ),
58                         newPattern
59                     );
60         }
61     }
62
63     @Override
64     public void resolve(TranslationContext context) {
65         value = value.resolve(context);
66         pattern = pattern.resolveAsPattern(context);
67     }
68
69     @Override
70     public void setLocationDeep(long loc) {
71         if(location == Locations.NO_LOCATION) {
72             location = loc;
73             pattern.setLocationDeep(loc);
74             value.setLocationDeep(loc);
75         }
76     }
77
78     @Override
79     public void accept(ListQualifierVisitor visitor) {
80         visitor.visit(this);
81     }
82
83     @Override
84     public ListQualifier accept(ListQualifierTransformer transformer) {
85         return transformer.transform(this);
86     }
87 }