]> gerrit.simantics Code Review - simantics/platform.git/blob
9cdb6eab222ae3f22d99f8932b073b552f33a255
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.elaboration.query.QExists;
9 import org.simantics.scl.compiler.elaboration.query.Query;
10 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
11 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationMode;
12 import org.simantics.scl.compiler.elaboration.query.compilation.UnsolvableQueryException;
13 import org.simantics.scl.compiler.errors.Locations;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
17 import org.simantics.scl.compiler.types.exceptions.UnificationException;
18 import org.simantics.scl.compiler.types.kinds.Kinds;
19
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.THashSet;
22 import gnu.trove.set.hash.TIntHashSet;
23
24 public class EWhen extends SimplifiableExpression {
25
26     public Query query;
27     public Expression action;
28     Variable[] variables;
29     
30     public EWhen(Query query, Expression action) {
31         this.query = query;
32         this.action = action;
33     }
34     
35     public EWhen(long location, Query query, Expression action, Variable[] variables) {
36         if(variables == null)
37             throw new NullPointerException();
38         if(location == Locations.NO_LOCATION)
39             new Exception().printStackTrace();
40         this.location = location;
41         this.query = query;
42         this.action = action;
43         this.variables = variables;
44     }
45
46     @Override
47     public void collectRefs(TObjectIntHashMap<Object> allRefs,
48             TIntHashSet refs) {
49         query.collectRefs(allRefs, refs);
50         action.collectRefs(allRefs, refs);
51     }
52
53     @Override
54     public void collectVars(TObjectIntHashMap<Variable> allVars,
55             TIntHashSet vars) {
56         query.collectVars(allVars, vars);
57         action.collectVars(allVars, vars);
58     }
59     
60     @Override
61     public void collectEffects(THashSet<Type> effects) {
62         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectEffects.");
63     }
64
65     @Override
66     protected void updateType() throws MatchException {
67         setType(Types.UNIT);
68     }
69     
70     @Override
71     public Expression checkBasicType(TypingContext context, Type requiredType) {
72         try {
73             Types.unify(requiredType, Types.UNIT);
74         } catch (UnificationException e) {
75             context.typeError(location, requiredType, Types.UNIT);
76             return this;
77         }
78         for(Variable variable : variables)
79             variable.setType(Types.metaVar(Kinds.STAR));
80         query.checkType(context);
81         action.checkIgnoredType(context);
82         
83         // Compile query
84         return compile(context);
85     }
86     
87     public Expression compile(TypingContext context) {
88         try {
89             QueryCompilationContext queryCompilationContext =
90                     new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, action);
91             new QExists(variables, query).generate(queryCompilationContext);
92             return queryCompilationContext.getContinuation();
93         } catch(UnsolvableQueryException e) {
94             context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" +  e.getMessage());
95             return new EError(getLocation());
96         }
97     }
98
99     @Override
100     public void collectFreeVariables(THashSet<Variable> vars) {
101         action.collectFreeVariables(vars);
102         query.collectFreeVariables(vars);
103         for(Variable var : variables)
104             vars.remove(var);
105     }
106
107     @Override
108     public Expression resolve(TranslationContext context) {
109         context.pushExistentialFrame();
110         query = query.resolve(context);
111         action = action.resolve(context);
112         variables = context.popExistentialFrame();
113         return this;
114     }
115     
116     @Override
117     public Expression replace(ReplaceContext context) {
118         Variable[] newVariables = new Variable[variables.length];
119         for(int i=0;i<variables.length;++i) {
120             Variable variable = variables[i];
121             Variable newVariable = new Variable(variable.name, variable.getType());
122             newVariables[i] = newVariable;
123             context.varMap.put(variable, new EVariable(newVariable));
124         }
125         EWhen result = new EWhen(location, query.replace(context), action.replace(context), newVariables);
126         for(int i=0;i<variables.length;++i)
127             context.varMap.remove(variables[i]);
128         return result;
129     }
130
131     @Override
132     public void setLocationDeep(long loc) {
133         if(location == Locations.NO_LOCATION) {
134             location = loc;
135             query.setLocationDeep(loc);
136             action.setLocationDeep(loc);
137         }
138     }
139     
140     @Override
141     public void accept(ExpressionVisitor visitor) {
142         visitor.visit(this);
143     }
144     
145     public Query getQuery() {
146         return query;
147     }
148     
149     public Expression getAction() {
150         return action;
151     }
152     
153     @Override
154     public Expression simplify(SimplificationContext context) {
155         throw new UnsupportedOperationException();
156     }
157     
158     @Override
159     public Expression accept(ExpressionTransformer transformer) {
160         return transformer.transform(this);
161     }
162
163 }