knit_script.knit_script_interpreter.expressions.Indexed_Expression module

Expressions associated with slicing and indexing elements.

This module provides classes for handling Python-style indexing and slicing operations in knit script expressions.

It includes support for both simple indexing and slice operations with optional step values, as well as indexed assignment operations.

class Slice_Index(start, end, spacer, parser_node)[source]

Bases: Expression

An expression that slices a given expression using python notation.

The Slice_Index class implements Python-style slice notation for knit script expressions. It supports start, end, and step parameters, allowing for flexible slicing operations on lists, strings, and other sequence types. The class creates Python slice objects that can be used with standard indexing operations.

start

The start index expression for the slice.

Type:

Expression | None

end

The end index expression for the slice.

Type:

Expression | None

spacer

The step/spacer expression for the slice.

Type:

Expression | None

Parameters:
__init__(start, end, spacer, parser_node)[source]

Initialize the Slice_Index expression.

Parameters:
  • start (Expression | None) – The start index expression for the slice, or None for default start.

  • end (Expression | None) – The end index expression for the slice, or None for default end.

  • spacer (Expression | None) – The step/spacer expression for the slice, or None for default step of 1.

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

evaluate(context)[source]

Evaluate the expression to create a Python slice object.

Evaluates the start, end, and step expressions and creates a Python slice object with the resulting values. None values are preserved to allow Python’s default slicing behavior.

Parameters:

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

Returns:

A Python slice object with the evaluated start, end, and step values.

Return type:

slice

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

class Indexed_Expression(parser_node, item, key, assign)[source]

Bases: Expression

An expression to index into an expression using python notation.

The Indexed_Expression class implements Python-style indexing operations for knit script expressions. It supports both reading from and writing to indexed positions, handling both simple indexing and slice operations. The class provides comprehensive error handling for common indexing issues.

item

The expression to index into.

Type:

Expression

key

The index or slice expression to use for indexing.

Type:

Expression

assign

Optional assignment expression for indexed assignment operations.

Type:

Expression | None

Parameters:
__init__(parser_node, item, key, assign)[source]

Initialize the Indexed_Expression.

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

  • item (Expression) – The expression to index into (list, dict, string, etc.).

  • key (Expression) – The index or slice expression to use for accessing the item.

  • assign (Expression | None) – Optional assignment expression for setting values at the indexed position.

evaluate(context)[source]

Evaluate the expression to perform indexing or indexed assignment.

Evaluates the item and key expressions, performs the indexing operation, and optionally handles assignment if an assignment expression is provided. Includes comprehensive error handling for common indexing issues.

Parameters:

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

Returns:

The value at the indexed position, or a list of values for slice operations. For assignment operations, returns the accessed value after assignment.

Return type:

list[Any] | Any

Raises:
  • TypeError – If attempting to assign to a slice index.

  • IndexError – If the index is out of range for the item.

  • KeyError – If the key does not exist in a dictionary-like item.

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