| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for blocks in a <see cref="LocationBinding"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | public class BlockType : Base, IEquatable<BlockType?>, ISubstitutable<BlockType> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a block type that maps the indices to the given <paramref name="type"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="index">Indices of the <paramref name="type"/> in the block.</param> |
| | | 14 | | /// <param name="type">Type of the allocation.</param> |
| | 880 | 15 | | public BlockType(Index index, Type type) { |
| | 440 | 16 | | Index = index; |
| | 440 | 17 | | Type = type; |
| | 440 | 18 | | } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Valid offset(s) within the block. |
| | | 21 | | /// </summary> |
| | 1171 | 22 | | public Index Index { get; } |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets or sets the type of the allocation at the <see cref="Index"/>. |
| | | 25 | | /// </summary> |
| | 1876 | 26 | | public Type Type { get; internal set; } |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets a new block with the refinements set to true (being types replaced with their base types). |
| | | 29 | | /// </summary> |
| | 11 | 30 | | public BlockType BaseType => new BlockType((Index)Index.Clone(), (Type)Type.BaseType.Clone()); |
| | | 31 | | /// <inheritdoc/> |
| | 63 | 32 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 600 | 33 | | foreach (var tk in Index.Tokens(args)) yield return tk; |
| | 126 | 34 | | if (args.SpaceBeforeTypeOperator) yield return " "; |
| | 63 | 35 | | yield return ":"; |
| | 126 | 36 | | if (args.SpaceAfterTypeOperator) yield return " "; |
| | 2067 | 37 | | foreach (var tk in Type.Tokens(args)) yield return tk; |
| | 63 | 38 | | } |
| | | 39 | | /// <summary> |
| | | 40 | | /// Checks if the current block is a subtype of the <paramref name="other"/> block in the given environment. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="gamma">Environment to check the subtype in.</param> |
| | | 43 | | /// <param name="other">Other block to check against.</param> |
| | | 44 | | /// <returns><see langword="true"/> iff the current block is a subtype of the <paramref name="other"/> type.</re |
| | | 45 | | public bool SubBlock(LocalEnvironment gamma, BlockType other) |
| | 0 | 46 | | => Type.SubType(gamma, other.Type); |
| | | 47 | | /// <inheritdoc/> |
| | | 48 | | public BlockType Replace(Substitutor sub) |
| | 264 | 49 | | => new BlockType((Index)Index.Clone(), Type.Replace(sub)); |
| | | 50 | | #region Equality checks |
| | | 51 | | /// <inheritdoc/> |
| | 0 | 52 | | public override bool Equals(object? obj) => Equals(obj as BlockType); |
| | | 53 | | /// <inheritdoc/> |
| | 91 | 54 | | public bool Equals(BlockType? other) => !(other is null) && EqualityComparer<Index>.Default.Equals(Index, other. |
| | | 55 | | /// <inheritdoc/> |
| | 0 | 56 | | public override int GetHashCode() => System.HashCode.Combine(Index, Type); |
| | | 57 | | /// <inheritdoc/> |
| | 0 | 58 | | public static bool operator ==(BlockType? left, BlockType? right) => EqualityComparer<BlockType?>.Default.Equals |
| | | 59 | | /// <inheritdoc/> |
| | 0 | 60 | | public static bool operator !=(BlockType? left, BlockType? right) => !(left == right); |
| | | 61 | | #endregion |
| | | 62 | | } |
| | | 63 | | } |