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