Esempio n. 1
0
        public void SetI(int idx, LuaInt i)
        {
            var t = _stack[idx];
            var v = _stack.Pop();

            InnerSetTable(t, new LuaValue(i), v, false);
        }
Esempio n. 2
0
 public LuaValue(LuaInt value) : this()
 {
     _numValue  = value;
     _objValue  = null;
     _boolValue = false;
     Type       = ELuaType.Int;
 }
Esempio n. 3
0
 public static LuaInt ShiftRight(LuaInt a, LuaInt n)
 {
     if (n >= 0)
     {
         return(a >> (int)n);
     }
     return(ShiftLeft(a, -n));
 }
Esempio n. 4
0
        public bool ToIntegerX(int idx, out LuaInt ret)
        {
            var val = _stack[idx];
            var ok  = val.IsInt();

            ret = ok ? val.GetIntValue() : 0;
            return(ok);
        }
Esempio n. 5
0
        public static LuaInt IFloorDiv(LuaInt a, LuaInt b)
        {
            if (a > 0 && b > 0 || a < 0 && b < 0 || a % b == 0)
            {
                return(a / b);
            }

            return(a / b - 1);
        }
Esempio n. 6
0
        private static Exp ParseNumberExp(Lexer.Lexer lexer)
        {
            lexer.NextToken(out var line, out _, out var token);

            var numberStyle = NumberStyles.Number;
            var e           = 1.0;

            if (token.StartsWith("0x"))
            {
                token = token.Substring(2);
                if (token.Contains("p"))
                {
                    var eIdx = token.IndexOf("p");
                    var t    = LuaFloat.Parse("1" + token.Substring(eIdx).Replace("p", "e"));
                    var d    = t > 1 ? 0.1 : 10;
                    while (t != 1)
                    {
                        e *= d > 1 ? 0.5 : 2;
                        t *= d;
                    }

                    token = token.Substring(0, eIdx);
                }

                numberStyle = NumberStyles.HexNumber;
            }

            if (LuaInt.TryParse(token, numberStyle, new NumberFormatInfo(), out var result))
            {
                if (e != 1.0)
                {
                    return new FloatExp {
                               Line = line, Val = result * e
                    }
                }
                ;

                return(new IntegerExp {
                    Line = line, Val = result
                });
            }

            if (LuaFloat.TryParse(token, out var fResult))
            {
                return new FloatExp {
                           Line = line, Val = fResult
                }
            }
            ;

            Debug.Panic("not a number: " + token);
            return(null);
        }
    }
}
Esempio n. 7
0
        private bool StringToInteger(string s, out LuaInt ret)
        {
            if (Parser.ParseInteger(s, out ret))
            {
                return(true);
            }

            if (Parser.ParseFloat(s, out var f))
            {
                return(LuaMath.FloatToInteger(f, out ret));
            }

            ret = 0;
            return(false);
        }
Esempio n. 8
0
        public bool ToInteger(out LuaInt ret)
        {
            if (IsInt())
            {
                ret = (LuaInt)_numValue;
                return(true);
            }
            else if (IsFloat())
            {
                return(LuaMath.FloatToInteger(_numValue, out ret));
            }
            else if (IsString())
            {
                return(StringToInteger((string)_objValue, out ret));
            }

            ret = 0;
            return(false);
        }
Esempio n. 9
0
        private static bool CastToInt(Exp exp, out LuaInt ret)
        {
            ret = 0;
            switch (exp)
            {
            case IntegerExp ie:
            {
                ret = ie.Val;
                return(true);
            }

            case FloatExp fe:
            {
                return(LuaMath.FloatToInteger(fe.Val, out ret));
            }

            default:
                return(false);
            }
        }
Esempio n. 10
0
        public LuaValue Get(LuaValue key)
        {
            LuaInt idx = 0;

            if (key.IsInt())
            {
                idx = key.GetIntValue();
            }
            else if (key.IsFloat())
            {
                if (LuaMath.FloatToInteger(key.GetFloatValue(), out var fi))
                {
                    idx = fi;
                }
            }

            if (idx >= 1 && idx <= Len())
            {
                return(_arr[(int)(idx - 1)]);
            }

            return(_map != null && _map.TryGetValue(key.GetValue(), out var ret) ? ret : LuaValue.Nil);
        }
Esempio n. 11
0
        public void Put(LuaValue key, LuaValue val)
        {
            if (key is null || key.IsNil())
            {
                Debug.Panic("table index is nil!");
            }

            if (key.IsFloat() && LuaFloat.IsNaN(key.GetFloatValue()))
            {
                Debug.Panic("table index is Nan!");
            }

            _changed = true;

            LuaInt idx = 0;

            if (key.IsInt())
            {
                idx = key.GetIntValue();
            }
            else if (key.IsFloat())
            {
                if (LuaMath.FloatToInteger(key.GetFloatValue(), out var fi))
                {
                    idx = fi;
                }
            }

            if (idx >= 1)
            {
                var arrLen = Len();
                if (idx <= arrLen)
                {
                    _arr[(int)(idx - 1)] = val;
                    if (idx == arrLen && val.IsNil())
                    {
                        ShrinkArray();
                    }
                    return;
                }

                if (idx == arrLen + 1)
                {
                    if (_map != null && _map.ContainsKey(key.GetIntValue()))
                    {
                        _map.Remove(key.GetIntValue());
                    }

                    if (!val.IsNil())
                    {
                        if (_arr == null)
                        {
                            _arr = new List <LuaValue>();
                        }

                        _arr.Add(val);
                        ExpandArray();
                    }

                    return;
                }
            }

            if (!val.IsNil())
            {
                if (_map == null)
                {
                    _map = new Dictionary <object, LuaValue>();
                }

                _map[key.GetValue()] = val;
            }
            else
            {
                _map.Remove(key.GetValue());
            }
        }
Esempio n. 12
0
 public static bool ParseInteger(string str, out LuaInt ret)
 {
     return(LuaInt.TryParse(str, out ret));
 }
Esempio n. 13
0
 public static LuaInt IMod(LuaInt a, LuaInt b)
 {
     return(a - IFloorDiv(a, b) * b);
 }
Esempio n. 14
0
 public static bool FloatToInteger(LuaFloat f, out LuaInt ret)
 {
     ret = (LuaInt)f;
     return(ret == f);
 }
Esempio n. 15
0
        public ELuaType GetI(int idx, LuaInt i)
        {
            var t = _stack[idx];

            return(InnerGetTable(t, new LuaValue(i), false));
        }