Architecture
How the engine works under the hood. For the DSL vocabulary (tags, rungs, instructions), see Core Concepts. For the execution API, see Runner.
The Redux model
pyrung is architected like Redux. State is immutable, logic is a pure function, and execution is consumer-driven.
Every step() call takes the current SystemState, evaluates all rungs as pure functions, and produces a new SystemState. The old state is still accessible. This makes programs deterministic, testable, and debuggable. The same state plus the same inputs always produce the same next state.
SystemState
class SystemState(Mapping[str, Any]):
scan_id : int # scan counter (resets to 0 on STOP→RUN/reboot)
timestamp : float # simulation clock (seconds)
tags : PMap # tag values, keyed by name string
memory : PMap # engine-internal state (edge detection, timer fractionals)
tags is everything user code touches. memory is internal engine bookkeeping. It holds the bit each rise()/fall() contact last saw (_edge:e{N}, one per contact), each counter's input memory, timer fractional accumulators, and so on.
SystemState is a small immutable class with one slot per field. Assigning to a field raises. set() and with_tags() return a new state. tags and memory are PMaps from the pyrsistent library. A PMap is a persistent map that shares structure between versions to save memory. Each scan produces a new SystemState without modifying the previous one.
Scan cycle
Every step() executes exactly one complete scan cycle through nine phases:
Phase 0 SCAN START System-point resets (RTC apply errors, command bits)
Phase 1 APPLY PATCH One-shot inputs from patch() written to context
Phase 2 READ INPUTS InputBlock values copied from external source
Phase 3 APPLY FORCES Pre-logic force pass (debug overrides)
Phase 4 EXECUTE LOGIC Rungs evaluated top-to-bottom
Phase 5 APPLY FORCES Post-logic force pass (re-assert force values)
Phase 6 WRITE OUTPUTS OutputBlock values pushed to external sink
Phase 7 ADVANCE CLOCK scan_id += 1, timestamp updated per time mode
Phase 8 SNAPSHOT New SystemState committed
All writes within a scan are batched and committed atomically at phase 8. Rungs see each other's writes immediately. A write in rung 3 is visible to rung 4 in the same scan.
Batched writes
Inside a scan, tag writes and memory updates accumulate in a mutable working space. The engine creates this space at scan start. Instructions write into it throughout execution. Phase 8 commits everything at once to produce the next immutable SystemState.
User code never interacts with this layer directly. Inside a with PLC(...) as plc: block, tag reads and writes are routed through the runner's pending scan state and committed atomically when the scan completes.
Consumer-driven execution
The engine never runs unsolicited. The consumer drives execution at whatever granularity it needs:
runner.step() # one complete scan
runner.run(cycles=100) # N scans
runner.run_for(1.0) # advance by simulation time
runner.run_until(~Motor) # stop on condition
This inversion of control is what makes pyrung suitable for testing, GUIs, and debuggers. A pytest test calls step() and asserts. A VS Code extension calls scan_steps_debug() and renders decorations. A soft PLC calls step() in a loop driven by a Modbus server.
Source location capture
During the DSL build phase (rung, rise(), out(), operators, builder-style APIs), each element captures its source file and line number. This metadata enables mapping from engine objects back to user code for editor integration.
Captured metadata per element:
- source_file: str | None
- source_line: int | None
- end_line: int | None (set for block contexts like rung and branch, best-effort from AST end_lineno)
Builder flows (shift(...).clock(...).reset(...), count_up(...).reset(...), etc.) preserve the original callsite metadata through the chain.
If rungs are built in a loop, multiple rung objects may share source lines. The mapping is best-effort in this case. Explicit DSL declarations keep a clean one-to-one mapping.
Debug stepping APIs
These APIs are used by the DAP adapter and are not part of the typical user workflow.
scan_steps() — rung-boundary generator
for rung_index, rung, ctx in runner.scan_steps():
print(f"After rung {rung_index}: {dict(ctx._tags_pending)}")
# scan commits when generator is exhausted
Executes one scan, yielding after each top-level rung evaluation. The scan only commits atomically when the generator is fully exhausted. If you stop early, the runner keeps its last committed state, and the patches that the scan applied stay pending. Call the generator's close() to drop the scan.
scan_steps_debug() — instruction-level stepping
for step in runner.scan_steps_debug():
print(step.rung_index, step.kind, step.source_line, step.enabled_state)
Yields ScanStep objects at rung, branch, subroutine, and instruction boundaries. This is the API the DAP adapter uses to drive execution with source location information. It commits the same way as scan_steps().
rung inspection
runner.debug.rung_trace() and runner.debug.last_event() return debug trace data for the most recently committed debug scan. Only one scan's worth of trace is retained. A non-debug commit (step()/run()) wipes it.
runner.debug.rung_trace(rung_id)
Returns a RungTrace for one rung in the current scan:
RungTrace.scan_id— committed scan idRungTrace.rung_id— top-level rung index (0-based)RungTrace.events— ordered tuple ofRungTraceEvent
Each RungTraceEvent captures one debug boundary:
kindis one of"rung","branch","subroutine"or"instruction"source_file,source_line,end_linesubroutine_name,depth,call_stackenabled_state,instruction_kindtraceis aTraceEvent | None
Raises KeyError(rung_id) when no debug trace exists for the current scan.
runner.debug.last_event()
Returns the latest debug-trace event as (scan_id, rung_id, RungTraceEvent) or None:
- Prefers in-flight events from an active
scan_steps_debug()scan - Falls back to latest committed retained debug-path event
- Returns
Nonewhen no debug trace context exists