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 import gnu.trove.map.hash.TObjectIntHashMap;
15 import gnu.trove.set.hash.TIntHashSet;
17 public class ListAssignment extends ListQualifier {
18 public Expression pattern;
19 public Expression value;
21 public ListAssignment(Expression pattern, Expression value) {
22 this.pattern = pattern;
27 public void checkType(TypingContext context) {
28 value = value.inferType(context);
29 pattern.checkTypeAsPattern(context, value.getType());
33 public void collectVars(TObjectIntHashMap<Variable> allVars,
35 value.collectVars(allVars, vars);
39 public CompiledQualifier compile(SimplificationContext context) {
40 if(pattern instanceof EVariable)
41 return new CompiledQualifier(context.singletonList(value), pattern);
43 Set<Variable> variables = pattern.getFreeVariables();
44 Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
45 Expression[] variableExps = new Expression[variableArray.length];
46 for(int i=0;i<variableArray.length;++i)
47 variableExps[i] = new EVariable(variableArray[i]);
48 Expression newPattern = context.tuple(variableExps);
50 EVariable blank = context.blank();
51 blank.getVariable().setType(pattern.getType());
53 return new CompiledQualifier(
55 new Case(pattern, context.singletonList(newPattern.copy())),
56 new Case(blank, context.emptyList(newPattern.getType()))
64 public void resolve(TranslationContext context) {
65 value = value.resolve(context);
66 pattern = pattern.resolveAsPattern(context);
70 public void setLocationDeep(long loc) {
71 if(location == Locations.NO_LOCATION) {
73 pattern.setLocationDeep(loc);
74 value.setLocationDeep(loc);
79 public void accept(ListQualifierVisitor visitor) {
84 public ListQualifier accept(ListQualifierTransformer transformer) {
85 return transformer.transform(this);