| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for structure bindings. |
| | | 8 | | /// </summary> |
| | | 9 | | public class StructBinding : Program, IEquatable<StructBinding?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a structure binding that binds the <paramref name="definition"/> to the structure |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="name">Name that the structure is bound to.</param> |
| | | 14 | | /// <param name="definition">Definition of the structure.</param> |
| | | 15 | | /// <param name="body">Program that the structure name is bound in.</param> |
| | 20 | 16 | | public StructBinding(string name, StructDefinition definition, Program body) { |
| | 10 | 17 | | Name = name; |
| | 10 | 18 | | Definition = definition; |
| | 10 | 19 | | Body = body; |
| | 10 | 20 | | } |
| | | 21 | | /// <summary> |
| | | 22 | | /// Definition of the structure. |
| | | 23 | | /// </summary> |
| | 23 | 24 | | public StructDefinition Definition { get; } |
| | | 25 | | /// <summary> |
| | | 26 | | /// Program that the structure name is bound in. |
| | | 27 | | /// </summary> |
| | 21 | 28 | | public Program Body { get; } |
| | | 29 | | /// <summary> |
| | | 30 | | /// Name that the structure is bound to. |
| | | 31 | | /// </summary> |
| | 24 | 32 | | public string Name { get; } |
| | | 33 | | /// <inheritdoc/> |
| | 6 | 34 | | public override World WellFormed(GlobalEnvironment phi) { |
| | 6 | 35 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-StructBinding: {Name}"); |
| | 11 | 36 | | return phi.With(this, phi => { |
| | 5 | 37 | | Definition.WellFormed(phi); |
| | 5 | 38 | | return Body.WellFormed(phi); |
| | 10 | 39 | | }); |
| | 4 | 40 | | } |
| | | 41 | | /// <inheritdoc/> |
| | 4 | 42 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 4 | 43 | | yield return "lets"; |
| | 4 | 44 | | yield return " "; |
| | 4 | 45 | | yield return Name; |
| | 8 | 46 | | if (args.SpaceBeforeBindingAssignment) yield return " "; |
| | 4 | 47 | | yield return "="; |
| | 8 | 48 | | if (args.SpaceAfterBindingAssignment) yield return " "; |
| | 876 | 49 | | foreach (var tk in Definition.Tokens(args)) yield return tk; |
| | 4 | 50 | | yield return " "; |
| | 4 | 51 | | yield return "in"; |
| | 4 | 52 | | if (args.NewlinesAfterStructBinding <= 0) yield return " "; |
| | 32 | 53 | | else for (int i = 0; i < args.NewlinesAfterStructBinding; i++) yield return new NewLineToken(); |
| | 11382 | 54 | | foreach (var tk in Body.Tokens(args)) yield return tk; |
| | 4 | 55 | | } |
| | | 56 | | /// <inheritdoc/> |
| | 4 | 57 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 30 | 58 | | foreach (var tk in Program.PrintHeader(args)) yield return tk; |
| | 6348 | 59 | | foreach (var tk in Body.Tokens(args)) yield return tk; |
| | 4 | 60 | | } |
| | | 61 | | #region Equality checks |
| | | 62 | | /// <inheritdoc/> |
| | 4 | 63 | | public override bool Equals(object? obj) => Equals(obj as StructBinding); |
| | | 64 | | /// <inheritdoc/> |
| | 4 | 65 | | public bool Equals(StructBinding? other) => !(other is null) && EqualityComparer<StructDefinition>.Default.Equal |
| | | 66 | | /// <inheritdoc/> |
| | 0 | 67 | | public override int GetHashCode() => HashCode.Combine(Definition, Body, Name); |
| | | 68 | | /// <inheritdoc/> |
| | 0 | 69 | | public static bool operator ==(StructBinding? left, StructBinding? right) => EqualityComparer<StructBinding?>.De |
| | | 70 | | /// <inheritdoc/> |
| | 0 | 71 | | public static bool operator !=(StructBinding? left, StructBinding? right) => !(left == right); |
| | | 72 | | #endregion |
| | | 73 | | } |
| | | 74 | | } |