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