]> gerrit.simantics Code Review - simantics/platform.git/blob
e80883969a95ed2c7453b45810fe544c5262ca4b
[simantics/platform.git] /
1 package org.simantics.scl.compiler.top;
2
3 import gnu.trove.impl.Constants;
4 import gnu.trove.map.hash.TObjectIntHashMap;
5
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;
10
11
12 public class ExpressionInterpretationContext {
13
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,
22                     -1);
23     
24     public ExpressionInterpretationContext(
25             RuntimeEnvironment runtimeEnvironment,
26             TransientClassBuilder localClassBuilder) {
27         this.runtimeEnvironment = runtimeEnvironment;
28         this.localClassBuilder = localClassBuilder;
29     }
30
31     public int push(Variable var) {
32         int id = curVariableId++;
33         variableIds.put(var, id);
34         maxVariableId = Math.max(maxVariableId, curVariableId);
35         return id;
36     }
37     
38     public void pop(Variable var) {
39         int id = variableIds.remove(var);
40         --curVariableId;
41         if(id != curVariableId)
42             throw new IllegalStateException("Variable pushs and pops don't match.");
43     }
44     
45     public int getVariableId(Variable var) {
46         int id = variableIds.get(var);
47         if(id < 0)
48             throw new InternalCompilerError("Encontered bug while interpreting exception: variable " + var + " was not bounded.");
49         return id;
50     }
51
52     public ExpressionInterpretationContext createNewContext() {
53         return new ExpressionInterpretationContext(runtimeEnvironment, localClassBuilder);
54     }
55     
56     public int getMaxVariableId() {
57         return maxVariableId;
58     }
59 }