knit_script.knit_script_interpreter.statements.code_block_statements module

Manages blocks of code executed in a new scope.

This module provides the Code_Block statement class, which handles the execution of multiple statements within a new variable scope. It manages scope creation, statement execution, and proper handling of return values and scope cleanup.

class Code_Block(parser_node, statements)[source]

Bases: Statement

Used for executing any block of code in a new scope.

Creates a new variable scope, executes all statements in order, then restores the previous scope. Handles return statements properly by preserving return values across scope boundaries.

This class is fundamental to knit script’s scoping system, providing isolation for variables while allowing controlled inheritance of changes.

The code block ensures that variables defined within the block don’t interfere with the outer scope unless explicitly designed to do so.

It supports early termination through return statements and properly propagates return values to the appropriate scope level.

_statements

Ordered list of statements to execute within the new scope.

Type:

list[Statement]

Parameters:
__init__(parser_node, statements)[source]

Initialize a code block.

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

  • statements (list[Statement]) – Ordered list of statements to execute within the new scope.

execute(context)[source]

Execute all statements in a new scope.

Creates a new variable scope, executes statements in order, then exits the scope. If any statement triggers a return, execution stops early and the return value is preserved. The scope is collapsed into the parent to preserve variable changes.

Parameters:

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

Return type:

None

__str__()[source]

Return string representation of the code block.

Returns:

A string showing all statements in the block separated by semicolons.

Return type:

str

__repr__()[source]

Return detailed string representation of the code block.

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