knit_script.knit_script_interpreter.statements.With_Statement module
With statement for setting variables in temporary variable space.
This module provides the With_Statement class, which implements temporary variable scoping in knit script programs. It allows variables to be temporarily assigned new values within a specific scope, then automatically restored when the scope exits.
- class With_Statement(parser_node, statement, assignments)[source]
Bases:
Statement
Statement that sets variables for a sub-statement block.
Creates a temporary scope where specified variables are assigned new values, executes a statement within that scope, then restores the previous values. Special handling is provided for machine variables like rack and carrier to ensure proper machine state management.
- This statement is useful for temporarily changing machine configuration or variable values for specific operations without permanently affecting the outer scope.
It provides a clean way to make scoped changes that are automatically reverted.
- _assignments
List of variable assignments to make in the temporary scope.
- Type:
- Parameters:
parser_node (
LRStackNode
)statement (
Statement
)assignments (
list
[Assignment
])
- __init__(parser_node, statement, assignments)[source]
Initialize a with statement.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
statement (Statement) – The statement to execute within the temporary variable scope.
assignments (list[Assignment]) – List of variable assignments to make in the temporary scope before executing the statement.
- execute(context)[source]
Execute the statement within a temporary variable scope.
Creates a new scope, applies all assignments, executes the statement, then restores machine variables to their previous states. Machine variables are handled specially to ensure proper state restoration and consistency.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Return type:
- __str__()[source]
Return string representation of the with statement.
- Returns:
A string showing the assignments and statement.
- Return type:
- __repr__()[source]
Return detailed string representation of the with statement.
- Returns:
Same as __str__ for this class.
- Return type:
- property line_number: int
Get the line number of the symbol that generated this statement.
- Returns:
The line number where this element appears in the source file.
- Return type:
- property location: Location
Get the location of this symbol in KnitScript file.
- Returns:
The location of this symbol in the source file, including file name, line number, and position information.
- Return type:
Location