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)[source]

Bases: Enum

Enumeration 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'
static get_op(op_str)[source]

Get the enumerated operator value from string.

Parameters:

op_str (str) – String representation of the operator.

Returns:

The corresponding operator enumeration value.

Return type:

Operator

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: Expression

Expression 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:

Expression

op_str

The string representation of the operator.

Type:

str

_rhs

The right-hand side expression operand.

Type:

Expression

Parameters:
__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

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