knit_script.knit_script_interpreter.expressions.expressions module

Base class of all expression values.

This module provides the Expression base class, which serves as the foundation for all expression types in the knit script language. It also includes utility functions for working with expression collections and converting them to value lists for execution.

class Expression(parser_node)[source]

Bases: KS_Element

Superclass for all expressions which evaluate to a value.

The Expression class provides the base functionality for all evaluable elements in knit script programs. It extends KS_Element to include evaluation capabilities, allowing expressions to be processed within a knit script execution context to produce values.

All knit script expressions inherit from this class and must implement the evaluate method to define how they produce their values during program execution.

This includes literals, variables, function calls, operators, and complex compound expressions.

__init__(parser_node)[source]

Initialize the base expression.

Parameters:

parser_node (LRStackNode) – The parser node from the parse tree that created this expression.

classmethod __init_subclass__(**kwargs)[source]

Automatically wrap evaluate methods in subclasses with appropriate error handling decorator.

This method is called whenever a class inherits from Expression. It checks if the subclass defines its own evaluate method and wraps it with the appropriate decorator.

Parameters:

**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__

evaluate(context)[source]

Evaluate the expression to produce a value.

This method must be overridden by subclasses to define how the expression produces its value during program execution. The base implementation returns None and should not be used directly.

Parameters:

context (Knit_Script_Context) – The execution context used to evaluate expressions, containing variable scopes, machine state, and other runtime information.

Returns:

The evaluated result of the expression. The specific type depends on the expression implementation.

Return type:

Any

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

get_expression_value_list(context, expressions)[source]

Convert a list of expressions into a list of their values.

This utility function evaluates a list of expressions and flattens the results into a single list. If any expression evaluates to a list, its elements are extended into the result rather than being added as a nested list.

Parameters:
  • context (Knit_Script_Context) – The execution context to evaluate expressions in.

  • expressions (list[Expression]) – The list of expressions to convert to values.

Returns:

Flattened list of values from the expressions, with list results extended rather than nested.

Return type:

list[Any]

Note

This function is commonly used in contexts where expressions might evaluate to collections that should be flattened, such as in function argument processing or collection construction.