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