예제 #1
0
        private static IEvaluable CreateUnaryChild(IEvaluable active, Operators op, Func <object, object> evaluate)
        {
            UnaryScope <Operators> self = new UnaryScope <Operators>(active, op, evaluate);

            active.TryAccept(self);
            return(self);
        }
        private static IEvaluable CreateBinaryChild(IEvaluable active, Operators op, Func <Operators, bool> precedesOperator, Func <object, object, object> evaluate)
        {
            BinaryScope <Operators> self;

            //If we could steal an arg...
            if (!active.IsIndivisible)
            {
                BinaryScope <Operators> left = active as BinaryScope <Operators>;
                if (left != null && precedesOperator(left.Operator))
                {
                    self = new BinaryScope <Operators>(active, op, evaluate)
                    {
                        Left = left.Right
                    };
                    left.Right.Parent = self;
                    left.Right        = self;
                    return(self);
                }
            }

            //We couldn't steal an arg, "active" is now our left, inject ourselves into
            //  active's parent in its place
            self = new BinaryScope <Operators>(active.Parent, op, evaluate);

            if (active.Parent != null)
            {
                UnaryScope <Operators> unary = active.Parent as UnaryScope <Operators>;

                if (unary != null)
                {
                    unary.Parent = self;
                }
                else
                {
                    BinaryScope <Operators> binary = active.Parent as BinaryScope <Operators>;

                    if (binary != null)
                    {
                        if (binary.Left == active)
                        {
                            binary.Left = self;
                        }
                        else if (binary.Right == active)
                        {
                            binary.Right = self;
                        }
                    }
                }
            }

            active.Parent = self;
            self.Left     = active;
            return(self);
        }