using System;
namespace ARMeilleure.CodeGen.Linking
{
///
/// Represents a symbol.
///
readonly struct Symbol
{
private readonly ulong _value;
///
/// Gets the of the .
///
public SymbolType Type { get; }
///
/// Gets the value of the .
///
/// is
public ulong Value
{
get
{
if (Type == SymbolType.None)
{
ThrowSymbolNone();
}
return _value;
}
}
///
/// Initializes a new instance of the structure with the specified and value.
///
/// Type of symbol
/// Value of symbol
public Symbol(SymbolType type, ulong value)
{
(Type, _value) = (type, value);
}
///
/// Determines if the specified instances are equal.
///
/// First instance
/// Second instance
/// if equal; otherwise
public static bool operator ==(Symbol a, Symbol b)
{
return a.Equals(b);
}
///
/// Determines if the specified instances are not equal.
///
/// First instance
/// Second instance
/// if not equal; otherwise
public static bool operator !=(Symbol a, Symbol b)
{
return !(a == b);
}
///
/// Determines if the specified is equal to this instance.
///
/// Other instance
/// if equal; otherwise
public bool Equals(Symbol other)
{
return other.Type == Type && other._value == _value;
}
///
public override bool Equals(object obj)
{
return obj is Symbol sym && Equals(sym);
}
///
public override int GetHashCode()
{
return HashCode.Combine(Type, _value);
}
///
public override string ToString()
{
return $"{Type}:{_value}";
}
private static void ThrowSymbolNone()
{
throw new InvalidOperationException("Symbol refers to nothing.");
}
}
}