knit_script.knit_script_interpreter.statements.Statement module

Basic statement structures.

This module provides the base Statement class and the Expression_Statement class, which form the foundation of the knit script statement system. These classes define the basic contract for executable code elements and provide mechanisms for using expressions as statements.

class Statement(parser_node)[source]

Bases: KS_Element

Superclass for all operations that do not produce a value.

This is the base class for all executable statements in the knit script language. Statements perform actions or side effects but do not return values (unlike expressions). They represent the fundamental building blocks of knit script programs and define the contract that all executable code elements must follow.

The Statement class extends KS_Element to provide execution capabilities while maintaining access to parser node information for error reporting and debugging. All specific statement types inherit from this class and implement the execute method to define their behavior.

__init__(parser_node)[source]

Initialize a statement.

Parameters:

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

classmethod __init_subclass__(**kwargs)[source]

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 statement at the current machine context.

This is the main method that subclasses override to implement their specific behavior. The base implementation does nothing and should be overridden by all concrete statement types.

Parameters:

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

Raises:

NotImplementedError – The base implementation does not implement the execute method.

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]): ….

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