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