1 package org.simantics.scl.compiler.top;
3 import gnu.trove.impl.Constants;
4 import gnu.trove.map.hash.TObjectIntHashMap;
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
7 import org.simantics.scl.compiler.elaboration.expressions.Variable;
8 import org.simantics.scl.compiler.internal.codegen.utils.TransientClassBuilder;
9 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
12 public class ExpressionInterpretationContext {
14 public final RuntimeEnvironment runtimeEnvironment;
15 public final TransientClassBuilder localClassBuilder;
16 private int curVariableId = 0;
17 private int maxVariableId = 0;
18 private final TObjectIntHashMap<Variable> variableIds =
19 new TObjectIntHashMap<Variable>(
20 Constants.DEFAULT_CAPACITY,
21 Constants.DEFAULT_LOAD_FACTOR,
24 public ExpressionInterpretationContext(
25 RuntimeEnvironment runtimeEnvironment,
26 TransientClassBuilder localClassBuilder) {
27 this.runtimeEnvironment = runtimeEnvironment;
28 this.localClassBuilder = localClassBuilder;
31 public int push(Variable var) {
32 int id = curVariableId++;
33 variableIds.put(var, id);
34 maxVariableId = Math.max(maxVariableId, curVariableId);
38 public void pop(Variable var) {
39 int id = variableIds.remove(var);
41 if(id != curVariableId)
42 throw new IllegalStateException("Variable pushs and pops don't match.");
45 public int getVariableId(Variable var) {
46 int id = variableIds.get(var);
48 throw new InternalCompilerError("Encontered bug while interpreting exception: variable " + var + " was not bounded.");
52 public ExpressionInterpretationContext createNewContext() {
53 return new ExpressionInterpretationContext(runtimeEnvironment, localClassBuilder);
56 public int getMaxVariableId() {