Class CallStack
- java.lang.Object
-
- com.tonic.analysis.execution.frame.CallStack
-
public final class CallStack extends Object
A bounded stack of interpreter frames with overflow and underflow checking.
-
-
Constructor Summary
Constructors Constructor Description CallStack(int maxDepth)Creates an empty call stack.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all frames from the stack.intdepth()StringformatStackTrace()Renders the stack as a multi-line trace with method, line, and PC per frame.booleanisEmpty()StackFramepeek()Returns the top frame without removing it.StackFramepeekAt(int depth)Returns the frame a given number of levels below the top without removing it.StackFramepop()Removes and returns the top frame.voidpush(StackFrame frame)Pushes a frame onto the stack.List<StackFrame>snapshot()Copies the current frames into an immutable list.Iterable<StackFrame>topToBottom()StringtoString()
-
-
-
Constructor Detail
-
CallStack
public CallStack(int maxDepth)
Creates an empty call stack.- Parameters:
maxDepth- maximum number of frames before overflow- Throws:
IllegalArgumentException- if maxDepth is not positive
-
-
Method Detail
-
push
public void push(StackFrame frame)
Pushes a frame onto the stack.- Parameters:
frame- the frame to push- Throws:
IllegalArgumentException- if the frame is nullStackOverflowError- if the stack is at maximum depth
-
pop
public StackFrame pop()
Removes and returns the top frame.- Returns:
- the popped frame
- Throws:
IllegalStateException- if the stack is empty
-
peek
public StackFrame peek()
Returns the top frame without removing it.- Returns:
- the top frame
- Throws:
IllegalStateException- if the stack is empty
-
peekAt
public StackFrame peekAt(int depth)
Returns the frame a given number of levels below the top without removing it.- Parameters:
depth- levels below the top, 0 being the top frame- Returns:
- the frame at that depth
- Throws:
IllegalArgumentException- if depth is negativeIndexOutOfBoundsException- if depth is beyond the current stack size
-
depth
public int depth()
- Returns:
- the number of frames on the stack
-
isEmpty
public boolean isEmpty()
- Returns:
- whether the stack has no frames
-
clear
public void clear()
Removes all frames from the stack.
-
snapshot
public List<StackFrame> snapshot()
Copies the current frames into an immutable list.- Returns:
- the frames ordered bottom to top
-
topToBottom
public Iterable<StackFrame> topToBottom()
- Returns:
- the live frames iterated from top to bottom
-
formatStackTrace
public String formatStackTrace()
Renders the stack as a multi-line trace with method, line, and PC per frame.- Returns:
- the formatted trace, or a placeholder when the stack is empty
-
-