| | | 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 read expressions. |
| | | 8 | | /// </summary> |
| | | 9 | | public class StructReadExpression : ReadExpression, IEquatable<StructReadExpression?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a structure read expression that reads the value of the <paramref name="structure" |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="structure">Structure to write to.</param> |
| | | 14 | | /// <param name="member">Member in the structure to access.</param> |
| | 20 | 15 | | public StructReadExpression(PureExpression structure, string member) { |
| | 10 | 16 | | Structure = structure; |
| | 10 | 17 | | Member = member; |
| | 10 | 18 | | } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Structure that is read from. |
| | | 21 | | /// </summary> |
| | 18 | 22 | | public PureExpression Structure { get; } |
| | | 23 | | /// <summary> |
| | | 24 | | /// Member in the structure to access. |
| | | 25 | | /// </summary> |
| | 10 | 26 | | public string Member { get; } |
| | | 27 | | /// <inheritdoc/> |
| | 6 | 28 | | protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) { |
| | 6 | 29 | | VerbConsole.WriteLine(VerbosityLevel.Default, "T-StrRead"); |
| | 6 | 30 | | return (BaseRead = new PointerReadExpression(StructWriteExpression.GetProjectionPointer(phi, gamma, Structur |
| | 6 | 31 | | .InferWorld(phi, gamma, heap); |
| | 5 | 32 | | } |
| | | 33 | | /// <inheritdoc/> |
| | 4 | 34 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 24 | 35 | | foreach (var tk in Structure.Tokens(args)) yield return tk; |
| | 4 | 36 | | yield return "->"; |
| | 4 | 37 | | yield return Member; |
| | 4 | 38 | | } |
| | | 39 | | /// <inheritdoc/> |
| | 8 | 40 | | public override IEnumerable<string> RequiredFunctions() { yield break; } |
| | | 41 | | #region Equality checks |
| | | 42 | | /// <inheritdoc/> |
| | 4 | 43 | | public override bool Equals(object? obj) => Equals(obj as StructReadExpression); |
| | | 44 | | /// <inheritdoc/> |
| | 4 | 45 | | public bool Equals(StructReadExpression? other) => !(other is null) && EqualityComparer<PureExpression>.Default. |
| | | 46 | | /// <inheritdoc/> |
| | 0 | 47 | | public override int GetHashCode() => HashCode.Combine(Structure); |
| | | 48 | | /// <inheritdoc/> |
| | 0 | 49 | | public static bool operator ==(StructReadExpression? left, StructReadExpression? right) => EqualityComparer<Stru |
| | | 50 | | /// <inheritdoc/> |
| | 0 | 51 | | public static bool operator !=(StructReadExpression? left, StructReadExpression? right) => !(left == right); |
| | | 52 | | #endregion |
| | | 53 | | } |
| | | 54 | | } |