Skip to content

Commit a8a1d6d

Browse files
authored
add debug mode for interpreter (#158)
this adds a new option for the interpreter for debugging purposes. This will enable some runtime checks that a linter (e.g `pyright`) might not be able to catch correctly, and can be turned off when actually using the interpreter. cc: @weinbe58
1 parent 3751cb1 commit a8a1d6d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/kirin/interp/base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from kirin.interp.impl import Signature
1414
from kirin.interp.frame import FrameABC
1515
from kirin.interp.state import InterpreterState
16-
from kirin.interp.value import Err, MethodResult, StatementResult
16+
from kirin.interp.value import Err, MethodResult, SpecialResult, StatementResult
1717

1818
if TYPE_CHECKING:
1919
from kirin.registry import StatementImpl
@@ -57,6 +57,7 @@ def __init__(
5757
bottom: ValueType,
5858
*,
5959
fuel: int | None = None,
60+
debug: bool = False,
6061
max_depth: int = 128,
6162
max_python_recursion_depth: int = 8192,
6263
):
@@ -69,6 +70,7 @@ def __init__(
6970
self.symbol_table: dict[str, Statement] = {}
7071
self.state: InterpreterState[FrameType] = InterpreterState()
7172
self.fuel = fuel
73+
self.debug = debug
7274
self.max_depth = max_depth
7375
self.max_python_recursion_depth = max_python_recursion_depth
7476

@@ -234,7 +236,13 @@ def run_stmt(self, frame: FrameType, stmt: Statement) -> StatementResult[ValueTy
234236
method = self.lookup_registry(frame, stmt)
235237
if method is not None:
236238
try:
237-
return method(self, frame, stmt)
239+
results = method(self, frame, stmt)
240+
if self.debug and not isinstance(results, (tuple, SpecialResult)):
241+
return Err(
242+
ValueError("method must return tuple or SpecialResult"),
243+
self.state.frames,
244+
)
245+
return results
238246
except InterpreterError as e:
239247
return Err(e, self.state.frames)
240248

0 commit comments

Comments
 (0)