knit_script.knit_script_interpreter.statements.Variable_Declaration module

Statement for declaring a variable.

This module provides the Variable_Declaration statement class, which handles variable declaration operations in knit script programs. It wraps assignment operations with scope-specific behavior for both local and global variable declarations.

class Variable_Declaration(parser_node, assignment, is_global=False)[source]

Bases: Statement

Used to set a variable value at the current or global scope.

This statement wraps an assignment operation and can optionally declare the variable in the global scope rather than the current local scope. It provides explicit variable declaration semantics that can override the normal scoping rules when global declaration is specified.

Variable declarations are used to establish new variables with specific scope targeting, ensuring that variables are created in the intended scope level regardless of existing variable names in parent scopes.

_is_global

True if the variable should be declared in global scope.

Type:

bool

_assignment

The assignment operation that defines the variable name and value.

Type:

Assignment

Parameters:
__init__(parser_node, assignment, is_global=False)[source]

Initialize a variable declaration.

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

  • assignment (Assignment) – The assignment operation that defines the variable name and value.

  • is_global (bool, optional) – If True, declares the variable in the global scope. If False, declares in the current local scope. Defaults to False.

execute(context)[source]

Execute the variable declaration by performing the assignment.

Evaluates the assignment expression and stores the result in the variable scope according to the global/local setting, ensuring the variable is created in the intended scope level.

Parameters:

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

Return type:

None

__str__()[source]

Return string representation of the variable declaration.

Returns:

A string showing the assignment with a semicolon.

Return type:

str

__repr__()[source]

Return detailed string representation of the variable declaration.

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