knit_script.knit_script_interpreter.expressions.carrier module
Carrier expression.
This module provides the Carrier_Expression class, which handles the parsing and evaluation of carrier string literals in knit script code. It converts carrier string identifiers (like “c1”, “c2”, etc.) into the corresponding Yarn_Carrier objects from the knitting machine’s carrier system.
- class Carrier_Expression(parser_node, carrier_str)[source]
Bases:
ExpressionUsed for processing carrier strings.
The Carrier_Expression class handles the conversion of carrier string literals in knit script source code into actual Yarn_Carrier objects. It parses carrier identifiers that follow the pattern “c” followed by a number (e.g., “c1”, “c2”, “c10”) and retrieves the corresponding carrier from the knitting machine’s carrier system.
This expression type is essential for yarn carrier operations in knit script programs, allowing developers to reference specific carriers by their conventional string identifiers.
- __init__(parser_node, carrier_str)[source]
Initialize the Carrier_Expression.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
carrier_str (str) – The string to identify the carrier from, typically in the format “c” followed by a number (e.g., “c1”, “c2”).
- evaluate(context)[source]
Evaluate the expression to get the corresponding yarn carrier.
Parses the carrier string to extract the carrier ID number and retrieves the corresponding Yarn_Carrier object from the knitting machine’s carrier system.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
The carrier object with the specified ID from the machine’s carrier system.
- Return type:
Yarn_Carrier
- Raises:
ValueError – If the carrier string format is invalid or cannot be parsed.
KeyError – If the extracted carrier ID does not exist in the machine’s carrier system.
- 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]): ….
- classmethod __init_subclass__(**kwargs)
Automatically wrap evaluate methods in subclasses with appropriate error handling decorator.
This method is called whenever a class inherits from Expression. It checks if the subclass defines its own evaluate method and wraps it with the appropriate decorator.
- Parameters:
**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__
- 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: