Symbol.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /// <inheritdoc/>
  55. public static bool operator !=(Symbol a, Symbol b)
  56. {
  57. return !(a == b);
  58. }
  59. /// <summary>
  60. /// Determines if the specified <see cref="Symbol"/> is equal to this <see cref="Symbol"/> instance.
  61. /// </summary>
  62. /// <param name="other">Other <see cref="Symbol"/> instance</param>
  63. /// <returns><see langword="true"/> if equal; otherwise <see langword="false"/></returns>
  64. public bool Equals(Symbol other)
  65. {
  66. return other.Type == Type && other._value == _value;
  67. }
  68. /// <inheritdoc/>
  69. public override bool Equals(object obj)
  70. {
  71. return obj is Symbol sym && Equals(sym);
  72. }
  73. /// <inheritdoc/>
  74. public override int GetHashCode()
  75. {
  76. return HashCode.Combine(Type, _value);
  77. }
  78. /// <inheritdoc/>
  79. public override string ToString()
  80. {
  81. return $"{Type}:{_value}";
  82. }
  83. private static void ThrowSymbolNone()
  84. {
  85. throw new InvalidOperationException("Symbol refers to nothing.");
  86. }
  87. }
  88. }