UNPKG

simc-ast-builder

Version:

Parser and AST generator for SimulationCraft files

147 lines 4.88 kB
import { ExpressionNode } from "../parser/visitors/ast/common-types"; import { OptimizerOptions } from "../types"; /** * A class that optimizes logical conditions in the AST before code generation. */ export declare class ConditionOptimizer { /** * Get the sort priority for a nodeType. Higher index = higher priority = earlier. * If not found, returns -1 (lowest priority). */ CONDITION_SORT_ORDER: string[]; private options; /** * Create a new ConditionOptimizer * @param options Optimization options */ constructor(options?: OptimizerOptions); /** * Optimize an expression node by applying logical transformations * @param node The expression node to optimize * @returns The optimized expression node */ optimize(node: ExpressionNode): ExpressionNode; /** * Apply absorption laws to simplify expressions * - A && (A || B) → A * - A || (A && B) → A * @param node The node to simplify * @returns The simplified node */ private applyAbsorptionLaws; /** * Apply De Morgan's law to NOT expressions * !(A && B) -> !A || !B * !(A || B) -> !A && !B * @param node The node to transform * @returns The transformed node */ private applyDeMorgansLaw; /** * Replace field with negatedName in NOT expressions (e.g., !buff.up → buff.down) * @param node The node to optimize * @returns The optimized node */ private applyNegatedFieldOptimization; /** * Apply all available optimizations to the node, respecting option flags * @param node The node to optimize * @returns The optimized node */ private applyOptimizations; /** * Check if two nodes are complementary terms (one is the negation of the other) * @param node1 First node to check * @param node2 Second node to check * @returns True if the nodes are complementary terms */ private areComplementaryTerms; /** * Create a node representing the boolean value 'false' as a number (0) * @returns A number node with value 0 */ private createFalseNode; /** * Create a node representing the boolean value 'true' as a number (1) * @returns A number node with value 1 */ private createTrueNode; /** * Identify and eliminate common subexpressions * - (A && B) || (A && C) → A && (B || C) * - (A || B) && (A || C) → A || (B && C) * @param node The node to simplify * @returns The simplified node */ private eliminateCommonSubexpressions; /** * Flatten nested AND/OR expressions * - A && (B && C) → A && B && C * - A || (B || C) → A || B || C * * Note: Since our AST doesn't support nodes with more than two operands, * we'll restructure the binary tree to achieve the same logical effect. * @param node The node to flatten * @returns The flattened node */ private flattenNestedOperations; private getNodeTypePriority; /** * Check if a node is an AND operator node * @param node The node to check * @returns True if the node is an AND node */ private isAndNode; /** * Check if a node is a NOT operator node (unary with "not" operator) * @param node The node to check * @returns True if the node is a NOT node */ private isNotNode; /** * Check if a node is an OR operator node * @param node The node to check * @returns True if the node is an OR node */ private isOrNode; /** * Log the structure of an expression node for debugging * @param node The expression node to log * @param depth Current recursion depth (for indentation) */ private logNodeStructure; /** * Simplify expressions with complementary terms * - A && !A → false * - A || !A → true * @param node The node to simplify * @returns The simplified node */ private simplifyComplementaryTerms; /** * Simplify expressions involving boolean constants and identical operands * - true && A → A * - false && A → false * - true || A → true * - false || A → A * - !true → false * - !false → true * - A && A → A * - A || A → A * @param node The node to simplify * @returns The simplified node */ private simplifyConstantsAndIdentities; /** * Simplify double negation (!!A -> A) * @param node The node to simplify * @returns The simplified node */ private simplifyDoubleNegation; /** * Recursively sorts AND/OR conditions by nodeType priority (higher = earlier). * Only sorts if more than 2 operands, otherwise just recurses. */ private sortConditionsByPriority; } //# sourceMappingURL=ConditionOptimizer.d.ts.map