| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace NanoCLang.Entities { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a class for block offset constants. |
| | | 7 | | /// </summary> |
| | | 8 | | public class BlockOffset : VariableExpression, IEquatable<BlockOffset?>, ICloneable { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Creates a new block <paramref name="offset"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="offset">Offset of the block offset literal.</param> |
| | 28 | 13 | | public BlockOffset(int offset) : base(Heap.OffsetVar(offset)) { |
| | 14 | 14 | | Offset = offset; |
| | 14 | 15 | | } |
| | | 16 | | /// <summary> |
| | | 17 | | /// The offset in the heap that this entity refers to. |
| | | 18 | | /// </summary> |
| | 73 | 19 | | public int Offset { get; } |
| | | 20 | | /// <inheritdoc/> |
| | 47 | 21 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 47 | 22 | | yield return "@"; |
| | 47 | 23 | | yield return Offset.ToString(); |
| | 47 | 24 | | } |
| | | 25 | | /// <inheritdoc/> |
| | | 26 | | public override PureExpression Replace(Substitutor sub) |
| | 7 | 27 | | => sub.BlockOffsets.TryGetValue(Offset, out var exp) |
| | 7 | 28 | | ? exp |
| | 7 | 29 | | : (BlockOffset)Clone(); |
| | | 30 | | /// <inheritdoc/> |
| | 5 | 31 | | public override object Clone() => new BlockOffset(Offset); |
| | | 32 | | #region Equality checks |
| | | 33 | | /// <inheritdoc/> |
| | 7 | 34 | | public override bool Equals(object? obj) => Equals(obj as BlockOffset); |
| | | 35 | | /// <inheritdoc/> |
| | 7 | 36 | | public bool Equals(BlockOffset? other) => !(other is null) && Offset == other.Offset; |
| | | 37 | | /// <inheritdoc/> |
| | 0 | 38 | | public override int GetHashCode() => HashCode.Combine(Offset); |
| | | 39 | | /// <inheritdoc/> |
| | 0 | 40 | | public static bool operator ==(BlockOffset? left, BlockOffset? right) => EqualityComparer<BlockOffset?>.Default. |
| | | 41 | | /// <inheritdoc/> |
| | 0 | 42 | | public static bool operator !=(BlockOffset? left, BlockOffset? right) => !(left == right); |
| | | 43 | | #endregion |
| | | 44 | | } |
| | | 45 | | } |