@akala/core
Version:
55 lines • 1.84 kB
JavaScript
import { Expression } from './expression.js';
import { ExpressionType } from './expression-type.js';
/**
* Represents a ternary conditional expression using the '? :' syntax.
* @template T - The type of the expressions involved in the condition.
*/
export class TernaryExpression extends Expression {
/**
* Gets the type of the expression.
* @returns {ExpressionType.TernaryExpression} The expression type constant
*/
get type() { return ExpressionType.TernaryExpression; }
/**
* The condition expression that determines which branch to take.
*/
first;
/**
* The ternary operator (always '?' in valid expressions).
*/
operator;
/**
* The expression to evaluate if the condition is truthy.
*/
second;
/**
* The expression to evaluate if the condition is falsy.
*/
third;
/**
* Creates a new TernaryExpression instance.
* @param {T} first - The condition expression
* @param {TernaryOperator} operator - The '?' operator token
* @param {T} second - The expression for the truthy case
* @param {T} third - The expression for the falsy case
*/
constructor(first, operator, second, third) {
super();
this.first = first;
this.operator = operator;
this.second = second;
this.third = third;
}
/**
* Accepts a visitor for the visitor pattern.
* @param {ExpressionVisitor} visitor - The visitor to process this node
* @returns {TernaryExpression<Expressions>} The result of visitor processing
*/
accept(visitor) {
return visitor.visitTernary(this);
}
toString() {
return `( ${this.first} ${this.operator[0]} ${this.second} ${this.operator[1]} ${this.third} )`;
}
}
//# sourceMappingURL=ternary-expression.js.map