예제 #1
0
        /// <summary>
        /// Performs some default arithmetic like comparisons and returns the result.
        /// Returns null if there is no default.
        /// </summary>
        /// <param name="type">The type of operation to perform.</param>
        /// <param name="other">The other value to use.</param>
        /// <returns>The result of the operation.</returns>
        private ILuaValue DefaultArithmetic(BinaryOperationType type, ILuaValue other)
        {
            switch (type)
            {
            case BinaryOperationType.Concat:
                return(new LuaString(this.ToString() + other.ToString()));

            case BinaryOperationType.Gt:
                return(LuaBoolean.Create(CompareTo(other) > 0));

            case BinaryOperationType.Lt:
                return(LuaBoolean.Create(CompareTo(other) < 0));

            case BinaryOperationType.Gte:
                return(LuaBoolean.Create(CompareTo(other) >= 0));

            case BinaryOperationType.Lte:
                return(LuaBoolean.Create(CompareTo(other) <= 0));

            case BinaryOperationType.Equals:
                return(LuaBoolean.Create(Equals(other)));

            case BinaryOperationType.NotEquals:
                return(LuaBoolean.Create(!Equals(other)));

            case BinaryOperationType.And:
                return(other);

            case BinaryOperationType.Or:
                return(this);

            default:
                return(null);
            }
        }
예제 #2
0
        public override ILuaValue Arithmetic(BinaryOperationType type, LuaNumber self)
        {
            // Cannot use DefaultArithmetic since self and this are swapped.
            switch (type)
            {
            case BinaryOperationType.Add:
                return(new LuaNumber(self.Value + Value));

            case BinaryOperationType.Subtract:
                return(new LuaNumber(self.Value - Value));

            case BinaryOperationType.Multiply:
                return(new LuaNumber(self.Value * Value));

            case BinaryOperationType.Divide:
                return(new LuaNumber(self.Value / Value));

            case BinaryOperationType.Power:
                return(new LuaNumber(Math.Pow(self.Value, Value)));

            case BinaryOperationType.Modulo:
                return(new LuaNumber(self.Value - Math.Floor(self.Value / Value) * Value));

            case BinaryOperationType.Concat:
                return(new LuaString(self.ToString() + ToString()));

            case BinaryOperationType.Gt:
                return(LuaBoolean.Create(self.CompareTo(this) > 0));

            case BinaryOperationType.Lt:
                return(LuaBoolean.Create(self.CompareTo(this) < 0));

            case BinaryOperationType.Gte:
                return(LuaBoolean.Create(self.CompareTo(this) >= 0));

            case BinaryOperationType.Lte:
                return(LuaBoolean.Create(self.CompareTo(this) <= 0));

            case BinaryOperationType.Equals:
                return(LuaBoolean.Create(self.Equals(this)));

            case BinaryOperationType.NotEquals:
                return(LuaBoolean.Create(!self.Equals(this)));

            case BinaryOperationType.And:
                return(!self.IsTrue ? self : this);

            case BinaryOperationType.Or:
                return(self.IsTrue ? self : this);

            default:
                throw new ArgumentException(Resources.BadBinOp);
            }
        }
예제 #3
0
 /// <summary>
 /// Performs a binary arithmetic operation and returns the result.
 /// </summary>
 /// <param name="type">The type of operation to perform.</param>
 /// <param name="self">The first value to use.</param>
 /// <returns>The result of the operation.</returns>
 /// <exception cref="System.InvalidOperationException">
 /// If the operation cannot be performed with the given values.
 /// </exception>
 /// <exception cref="System.InvalidArgumentException">
 /// If the argument is an invalid value.
 /// </exception>
 ILuaValue ILuaValueVisitor.Arithmetic(BinaryOperationType type, LuaBoolean self)
 {
     throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Bool));
 }
예제 #4
0
 /// <summary>
 /// Performs a binary arithmetic operation and returns the result.
 /// </summary>
 /// <param name="type">The type of operation to perform.</param>
 /// <param name="self">The first value to use.</param>
 /// <returns>The result of the operation.</returns>
 /// <exception cref="System.InvalidOperationException">
 /// If the operation cannot be performed with the given values.
 /// </exception>
 /// <exception cref="System.InvalidArgumentException">
 /// If the argument is an invalid value.
 /// </exception>
 public override ILuaValue Arithmetic(BinaryOperationType type, LuaBoolean self)
 {
     return(self.Arithmetic(type, values_[0]));
 }
예제 #5
0
 // this + self
 public ILuaValue ArithmeticFrom(BinaryOperationType type, LuaBoolean self)
 {
     throw new NotImplementedException();
 }
예제 #6
0
 /// <summary>
 /// Performs a binary arithmetic operation and returns the result.
 /// </summary>
 /// <param name="type">The type of operation to perform.</param>
 /// <param name="self">The first value to use.</param>
 /// <returns>The result of the operation.</returns>
 /// <exception cref="System.InvalidOperationException">
 /// If the operation cannot be performed with the given values.
 /// </exception>
 /// <exception cref="System.InvalidArgumentException">
 /// If the argument is an invalid value.
 /// </exception>
 public virtual ILuaValue Arithmetic(BinaryOperationType type, LuaBoolean self)
 {
     throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Bool));
 }
예제 #7
0
 /// <summary>
 /// Gets the boolean negation of the value.
 /// </summary>
 /// <returns>The boolean negation of the value.</returns>
 public ILuaValue Not()
 {
     return(LuaBoolean.Create(!IsTrue));
 }
예제 #8
0
        /// <summary>
        /// Attempts to invoke a meta-method and returns the result.
        /// </summary>
        /// <param name="type">The type of operation.</param>
        /// <param name="other">The other value.</param>
        /// <returns>The result of the meta-method, or null if not found.</returns>
        internal static ILuaValue AttempMetamethod(BinaryOperationType type, ILuaValue self, ILuaValue other)
        {
            if (type == BinaryOperationType.And || type == BinaryOperationType.Or)
            {
                return(null);
            }

            // Search the first object.
            ILuaValue ret   = null;
            var       self2 = self as ILuaTable;

            if (self2 != null && self2.MetaTable != null)
            {
                var t = self2.MetaTable.GetItemRaw(LuaString._metamethods[type]);
                if (t != null)
                {
                    ret = t.Invoke(LuaNil.Nil, false, -1, new LuaMultiValue(self, other));
                }
                else if (type == BinaryOperationType.Lte || type == BinaryOperationType.Gt)
                {
                    t = self2.MetaTable.GetItemRaw(LuaString._metamethods[BinaryOperationType.Lt]);
                    if (t != null)
                    {
                        ret = t.Invoke(LuaNil.Nil, false, -1, new LuaMultiValue(other, self)).Not();
                    }
                }
            }

            // Search the second object.
            var other2 = other as ILuaTable;

            if (ret == null && other2 != null && other2.MetaTable != null)
            {
                var t = other2.MetaTable.GetItemRaw(LuaString._metamethods[type]);
                if (t != null)
                {
                    ret = t.Invoke(LuaNil.Nil, true, -1, new LuaMultiValue(self, other));
                }
                else if (type == BinaryOperationType.Lte || type == BinaryOperationType.Gt)
                {
                    t = other2.MetaTable.GetItemRaw(LuaString._metamethods[BinaryOperationType.Lt]);
                    if (t != null)
                    {
                        ret = t.Invoke(LuaNil.Nil, true, -1, new LuaMultiValue(other, self)).Not();
                    }
                }
            }

            // Fix the arguments for comparisons.
            if (ret != null)
            {
                ret = ret.Single();
                switch (type)
                {
                case BinaryOperationType.Gt:
                case BinaryOperationType.Gte:
                case BinaryOperationType.NotEquals:
                    ret = LuaBoolean.Create(!ret.IsTrue);
                    break;

                case BinaryOperationType.Equals:
                case BinaryOperationType.Lt:
                case BinaryOperationType.Lte:
                    ret = LuaBoolean.Create(ret.IsTrue);
                    break;
                }
            }

            return(ret);
        }