knit_script.knit_script_interpreter.expressions.instruction_expression module

Instructions Expressions.

This module provides expression classes for handling knitting machine instructions in knit script programs.

It includes classes for needle-specific instructions and general machine instructions, with proper type checking and validation.

class Needle_Instruction_Exp(parser_node, instruction, needles)[source]

Bases: Expression

Instructions that happen on a needle.

The Needle_Instruction_Exp class represents instructions that are applied to specific needles on the knitting machine.

It combines an instruction type with a set of target needles, ensuring that the instruction is valid for needle operations and that all targets are proper Needle objects.

This class is essential for carriage pass operations where specific instructions (like knit, tuck, miss, etc.) need to be applied to particular needles. It provides validation to ensure instruction types are appropriate for needle operations.

_needles

List of expressions that evaluate to needles.

Type:

list[Expression]

_instruction_type

The instruction type to apply to the needles.

Type:

Expression | Knitout_Instruction_Type

__init__(parser_node, instruction, needles)[source]

Initialize the Needle_Instruction_Exp.

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

  • instruction (Expression | Knitout_Instruction_Type) – The instruction to apply to the needle set, either as a direct instruction type or an expression that evaluates to one.

  • needles (list[Expression] | Expression) – The needles to apply the instruction to, either as a list of expressions or a single expression that may evaluate to multiple needles.

evaluate(context)[source]

Evaluate the expression to get the instruction and target needles.

Evaluates the instruction type and needle expressions, validates that the instruction is appropriate for needle operations, and ensures all targets are valid Needle objects.

Parameters:

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

Returns:

A tuple containing the validated instruction type and the list of target needles.

Return type:

tuple[Knitout_Instruction_Type, list[Needle]]

Raises:
  • TypeError – If the instruction type is not a Knitout_Instruction_Type or if any needle is not a Needle object.

  • Needle_Instruction_Type_Exception – If the instruction type is not valid for needle operations.

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:

int

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:

str

class Machine_Instruction_Exp(parser_node, inst_str)[source]

Bases: Expression

Expression evaluates to machine instructions.

The Machine_Instruction_Exp class represents general machine instruction expressions that are not specific to individual needles.

It converts instruction string identifiers into the corresponding Knitout_Instruction_Type objects.

This class is used for machine-level operations that don’t target specific needles but rather control overall machine behavior or state.

inst_str

The instruction string identifier.

Type:

str

__init__(parser_node, inst_str)[source]

Initialize the Machine_Instruction_Exp.

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

  • inst_str (str) – The instruction string identifier to convert to an instruction type.

evaluate(context)[source]

Evaluate the expression to get the machine instruction type.

Converts the instruction string identifier into the corresponding Knitout_Instruction_Type object.

Parameters:

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

Returns:

The instruction type matching the string identifier.

Return type:

Knitout_Instruction_Type

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:

int

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:

str