/** * Relational expression, which can be used to compare two numeric expressions. * @since 0.1.0 * @example * 19 > 11 // true * 91 > 99 // false * 12 >= 11 // true * 77 >= 77 // true * 36 >= 43 // false * 19 < 36 // true * 91 < 45 // false * 12 <= 68 // true * 77 <= 77 // true * 36 <= 12 // false */ import type { RelationalExpressionSemantics } from "./relational-expression-semantics"; import type { RelationalExpressionTypeSemantics } from "./relational-expression-type-semantics"; import type { Expression } from "../../expression"; import { ComparativeExpression } from "../comparative-expression"; import type { RelationalExpressionContext } from "../../../../../lexer-parser"; import { KindParseRuleMapping, ParseRuleKindMapping } from "../../../../../lexer-parser"; import type { CompilableASTNode } from "../../../../compilable-ast-node"; import type { KipperRelationalOperator } from "../../../../../const"; import { kipperRelationalOperators } from "../../../../../const"; import { TerminalNode } from "antlr4ts/tree/TerminalNode"; import { UnableToDetermineSemanticDataError } from "../../../../../../errors"; import { BuiltInTypes } from "../../../../../semantics"; /** * Relational expression, which can be used to compare two numeric expressions. * @since 0.1.0 * @example * 19 > 11 // true * 91 > 99 // false * 12 >= 11 // true * 77 >= 77 // true * 36 >= 43 // false * 19 < 36 // true * 91 < 45 // false * 12 <= 68 // true * 77 <= 77 // true * 36 <= 12 // false */ export class RelationalExpression extends ComparativeExpression< RelationalExpressionSemantics, RelationalExpressionTypeSemantics > { /** * The static kind for this AST Node. * @since 0.11.0 */ public static readonly kind = ParseRuleKindMapping.RULE_relationalExpression; /** * The static rule name for this AST Node. * @since 0.11.0 */ public static readonly ruleName = KindParseRuleMapping[this.kind]; /** * The private field '_antlrRuleCtx' that actually stores the variable data, * which is returned inside the {@link this.antlrRuleCtx}. * @private */ protected override readonly _antlrRuleCtx: RelationalExpressionContext; constructor(antlrRuleCtx: RelationalExpressionContext, parent: CompilableASTNode) { super(antlrRuleCtx, parent); this._antlrRuleCtx = antlrRuleCtx; } /** * Returns the kind of this AST node. This represents the specific type of the {@link antlrRuleCtx} that this AST * node wraps. * * This may be compared using the {@link ParseRuleKindMapping rule fields}, for example * {@link ParseRuleKindMapping.RULE_expression}. * @since 0.10.0 */ public override get kind() { return RelationalExpression.kind; } /** * Returns the rule name of this AST Node. This represents the specific type of the {@link antlrRuleCtx} that this * AST node wraps. * * This may be compared using the {@link ParseRuleKindMapping rule fields}, for example * {@link ParseRuleKindMapping.RULE_expression}. * @since 0.11.0 */ public override get ruleName() { return RelationalExpression.ruleName; } /** * The antlr context containing the antlr4 metadata for this expression. */ public override get antlrRuleCtx(): RelationalExpressionContext { return this._antlrRuleCtx; } /** * Performs the semantic analysis for this Kipper token. This will log all warnings using {@link programCtx.logger} * and throw errors if encountered. * * This will not run in case that {@link this.hasFailed} is true, as that indicates that the semantic analysis of * the children has already failed and as such no parent node should run type checking. */ public async primarySemanticAnalysis(): Promise { // Get the raw antlr4 parse-tree children, which should store the operator const children = this.getAntlrRuleChildren(); // Get the expressions of this relational expression const leftOp: Expression = this.children[0]; const rightOp: Expression = this.children[1]; const operator = children.find((token) => { return token instanceof TerminalNode && kipperRelationalOperators.find((op) => op === token.text) !== undefined; })?.text; // Ensure that the children are fully present and not undefined if (!leftOp || !rightOp || !operator) { throw new UnableToDetermineSemanticDataError(); } this.semanticData = { leftOp: leftOp, rightOp: rightOp, operator: operator, }; } /** * Performs type checking for this AST Node. This will log all warnings using {@link programCtx.logger} * and throw errors if encountered. * * This will not run in case that {@link this.hasFailed} is true, as that indicates that the type checking of * the children has already failed and as such no parent node should run type checking. * @since 0.7.0 */ public async primarySemanticTypeChecking(): Promise { // Relational expressions always return 'bool' this.typeSemantics = { evaluatedType: BuiltInTypes.bool, }; // Type check the relational expression and ensure its operands are of type 'num' this.programCtx.typeCheck(this).validRelationalExpression(this); } public checkForWarnings = undefined; // TODO! readonly targetSemanticAnalysis = this.semanticAnalyser.relationalExpression; readonly targetCodeGenerator = this.codeGenerator.relationalExpression; }