| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | namespace NanoCLang.Entities { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides a class for location bindings. |
| | | 9 | | /// </summary> |
| | | 10 | | public class LocationBinding : Base, IEquatable<LocationBinding?>, IHeapElement { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a new instance of a location binding that binds the <paramref name="blocks"/> to the <paramref name= |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="location">Location that is bound to.</param> |
| | | 15 | | /// <param name="blocks">Blocks that are bound in the location.</param> |
| | 442 | 16 | | public LocationBinding(Location location, BlockType[] blocks) { |
| | 221 | 17 | | Location = location; |
| | 221 | 18 | | Blocks = blocks; |
| | 221 | 19 | | } |
| | | 20 | | /// <summary> |
| | | 21 | | /// Location that is bound to. |
| | | 22 | | /// </summary> |
| | 594 | 23 | | public Location Location { get; } |
| | | 24 | | /// <summary> |
| | | 25 | | /// Blocks that are bound in the location. |
| | | 26 | | /// </summary> |
| | 321 | 27 | | public BlockType[] Blocks { get; } |
| | | 28 | | /// <inheritdoc/> |
| | 48 | 29 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 390 | 30 | | foreach (var tk in Location.Tokens(args)) yield return tk; |
| | 96 | 31 | | if (args.SpaceBetweenLocationAndBindOperator) yield return " "; |
| | 48 | 32 | | yield return "~>"; |
| | 96 | 33 | | if (args.SpaceBetweenBindOperatorAndBlock) yield return " "; |
| | 2637 | 34 | | foreach (var tk in StringFormatterToken.Separated(Blocks, args, args.BlockSeparator)) yield return tk; |
| | 48 | 35 | | } |
| | | 36 | | /// <inheritdoc cref="IHeapElement.GetBindings(GlobalEnvironment, out IEnumerable{KeyValuePair{string, string}}) |
| | 91 | 37 | | public IEnumerable<LocationBinding> GetBindings(GlobalEnvironment phi, out IEnumerable<KeyValuePair<string, stri |
| | 91 | 38 | | links = Enumerable.Empty<KeyValuePair<string, string>>(); |
| | 91 | 39 | | return new LocationBinding[] { this }; |
| | 91 | 40 | | } |
| | | 41 | | #region Equality checks |
| | | 42 | | /// <inheritdoc/> |
| | 0 | 43 | | public override bool Equals(object? obj) => Equals(obj as LocationBinding); |
| | | 44 | | /// <inheritdoc/> |
| | 0 | 45 | | public bool Equals(LocationBinding? other) => !(other is null) && EqualityComparer<Location>.Default.Equals(Loca |
| | | 46 | | /// <inheritdoc/> |
| | 0 | 47 | | public override int GetHashCode() { |
| | 0 | 48 | | var hash = new HashCode(); |
| | 0 | 49 | | hash.Add(Location); |
| | 0 | 50 | | foreach (var block in Blocks) hash.Add(block); |
| | 0 | 51 | | return hash.ToHashCode(); |
| | 0 | 52 | | } |
| | | 53 | | /// <inheritdoc/> |
| | 0 | 54 | | public static bool operator ==(LocationBinding? left, LocationBinding? right) => EqualityComparer<LocationBindin |
| | | 55 | | /// <inheritdoc/> |
| | 0 | 56 | | public static bool operator !=(LocationBinding? left, LocationBinding? right) => !(left == right); |
| | | 57 | | #endregion |
| | | 58 | | } |
| | | 59 | | } |