@akala/core
Version:
68 lines • 2.92 kB
JavaScript
import { BinaryOperator } from './binary-operator.js';
import { Expression } from './expression.js';
import { ExpressionType } from './expression-type.js';
import { MemberExpression } from './member-expression.js';
import { TernaryExpression } from './ternary-expression.js';
/**
* Represents a binary expression.
* @template T - The type of the expressions.
*/
export class BinaryExpression extends Expression {
left;
operator;
right;
/**
* Gets the type of the expression.
* @returns {ExpressionType.BinaryExpression} The type of the expression.
*/
get type() { return ExpressionType.BinaryExpression; }
/**
* Initializes a new instance of the BinaryExpression class.
* @param {T} left - The left expression.
* @param {BinaryOperator} operator - The binary operator.
* @param {T} right - The right expression.
*/
constructor(left, operator, right) {
super();
this.left = left;
this.operator = operator;
this.right = right;
}
/**
* Accepts a visitor.
* @param {ExpressionVisitor} visitor - The visitor.
* @returns {any} The result of the visit.
*/
accept(visitor) {
return visitor.visitBinary(this);
}
/**
* Applies precedence to a parsed binary expression.
* @param {ParsedBinary} operation - The parsed binary expression.
* @returns {ParsedBinary} The parsed binary expression with applied precedence.
*/
static applyPrecedence(operation) {
if (operation.operator != BinaryOperator.Plus && operation.operator != BinaryOperator.Minus) {
if (operation.right instanceof BinaryExpression) {
const right = BinaryExpression.applyPrecedence(operation.right);
switch (operation.operator) {
case BinaryOperator.Times: // b*c+d ==> (b*c)+d
case BinaryOperator.Div:
case BinaryOperator.And:
return new BinaryExpression(new BinaryExpression(operation.left, operation.operator, right.left), right.operator, right.right);
case BinaryOperator.QuestionDot:
case BinaryOperator.Dot:
return new MemberExpression(new MemberExpression(operation.left, right.left, operation.operator == BinaryOperator.QuestionDot), right.right, right.operator == BinaryOperator.QuestionDot);
}
}
if (operation.right instanceof TernaryExpression) {
return new TernaryExpression(new BinaryExpression(operation.left, operation.operator, operation.right.first), operation.right.operator, operation.right.second, operation.right.third);
}
}
return operation;
}
toString() {
return `( ${this.left} ${this.operator} ${this.right} )`;
}
}
//# sourceMappingURL=binary-expression.js.map