/** * Tangled/Parenthesised expression, which represents a parenthesised expression that wraps another expression and * increases its order of precedence. * @example * (4 + 5) * 5; // 4 + 5 will be evaluated first, then the multiplication will be performed * @since 0.1.0 */ import type { TangledPrimaryExpressionSemantics } from "./tangled-primary-expression-semantics"; import type { TangledPrimaryExpressionTypeSemantics } from "./tangled-primary-expression-type-semantics"; import type { CompilableASTNode } from "../../../../compilable-ast-node"; import type { Expression } from "../../expression"; import type { TangledPrimaryExpressionContext } from "../../../../../lexer-parser"; import { KindParseRuleMapping, ParseRuleKindMapping } from "../../../../../lexer-parser"; import { UnableToDetermineSemanticDataError } from "../../../../../../errors"; import { PrimaryExpression } from "../primary-expression"; /** * Tangled/Parenthesised expression, which represents a parenthesised expression that wraps another expression and * increases its order of precedence. * @example * (4 + 5) * 5; // 4 + 5 will be evaluated first, then the multiplication will be performed * @since 0.1.0 */ export class TangledPrimaryExpression extends PrimaryExpression< TangledPrimaryExpressionSemantics, TangledPrimaryExpressionTypeSemantics > { /** * The static kind for this AST Node. * @since 0.11.0 */ public static readonly kind = ParseRuleKindMapping.RULE_tangledPrimaryExpression; /** * 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: TangledPrimaryExpressionContext; constructor(antlrRuleCtx: TangledPrimaryExpressionContext, 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 TangledPrimaryExpression.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 TangledPrimaryExpression.ruleName; } /** * The antlr context containing the antlr4 metadata for this expression. */ public override get antlrRuleCtx(): TangledPrimaryExpressionContext { 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 { // Tangled expressions always contain one expression child const exp: Expression = this.children[0]; // Ensure that the child expression is present if (!exp) { throw new UnableToDetermineSemanticDataError(); } this.semanticData = { childExp: exp, }; } /** * 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 { const exp = this.getSemanticData().childExp; this.typeSemantics = { // Tangled expressions always evaluate to the type of its child expression evaluatedType: exp.getTypeSemanticData().evaluatedType, }; } public checkForWarnings = undefined; // TODO! readonly targetSemanticAnalysis = this.semanticAnalyser.tangledPrimaryExpression; readonly targetCodeGenerator = this.codeGenerator.tangledPrimaryExpression; }