Package com.schibsted.spt.data.jslt.impl
Class ScopeManager
- java.lang.Object
-
- com.schibsted.spt.data.jslt.impl.ScopeManager
-
public class ScopeManager extends java.lang.ObjectKeeps track of declared variables and maps them to their slots in the stack frames. A stack frame is just an array, with one slot for each variable. There are two kinds of stack frame: the global one, which has top-level variables plus those from the top level of modules. The second type is inside a function.When a variable is declared so that it shadows an outer variable those two get different slots, even though they have the same name.
The slot number combines two values in one: which stack frame the variable resolves to, and its position in that frame. The first bit says which frame, and the rest of the bits are left for the slot number.
Basically:
- If first bit set: function frame
- If first bit not set: global frame.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classScopeManager.ScopeFrameA scope frame is smaller than a stack frame: each object, object comprehension, for expression, and if expression will have its own scope frame.private static classScopeManager.StackFrame
-
Field Summary
Fields Modifier and Type Field Description private java.util.Deque<ScopeManager.ScopeFrame>currentprivate ScopeManager.StackFramecurrentFrameprivate ScopeManager.StackFramefunctionFrameprivate java.util.Deque<ScopeManager.ScopeFrame>functionScopesprivate ScopeManager.StackFrameglobalFrameprivate java.util.Map<java.lang.String,java.lang.Integer>parameterSlotsprivate java.util.Deque<ScopeManager.ScopeFrame>scopesstatic intUNFOUND
-
Constructor Summary
Constructors Constructor Description ScopeManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidenterFunction()Called when we enter a new function.voidenterScope()Called when we enter a new lexical scope in which variables can be declared, hiding those declared further out.java.util.Map<java.lang.String,java.lang.Integer>getParameterSlots()intgetStackFrameSize()voidleaveFunction()voidleaveScope()intregisterParameter(java.lang.String parameter, Location loc)Registers a parameter to a function.VariableInforegisterVariable(LetExpression let)Registers a variable.VariableInforesolveVariable(VariableExpression variable)
-
-
-
Field Detail
-
globalFrame
private ScopeManager.StackFrame globalFrame
-
scopes
private java.util.Deque<ScopeManager.ScopeFrame> scopes
-
functionFrame
private ScopeManager.StackFrame functionFrame
-
functionScopes
private java.util.Deque<ScopeManager.ScopeFrame> functionScopes
-
current
private java.util.Deque<ScopeManager.ScopeFrame> current
-
currentFrame
private ScopeManager.StackFrame currentFrame
-
parameterSlots
private java.util.Map<java.lang.String,java.lang.Integer> parameterSlots
-
UNFOUND
public static final int UNFOUND
- See Also:
- Constant Field Values
-
-
Method Detail
-
getStackFrameSize
public int getStackFrameSize()
-
getParameterSlots
public java.util.Map<java.lang.String,java.lang.Integer> getParameterSlots()
-
enterFunction
public void enterFunction()
Called when we enter a new function. A function is not just a new scope, because it needs its own stack frame.
-
leaveFunction
public void leaveFunction()
-
enterScope
public void enterScope()
Called when we enter a new lexical scope in which variables can be declared, hiding those declared further out. Although the scopes are nested we flatten them into a single stack frame by simply giving the variables different slots in the same frame. Variable 'v' may map to different slots depending on where in the code it is used.
-
leaveScope
public void leaveScope()
-
registerVariable
public VariableInfo registerVariable(LetExpression let)
Registers a variable.
-
registerParameter
public int registerParameter(java.lang.String parameter, Location loc)Registers a parameter to a function.
-
resolveVariable
public VariableInfo resolveVariable(VariableExpression variable)
-
-