knit_script.knit_script_interpreter.statements.branch_statements module

Manages branching condition statements.

This module provides the If_Statement class, which implements conditional execution control flow in knit script programs. It allows programs to execute different code paths based on boolean condition evaluation, supporting the fundamental control flow needed for complex knitting logic.

class If_Statement(parser_node, condition, true_statement, false_statement=None)[source]

Bases: Statement

Conditional if-else branch structure.

Implements conditional execution where one of two statement branches is executed based on the evaluation of a boolean condition. The If_Statement class provides the fundamental branching control flow for knit script programs, allowing different knitting operations to be performed based on runtime conditions.

This statement supports both simple if statements and if-else structures, with the else branch being optional. The condition is evaluated using Python’s truthiness conventions, where empty collections, None, zero values, and False are considered falsy.

_condition

The boolean expression that determines which branch to execute.

Type:

Expression

_true_statement

The statement to execute when the condition is True.

Type:

Statement

_false_statement

The statement to execute when the condition is False.

Type:

Statement | None

__init__(parser_node, condition, true_statement, false_statement=None)[source]

Initialize an if-else statement.

Parameters:
  • parser_node (LRStackNode) – The parser node from the abstract syntax tree.

  • condition (Expression) – The boolean expression that determines which branch to execute. Will be evaluated and converted to boolean using Python truthiness rules.

  • true_statement (Statement) – The statement to execute when the condition evaluates to a truthy value.

  • false_statement (Statement | None, optional) – The statement to execute when the condition evaluates to a falsy value. If None, no action is taken when the condition is False. Defaults to None.

execute(context)[source]

Execute the appropriate branch based on the condition result.

Evaluates the condition expression and executes either the true statement or false statement based on the result. The condition evaluation follows Python’s truthiness conventions.

Parameters:

context (Knit_Script_Context) – The current execution context of the knit script interpreter.

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