knit_script.knit_script_interpreter.expressions.operator_expressions module
Expressions with operators between left and right hand side.
This module provides classes for handling binary operator expressions in knit script programs. It includes the Operator enumeration that defines available operators and their behavior, and the Operator_Expression class that evaluates binary operations between two expressions.
- class Operator(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumEnumeration of different standard operators.
The Operator enumeration defines all the binary operators supported in knit script expressions. It includes arithmetic operators, comparison operators, logical operators, and membership operators, following Python’s operator conventions and behavior.
Each operator enum value provides both the string representation and the operation implementation, ensuring consistent behavior across all operator expressions.
- Add = '+'
- Sub = '-'
- Div = '/'
- Mod = '%'
- Mul = '*'
- Exp = '^'
- LT = '<'
- LTE = '<='
- GT = '>'
- GTE = '>='
- Equal = '=='
- NE = '!='
- Is = 'is'
- In = 'in'
- And = 'and'
- Or = 'or'
- operate(lhs, rhs)[source]
Execute the operation on the left and right operands.
Performs the operation represented by this operator enum value on the provided operands, following Python’s standard operator behavior and type coercion rules.
- Parameters:
lhs (Any) – Left-hand side operand (first value).
rhs (Any) – Right-hand side operand (second value).
- Returns:
Result of applying this operation to lhs and rhs, with return type depending on the operator and operand types.
- Return type:
Any
Note
Arithmetic operators return numeric results, comparison operators return booleans, logical operators follow Python’s short-circuit evaluation, and membership operators return booleans.
- classmethod __contains__(member)
Return True if member is a member of this enum raises TypeError if member is not an enum member
note: in 3.12 TypeError will no longer be raised, and True will also be returned if member is the value of a member in this enum
- classmethod __getitem__(name)
Return the member matching name.
- classmethod __iter__()
Return members in definition order.
- classmethod __len__()
Return the number of members (no aliases)
- class Operator_Expression(parser_node, lhs, op_str, rhs)[source]
Bases:
ExpressionExpression for managing operations between two expressions.
The Operator_Expression class handles binary operator expressions in knit script programs. It takes two operand expressions and an operator, evaluates the operands in the current context, and applies the specified operation to produce a result.
This expression type is fundamental to knit script programs, enabling arithmetic calculations, logical operations, comparisons, and other binary operations between any types of expressions.
- _lhs
The left-hand side expression operand.
- Type:
- _rhs
The right-hand side expression operand.
- Type:
- __init__(parser_node, lhs, op_str, rhs)[source]
Initialize the Operator_Expression.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
lhs (Expression) – Left-hand side expression operand.
op_str (str) – String representation of the operator to apply.
rhs (Expression) – Right-hand side expression operand.
- evaluate(context)[source]
Evaluate the expression to perform the binary operation.
Evaluates both operand expressions in the current context, retrieves the corresponding operator, and applies the operation to the evaluated operands.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
The result of applying the operator to the evaluated operands, with type depending on the operator and operand types.
- Return type:
Any
- 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: