knit_script.knit_script_interpreter.statements.control_loop_statements module
Loop control structures.
This module provides statement classes for implementing loop control flow in knit script programs. It includes while loops for condition-based iteration and for-each loops for iterating over collections, both essential for repetitive knitting operations and pattern generation.
- class While_Statement(parser_node, condition, statement)[source]
Bases:
Statement
While loop execution structure.
Repeatedly evaluates a condition and executes a statement while the condition remains true. This provides the fundamental loop control structure for condition-based iteration in knit script programs, enabling repetitive operations based on dynamic conditions.
The while loop follows Python’s truthiness conventions for condition evaluation and provides proper scope management for loop variables and state.
- _condition
The boolean expression to evaluate before each iteration.
- Type:
- Parameters:
parser_node (
LRStackNode
)condition (
Expression
)statement (
Statement
)
- __init__(parser_node, condition, statement)[source]
Initialize a while loop.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
condition (Expression) – The boolean expression to evaluate before each iteration. Loop continues while this evaluates to a truthy value.
statement (Statement) – The statement to execute with each iteration of the loop.
- execute(context)[source]
Execute the while loop.
Evaluates the condition and executes the statement repeatedly until the condition becomes false. The condition is re-evaluated before each iteration.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Return type:
- __str__()[source]
Return string representation of the while loop.
- Returns:
A string showing the condition and statement.
- Return type:
- __repr__()[source]
Return detailed string representation of the while loop.
- 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
- class For_Each_Statement(parser_node, variables, iter_expression, statement)[source]
Bases:
Statement
For-each loop that iterates over iterable elements.
Provides access to iterable variables over lists or other iterable objects. Supports both single variable and multiple variable unpacking for complex iteration patterns. This is the primary iteration mechanism for processing collections in knit script programs.
The for-each loop creates a new scope for iteration variables, ensuring proper variable isolation while allowing access to outer scope variables. It supports unpacking for tuple iteration and provides comprehensive error handling for iteration issues.
- _variables
List of variables to assign on each iteration.
- Type:
- _iter_expression
Expression that evaluates to an iterable.
- Type:
- Parameters:
parser_node (
LRStackNode
)variables (
list
[Variable_Expression
])iter_expression (
Expression
|list
[Expression
])statement (
Statement
)
- __init__(parser_node, variables, iter_expression, statement)[source]
Initialize a for-each loop.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
variables (If multiple) – List of variables to assign on each iteration. If single variable, assigns the iterated value directly.
variables
value. (unpacks each iterated)
iter_expression (Expression | list[Expression]) – Expression that evaluates to an iterable, or list of expressions to iterate over.
statement (Statement) – Statement to execute with each iteration of the loop.
- execute(context)[source]
Execute the for-each loop.
Creates a new scope for the loop variables, then iterates over the iterable expression, assigning values and executing the statement for each iteration. Handles both single variable assignment and multiple variable unpacking.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Raises:
TypeError – If the expression does not evaluate to an iterable.
ValueError – If unpacking multiple variables and the number of values doesn’t match the number of variables.
- Return type:
- __str__()[source]
Return string representation of the for-each loop.
- Returns:
A string showing the variables, iterable, and statement.
- Return type:
- __repr__()[source]
Return detailed string representation of the for-each loop.
- 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