1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
4 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
7 import org.simantics.scl.compiler.elaboration.query.QExists;
8 import org.simantics.scl.compiler.elaboration.query.Query;
9 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
10 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationMode;
11 import org.simantics.scl.compiler.elaboration.query.compilation.UnsolvableQueryException;
12 import org.simantics.scl.compiler.errors.Locations;
13 import org.simantics.scl.compiler.types.Type;
14 import org.simantics.scl.compiler.types.Types;
15 import org.simantics.scl.compiler.types.exceptions.MatchException;
16 import org.simantics.scl.compiler.types.exceptions.UnificationException;
17 import org.simantics.scl.compiler.types.kinds.Kinds;
19 import gnu.trove.map.hash.TObjectIntHashMap;
20 import gnu.trove.set.hash.TIntHashSet;
22 public class EWhen extends SimplifiableExpression {
25 public Expression action;
26 public Variable[] variables;
28 public EWhen(Query query, Expression action) {
33 public EWhen(long location, Query query, Expression action, Variable[] variables) {
35 throw new NullPointerException();
36 if(location == Locations.NO_LOCATION)
37 new Exception().printStackTrace();
38 this.location = location;
41 this.variables = variables;
45 public void collectVars(TObjectIntHashMap<Variable> allVars,
47 query.collectVars(allVars, vars);
48 action.collectVars(allVars, vars);
52 protected void updateType() throws MatchException {
57 public Expression checkBasicType(TypingContext context, Type requiredType) {
59 Types.unify(requiredType, Types.UNIT);
60 } catch (UnificationException e) {
61 context.typeError(location, requiredType, Types.UNIT);
64 for(Variable variable : variables)
65 variable.setType(Types.metaVar(Kinds.STAR));
66 query.checkType(context);
67 action.checkIgnoredType(context);
70 return compile(context);
73 public Expression compile(TypingContext context) {
75 QueryCompilationContext queryCompilationContext =
76 new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, action);
77 new QExists(variables, query).generate(queryCompilationContext);
78 return queryCompilationContext.getContinuation();
79 } catch(UnsolvableQueryException e) {
80 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
81 return new EError(getLocation());
86 public Expression resolve(TranslationContext context) {
87 context.pushExistentialFrame();
88 query = query.resolve(context);
89 action = action.resolve(context);
90 variables = context.popExistentialFrame();
95 public Expression replace(ReplaceContext context) {
96 Variable[] newVariables = new Variable[variables.length];
97 for(int i=0;i<variables.length;++i) {
98 Variable variable = variables[i];
99 Variable newVariable = new Variable(variable.name, variable.getType());
100 newVariables[i] = newVariable;
101 context.varMap.put(variable, new EVariable(newVariable));
103 EWhen result = new EWhen(location, query.replace(context), action.replace(context), newVariables);
104 for(int i=0;i<variables.length;++i)
105 context.varMap.remove(variables[i]);
110 public void setLocationDeep(long loc) {
111 if(location == Locations.NO_LOCATION) {
113 query.setLocationDeep(loc);
114 action.setLocationDeep(loc);
119 public void accept(ExpressionVisitor visitor) {
123 public Query getQuery() {
127 public Expression getAction() {
132 public Expression simplify(SimplificationContext context) {
133 throw new UnsupportedOperationException();
137 public Expression accept(ExpressionTransformer transformer) {
138 return transformer.transform(this);