knit_script.knit_script_interpreter.statements.assignment module
Assignment structure.
This module provides the Assignment class, which handles variable assignment operations in knit script programs. It manages the binding of values to variable names, supporting both local and global variable assignment with proper scope handling.
- class Assignment(parser_node, var_name, value_expression)[source]
Bases:
KS_ElementClass for managing assignment expressions.
- Handles the assignment of values to variables in the knit script language, supporting both local and global variable assignment.
The Assignment class evaluates the value expression in the current context and binds the result to the specified variable name according to the knit script scoping rules.
This class serves as both a standalone assignment operation and as a component in other language constructs like variable declarations, function parameters, and keyword arguments.
- _value_expression
The expression whose value will be assigned to the variable.
- Type:
- __init__(parser_node, var_name, value_expression)[source]
Initialize an assignment operation.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
var_name (str) – The name of the variable to assign to. Must be a valid identifier.
value_expression (Expression) – The expression whose value will be assigned to the variable.
- assign_value(context, is_global=False)[source]
Assign the evaluated value to the variable.
Evaluates the value expression and assigns the result to the variable in the appropriate scope. The assignment respects the knit script scoping hierarchy and can target either local or global scope.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
is_global (bool, optional) – If True, assigns the variable to the global scope. Otherwise, assigns to the current local scope following normal scoping rules. Defaults to False.
- Returns:
The value that was assigned to the variable.
- Return type:
Any
- value(context)[source]
Get the value to be assigned by evaluating the expression.
Evaluates the value expression in the current context to determine what value should be assigned to the variable.
- Parameters:
context (Knit_Script_Context) – The current execution context to evaluate the value expression.
- Returns:
The evaluated value that will be assigned to the variable.
- 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:
- 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: