]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/list/ListThen.java
(refs #7375) Replaced collectEffects by CollectEffectsVisitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / list / ListThen.java
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.Expression;
7 import org.simantics.scl.compiler.elaboration.expressions.Variable;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.types.TMetaVar;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
12 import org.simantics.scl.compiler.types.exceptions.UnificationException;
13 import org.simantics.scl.compiler.types.kinds.Kinds;
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 ListThen extends ListQualifier {
20     public ListQualifier left;
21     public Expression transformer;
22     public Expression by; // optional
23     TMetaVar cType;
24     
25     public ListThen(Expression transformer, Expression by) {
26         this.transformer = transformer;
27         this.by = by;
28     }
29     
30     public void setLeft(ListQualifier inner) {
31         this.left = inner;
32     }
33     
34     @Override
35     public void checkType(TypingContext context) {
36         left.checkType(context);
37         
38         cType = Types.metaVar(Kinds.STAR);
39         Type transformerType = Types.function(Types.list(cType), Types.list(cType));
40         if(by != null) {
41             by = by.checkType(context, Types.metaVar(Kinds.STAR));
42             transformerType = Types.function(Types.function(cType, by.getType()), transformerType);
43         }
44         transformer = transformer.checkType(context, transformerType);
45         if(!(Types.canonical(cType) instanceof TMetaVar)) {
46             context.getErrorLog().log(location, "Transformation function must be generic on list elements.");
47         }
48     }
49
50     @Override
51     public void collectVars(TObjectIntHashMap<Variable> allVars,
52             TIntHashSet vars) {
53         left.collectVars(allVars, vars);
54         transformer.collectVars(allVars, vars);
55         if(by != null)
56             by.collectVars(allVars, vars);
57     }
58
59     @Override
60     public void collectFreeVariables(THashSet<Variable> vars) {
61         left.collectFreeVariables(vars);
62         transformer.collectFreeVariables(vars);
63         if(by != null)
64             by.collectFreeVariables(vars);
65     }
66
67     @Override
68     public CompiledQualifier compile(SimplificationContext context) {
69         CompiledQualifier q = left.compile(context);
70         
71         try {
72             Types.unify(cType, q.pattern.getType());
73         } catch (UnificationException e) {
74             context.getErrorLog().log(location, "Transformation function must be generic on list elements.");
75         }
76         
77         if(by == null)
78             q.value = context.apply(transformer, q.value);
79         else
80             q.value = context.apply(transformer, context.lambda(q.pattern.copy(), by), q.value);
81         return q;
82     }
83
84     @Override
85     public void resolve(TranslationContext context) {
86         transformer = transformer.resolve(context);
87         left.resolve(context);
88         if(by != null)
89             by = by.resolve(context);
90     }
91
92     @Override
93     public void setLocationDeep(long loc) {
94         if(location == Locations.NO_LOCATION) {
95             location = loc;
96             left.setLocationDeep(loc);
97             transformer.setLocationDeep(loc);
98             if(by != null)
99                 by.setLocationDeep(loc);
100         }
101     }
102     
103     @Override
104     public void accept(ListQualifierVisitor visitor) {
105         visitor.visit(this);
106     }
107     
108     @Override
109     public ListQualifier accept(ListQualifierTransformer transformer) {
110         return transformer.transform(this);
111     }
112 }