knit_script.knit_script_interpreter.statements.Swap_Statement module
Statement that swaps layers between two needles.
This module provides the Swap_Statement class, which handles layer position exchanges between needles in multi-sheet gauge configurations. It supports both layer-based swapping and sheet-based swapping for complex stitch organization.
- class Swap_Statement(parser_node, needles, swap_type, value)[source]
Bases:
Statement
Statement that swaps stitch layers between needles.
This statement exchanges the layer positioning of stitches either between specific layer numbers or between needles on different sheets of a multi-sheet gauge setup. The swap operation allows for complex stitch organization and layer management in advanced knitting patterns.
- The statement supports two types of swapping: layer swaps that exchange positions with needles at specific layer indices,
and sheet swaps that exchange positions between corresponding needles on different sheets.
- _needles
List of expressions that evaluate to needles whose layers should be swapped.
- Type:
- _layer
Expression for layer-based swapping.
- Type:
Expression | None
- _sheet
Expression for sheet-based swapping.
- Type:
Expression | None
- Parameters:
parser_node (
LRStackNode
)needles (
list
[Expression
])swap_type (
str
)value (
Expression
)
- __init__(parser_node, needles, swap_type, value)[source]
Initialize a swap statement.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
needles (list[Expression]) – List of expressions that evaluate to needles whose layers should be swapped.
swap_type (str) – Type of swap operation, either “sheet” for swapping between sheets or “layer” for swapping with specific layer positions.
value (Expression) – Expression that evaluates to either a sheet number (for sheet swaps) or layer number (for layer swaps).
- execute(context)[source]
Execute the swap operation on the specified needles.
- For layer swaps, finds needles with the specified layer number and swaps their positions with the target needles.
For sheet swaps, swaps layers between needles on different sheets at corresponding positions within the gauge configuration.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Raises:
TypeError – If sheet or layer values are not integers.
- Return type:
- __str__()[source]
Return string representation of the swap statement.
- Returns:
A string showing the needles and swap operation type.
- 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