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:
Scoped_StatementStatement 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:
- __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.
- pre_scope_action(context)[source]
Apply all assignments from the with-preamble to the subscope of the with statement. :param context: The current execution context of the knit script interpreter. :type context: Knit_Script_Context
- Returns:
True because the statement using the with-assignments should always be executed.
- Return type:
- classmethod __class_getitem__(params)
Parameterizes a generic class.
At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.
However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….
- classmethod __init_subclass__(**kwargs)
Automatically wrap execute methods in subclasses with appropriate error handling and debugging decorators.
This method is called whenever a class inherits from Statement. It checks if the subclass defines its own execute method and wraps it with the appropriate decorators
- Parameters:
**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__
- execute(context)
Execute any pre-scope actions and then executing the sub-scoped statement(s) within a new scope. If any statement triggers a return, execution stops early and the return value is preserved.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- property file_name: str | None
Returns: str | None: The file name of the knitscript program this was parsed from or None if the program was passed as a string.
- 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 local_path: str | None
Returns: str | None: The path to the directory containing the file from which this element was parsed or None if the value was parsed from a python string.
- 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
- property location_str: str
Returns: str: The string referencing the line number and possible file name information about this element.
- property position_context: str
The position context string is the string from the knitscript program from which this element was parsed. The context string will begin at the start of this element and continue to the end of the line of knitscript or a semicolon on new line are reached.
- Returns:
The string used to contextualize this element in the knitscript program.
- Return type: