]> gerrit.simantics Code Review - simantics/platform.git/blob
ce4d66bd7567e268f395d12835a23a9258a906fa
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.Just;
4 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.apply;
5 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.let;
6 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.loc;
7 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.newVar;
8 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.seq;
9 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.tuple;
10 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.var;
11
12 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
13 import org.simantics.scl.compiler.common.names.Names;
14 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
15 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
16 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
17 import org.simantics.scl.compiler.elaboration.query.QExists;
18 import org.simantics.scl.compiler.elaboration.query.Query;
19 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
20 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationMode;
21 import org.simantics.scl.compiler.elaboration.query.compilation.UnsolvableQueryException;
22 import org.simantics.scl.compiler.errors.Locations;
23 import org.simantics.scl.compiler.internal.parsing.parser.SCLTerminals;
24 import org.simantics.scl.compiler.types.Type;
25 import org.simantics.scl.compiler.types.Types;
26 import org.simantics.scl.compiler.types.exceptions.MatchException;
27 import org.simantics.scl.compiler.types.kinds.Kinds;
28
29 import gnu.trove.map.hash.TObjectIntHashMap;
30 import gnu.trove.set.hash.THashSet;
31 import gnu.trove.set.hash.TIntHashSet;
32
33 public class ESelect extends SimplifiableExpression {
34
35     private final Type ARRAY_LIST = Types.con("ArrayList", "T"); 
36     
37     int selectVariant;
38     Expression expression;
39     Query query;
40     Variable[] variables;
41     
42     public ESelect(int selectVariant, Expression expression, Query query) {
43         this.selectVariant = selectVariant;
44         this.expression = expression;
45         this.query = query;
46     }
47
48     @Override
49     public void collectVars(TObjectIntHashMap<Variable> allVars,
50             TIntHashSet vars) {
51         expression.collectVars(allVars, vars);
52         query.collectVars(allVars, vars);
53     }
54     
55     @Override
56     public void collectEffects(THashSet<Type> effects) {
57         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectEffects.");
58     }
59
60     @Override
61     protected void updateType() throws MatchException {
62         setType(selectVariant==SCLTerminals.SELECT_FIRST 
63                 ? Types.apply(Types.MAYBE, expression.getType()) 
64                 : Types.list(expression.getType()));
65     }
66     
67     @Override
68     public Expression checkBasicType(TypingContext context, Type requiredType) {
69         Type componentType;
70         switch(selectVariant) {
71         case SCLTerminals.SELECT:
72         case SCLTerminals.SELECT_DISTINCT:
73             try {
74                 componentType = Types.unifyApply(Types.LIST, requiredType);
75             } catch (MatchException e) {
76                 context.getErrorLog().log(location, "Select expression produces a list of values.");
77                 return new EError(location);
78             }
79             break;
80         case SCLTerminals.SELECT_FIRST:
81             try {
82                 componentType = Types.unifyApply(Types.MAYBE, requiredType);
83             } catch (MatchException e) {
84                 context.getErrorLog().log(location, "Select first expression produces an optional value.");
85                 return new EError(location);
86             }
87             break;
88         default: throw new InternalCompilerError();
89         }
90         for(Variable variable : variables)
91             variable.setType(Types.metaVar(Kinds.STAR));
92         expression.checkType(context, componentType);
93         query.checkType(context);
94         
95         // Compile query
96         Type elType = expression.getType();
97         Expression result;
98         if(selectVariant == SCLTerminals.SELECT_FIRST) {
99             QueryCompilationContext queryCompilationContext =
100                     new QueryCompilationContext(context, QueryCompilationMode.GET_FIRST,
101                             expression.getType(),
102                             Just(expression));
103             try {
104                 new QExists(variables, query).generate(queryCompilationContext);
105             } catch (UnsolvableQueryException e) {
106                 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
107                 return new EError(getLocation());
108             }
109             result = queryCompilationContext.getContinuation();
110         }
111         else {
112             Variable accumulator = newVar("accum", Types.apply(ARRAY_LIST, elType));
113             result =
114                     apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_freeze, elType,
115                             var(accumulator));
116             Expression innerExpression = 
117                     apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_add, elType,
118                             var(accumulator), expression);
119             try {
120                 QueryCompilationContext queryCompilationContext =
121                         new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, innerExpression);
122                 new QExists(variables, query).generate(queryCompilationContext);
123                 result = seq(queryCompilationContext.getContinuation(), result);
124             } catch(UnsolvableQueryException e) {
125                 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
126                 return new EError(getLocation());
127             }
128             result = let(accumulator,
129                     apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_new, elType, tuple()),
130                     result
131                     );
132         }
133         return loc(location, result);
134     }
135
136     @Override
137     public void collectFreeVariables(THashSet<Variable> vars) {
138         expression.collectFreeVariables(vars);
139         query.collectFreeVariables(vars);
140         for(Variable variable : variables)
141             vars.remove(variable);
142     }
143
144     @Override
145     public Expression resolve(TranslationContext context) {
146         context.pushExistentialFrame();
147         expression = expression.resolve(context);
148         query = query.resolve(context);
149         variables = context.popExistentialFrame();
150         return this;
151     }
152     
153     @Override
154     public void setLocationDeep(long loc) {
155         if(location == Locations.NO_LOCATION) {
156             location = loc;
157             expression.setLocationDeep(loc);
158             query.setLocationDeep(loc);
159         }
160     }
161     
162     @Override
163     public void accept(ExpressionVisitor visitor) {
164         visitor.visit(this);
165     }
166     
167     @Override
168     public Expression simplify(SimplificationContext context) {
169         throw new UnsupportedOperationException();
170     }
171     
172     @Override
173     public Expression accept(ExpressionTransformer transformer) {
174         return transformer.transform(this);
175     }
176
177 }