| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for folding expressions. |
| | | 8 | | /// </summary> |
| | | 9 | | public class FoldExpression : Expression, IEquatable<FoldExpression?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a folding expression that folds the <paramref name="location"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="location">Location that is folded.</param> |
| | 66 | 14 | | public FoldExpression(string location) { |
| | 33 | 15 | | Location = location; |
| | 33 | 16 | | } |
| | | 17 | | /// <summary> |
| | | 18 | | /// Concrete location that is folded to its abstract location. |
| | | 19 | | /// </summary> |
| | 93 | 20 | | public string Location { get; } |
| | | 21 | | /// <inheritdoc/> |
| | 17 | 22 | | protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) { |
| | 17 | 23 | | var labs = new Location(Location, true); |
| | 17 | 24 | | if (!heap.TryGetBinding(labs, out var b1)) |
| | 1 | 25 | | throw new IllFormedException(this, $"Cannot fold ~{Location} without heap binding!"); |
| | 16 | 26 | | var lj = new Location(Location, false); |
| | 16 | 27 | | if (!heap.TryGetBinding(lj, out var b2)) |
| | 1 | 28 | | throw new IllFormedException(this, $"Cannot fold {Location} because it was never unfolded!"); |
| | 15 | 29 | | if (!Heap.SubBlock(gamma, b2, b1)) |
| | 1 | 30 | | throw new IllFormedException(this, $"Cannot fold {Location} because it doesnt match abstract constraints |
| | 54 | 31 | | return new World(IntegerType.Void, heap.Filter(i => i != lj)); |
| | 14 | 32 | | } |
| | | 33 | | /// <inheritdoc/> |
| | 27 | 34 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 27 | 35 | | yield return "["; |
| | 27 | 36 | | if (args.SpaceBeforeFoldingKeywords) yield return " "; |
| | 27 | 37 | | yield return "fold"; |
| | 27 | 38 | | yield return " "; |
| | 27 | 39 | | yield return Location; |
| | 27 | 40 | | if (args.SpaceAfterFoldingKeywords) yield return " "; |
| | 27 | 41 | | yield return "]"; |
| | 27 | 42 | | } |
| | | 43 | | /// <inheritdoc/> |
| | 13 | 44 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 13 | 45 | | yield return "/* "; |
| | 234 | 46 | | foreach (var tk in Tokens((NanoCSourceFormat)args)) yield return tk; |
| | 13 | 47 | | yield return " */"; |
| | 13 | 48 | | } |
| | | 49 | | /// <inheritdoc/> |
| | 12 | 50 | | public override IEnumerable<string> RequiredFunctions() { yield break; } |
| | | 51 | | #region Equality checks |
| | | 52 | | /// <inheritdoc/> |
| | 15 | 53 | | public override bool Equals(object? obj) => Equals(obj as FoldExpression); |
| | | 54 | | /// <inheritdoc/> |
| | 15 | 55 | | public bool Equals(FoldExpression? other) => !(other is null) && Location == other.Location; |
| | | 56 | | /// <inheritdoc/> |
| | 0 | 57 | | public override int GetHashCode() => HashCode.Combine(Location); |
| | | 58 | | /// <inheritdoc/> |
| | 0 | 59 | | public static bool operator ==(FoldExpression? left, FoldExpression? right) => EqualityComparer<FoldExpression?> |
| | | 60 | | /// <inheritdoc/> |
| | 0 | 61 | | public static bool operator !=(FoldExpression? left, FoldExpression? right) => !(left == right); |
| | | 62 | | #endregion |
| | | 63 | | } |
| | | 64 | | } |