| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for array read expressions. |
| | | 8 | | /// </summary> |
| | | 9 | | public class ArrayReadExpression : ReadExpression, IEquatable<ArrayReadExpression?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a array read expression that reads the value of the <paramref name="array"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="array">Array to write to.</param> |
| | | 14 | | /// <param name="offset">Offset in the array to access.</param> |
| | 12 | 15 | | public ArrayReadExpression(PureExpression array, PureExpression offset) { |
| | 6 | 16 | | Array = array; |
| | 6 | 17 | | Offset = offset; |
| | 6 | 18 | | } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Array that is read from. |
| | | 21 | | /// </summary> |
| | 10 | 22 | | public PureExpression Array { get; } |
| | | 23 | | /// <summary> |
| | | 24 | | /// Offset in the array to access. |
| | | 25 | | /// </summary> |
| | 6 | 26 | | public PureExpression Offset { get; } |
| | | 27 | | /// <inheritdoc/> |
| | 4 | 28 | | protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) { |
| | 4 | 29 | | VerbConsole.WriteLine(VerbosityLevel.Default, "T-ArrRead"); |
| | 4 | 30 | | return (BaseRead = new PointerReadExpression(ArrayWriteExpression.GetArrayPointer(gamma, Array, Offset))) |
| | 4 | 31 | | .InferWorld(phi, gamma, heap); |
| | 3 | 32 | | } |
| | | 33 | | /// <inheritdoc/> |
| | 2 | 34 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 12 | 35 | | foreach (var tk in Array.Tokens(args)) yield return tk; |
| | 2 | 36 | | yield return "["; |
| | 24 | 37 | | foreach (var tk in Offset.Tokens(args)) yield return tk; |
| | 2 | 38 | | yield return "]"; |
| | 2 | 39 | | } |
| | | 40 | | /// <inheritdoc/> |
| | 4 | 41 | | public override IEnumerable<string> RequiredFunctions() { yield break; } |
| | | 42 | | #region Equality checks |
| | | 43 | | /// <inheritdoc/> |
| | 2 | 44 | | public override bool Equals(object? obj) => Equals(obj as ArrayReadExpression); |
| | | 45 | | /// <inheritdoc/> |
| | 2 | 46 | | public bool Equals(ArrayReadExpression? other) => !(other is null) && EqualityComparer<PureExpression>.Default.E |
| | | 47 | | /// <inheritdoc/> |
| | 0 | 48 | | public override int GetHashCode() => HashCode.Combine(Array); |
| | | 49 | | /// <inheritdoc/> |
| | 0 | 50 | | public static bool operator ==(ArrayReadExpression? left, ArrayReadExpression? right) => EqualityComparer<ArrayR |
| | | 51 | | /// <inheritdoc/> |
| | 0 | 52 | | public static bool operator !=(ArrayReadExpression? left, ArrayReadExpression? right) => !(left == right); |
| | | 53 | | #endregion |
| | | 54 | | } |
| | | 55 | | } |