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.TIntHashSet;
16 public class EListComprehension extends SimplifiableExpression {
18 public Expression head;
19 public ListQualifier qualifier;
21 public EListComprehension(Expression head, ListQualifier qualifier) {
23 this.qualifier = qualifier;
26 public EListComprehension(long loc, Expression head,
27 ListQualifier qualifier) {
30 this.qualifier = qualifier;
34 public void collectVars(TObjectIntHashMap<Variable> allVars,
36 head.collectVars(allVars, vars);
37 qualifier.collectVars(allVars, vars);
41 public Expression checkBasicType(TypingContext context, Type requiredType) {
42 qualifier.checkType(context);
45 componentType = Types.unifyApply(Types.LIST, requiredType);
46 } catch (MatchException e) {
47 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
48 return new EError(location);
50 head = head.checkType(context, componentType);
55 protected void updateType() throws MatchException {
56 setType(Types.list(head.getType()));
60 public Expression simplify(SimplificationContext context) {
61 context.pushLocation(location);
63 CompiledQualifier cq = qualifier.compile(context);
64 Expression exp = context.mapList(
65 context.lambda(cq.pattern, head),
67 //System.out.println("simplified: " + exp);
69 exp.validateType(context.getEnvironment());
70 } catch (TypeValidationException e) {
71 throw new InternalCompilerError(e);
73 exp = exp.simplify(context);
76 context.popLocation();
81 public Expression resolve(TranslationContext context) {
83 qualifier.resolve(context);
84 head = head.resolve(context);
90 public void setLocationDeep(long loc) {
91 if(location == Locations.NO_LOCATION) {
93 head.setLocationDeep(loc);
94 qualifier.setLocationDeep(loc);
99 public void accept(ExpressionVisitor visitor) {
104 public Expression accept(ExpressionTransformer transformer) {
105 return transformer.transform(this);