1 package org.simantics.scl.compiler.elaboration.expressions;
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.list.CompiledQualifier;
7 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.types.Type;
10 import org.simantics.scl.compiler.types.Types;
11 import org.simantics.scl.compiler.types.exceptions.MatchException;
13 import gnu.trove.map.hash.TObjectIntHashMap;
14 import gnu.trove.set.hash.THashSet;
15 import gnu.trove.set.hash.TIntHashSet;
17 public class EListComprehension extends SimplifiableExpression {
19 public Expression head;
20 public ListQualifier qualifier;
22 public EListComprehension(Expression head, ListQualifier qualifier) {
24 this.qualifier = qualifier;
27 public EListComprehension(long loc, Expression head,
28 ListQualifier qualifier) {
31 this.qualifier = qualifier;
35 public void collectRefs(TObjectIntHashMap<Object> allRefs,
37 head.collectRefs(allRefs, refs);
38 qualifier.collectRefs(allRefs, refs);
42 public void collectVars(TObjectIntHashMap<Variable> allVars,
44 head.collectVars(allVars, vars);
45 qualifier.collectVars(allVars, vars);
49 public Expression checkBasicType(TypingContext context, Type requiredType) {
50 qualifier.checkType(context);
53 componentType = Types.unifyApply(Types.LIST, requiredType);
54 } catch (MatchException e) {
55 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
56 return new EError(location);
58 head = head.checkType(context, componentType);
63 protected void updateType() throws MatchException {
64 setType(Types.list(head.getType()));
68 public void collectFreeVariables(THashSet<Variable> vars) {
69 head.collectFreeVariables(vars);
70 qualifier.collectFreeVariables(vars);
74 public Expression simplify(SimplificationContext context) {
75 context.pushLocation(location);
77 CompiledQualifier cq = qualifier.compile(context);
78 Expression exp = context.mapList(
79 context.lambda(cq.pattern, head),
81 //System.out.println("simplified: " + exp);
83 exp.validateType(context.getEnvironment());
84 } catch (TypeValidationException e) {
85 throw new InternalCompilerError(e);
87 exp = exp.simplify(context);
90 context.popLocation();
95 public Expression resolve(TranslationContext context) {
97 qualifier.resolve(context);
98 head = head.resolve(context);
104 public void collectEffects(THashSet<Type> effects) {
105 head.collectEffects(effects);
106 qualifier.collectEffects(effects);
110 public void setLocationDeep(long loc) {
111 if(location == Locations.NO_LOCATION) {
113 head.setLocationDeep(loc);
114 qualifier.setLocationDeep(loc);
119 public void accept(ExpressionVisitor visitor) {
124 public Expression accept(ExpressionTransformer transformer) {
125 return transformer.transform(this);