예제 #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);
            }
        }
 static ILuaValue assert(ILuaValue value, ILuaValue obj = null)
 {
     string message = "Assertion failed: '" + (obj != null ? obj.ToString() : "") + "'.";
     if (value.IsTrue)
         return obj;
     else
         throw new AssertException(message);
 }
            static ILuaValue assert(ILuaValue value, ILuaValue obj = null)
            {
                string message = "Assertion failed: '" + (obj != null ? obj.ToString() : "") + "'.";

                if (value.IsTrue)
                {
                    return(obj);
                }
                else
                {
                    throw new AssertException(message);
                }
            }
예제 #4
0
            static ILuaValue assert(ILuaValue value, ILuaValue obj = null)
            {
                string message = $"Assertion failed: '{obj?.ToString() ?? ""}'.";

                if (value.IsTrue)
                {
                    return(obj);
                }
                else
                {
                    throw new AssertException(message);
                }
            }
예제 #5
0
                public string Match(Match match)
                {
                    if (count_ >= max_)
                    {
                        return(match.Value);
                    }

                    count_++;
                    if (string_ != null)
                    {
                        return(Regex.Replace(string_, @"%[0-9%]", m =>
                        {
                            if (m.Value == "%%")
                            {
                                return "%";
                            }
                            int i = int.Parse(m.Groups[0].Value.Substring(1));
                            return i == 0 ? match.Value : (match.Groups.Count > i ? match.Groups[i].Value : "");
                        }));
                    }
                    else if (table_ != null)
                    {
                        string    key   = match.Groups.Count == 0 ? match.Value : match.Groups[1].Value;
                        ILuaValue value = table_.GetItemRaw(new LuaString(key));
                        if (value != null && value.IsTrue)
                        {
                            return(value.ToString());
                        }
                    }
                    else if (method_ != null)
                    {
                        var            groups = match.Groups.Cast <Group>().Skip(1).Select(c => c.Value).ToArray();
                        var            args   = LuaMultiValue.CreateMultiValueFromObj(groups);
                        ILuaMultiValue obj    = method_.Invoke(LuaNil.Nil, false, -1, args);
                        if (obj != null && obj.Count > 0)
                        {
                            ILuaValue value = obj[0];
                            if (value != null && value.IsTrue)
                            {
                                return(value.ToString());
                            }
                        }
                    }

                    return(match.Value);
                }
            static string tostring(ILuaValue value)
            {
                if (value.ValueType == LuaValueType.Table)
                {
                    var meta = ((ILuaTable)value).MetaTable;
                    if (meta != null)
                    {
                        var m = meta.GetItemRaw(_tostring);
                        if (m != null && m.ValueType == LuaValueType.Function)
                        {
                            var result = m.Invoke(value, true, -1, LuaMultiValue.Empty);
                            return(result[0].ToString());
                        }
                    }
                }

                return(value.ToString());
            }
                protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args)
                {
                    StringBuilder str = new StringBuilder();

                    if (args != null)
                    {
                        for (int i = 0; i < args.Count; i++)
                        {
                            ILuaValue temp = args[i];
                            str.Append(temp.ToString());
                            str.Append('\t');
                        }
                        str.Append("\n");
                    }

                    Stream s = Environment.Settings.Stdout;

                    byte[] txt = (Environment.Settings.Encoding ?? Encoding.UTF8).GetBytes(str.ToString());
                    s.Write(txt, 0, txt.Length);

                    return(LuaMultiValue.Empty);
                }
            static string tostring(ILuaValue value)
            {
                if (value.ValueType == LuaValueType.Table)
                {
                    var meta = ((ILuaTable)value).MetaTable;
                    if (meta != null)
                    {
                        var m = meta.GetItemRaw(_tostring);
                        if (m != null && m.ValueType == LuaValueType.Function)
                        {
                            var result = m.Invoke(value, true, -1, LuaMultiValue.Empty);
                            return result[0].ToString();
                        }
                    }
                }

                return value.ToString();
            }