1 package org.simantics.scl.compiler.elaboration.expressions;
\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
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
18 public class EListComprehension extends SimplifiableExpression {
\r
20 public Expression head;
\r
21 public ListQualifier qualifier;
\r
23 public EListComprehension(Expression head, ListQualifier qualifier) {
\r
25 this.qualifier = qualifier;
\r
28 public EListComprehension(long loc, Expression head,
\r
29 ListQualifier qualifier) {
\r
32 this.qualifier = qualifier;
\r
36 public void collectRefs(TObjectIntHashMap<Object> allRefs,
\r
38 head.collectRefs(allRefs, refs);
\r
39 qualifier.collectRefs(allRefs, refs);
\r
43 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
45 head.collectVars(allVars, vars);
\r
46 qualifier.collectVars(allVars, vars);
\r
50 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
51 qualifier.checkType(context);
\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
59 head = head.checkType(context, componentType);
\r
64 protected void updateType() throws MatchException {
\r
65 setType(Types.list(head.getType()));
\r
69 public void collectFreeVariables(THashSet<Variable> vars) {
\r
70 head.collectFreeVariables(vars);
\r
71 qualifier.collectFreeVariables(vars);
\r
75 public Expression simplify(SimplificationContext context) {
\r
76 context.pushLocation(location);
\r
78 CompiledQualifier cq = qualifier.compile(context);
\r
79 Expression exp = context.mapList(
\r
80 context.lambda(cq.pattern, head),
\r
82 //System.out.println("simplified: " + exp);
\r
84 exp.validateType(context.getEnvironment());
\r
85 } catch (TypeValidationException e) {
\r
86 throw new InternalCompilerError(e);
\r
88 exp = exp.simplify(context);
\r
91 context.popLocation();
\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
105 public Expression decorate(ExpressionDecorator decorator) {
\r
106 head = head.decorate(decorator);
\r
107 qualifier.decorate(decorator);
\r
108 return decorator.decorate(this);
\r
112 public void collectEffects(THashSet<Type> effects) {
\r
113 head.collectEffects(effects);
\r
114 qualifier.collectEffects(effects);
\r
118 public void setLocationDeep(long loc) {
\r
119 if(location == Locations.NO_LOCATION) {
\r
121 head.setLocationDeep(loc);
\r
122 qualifier.setLocationDeep(loc);
\r
127 public void accept(ExpressionVisitor visitor) {
\r
128 visitor.visit(this);
\r
132 public void forVariables(VariableProcedure procedure) {
\r
133 head.forVariables(procedure);
\r
134 qualifier.forVariables(procedure);
\r
138 public Expression accept(ExpressionTransformer transformer) {
\r
139 return transformer.transform(this);
\r