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

Parameters:
__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.

Return type:

None

__str__()[source]

Return string representation of the if statement.

Returns:

A string showing the condition and both statement branches.

Return type:

str

__repr__()[source]

Return detailed string representation of the if statement.

Returns:

Same as __str__ for this class.

Return type:

str

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 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