knit_script.knit_script_interpreter.expressions.list_expression module
Used for container structures (tuples, lists, dicts, comprehensions).
This module provides expression classes for handling various container data structures in knit script programs. It includes support for lists, dictionaries, tuples, list comprehensions, dictionary comprehensions, and unpacking operations, following Python conventions for syntax and behavior.
- class Unpack(parser_node, exp)[source]
Bases:
Expression
Used to unpack values into a tuple with * function.
The Unpack class implements the unpacking operator (*) functionality for knit script expressions. It takes an iterable expression and unpacks its elements into a tuple, similar to Python’s unpacking behavior.
- _exp
The expression to unpack into individual elements.
- Type:
- Parameters:
parser_node (
LRStackNode
)exp (
Expression
)
- __init__(parser_node, exp)[source]
Initialize the Unpack expression.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
exp (Expression) – Expression to unpack into individual elements.
- evaluate(context)[source]
Evaluate the expression to unpack the contained expression.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Tuple with unpacked values from the expression.
- Return type:
- 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 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 Knit_Script_List(parser_node, expressions)[source]
Bases:
Expression
Evaluates to list of expression values. Lists are not typed following python style conventions.
The Knit_Script_List class implements list literal expressions in knit script programs. It supports mixed-type elements and handles unpacking operations within list construction, following Python’s list syntax and behavior.
- expressions
The expressions to evaluate and include in the list.
- Type:
- Parameters:
parser_node (
LRStackNode
)expressions (
list
[Expression
])
- __init__(parser_node, expressions)[source]
Initialize the Knit_Script_List.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
expressions (list[Expression]) – Expressions to fill the list with.
- evaluate(context)[source]
Evaluate the expression to create a list.
Evaluates each expression in the list and handles unpacking operations. If an expression is an Unpack, its elements are extended into the list rather than added as a nested structure.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
List of expression evaluations at current context, with unpacked elements properly expanded.
- Return type:
list[Any]
- 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 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 Sliced_List(parser_node, iter_exp, start=None, start_to_end=False, end=None, end_to_spacer=False, spacer=None, is_index=False)[source]
Bases:
Expression
Slices a list using standard python syntax.
The Sliced_List class implements Python-style slicing and indexing operations for iterable expressions. It supports start, end, and step parameters for slicing, as well as simple indexing operations. Special handling is provided for knitting machine objects.
- _spacer
The step/spacer expression for slicing.
- Type:
Expression | None
- _end
The end index expression for slicing.
- Type:
Expression | None
- _start
The start index expression for slicing.
- Type:
Expression | None
- _iter_exp
The iterable expression to slice.
- Type:
- Parameters:
parser_node (
LRStackNode
)iter_exp (
Expression
)start (
Expression
|None
)start_to_end (
bool
)end (
Expression
|None
)end_to_spacer (
bool
)spacer (
Expression
|None
)is_index (
bool
)
- __init__(parser_node, iter_exp, start=None, start_to_end=False, end=None, end_to_spacer=False, spacer=None, is_index=False)[source]
Initialize the Sliced_List.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
iter_exp (Expression) – Iterable expression to slice.
start (Expression | None, optional) – Start of slice, inclusive, defaults to 0. Defaults to None.
start_to_end (bool, optional) – Whether to include start to end range. Defaults to False.
end (Expression | None, optional) – End of slice, exclusive, defaults to last element. Defaults to None.
end_to_spacer (bool, optional) – Whether to include end to spacer range. Defaults to False.
spacer (Expression | None, optional) – Step/spacer of slice, defaults to 1. Defaults to None.
is_index (bool, optional) – Whether this is an index operation rather than a slice. Defaults to False.
- evaluate(context)[source]
Evaluate the expression to perform slicing or indexing.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
The list of values in the given slice or the indexed value for single index operations.
- Return type:
Iterable[Any] | Any
- Raises:
TypeError – If attempting to slice a knitting machine or if the target is not iterable.
- 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 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 List_Comp(parser_node, fill_exp, variables, iter_exp, comp_cond)[source]
Bases:
Expression
Runs a list comprehension over an iterator.
The List_Comp class implements Python-style list comprehensions for knit script programs. It supports iteration variables, optional filtering conditions, and custom spacing patterns for iteration control.
- _comp_cond
Optional condition expression for filtering elements.
- Type:
Expression | None
- _spacer
Optional spacing control for iteration.
- Type:
str | None | Expression
- _fill_exp
Expression that generates values for the list.
- Type:
- _vars
Variables to bind during iteration.
- Type:
- _iter_exp
The iterable expression to iterate over.
- Type:
- Parameters:
parser_node (
LRStackNode
)fill_exp (
Expression
)variables (
list
[Variable_Expression
])iter_exp (
Expression
)comp_cond (
Expression
|None
)
- __init__(parser_node, fill_exp, variables, iter_exp, comp_cond)[source]
Initialize the List_Comp.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
fill_exp (Expression) – Expression that fills the list for each iteration.
variables (list[Variable_Expression]) – Variables to bind from the iterable.
iter_exp (Expression) – The iterable expression to iterate over.
comp_cond (Expression | None) – Optional condition expression for filtering values.
- evaluate(context)[source]
Evaluate the expression to generate a list through comprehension.
Creates a new scope for iteration variables, iterates over the iterable, and builds a list by evaluating the fill expression for each iteration that passes the optional condition.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Result of list comprehension with filtered and transformed values.
- Return type:
list[Any]
- Raises:
AssertionError – If the iterable is not actually iterable or if variable unpacking doesn’t match the provided variables.
- 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 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 Knit_Script_Dictionary(parser_node, kwargs)[source]
Bases:
Expression
Used to process dictionary structures.
- The Knit_Script_Dictionary class implements dictionary literal expressions in knit script programs.
It evaluates key-value pairs and constructs dictionaries following Python’s dictionary syntax and behavior.
- _kwargs
List of key-value expression pairs for the dictionary.
- Type:
- Parameters:
parser_node (
LRStackNode
)kwargs (
list
[tuple
[Expression
,Expression
]])
- __init__(parser_node, kwargs)[source]
Initialize the Knit_Script_Dictionary.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
kwargs (list[tuple[Expression, Expression]]) – The key-value pairs of expressions for the dictionary.
- evaluate(context)[source]
Evaluate the expression to create a dictionary.
Evaluates each key-value pair and constructs a dictionary with the results.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Dictionary with evaluated keys mapped to evaluated values.
- Return type:
dict[Any, Any]
- 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 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 Dictionary_Comprehension(parser_node, key, value, variables, iter_exp, comp_cond=None)[source]
Bases:
Expression
Used for supporting dictionary comprehension.
The Dictionary_Comprehension class implements Python-style dictionary comprehensions for knit script programs. It supports iteration variables, optional filtering conditions, and custom spacing patterns, generating dictionaries through iteration.
- _spacer
Optional spacing control for iteration.
- Type:
None | str | Expression
- _comp_cond
Optional condition expression for filtering elements.
- Type:
Expression | None
- _key
Expression that generates keys for the dictionary.
- Type:
- _value
Expression that generates values for the dictionary.
- Type:
- _iter_exp
The iterable expression to iterate over.
- Type:
- _vars
Variables to bind during iteration.
- Type:
- Parameters:
parser_node (
LRStackNode
)key (
Expression
)value (
Expression
)variables (
list
[Variable_Expression
])iter_exp (
Expression
)comp_cond (
Expression
|None
)
- __init__(parser_node, key, value, variables, iter_exp, comp_cond=None)[source]
Initialize the Dictionary_Comprehension.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
key (Expression) – Expression that generates keys for each dictionary entry.
value (Expression) – Expression that generates values for each dictionary entry.
variables (list[Variable_Expression]) – Variables to bind from the iterable.
iter_exp (Expression) – The iterable expression to iterate over.
comp_cond (Expression | None, optional) – Optional condition expression for filtering entries. Defaults to None.
- evaluate(context)[source]
Evaluate the expression to generate a dictionary through comprehension.
- Creates a new scope for iteration variables, iterates over the iterable,
and builds a dictionary by evaluating the key and value expressions for each iteration that passes the optional condition.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Result of dictionary comprehension with filtered and transformed key-value pairs.
- Return type:
dict[Any, Any]
- Raises:
KeyError – If the iterable is not actually iterable or if variable unpacking doesn’t match the provided variables.
- 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 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