| | | 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 write expressions. |
| | | 8 | | /// </summary> |
| | | 9 | | public class ArrayWriteExpression : WriteExpression, IEquatable<ArrayWriteExpression?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a array write 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> |
| | | 15 | | /// <param name="value">Value to write to the array.</param> |
| | 20 | 16 | | public ArrayWriteExpression(PureExpression array, PureExpression offset, PureExpression value) { |
| | 10 | 17 | | Array = array; |
| | 10 | 18 | | Offset = offset; |
| | 10 | 19 | | Value = value; |
| | 10 | 20 | | } |
| | | 21 | | /// <summary> |
| | | 22 | | /// Array that is written to. |
| | | 23 | | /// </summary> |
| | 18 | 24 | | public PureExpression Array { get; } |
| | | 25 | | /// <summary> |
| | | 26 | | /// Offset in the array to access. |
| | | 27 | | /// </summary> |
| | 10 | 28 | | public PureExpression Offset { get; } |
| | | 29 | | /// <summary> |
| | | 30 | | /// Value that is written to the array. |
| | | 31 | | /// </summary> |
| | 9 | 32 | | public PureExpression Value { get; } |
| | | 33 | | /// <inheritdoc/> |
| | 6 | 34 | | protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) { |
| | 6 | 35 | | VerbConsole.WriteLine(VerbosityLevel.Default, "T-ArrWrite"); |
| | 6 | 36 | | return (BaseWrite = new PointerWriteExpression(GetArrayPointer(gamma, Array, Offset), Value)) |
| | 6 | 37 | | .InferWorld(phi, gamma, heap); |
| | 5 | 38 | | } |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets a properly offset pointer to the array, can also be used to offset structure pointers manually, so no c |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="gamma">Local environment that contains type of the array value.</param> |
| | | 43 | | /// <param name="array">Pointer to the memory block.</param> |
| | | 44 | | /// <param name="offset">Offset from that <paramref name="array"/> pointer.</param> |
| | | 45 | | /// <returns>Offset Pointer</returns> |
| | 10 | 46 | | internal static PureExpression GetArrayPointer(LocalEnvironment gamma, PureExpression array, PureExpression offs |
| | 10 | 47 | | var type = array.InferType(gamma); |
| | 10 | 48 | | if (!(type.BaseType is ReferenceType)) |
| | 2 | 49 | | throw new IllFormedException(array, $"Cannot perform pointer access on non-reference expression!"); |
| | 8 | 50 | | return array + offset; |
| | 8 | 51 | | } |
| | | 52 | | /// <inheritdoc/> |
| | 4 | 53 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 24 | 54 | | foreach (var tk in Array.Tokens(args)) yield return tk; |
| | 4 | 55 | | yield return "["; |
| | 36 | 56 | | foreach (var tk in Offset.Tokens(args)) yield return tk; |
| | 4 | 57 | | yield return "]"; |
| | 8 | 58 | | if (args.SpaceBeforeBindingAssignment) yield return " "; |
| | 4 | 59 | | yield return "="; |
| | 8 | 60 | | if (args.SpaceAfterBindingAssignment) yield return " "; |
| | 36 | 61 | | foreach (var tk in Value.Tokens(args)) yield return tk; |
| | 4 | 62 | | } |
| | | 63 | | /// <inheritdoc/> |
| | 4 | 64 | | public override IEnumerable<string> RequiredFunctions() { yield break; } |
| | | 65 | | #region Equality checks |
| | | 66 | | /// <inheritdoc/> |
| | 4 | 67 | | public override bool Equals(object? obj) => Equals(obj as ArrayWriteExpression); |
| | | 68 | | /// <inheritdoc/> |
| | 4 | 69 | | public bool Equals(ArrayWriteExpression? other) => !(other is null) && EqualityComparer<PureExpression>.Default. |
| | | 70 | | /// <inheritdoc/> |
| | 0 | 71 | | public override int GetHashCode() => HashCode.Combine(Array); |
| | | 72 | | /// <inheritdoc/> |
| | 0 | 73 | | public static bool operator ==(ArrayWriteExpression? left, ArrayWriteExpression? right) => EqualityComparer<Arra |
| | | 74 | | /// <inheritdoc/> |
| | 0 | 75 | | public static bool operator !=(ArrayWriteExpression? left, ArrayWriteExpression? right) => !(left == right); |
| | | 76 | | #endregion |
| | | 77 | | } |
| | | 78 | | } |