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.THashSet;
21 import gnu.trove.set.hash.TIntHashSet;
23 public class EWhen extends SimplifiableExpression {
26 public Expression action;
29 public EWhen(Query query, Expression action) {
34 public EWhen(long location, Query query, Expression action, Variable[] variables) {
36 throw new NullPointerException();
37 if(location == Locations.NO_LOCATION)
38 new Exception().printStackTrace();
39 this.location = location;
42 this.variables = variables;
46 public void collectVars(TObjectIntHashMap<Variable> allVars,
48 query.collectVars(allVars, vars);
49 action.collectVars(allVars, vars);
53 protected void updateType() throws MatchException {
58 public Expression checkBasicType(TypingContext context, Type requiredType) {
60 Types.unify(requiredType, Types.UNIT);
61 } catch (UnificationException e) {
62 context.typeError(location, requiredType, Types.UNIT);
65 for(Variable variable : variables)
66 variable.setType(Types.metaVar(Kinds.STAR));
67 query.checkType(context);
68 action.checkIgnoredType(context);
71 return compile(context);
74 public Expression compile(TypingContext context) {
76 QueryCompilationContext queryCompilationContext =
77 new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, action);
78 new QExists(variables, query).generate(queryCompilationContext);
79 return queryCompilationContext.getContinuation();
80 } catch(UnsolvableQueryException e) {
81 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
82 return new EError(getLocation());
87 public void collectFreeVariables(THashSet<Variable> vars) {
88 action.collectFreeVariables(vars);
89 query.collectFreeVariables(vars);
90 for(Variable var : variables)
95 public Expression resolve(TranslationContext context) {
96 context.pushExistentialFrame();
97 query = query.resolve(context);
98 action = action.resolve(context);
99 variables = context.popExistentialFrame();
104 public Expression replace(ReplaceContext context) {
105 Variable[] newVariables = new Variable[variables.length];
106 for(int i=0;i<variables.length;++i) {
107 Variable variable = variables[i];
108 Variable newVariable = new Variable(variable.name, variable.getType());
109 newVariables[i] = newVariable;
110 context.varMap.put(variable, new EVariable(newVariable));
112 EWhen result = new EWhen(location, query.replace(context), action.replace(context), newVariables);
113 for(int i=0;i<variables.length;++i)
114 context.varMap.remove(variables[i]);
119 public void setLocationDeep(long loc) {
120 if(location == Locations.NO_LOCATION) {
122 query.setLocationDeep(loc);
123 action.setLocationDeep(loc);
128 public void accept(ExpressionVisitor visitor) {
132 public Query getQuery() {
136 public Expression getAction() {
141 public Expression simplify(SimplificationContext context) {
142 throw new UnsupportedOperationException();
146 public Expression accept(ExpressionTransformer transformer) {
147 return transformer.transform(this);