Symbol.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. namespace ARMeilleure.CodeGen.Linking
  3. {
  4. /// <summary>
  5. /// Represents a symbol.
  6. /// </summary>
  7. readonly struct Symbol
  8. {
  9. private readonly ulong _value;
  10. /// <summary>
  11. /// Gets the <see cref="SymbolType"/> of the <see cref="Symbol"/>.
  12. /// </summary>
  13. public SymbolType Type { get; }
  14. /// <summary>
  15. /// Gets the value of the <see cref="Symbol"/>.
  16. /// </summary>
  17. /// <exception cref="InvalidOperationException"><see cref="Type"/> is <see cref="SymbolType.None"/></exception>
  18. public ulong Value
  19. {
  20. get
  21. {
  22. if (Type == SymbolType.None)
  23. {
  24. ThrowSymbolNone();
  25. }
  26. return _value;
  27. }
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="Symbol"/> structure with the specified <see cref="SymbolType"/> and value.
  31. /// </summary>
  32. /// <param name="type">Type of symbol</param>
  33. /// <param name="value">Value of symbol</param>
  34. public Symbol(SymbolType type, ulong value)
  35. {
  36. (Type, _value) = (type, value);
  37. }
  38. /// <summary>
  39. /// Determines if the specified <see cref="Symbol"/> instances are equal.
  40. /// </summary>
  41. /// <param name="a">First instance</param>
  42. /// <param name="b">Second instance</param>
  43. /// <returns><see langword="true"/> if equal; otherwise <see langword="false"/></returns>
  44. public static bool operator ==(Symbol a, Symbol b)
  45. {
  46. return a.Equals(b);
  47. }
  48. /// <summary>
  49. /// Determines if the specified <see cref="Symbol"/> instances are not equal.
  50. /// </summary>
  51. /// <param name="a">First instance</param>
  52. /// <param name="b">Second instance</param>
  53. /// <returns><see langword="true"/> if not equal; otherwise <see langword="false"/></returns>
  54. public static bool operator !=(Symbol a, Symbol b)
  55. {
  56. return !(a == b);
  57. }
  58. /// <summary>
  59. /// Determines if the specified <see cref="Symbol"/> is equal to this <see cref="Symbol"/> instance.
  60. /// </summary>
  61. /// <param name="other">Other <see cref="Symbol"/> instance</param>
  62. /// <returns><see langword="true"/> if equal; otherwise <see langword="false"/></returns>
  63. public bool Equals(Symbol other)
  64. {
  65. return other.Type == Type && other._value == _value;
  66. }
  67. /// <inheritdoc/>
  68. public override bool Equals(object obj)
  69. {
  70. return obj is Symbol sym && Equals(sym);
  71. }
  72. /// <inheritdoc/>
  73. public override int GetHashCode()
  74. {
  75. return HashCode.Combine(Type, _value);
  76. }
  77. /// <inheritdoc/>
  78. public override string ToString()
  79. {
  80. return $"{Type}:{_value}";
  81. }
  82. private static void ThrowSymbolNone()
  83. {
  84. throw new InvalidOperationException("Symbol refers to nothing.");
  85. }
  86. }
  87. }