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 collectVars(TObjectIntHashMap<Variable> allVars,
37 head.collectVars(allVars, vars);
38 qualifier.collectVars(allVars, vars);
42 public Expression checkBasicType(TypingContext context, Type requiredType) {
43 qualifier.checkType(context);
46 componentType = Types.unifyApply(Types.LIST, requiredType);
47 } catch (MatchException e) {
48 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
49 return new EError(location);
51 head = head.checkType(context, componentType);
56 protected void updateType() throws MatchException {
57 setType(Types.list(head.getType()));
61 public void collectFreeVariables(THashSet<Variable> vars) {
62 head.collectFreeVariables(vars);
63 qualifier.collectFreeVariables(vars);
67 public Expression simplify(SimplificationContext context) {
68 context.pushLocation(location);
70 CompiledQualifier cq = qualifier.compile(context);
71 Expression exp = context.mapList(
72 context.lambda(cq.pattern, head),
74 //System.out.println("simplified: " + exp);
76 exp.validateType(context.getEnvironment());
77 } catch (TypeValidationException e) {
78 throw new InternalCompilerError(e);
80 exp = exp.simplify(context);
83 context.popLocation();
88 public Expression resolve(TranslationContext context) {
90 qualifier.resolve(context);
91 head = head.resolve(context);
97 public void setLocationDeep(long loc) {
98 if(location == Locations.NO_LOCATION) {
100 head.setLocationDeep(loc);
101 qualifier.setLocationDeep(loc);
106 public void accept(ExpressionVisitor visitor) {
111 public Expression accept(ExpressionTransformer transformer) {
112 return transformer.transform(this);