Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class dvar
- {
- public object val { get; set; }
- public static implicit operator dvar(int s) => new dvar() { val = s };
- public static implicit operator dvar(string s) => new dvar() { val = s };
- public static implicit operator dvar(bool s) => new dvar() { val = s };
- public static implicit operator dvar(float s) => new dvar() { val = s };
- public static implicit operator dvar(double s) => new dvar() { val = s };
- public static implicit operator dvar(decimal s) => new dvar() { val = s };
- public static implicit operator dvar(object[] s) => new dvar() { val = s.ToList()};
- public static implicit operator dvar(List<object> s) => new dvar() { val = s };
- public dvar()
- {
- }
- public dvar(object s)
- {
- val = s;
- }
- public static string listPrinter(List<object> list)
- {
- string op = "[";
- foreach (var item in list)
- {
- string z = item switch
- {
- dvar s => s.val switch
- {
- string c => $"\"{c}\"",
- _ => (string)s
- },
- int s => s.ToString(),
- float s => s.ToString(),
- double s => s.ToString(),
- decimal s => s.ToString(),
- bool s => s.ToString(),
- string s => $"\"{s}\"",
- IFormattable s => s.ToString(),
- _ => "object"
- };
- op += z + ",";
- }
- op = op.TrimEnd(","[0]);
- op += "]";
- return op;
- }
- public static implicit operator string(dvar d) => d.val switch {
- bool s => s.ToString(),
- string s => s,
- int s => s.ToString(),
- decimal s => s.ToString(),
- double s => s.ToString(),
- float s => s.ToString(),
- List<object> s => listPrinter(s),
- _ => "object"
- };
- public static double parseString(string str)
- {
- double d;
- if (double.TryParse(str, out d))
- {
- return d;
- }
- return double.NaN;
- }
- public static implicit operator int(dvar d) => d.val switch {
- bool s => s ? 1 : 0,
- string s => double.IsNaN(parseString(s)) || parseString(s) > int.MaxValue || parseString(s) < int.MinValue ? 0 : (int)parseString(s),
- int s => s,
- decimal s => s > int.MaxValue || s < int.MinValue ? 0 : (int)s,
- double s => s > int.MaxValue || s < int.MinValue || double.IsNaN(s) || double.IsInfinity(s) ? 0 : (int)s,
- float s => s > int.MaxValue || s < int.MinValue || float.IsNaN(s) || float.IsInfinity(s) ? 0 : (int)s,
- List<object> s => s.Count,
- _ => 0
- };
- public static implicit operator bool(dvar d) => d.val switch
- {
- bool s => s,
- string s => s == string.Empty || s == null ? false : true,
- int s => s == 0 ? false : true,
- decimal s => s == 0 ? false : true,
- double s => s == 0 || double.IsNaN(s) ? false : true,
- float s => s == 0 || float.IsNaN(s) ? false : true,
- object s => s == null ? false : true,
- _ => true
- };
- public static implicit operator float(dvar d) => d.val switch
- {
- bool s => s ? 1 : 0,
- string s => (float)parseString(s),
- int s => s,
- decimal s => (float)s,
- double s => s > float.MaxValue || s < float.MinValue ? (s > float.MaxValue ? float.PositiveInfinity : float.NegativeInfinity) : (float)s,
- float s => s,
- _ => float.NaN
- };
- public static implicit operator double(dvar d) => d.val switch
- {
- bool s => s ? 1 : 0,
- string s => parseString(s),
- int s => s,
- decimal s => (double)s,
- double s => s,
- float s => s,
- _ => double.NaN
- };
- public static dvar operator +(dvar d1, dvar d2) {
- if (d1.val != null && d1.val.GetType() == typeof(List<object>)) {
- var list = (List<object>)d1.val;
- object[] arr = new object[list.Count];
- list.CopyTo(arr);
- List<object> newList = arr.ToList();
- newList.Add(d2);
- return newList;
- } else if (d1.val == null || d2.val == null)
- {
- return (double)((string)d1.val == null ? ((string)d2 == null ? (dvar)0 : d2) : d1);
- }
- else if (d1.val.GetType() == typeof(string) || d2.val.GetType() == typeof(string))
- {
- return ((string)d1) + ((string)d2);
- }
- else if (d1.val.GetType() == typeof(int) && d2.val.GetType() == typeof(int))
- {
- return (int)d1.val + (int)d2.val;
- }
- else
- {
- double a = (double)d1;
- double b = (double)d2;
- return a + b;
- }
- }
- public static dvar operator -(dvar d1, dvar d2)
- {
- if (d1.val == null || d2.val == null)
- {
- return (string)d1.val == null ? ((string)d2.val == null ? (dvar)0 : (0 - d2)) : d1;
- }
- else if (d1.val.GetType() == typeof(int) && d2.val.GetType() == typeof(int))
- {
- return (int)d1.val - (int)d2.val;
- }
- else
- {
- return (double)d1 - (double)d2;
- }
- }
- public static bool operator ==(dvar d1, dvar d2)
- {
- if (d1.val == null && d2.val == null)
- {
- return true;
- }
- else if (d1.val.GetType() == typeof(int) && d2.val.GetType() == typeof(int))
- {
- return (int)d1.val == (int)d2.val;
- }
- else
- {
- return (double)d1 == (double)d2;
- }
- }
- public static bool operator !=(dvar d1, dvar d2)
- {
- return !(d1 == d2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement