/// <summary> /// Returns value for Twister equivalent type of T, returns default(T) if instance /// has a differing PrimitiveType /// </summary> public static T GetValueOrDefault <T>(this TwisterPrimitive instance) => TwisterPrimitive.GetValueOrDefault <T>(instance);
private static TwisterPrimitive? Calculate(TwisterPrimitive left, TwisterPrimitive right, Operator o) { switch (left.Type) { case PrimitiveType.Int: { var l = left.GetValueOrDefault<int>(); switch (right.Type) { case PrimitiveType.Int: { var r = right.GetValueOrDefault<int>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; case Operator.BitAnd: return l & r; case Operator.BitOr: return l | r; case Operator.BitExOr: return l ^ r; case Operator.LeftShift: return l << r; case Operator.RightShift: return l >> r; } break; } case PrimitiveType.UInt: { var r = right.GetValueOrDefault<uint>(); switch (o) { case Operator.Plus: return (uint)(l + r); case Operator.Minus: return (uint)(l - r); case Operator.Modulo: return (uint)(l % r); case Operator.Multiplication: return (uint)(l * r); case Operator.ForwardSlash: return (uint)(l / r); case Operator.BitAnd: return (uint)(l & r); case Operator.BitOr: return (uint)(l | (int)r); case Operator.BitExOr: return (uint)(l ^ r); case Operator.LeftShift: return (uint)(l << (int)r); case Operator.RightShift: return (uint)(l >> (int)r); } break; } case PrimitiveType.Float: { var r = right.GetValueOrDefault<double>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; } break; } case PrimitiveType.Char: { var r = right.GetValueOrDefault<char>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; case Operator.BitAnd: return l & r; case Operator.BitOr: return l | r; case Operator.BitExOr: return l ^ r; case Operator.LeftShift: return l << r; case Operator.RightShift: return l >> r; } break; } case PrimitiveType.Bool: { var r = right.GetValueOrDefault<bool>(); switch (o) { case Operator.BitAnd: return (l > 0) && r; case Operator.BitOr: return (l > 0) || r; } } break; } break; } case PrimitiveType.UInt: { var l = left.GetValueOrDefault<uint>(); switch (right.Type) { case PrimitiveType.Int: { var r = right.GetValueOrDefault<int>(); switch (o) { case Operator.Plus: return (int)(l + r); case Operator.Minus: return (int)(l - r); case Operator.Modulo: return (int)(l % r); case Operator.Multiplication: return (int)(l * r); case Operator.ForwardSlash: return (int)(l / r); case Operator.BitAnd: return (int)(l & r); case Operator.BitOr: return (int)l | r; case Operator.BitExOr: return (int)(l ^ r); case Operator.LeftShift: return (int)(l << r); case Operator.RightShift: return (int)(l >> r); } break; } case PrimitiveType.UInt: { var r = right.GetValueOrDefault<uint>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; case Operator.BitAnd: return l & r; case Operator.BitOr: return l | r; case Operator.BitExOr: return l ^ r; case Operator.LeftShift: return l << (int)r; case Operator.RightShift: return l >> (int)r; } break; } case PrimitiveType.Float: { var r = right.GetValueOrDefault<double>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; } break; } case PrimitiveType.Char: { var r = right.GetValueOrDefault<char>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; case Operator.BitAnd: return l & r; case Operator.BitOr: return l | r; case Operator.BitExOr: return l ^ r; case Operator.LeftShift: return l << r; case Operator.RightShift: return l >> r; } break; } case PrimitiveType.Bool: { var r = right.GetValueOrDefault<bool>(); switch (o) { case Operator.BitAnd: return (l > 0u) && r; case Operator.BitOr: return (l > 0u) || r; } } break; } break; } case PrimitiveType.Float: { var l = left.GetValueOrDefault<double>(); switch (right.Type) { case PrimitiveType.Int: { var r = right.GetValueOrDefault<int>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; } break; } case PrimitiveType.UInt: { var r = right.GetValueOrDefault<uint>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; } break; } case PrimitiveType.Float: { var r = right.GetValueOrDefault<double>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; } break; } case PrimitiveType.Char: { var r = right.GetValueOrDefault<char>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; } break; } case PrimitiveType.Bool: { var r = right.GetValueOrDefault<bool>(); switch (o) { case Operator.BitAnd: return (l > 0d) && r; case Operator.BitOr: return (l > 0d) || r; } } break; } break; } case PrimitiveType.Char: { var l = left.GetValueOrDefault<char>(); switch (right.Type) { case PrimitiveType.Int: { var r = right.GetValueOrDefault<int>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; case Operator.BitAnd: return l & r; case Operator.BitOr: return l | r; case Operator.BitExOr: return l ^ r; case Operator.LeftShift: return l << r; case Operator.RightShift: return l >> r; } break; } case PrimitiveType.UInt: { var r = right.GetValueOrDefault<uint>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; case Operator.BitAnd: return l & r; case Operator.BitOr: return l | r; case Operator.BitExOr: return l ^ r; case Operator.LeftShift: return (uint)l << (int)r; case Operator.RightShift: return (uint)l >> (int)r; } break; } case PrimitiveType.Float: { var r = right.GetValueOrDefault<double>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; } break; } case PrimitiveType.Char: { var r = right.GetValueOrDefault<char>(); switch (o) { case Operator.Plus: return l + r; case Operator.Minus: return l - r; case Operator.Modulo: return l % r; case Operator.Multiplication: return l * r; case Operator.ForwardSlash: return l / r; case Operator.BitAnd: return l & r; case Operator.BitOr: return l | r; case Operator.BitExOr: return l ^ r; case Operator.LeftShift: return l << r; case Operator.RightShift: return l >> r; } break; } case PrimitiveType.Bool: { var r = right.GetValueOrDefault<bool>(); switch (o) { case Operator.BitAnd: return (l > 0) && r; case Operator.BitOr: return (l > 0) || r; } } break; } break; } case PrimitiveType.Bool: { var l = left.GetValueOrDefault<bool>(); switch (right.Type) { case PrimitiveType.Int: { var r = right.GetValueOrDefault<int>(); switch (o) { case Operator.BitAnd: return l && (r > 0); case Operator.BitOr: return l || (r > 0); } break; } case PrimitiveType.UInt: { var r = right.GetValueOrDefault<uint>(); switch (o) { case Operator.BitAnd: return l && (r > 0u); case Operator.BitOr: return l || (r > 0u); } break; } case PrimitiveType.Float: { var r = right.GetValueOrDefault<double>(); switch (o) { case Operator.BitAnd: return l && (r > 0d); case Operator.BitOr: return l || (r > 0d); } break; } case PrimitiveType.Char: { var r = right.GetValueOrDefault<char>(); switch (o) { case Operator.BitAnd: return l && (r > 0); case Operator.BitOr: return l || (r > 0); } break; } case PrimitiveType.Bool: { var r = right.GetValueOrDefault<bool>(); switch (o) { case Operator.BitAnd: return l && r; case Operator.BitOr: return l || r; } } break; } } break; } return null; }