omdBinaryExpressionNode
Represents a binary operation in a mathematical expression, such as addition (a + b), subtraction, multiplication, or division. This node is a cornerstone of the expression tree, containing a left operand, a right operand, and an operator.
Class Definition
export class omdBinaryExpressionNode extends omdNode
Constructor
new omdBinaryExpressionNode(ast)
Creates a new omdBinaryExpressionNode instance.
ast(object): The abstract syntax tree (AST) node from a parser like math.js. It must contain:args: An array with at least two operand nodes.op: The operator symbol (e.g.,+,*).fn: The function name for the operation (e.g.,add,multiply).implicit(boolean, optional):trueif the multiplication is implicit (e.g.,2x).
During construction, the node automatically handles a critical piece of mathematical convention: implicit multiplication. Based on the configuration in omdConfigManager, it will:
- Reorder Operands: If it encounters an expression like
x * 2, it will automatically reorder it to the conventional2 * xby swapping the left and right child nodes. - Determine Implicit Form: It checks if the combination of operands (e.g., a constant and a variable) should be represented implicitly. If so, it removes the visible operator (
*) and marks the node as implicit.
Public Properties
left(omdNode): The node representing the left-hand side of the operation.right(omdNode): The node representing the right-hand side of the operation.op(omdOperatorNode|null): The node for the operator. This isnullfor implicit multiplication.operation(string): The name of the mathematical function (e.g.,'add','multiply').isImplicit(boolean):trueif the node represents implicit multiplication.
Public Methods
clone()
Creates a deep, recursive clone of the node, including its children (left, right, op). The new node's provenance property will link back to the ID of this original node.
- Returns: A new
omdBinaryExpressionNodeinstance.
evaluate(variables)
Recursively evaluates the expression and returns the numerical result.
variables(object, optional): A map of variable names to their numeric values (e.g.,{ x: 5 }).- Returns:
number- The result of the calculation. - Throws: An
Errorfor unsupported operations or division by zero.
needsParentheses()
Determines if this expression needs to be wrapped in parentheses to maintain the correct order of operations when it is a child of another binary expression.
- Returns:
boolean-trueif parentheses are required.
setHighlight(highlightOn, color)
Applies or removes a highlight from the node and all of its children (left, right, and operator).
highlightOn(boolean):trueto highlight,falseto remove.color(string, optional): The color of the highlight.
clearProvenanceHighlights()
Recursively clears all provenance-related highlights from this node and its children.
toMathJSNode()
Converts the node back into a math.js-compatible AST format.
- Returns:
object- A math.js OperatorNode.
toString()
Converts the node into a human-readable string, automatically handling parentheses for precedence.
- Returns:
string- The string representation of the expression.
Internal Methods
_shouldUseImplicitMultiplication(left, right): Checks the configuration to decide if implicit multiplication is appropriate for the given operand types._shouldReorderMultiplication(left, right): Determines if operands should be swapped to follow convention (e.g.,variable * constant->constant * variable).computeDimensions(): Calculates the bounding box of the expression.updateLayout(): Positions the left operand, operator, and right operand correctly, aligning them along their baseline.