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_Element

Class 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:

Expression

variable_name

The name of the variable to assign to.

Type:

str

Parameters:
  • parser_node (LRStackNode)

  • var_name (str)

  • value_expression (Expression)

__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

__str__()[source]

Return string representation of the assignment.

Returns:

A string showing the variable name and value expression.

Return type:

str

__repr__()[source]

Return detailed string representation of the assignment.

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