Пример #1
0
        /// <summary>
        /// Removes the two objects at the top of the stack and subtracts the top most object on the stack from the other.
        /// Then, pushes the result on to the top of the stack.
        /// </summary>
        public override void PerformOperation()
        {
            base.PerformOperation();

            // If we have fewer than 2 objects on the stack we are el-bonerino-ed
            Debug.Assert(CelesteStack.StackSize >= 2, "Not enough elements on the stack for subtract operator");

            CelesteObject rhs = CelesteStack.Pop();
            CelesteObject lhs = CelesteStack.Pop();

            // The stack will wrap our subtraction result in a CelesteObject, so just push the actual value of the subtraction
            if (lhs.IsNumber() && rhs.IsNumber())
            {
                CelesteStack.Push(lhs.As <float>() - rhs.As <float>());
            }
            else if (lhs.IsString() && rhs.IsString())
            {
                string lhsAsString = lhs.As <string>();
                string rhsAsString = rhs.As <string>();

                if (lhsAsString.Contains(rhsAsString))
                {
                    // Push the lhs string having removed the rhs string
                    CelesteStack.Push(lhsAsString.Remove(lhsAsString.IndexOf(rhsAsString), rhsAsString.Length));
                }
                else
                {
                    // If our lhs does not contain our rhs, we just push the original unaltered lhs string
                    CelesteStack.Push(lhsAsString);
                }
            }
            else if (lhs.IsList() && rhs.IsList())
            {
                // The subtract operator for lists removes all elements in the first list who are equal to an element in the second, either by reference or value
                List <object> lhsList = lhs.AsList <object>();
                List <object> rhsList = rhs.AsList <object>();

                // Remove ALL occurrences in the list of any object in the rhs list - otherwise there is non-deterministic behaviour in which instance to remove
                lhsList.RemoveAll(x => rhsList.Exists(y => y.Equals(x) || y.ValueEquals(x)));
                CelesteStack.Push(lhs);
            }
            else if (lhs.IsTable() && rhs.IsList())
            {
                // Subtraction of tables does not make sense
                // What does make sense, is subtracting elements in a table using a list of keys
                // Subtracting a list from a table will remove any elements in the table with a matching key as an element in our list

                Dictionary <object, object> lhsTable = lhs.AsTable();
                List <object> rhsList = rhs.AsList <object>();

                foreach (object obj in rhsList)
                {
                    if (lhsTable.Contains(new KeyValuePair <object, object>(obj, null), new TableKeyComparer()))
                    {
                        lhsTable.Remove(lhsTable.First(x => x.Key.Equals(obj) || x.Key.ValueEquals(obj)));
                    }
                }

                CelesteStack.Push(lhs);
            }
            else
            {
                Debug.Fail("Invalid parameters to subtract operation.");
            }
        }
Пример #2
0
        /// <summary>
        /// Removes the two objects at the top of the stack and adds them together.
        /// Then, pushes the result on to the top of the stack.
        /// </summary>
        public override void PerformOperation()
        {
            base.PerformOperation();

            // If we have fewer than 2 objects on the stack we are el-bonerino-ed
            Debug.Assert(CelesteStack.StackSize >= 2, "Not enough elements on the stack for add operator");

            CelesteObject rhs = CelesteStack.Pop();
            CelesteObject lhs = CelesteStack.Pop();

            // The stack will wrap our addition result in a CelesteObject, so just push the actual value of the addition
            if (lhs.IsNumber() && rhs.IsNumber())
            {
                CelesteStack.Push(lhs.As <float>() + rhs.As <float>());
            }
            else if (lhs.IsString() && rhs.IsString())
            {
                CelesteStack.Push(lhs.As <string>() + rhs.As <string>());
            }
            else if (lhs.IsList() && rhs.IsList())
            {
                lhs.AsList <object>().AddRange(rhs.AsList <object>());
                CelesteStack.Push(lhs);
            }
            else if (lhs.IsTable() && rhs.IsTable())
            {
                Dictionary <object, object> lhsTable = lhs.AsTable();
                Dictionary <object, object> rhsTable = rhs.AsTable();

                // Tables can be added, but only if they are all indexed by number or string completely
                // No other way can be used because the reference type of 'object' makes value equality difficult
                // These two types of adding are the only two we can realistically support

                // Check whether our lhs table has all number keys
                if (lhsTable.Keys.Count(x => x is float) == lhsTable.Keys.Count)
                {
                    // If it does, check the rhs table too
                    if (rhsTable.Keys.Count(x => x is float) == rhsTable.Keys.Count)
                    {
                        // We now go through and see if any keys overlap in the two tables
                        if (lhsTable.Intersect(rhsTable, new TableKeyComparer()).Count() == 0)
                        {
                            foreach (KeyValuePair <object, object> pair in rhsTable)
                            {
                                lhsTable.Add((float)pair.Key, pair.Value);
                            }
                        }
                        else
                        {
                            Debug.Fail("Invalid parameters to add operation.  Right hand side table has at least one key the same as the left hand side table");
                        }
                    }
                    else
                    {
                        Debug.Fail("Invalid parameters to add operation.  Right hand side table has inconsistent key types (should be numbers)");
                    }
                }
                // Check whether our lhs table has all string keys
                else if (lhsTable.Keys.Count(x => x is string) == lhsTable.Keys.Count)
                {
                    // If it does, check the rhs table too
                    if (rhsTable.Keys.Count(x => x is string) == rhsTable.Keys.Count)
                    {
                        // We now go through and see if any keys overlap in the two tables
                        if (lhsTable.Intersect(rhsTable, new TableKeyComparer()).Count() == 0)
                        {
                            foreach (KeyValuePair <object, object> pair in rhsTable)
                            {
                                lhsTable.Add((string)pair.Key, pair.Value);
                            }
                        }
                        else
                        {
                            Debug.Fail("Invalid parameters to add operation.  Right hand side table has at least one key the same as the left hand side table");
                        }
                    }
                    else
                    {
                        Debug.Fail("Invalid parameters to add operation.  Right hand side table has inconsistent key types (should be strings)");
                    }
                }
                else
                {
                    Debug.Fail("Invalid parameters to add operation.  Left hand side table has inconsistent key types (should be numbers or strings)");
                }

                CelesteStack.Push(lhs);
            }
            else
            {
                Debug.Fail("Invalid parameters to add operation.");
            }
        }