1 package org.simantics.scl.compiler.elaboration.contexts;
3 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
4 import org.simantics.scl.compiler.elaboration.expressions.Expression;
5 import org.simantics.scl.compiler.elaboration.expressions.Variable;
6 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
7 import org.simantics.scl.compiler.types.TVar;
8 import org.simantics.scl.compiler.types.Type;
9 import org.simantics.scl.compiler.types.Types;
11 import gnu.trove.map.hash.THashMap;
13 public class ReplaceContext {
14 public final THashMap<TVar, Type> EMPTY_VARIABLE_MAP = new THashMap<TVar, Type>();
16 public THashMap<TVar, Type> tvarMap;
17 public THashMap<Variable, Expression> varMap;
18 public final TypingContext typingContext;
19 public boolean inPattern;
21 public ReplaceContext(THashMap<TVar, Type> tvarMap,
22 THashMap<Variable, Expression> varMap,
23 TypingContext typingContext) {
24 this.tvarMap = tvarMap;
26 this.typingContext = typingContext;
27 this.inPattern = false;
30 public ReplaceContext(Variable[] from, Variable[] to) {
31 this.tvarMap = EMPTY_VARIABLE_MAP;
32 this.typingContext = null;
33 this.inPattern = false;
34 varMap = new THashMap<Variable, Expression>(from.length);
35 for(int i=0;i<from.length;++i)
36 varMap.put(from[i], new EVariable(to[i]));
39 public ReplaceContext(TypingContext typingContext) {
40 this(new THashMap<TVar, Type>(), new THashMap<Variable, Expression>(), typingContext);
43 public Expression[] replace(Expression[] expressions) {
44 if(expressions == null)
46 Expression[] result = new Expression[expressions.length];
47 for(int i=0;i<expressions.length;++i)
48 result[i] = expressions[i].replace(this);
52 public Type[] replace(Type[] types) {
55 return Types.replace(types, tvarMap);
58 public FieldAssignment[] replace(FieldAssignment[] fields) {
61 FieldAssignment[] result = new FieldAssignment[fields.length];
62 for(int i=0;i<fields.length;++i)
63 result[i] = fields[i].replace(this);