Esempio n. 1
0
        /// <summary>
        /// Subtracts the right operand from the left one.
        /// </summary>
        /// <param name="Left">Left operand.</param>
        /// <param name="Right">Right operand.</param>
        /// <param name="Node">Node performing the operation.</param>
        /// <returns>Result</returns>
        public static IElement EvaluateSubtraction(IElement Left, IElement Right, ScriptNode Node)
        {
            IGroupElement LE = Left as IGroupElement;
            IGroupElement RE = Right as IGroupElement;
            IElement      Result;
            IGroupElement Temp;

            if (LE != null && RE != null)
            {
                Temp = RE.Negate();
                if (Temp != null)
                {
                    Result = LE.AddRight(Temp);
                    if (Result != null)
                    {
                        return(Result);
                    }

                    Result = Temp.AddLeft(LE);
                    if (Result != null)
                    {
                        return(Result);
                    }
                }
            }

            if (Left.IsScalar)
            {
                if (Right.IsScalar)
                {
                    ISet LeftSet  = Left.AssociatedSet;
                    ISet RightSet = Right.AssociatedSet;

                    if (!LeftSet.Equals(RightSet))
                    {
                        if (!Expression.Upgrade(ref Left, ref LeftSet, ref Right, ref RightSet, Node))
                        {
                            throw new ScriptRuntimeException("Incompatible operands.", Node);
                        }

                        LE = Left as IGroupElement;
                        RE = Right as IGroupElement;
                        if (LE != null && RE != null)
                        {
                            Temp = RE.Negate();
                            if (Temp != null)
                            {
                                Result = LE.AddRight(Temp);
                                if (Result != null)
                                {
                                    return(Result);
                                }

                                Result = Temp.AddLeft(LE);
                                if (Result != null)
                                {
                                    return(Result);
                                }
                            }
                        }
                    }

                    throw new ScriptRuntimeException("Operands cannot be subtracted.", Node);
                }
                else
                {
                    LinkedList <IElement> Elements = new LinkedList <IElement>();

                    foreach (IElement RightChild in Right.ChildElements)
                    {
                        Elements.AddLast(EvaluateSubtraction(Left, RightChild, Node));
                    }

                    return(Right.Encapsulate(Elements, Node));
                }
            }
            else
            {
                if (Right.IsScalar)
                {
                    LinkedList <IElement> Elements = new LinkedList <IElement>();

                    foreach (IElement LeftChild in Left.ChildElements)
                    {
                        Elements.AddLast(EvaluateSubtraction(LeftChild, Right, Node));
                    }

                    return(Left.Encapsulate(Elements, Node));
                }
                else
                {
                    ICollection <IElement> LeftChildren  = Left.ChildElements;
                    ICollection <IElement> RightChildren = Right.ChildElements;

                    if (LeftChildren.Count == RightChildren.Count)
                    {
                        LinkedList <IElement>  Elements = new LinkedList <IElement>();
                        IEnumerator <IElement> eLeft    = LeftChildren.GetEnumerator();
                        IEnumerator <IElement> eRight   = RightChildren.GetEnumerator();

                        try
                        {
                            while (eLeft.MoveNext() && eRight.MoveNext())
                            {
                                Elements.AddLast(EvaluateSubtraction(eLeft.Current, eRight.Current, Node));
                            }
                        }
                        finally
                        {
                            eLeft.Dispose();
                            eRight.Dispose();
                        }

                        return(Left.Encapsulate(Elements, Node));
                    }
                    else
                    {
                        LinkedList <IElement> LeftResult = new LinkedList <IElement>();

                        foreach (IElement LeftChild in LeftChildren)
                        {
                            LinkedList <IElement> RightResult = new LinkedList <IElement>();

                            foreach (IElement RightChild in RightChildren)
                            {
                                RightResult.AddLast(EvaluateSubtraction(LeftChild, RightChild, Node));
                            }

                            LeftResult.AddLast(Right.Encapsulate(RightResult, Node));
                        }

                        return(Left.Encapsulate(LeftResult, Node));
                    }
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Subtracts the left group element from the right one: (-Left)+Right.
 /// </summary>
 /// <param name="Left">Left element.</param>
 /// <param name="Right">Right element.</param>
 /// <returns>Result, if understood, null otherwise.</returns>
 public virtual IGroupElement LeftSubtract(IGroupElement Left, IGroupElement Right)
 {
     return(this.Add(Left.Negate(), Right) as IGroupElement);
 }