1 package org.simantics.scl.compiler.elaboration.expressions.list;
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;
14 public class ListAssignment extends ListQualifier {
15 public Expression pattern;
16 public Expression value;
18 public ListAssignment(Expression pattern, Expression value) {
19 this.pattern = pattern;
24 public void checkType(TypingContext context) {
25 value = value.inferType(context);
26 pattern.checkTypeAsPattern(context, value.getType());
30 public CompiledQualifier compile(SimplificationContext context) {
31 if(pattern instanceof EVariable)
32 return new CompiledQualifier(context.singletonList(value), pattern);
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);
41 EVariable blank = context.blank();
42 blank.getVariable().setType(pattern.getType());
44 return new CompiledQualifier(
46 new Case(pattern, context.singletonList(newPattern.copy())),
47 new Case(blank, context.emptyList(newPattern.getType()))
55 public void resolve(TranslationContext context) {
56 value = value.resolve(context);
57 pattern = pattern.resolveAsPattern(context);
61 public void setLocationDeep(long loc) {
62 if(location == Locations.NO_LOCATION) {
64 pattern.setLocationDeep(loc);
65 value.setLocationDeep(loc);
70 public void accept(ListQualifierVisitor visitor) {
75 public ListQualifier accept(ListQualifierTransformer transformer) {
76 return transformer.transform(this);