예제 #1
0
        private static bool TryTakeOperatorExp(TokenKeeper pos, out ExpressionNode node)
        {
            var work = new TokenKeeper(pos);

            if (TryTakeTerm(work, out var left) &&
                TryTakeOperator(work, out var op) &&
                TryTakeSecondTerm(work, out var right))
            {
                node = new OperatorExpNode(left, op, right);
                pos.Swap(work);
                return(true);
            }

            node = null;
            return(false);
        }
예제 #2
0
        private static ComputationNode ConvertOperator(OperatorExpNode opNode)
        {
            if (opNode.Right is OperatorExpNode rightOpExp)
            {
                var rightPrecedence = OperatorPrecedence[rightOpExp.Op.Text];
                var leftPrecedence  = OperatorPrecedence[opNode.Op.Text];
                if (rightPrecedence > leftPrecedence)
                {
                    var myLeft    = opNode.Left;
                    var yourLeft  = rightOpExp.Left;
                    var yourRight = rightOpExp.Right;

                    var newRight = new OperatorExpNode(myLeft, opNode.Op, yourLeft);
                    var newOpExp = new OperatorExpNode(yourRight, rightOpExp.Op, newRight);
                    var desc     = newOpExp.Describe();
                    return(Convert(newOpExp));
                }
            }

            return(new BinaryOperation(opNode.Op, Convert(opNode.Left), Convert(opNode.Right)));
        }