public LuaValue(object value, ELuaType type) : this() { _objValue = value; _boolValue = false; _numValue = 0; Type = type; }
public LuaValue(bool value) : this() { _boolValue = value; _numValue = 0; _objValue = null; Type = ELuaType.Boolean; }
public LuaValue(LuaFloat value) : this() { _numValue = value; _objValue = null; _boolValue = false; Type = ELuaType.Float; }
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); } } }
public static LuaFloat FMod(LuaFloat a, LuaFloat b) { if (a > 0 && LuaFloat.IsPositiveInfinity(b) || a < 0 && LuaFloat.IsNegativeInfinity(b)) { return(a); } if (a > 0 && LuaFloat.IsNegativeInfinity(b) || a < 0 && LuaFloat.IsPositiveInfinity(b)) { return(b); } return(a - Math.Floor(a / b) * b); }
public bool ToFloat(out LuaFloat ret) { if (IsNumber()) { ret = _numValue; return(true); } else if (IsString()) { return(LuaFloat.TryParse((string)_objValue, out ret)); } ret = 0; return(false); }
public bool ToNumberX(int idx, out LuaFloat ret) { var val = _stack[idx]; if (val.IsFloat()) { ret = val.GetFloatValue(); return(true); } if (val.IsInt()) { ret = val.GetIntValue(); return(true); } ret = 0; return(false); }
private LuaValue(object value) : this() { _numValue = 0; _boolValue = false; _objValue = null; if (value is LuaInt l) { _numValue = l; Type = ELuaType.Int; } else if (value is LuaFloat f) { _numValue = f; Type = ELuaType.Float; } else if (value is string) { _objValue = value; Type = ELuaType.String; } else if (value is LuaTable) { _objValue = value; Type = ELuaType.Table; } else if (value is Closure c) { Type = c.CSFunction != null ? ELuaType.CSFunction : ELuaType.Closure; _objValue = c; } else if (value is LuaState) { _objValue = value; Type = ELuaType.Thread; } else { _objValue = value; Type = ELuaType.LightUserData; } }
private static bool CastToFloat(Exp exp, out LuaFloat ret) { ret = 0; switch (exp) { case IntegerExp ie: { ret = ie.Val; return(true); } case FloatExp fe: { ret = fe.Val; return(true); } default: return(false); } }
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()); } }
public static bool ParseFloat(string str, out LuaFloat ret) { return(LuaFloat.TryParse(str, out ret)); }
public static LuaFloat FFloorDiv(LuaFloat a, LuaFloat b) { return(Math.Floor(a / b)); }
public static bool FloatToInteger(LuaFloat f, out LuaInt ret) { ret = (LuaInt)f; return(ret == f); }