LuaArguments tonumber(LuaArguments args) { LuaObject obj = args[0]; if (args.Length == 1) { double d = 0; if (obj.IsString) { if (double.TryParse(obj.AsString(), out d)) { return(Return(d)); } else { return(Return()); } } else if (obj.IsNumber) { return(Return(obj.AsNumber())); } else { return(Return()); } } else { //TODO: Implement tonumber for bases different from 10 throw new NotImplementedException(); } }
static LuaObject EvalUnaryExpression(UnaryExpression Expression, LuaContext Context) { LuaObject obj = EvalExpression(Expression.Expression, Context)[0]; switch (Expression.Operation) { case UnaryOp.Invert: return(-(obj.AsNumber())); case UnaryOp.Length: { if (obj.Is(LuaType.table)) { return(obj.AsTable().Count); } else { return(obj.AsString().Length); } } case UnaryOp.Negate: return(!(obj.AsBool())); default: throw new NotImplementedException(); } }
internal static LuaObject toNumber(LuaObject obj) { if (obj.IsNumber) { return(LuaObject.FromNumber(obj.AsNumber())); } else if (obj.IsString) { double d; if (double.TryParse(obj.AsString(), out d)) { return(LuaObject.FromNumber(d)); } else { return(LuaObject.Nil); } } else { return(LuaObject.Nil); } }
internal static LuaObject le_event(LuaObject op1, LuaObject op2) { if (op1.IsNumber && op2.IsNumber) { return(op1.AsNumber() <= op2.AsNumber()); } else if (op1.IsString && op2.IsString) { int n = StringComparer.CurrentCulture.Compare(op1.AsString(), op2.AsString()); return(n <= 0); } else { var handler = getBinhandler(op1, op2, "__le"); if (!handler.IsNil) { return(!(!(handler.Call(op1, op2)[0].AsBool()))); } else { return(LuaObject.Nil); } } }
internal static LuaObject le_event(LuaObject op1, LuaObject op2) { if (op1.IsNumber && op2.IsNumber) { return(op1.AsNumber() <= op2.AsNumber()); } else if (op1.IsString && op2.IsString) { int n = StringComparer.CurrentCulture.Compare(op1.AsString(), op2.AsString()); return(n <= 0); } else { var handler = getBinhandler(op1, op2, "__le"); if (!handler.IsNil) { return(!(!(handler.Call(op1, op2)[0].AsBool()))); } else { throw new ArgumentException("attempt to compare " + op1.type.ToString() + " with " + op2.type.ToString()); } } }