]> gerrit.simantics Code Review - simantics/platform.git/blob
a7e36247ef7b70f5a4c0e769ab655cc832b57b87
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;\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.list.CompiledQualifier;\r
11 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;\r
12 import org.simantics.scl.compiler.errors.Locations;\r
13 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;\r
14 import org.simantics.scl.compiler.types.Type;\r
15 import org.simantics.scl.compiler.types.Types;\r
16 import org.simantics.scl.compiler.types.exceptions.MatchException;\r
17 \r
18 public class EListComprehension extends SimplifiableExpression {\r
19 \r
20     public Expression head;\r
21     public ListQualifier qualifier;\r
22     \r
23     public EListComprehension(Expression head, ListQualifier qualifier) {\r
24         this.head = head;\r
25         this.qualifier = qualifier;\r
26     }\r
27 \r
28     public EListComprehension(long loc, Expression head,\r
29             ListQualifier qualifier) {\r
30         super(loc);\r
31         this.head = head;\r
32         this.qualifier = qualifier;\r
33     }\r
34 \r
35     @Override\r
36     public void collectRefs(TObjectIntHashMap<Object> allRefs,\r
37             TIntHashSet refs) {\r
38         head.collectRefs(allRefs, refs);\r
39         qualifier.collectRefs(allRefs, refs);\r
40     }\r
41 \r
42     @Override\r
43     public void collectVars(TObjectIntHashMap<Variable> allVars,\r
44             TIntHashSet vars) {\r
45         head.collectVars(allVars, vars);\r
46         qualifier.collectVars(allVars, vars);\r
47     }\r
48     \r
49     @Override\r
50     public Expression checkBasicType(TypingContext context, Type requiredType) {\r
51         qualifier.checkType(context);\r
52         Type componentType;\r
53         try {\r
54             componentType = Types.unifyApply(Types.LIST, requiredType);\r
55         } catch (MatchException e) {\r
56             context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");\r
57             return new EError(location);\r
58         }\r
59         head = head.checkType(context, componentType);\r
60         return this;\r
61     }\r
62     \r
63     @Override\r
64     protected void updateType() throws MatchException {\r
65         setType(Types.list(head.getType()));\r
66     }\r
67 \r
68     @Override\r
69     public void collectFreeVariables(THashSet<Variable> vars) {\r
70         head.collectFreeVariables(vars);\r
71         qualifier.collectFreeVariables(vars);\r
72     }\r
73 \r
74     @Override\r
75     public Expression simplify(SimplificationContext context) {\r
76         context.pushLocation(location);\r
77         try {\r
78             CompiledQualifier cq = qualifier.compile(context);            \r
79             Expression exp = context.mapList(\r
80                     context.lambda(cq.pattern, head), \r
81                     cq.value);\r
82             //System.out.println("simplified: " + exp);\r
83             /*try {\r
84                 exp.validateType(context.getEnvironment());\r
85             } catch (TypeValidationException e) {\r
86                 throw new InternalCompilerError(e);\r
87             }*/\r
88             exp = exp.simplify(context);\r
89             return exp;\r
90         } finally {\r
91             context.popLocation();\r
92         }\r
93     }\r
94 \r
95     @Override\r
96     public Expression resolve(TranslationContext context) {\r
97         context.pushFrame();\r
98         qualifier.resolve(context);\r
99         head = head.resolve(context);\r
100         context.popFrame();\r
101         return this;\r
102     }\r
103 \r
104     @Override\r
105     public Expression decorate(ExpressionDecorator decorator) {\r
106         head = head.decorate(decorator);\r
107         qualifier.decorate(decorator);\r
108         return decorator.decorate(this);\r
109     }\r
110 \r
111     @Override\r
112     public void collectEffects(THashSet<Type> effects) {\r
113         head.collectEffects(effects);\r
114         qualifier.collectEffects(effects);\r
115     }\r
116 \r
117     @Override\r
118     public void setLocationDeep(long loc) {\r
119         if(location == Locations.NO_LOCATION) {\r
120             location = loc;\r
121             head.setLocationDeep(loc);\r
122             qualifier.setLocationDeep(loc);\r
123         }\r
124     }\r
125     \r
126     @Override\r
127     public void accept(ExpressionVisitor visitor) {\r
128         visitor.visit(this);\r
129     }\r
130 \r
131     @Override\r
132     public void forVariables(VariableProcedure procedure) {\r
133         head.forVariables(procedure);\r
134         qualifier.forVariables(procedure);\r
135     }\r
136     \r
137     @Override\r
138     public Expression accept(ExpressionTransformer transformer) {\r
139         return transformer.transform(this);\r
140     }\r
141 \r
142 }\r