]> gerrit.simantics Code Review - simantics/platform.git/blob
78f7ce115c24b6b22eb06df747cb2ca7890df050
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
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;
12
13 public class EListComprehension extends SimplifiableExpression {
14
15     public Expression head;
16     public ListQualifier qualifier;
17     
18     public EListComprehension(Expression head, ListQualifier qualifier) {
19         this.head = head;
20         this.qualifier = qualifier;
21     }
22
23     public EListComprehension(long loc, Expression head,
24             ListQualifier qualifier) {
25         super(loc);
26         this.head = head;
27         this.qualifier = qualifier;
28     }
29     
30     @Override
31     public Expression checkBasicType(TypingContext context, Type requiredType) {
32         qualifier.checkType(context);
33         Type componentType;
34         try {
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);
39         }
40         head = head.checkType(context, componentType);
41         return this;
42     }
43     
44     @Override
45     protected void updateType() throws MatchException {
46         setType(Types.list(head.getType()));
47     }
48
49     @Override
50     public Expression simplify(SimplificationContext context) {
51         context.pushLocation(location);
52         try {
53             CompiledQualifier cq = qualifier.compile(context);            
54             Expression exp = context.mapList(
55                     context.lambda(cq.pattern, head), 
56                     cq.value);
57             //System.out.println("simplified: " + exp);
58             /*try {
59                 exp.validateType(context.getEnvironment());
60             } catch (TypeValidationException e) {
61                 throw new InternalCompilerError(e);
62             }*/
63             exp = exp.simplify(context);
64             return exp;
65         } finally {
66             context.popLocation();
67         }
68     }
69
70     @Override
71     public Expression resolve(TranslationContext context) {
72         context.pushFrame();
73         qualifier.resolve(context);
74         head = head.resolve(context);
75         context.popFrame();
76         return this;
77     }
78
79     @Override
80     public void setLocationDeep(long loc) {
81         if(location == Locations.NO_LOCATION) {
82             location = loc;
83             head.setLocationDeep(loc);
84             qualifier.setLocationDeep(loc);
85         }
86     }
87     
88     @Override
89     public void accept(ExpressionVisitor visitor) {
90         visitor.visit(this);
91     }
92     
93     @Override
94     public Expression accept(ExpressionTransformer transformer) {
95         return transformer.transform(this);
96     }
97
98 }