knit_script.knit_script_interpreter.expressions.variables module

Expression for accessing variables.

This module provides the Variable_Expression class, which handles variable access operations in knit script programs. It provides the mechanism for retrieving variable values from the current execution scope, following the scope resolution hierarchy established by the knit script context system.

class Variable_Expression(parser_node, variable_name)[source]

Bases: Expression

A structure for accessing variables by name from the current context scope.

The Variable_Expression class implements variable access operations in knit script programs. It stores a variable name and retrieves the corresponding value from the current execution context using the scope resolution system. This includes searching through local scopes, parent scopes, module scopes, and global scopes as defined by the knit script scoping rules.

This expression type is fundamental to knit script programs, enabling access to all types of variables including user-defined variables, function parameters, machine state variables, and built-in constants.

_variable_name

The name of the variable to access from the current scope.

Type:

str

__init__(parser_node, variable_name)[source]

Initialize the Variable_Expression.

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

  • variable_name (str) – Name of the variable to access from the execution context.

property variable_name: str

Get the name of the variable to access.

Returns:

The name of the variable that this expression will retrieve from the current scope.

Return type:

str

evaluate(context)[source]

Evaluate the expression to retrieve the variable value.

Performs variable lookup in the current execution context using the scope resolution system.

This follows the knit script scoping hierarchy, searching through local scopes, parent scopes, module scopes, and global scopes to find the variable.

Parameters:

context (Knit_Script_Context) – The current context of the knit_script_interpreter.

Returns:

The value of the variable found in the lowest applicable scope level.

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

classmethod __init_subclass__(**kwargs)

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__

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