Skip to content

Commit 19db876

Browse files
Refactor state initialization in the run function for improved clarity and maintainability
1 parent af5ca67 commit 19db876

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

techlang/interpreter.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
from .system_ops import ProcessOpsHandler
99

1010

11+
def initialize_state(
12+
state: InterpreterState,
13+
inputs: Optional[List[str]] = None,
14+
loaded_files: Optional[Set[str]] = None,
15+
) -> None:
16+
"""Initialize an existing InterpreterState for execution.
17+
18+
The CLI REPL keeps a persistent state object and needs the same baseline
19+
initialization that `run()` applies to a fresh state.
20+
"""
21+
22+
ProcessOpsHandler.prime_cached_streams(state)
23+
state.input_queue = list(inputs or [])
24+
state.loaded_files = set(loaded_files or set())
25+
26+
1127
def run(code: str, inputs: Optional[List[str]] = None, loaded_files: Optional[Set[str]] = None, base_dir: Optional[str] = None) -> str:
1228
# Entry point: parse, expand aliases, execute, return output
1329
if base_dir is None:
@@ -16,9 +32,7 @@ def run(code: str, inputs: Optional[List[str]] = None, loaded_files: Optional[Se
1632
base_dir = os.path.abspath(base_dir)
1733

1834
state = InterpreterState()
19-
ProcessOpsHandler.prime_cached_streams(state)
20-
state.input_queue = inputs or []
21-
state.loaded_files = loaded_files or set()
35+
initialize_state(state, inputs=inputs, loaded_files=loaded_files)
2236

2337
tokens: List[str] = parse(code)
2438

0 commit comments

Comments
 (0)