Skip to content

State

InterpreterState dataclass

InterpreterState()

Bases: Generic[FrameType]

Interpreter state.

This class represents the state of the interpreter. It contains the stack of frames for the interpreter. The stack of frames is used to store the current state of the interpreter during interpretation.

current_frame property

current_frame: FrameType

Get the current frame.

Returns:

Name Type Description
FrameType FrameType

The current frame.

depth class-attribute instance-attribute

depth: int = field(
    default=0, kw_only=True, init=False, repr=False
)

stack depth of the interpreter.

pop_frame

pop_frame() -> FrameType

Pop a frame from the stack.

Returns:

Name Type Description
FrameType FrameType

The frame that was popped.

Source code in src/kirin/interp/state.py
54
55
56
57
58
59
60
61
62
63
64
65
66
def pop_frame(self) -> FrameType:
    """Pop a frame from the stack.

    Returns:
        FrameType: The frame that was popped.
    """
    if self._current_frame is None:
        raise ValueError("no frame to pop")
    frame = self._current_frame
    self._current_frame = self._current_frame.parent
    self.depth -= 1
    frame.parent = None
    return frame

push_frame

push_frame(frame: FrameType) -> FrameType

Push a frame onto the stack.

Parameters:

Name Type Description Default
frame(FrameType)

The frame to push onto the stack.

required

Returns:

Name Type Description
FrameType FrameType

The frame that was pushed.

Source code in src/kirin/interp/state.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def push_frame(self, frame: FrameType) -> FrameType:
    """Push a frame onto the stack.

    Args:
        frame(FrameType): The frame to push onto the stack.

    Returns:
        FrameType: The frame that was pushed.
    """
    assert frame.parent is None, "frame already has a parent"
    self.depth += 1
    if self._current_frame is None:
        self._current_frame = frame
    else:
        frame.parent = self._current_frame
        self._current_frame = frame
    return self._current_frame