Exemplo n.º 1
0
        private ASTNode GetTwoParamOperatorPrecedence(ASTNode left, ASTNode @operator, ASTNode right)
        {
            if (!ManualPrecedenceOperators.Contains(@operator.NodeType) || !(ManualPrecedenceOperators.Contains(right.NodeType)))
            {
                @operator.Children[0] = left;
                @operator.Children[1] = right;
                return(@operator);
            }

            // Lower numbers is higher precedence
            int operatorPriority = OperatorPrecedences[@operator.NodeType],
                rightPriority    = OperatorPrecedences[right.NodeType];

            // Right has stronger precedence
            if (operatorPriority > rightPriority)
            {
                @operator.Children[0] = left;
                @operator.Children[1] = right;
                return(@operator);
            }

            // Left has stronger precedence
            if (right.Children.Count == 0)
            {
                throw new InvalidOperationException("Manual precedence operator AST Node did not have at least one child during precedence resolution - how'd this happen?");
            }

            @operator.Children[0] = left;
            @operator.Children[1] = right.Children[0];
            right.Children[0]     = @operator;
            return(right);
        }
Exemplo n.º 2
0
        private ASTNode GetUnaryOperatorPrecedence(ASTNode @operator, ASTNode operand)
        {
            if (!ManualPrecedenceOperators.Contains(operand.NodeType))
            {
                @operator.Children[0] = operand;
                return(@operator);
            }

            // Lower numbers is higher precedence
            int operatorPriority = OperatorPrecedences[@operator.NodeType],
                rightPriority    = OperatorPrecedences[operand.NodeType];

            // Right has stronger precedence
            if (operatorPriority >= rightPriority)
            {
                @operator.Children[0] = operand;
                return(@operator);
            }

            // Left has stronger precedence
            if (operand.Children.Count != 2)
            {
                throw new InvalidOperationException("Manual precedence operator AST Node did not have two children during precedence resolution - how'd this happen?");
            }

            @operator.Children[0] = operand.Children[0];
            operand.Children[0]   = @operator;
            return(operand);
        }