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