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 public class EListComprehension extends SimplifiableExpression {
15 public Expression head;
16 public ListQualifier qualifier;
18 public EListComprehension(Expression head, ListQualifier qualifier) {
20 this.qualifier = qualifier;
23 public EListComprehension(long loc, Expression head,
24 ListQualifier qualifier) {
27 this.qualifier = qualifier;
31 public Expression checkBasicType(TypingContext context, Type requiredType) {
32 qualifier.checkType(context);
35 componentType = Types.unifyApply(Types.LIST, requiredType);
36 } catch (MatchException e) {
37 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
38 return new EError(location);
40 head = head.checkType(context, componentType);
45 protected void updateType() throws MatchException {
46 setType(Types.list(head.getType()));
50 public Expression simplify(SimplificationContext context) {
51 context.pushLocation(location);
53 CompiledQualifier cq = qualifier.compile(context);
54 Expression exp = context.mapList(
55 context.lambda(cq.pattern, head),
57 //System.out.println("simplified: " + exp);
59 exp.validateType(context.getEnvironment());
60 } catch (TypeValidationException e) {
61 throw new InternalCompilerError(e);
63 exp = exp.simplify(context);
66 context.popLocation();
71 public Expression resolve(TranslationContext context) {
73 qualifier.resolve(context);
74 head = head.resolve(context);
80 public void setLocationDeep(long loc) {
81 if(location == Locations.NO_LOCATION) {
83 head.setLocationDeep(loc);
84 qualifier.setLocationDeep(loc);
89 public void accept(ExpressionVisitor visitor) {
94 public Expression accept(ExpressionTransformer transformer) {
95 return transformer.transform(this);