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:

Expression

_statement

The statement to execute with each iteration.

Type:

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.

__str__()[source]

Return string representation of the while loop.

Returns:

A string showing the condition and statement.

Return type:

str

__repr__()[source]

Return detailed string representation of the while loop.

Returns:

Same as __str__ for this class.

Return type:

str

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__

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:

int

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:

str

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:

list[Variable_Expression]

var_name

Single variable name for simple iterations.

Type:

str | None

_iter_expression

Expression that evaluates to an iterable.

Type:

Expression | list[Expression]

_statement

Statement to execute with each iteration.

Type:

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.

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)[source]

Execute the for-each loop.

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:

ValueError – If unpacking multiple variables and the number of values doesn’t match the number of variables.

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:

int

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:

str