| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Linq; |
| | | 7 | | |
| | | 8 | | namespace NanoCLang.Entities { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides a class for heap entities. |
| | | 11 | | /// Also trivially implements the heap element interface. |
| | | 12 | | /// </summary> |
| | | 13 | | [DebuggerDisplay("{" + nameof(ToString) + "(),nq}")] |
| | | 14 | | public class Heap : Base, ISubstitutable<Heap>, IEquatable<Heap?>, IHeapElement, ICloneable { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates a new heap instance from the list of location bindings. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="locations">Locations that are bound in the heap.</param> |
| | | 19 | | /// <exception cref="IllFormedException">Locations are not uniquely defined.</exception> |
| | 465 | 20 | | public Heap(IEnumerable<LocationBinding> locations) : this(locations.ToList()) { } |
| | | 21 | | /// <inheritdoc cref="Heap(IEnumerable{LocationBinding})"/> |
| | 474 | 22 | | public Heap(IList<LocationBinding> locations) { |
| | 502 | 23 | | var duplicates = locations.GroupBy(i => i.Location) |
| | 265 | 24 | | .Where(g => g.Count() > 1) |
| | 237 | 25 | | .Select(y => y.Key).ToList(); |
| | 237 | 26 | | if (duplicates.Count > 0) { |
| | 0 | 27 | | var firstDuplicate = duplicates.First(); |
| | 0 | 28 | | throw new IllFormedException(firstDuplicate, $"Cannot bind to location {firstDuplicate} twice in a heap! |
| | | 29 | | } |
| | 767 | 30 | | locationBindings = locations.ToDictionary(i => i.Location, i => i.Blocks); |
| | 237 | 31 | | } |
| | | 32 | | /// <summary> |
| | | 33 | | /// Checks if the heap has a binding for the <paramref name="location"/>. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="location">Location to check for.</param> |
| | | 36 | | /// <returns><see langword="true"/> iff there exists a matching location binding.</returns> |
| | 6 | 37 | | public bool Contains(Location location) => locationBindings.ContainsKey(location); |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets an iterator for the domain of the heap. I.e. the set of locations bound in the heap. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <returns>Iterator over the locations.</returns> |
| | 63 | 42 | | public IEnumerable<Location> Domain() => locationBindings.Keys; |
| | | 43 | | /// <summary> |
| | | 44 | | /// Performs a strong update to the location and offset appointed by the reference <paramref name="ptrRef"/>. |
| | | 45 | | /// Writes the <paramref name="value"/> to the location inside the new heap. |
| | | 46 | | /// Additionally performs a typecheck to verify that the written type matches current base type (refinement does |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="ptrRef">Reference pointing to location and block offset for strong update.</param> |
| | | 49 | | /// <param name="value">Value expression to be written to the heap.</param> |
| | | 50 | | /// <param name="tau">Base type of the type that was bound at the written location. Can be typechecked against i |
| | | 51 | | /// <returns>New heap that contains the strong update.</returns> |
| | 17 | 52 | | public Heap StrongUpdate(ReferenceType ptrRef, PureExpression value, out BasicType tau) { |
| | 17 | 53 | | var heap = (Heap)Clone(); |
| | 17 | 54 | | var bindings = heap.TryGetBinding(ptrRef.Location, out var tmp) ? tmp |
| | 17 | 55 | | : throw new IllFormedException(ptrRef, $"Cannot access pointer to location {ptrRef.Location} if the loca |
| | | 56 | | |
| | 17 | 57 | | BlockType? bound = null; |
| | 17 | 58 | | switch (ptrRef.Offsets) { |
| | | 59 | | case SingletonIndex i: |
| | 10 | 60 | | VerbConsole.WriteLine(VerbosityLevel.Default, "T-Write-Field"); |
| | 89 | 61 | | foreach (var block in bindings) if (i <= block.Index) { bound = block; break; } |
| | 10 | 62 | | if (bound is null) |
| | 0 | 63 | | throw new IllFormedException(this, $"Heap location does not have a writable field at index {i}!"); |
| | 20 | 64 | | bound.Type = new RefinedType(bound.Type.BaseType, v => PureExpression.EqualExpression(v, value)); |
| | 10 | 65 | | break; |
| | | 66 | | case SequenceIndex i: |
| | 7 | 67 | | VerbConsole.WriteLine(VerbosityLevel.Default, "T-Write-Array"); |
| | 56 | 68 | | foreach (var block in bindings) if (i <= block.Index) { bound = block; break; } |
| | 7 | 69 | | if (bound is null) |
| | 0 | 70 | | throw new IllFormedException(this, $"Heap location does not have a writable field at index {i}!"); |
| | 7 | 71 | | break; |
| | 0 | 72 | | default: throw new InvalidProgramException($"Expected valid index but got {ptrRef.Offsets}"); |
| | | 73 | | } |
| | 17 | 74 | | tau = bound.Type.BaseType; |
| | 17 | 75 | | return heap; |
| | 17 | 76 | | } |
| | | 77 | | /// <summary> |
| | | 78 | | /// Clones the heap and returns the clone. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <returns>Clone of the current heap.</returns> |
| | 17 | 81 | | public object Clone() => Replace(new Substitutor()); |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets a heap that is a super heap of both input heaps. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="gamma">Environment to compare heaps within.</param> |
| | | 86 | | /// <param name="heap1">First input heap.</param> |
| | | 87 | | /// <param name="heap2">Second input heap.</param> |
| | | 88 | | /// <returns>Superheap of both heaps.</returns> |
| | | 89 | | /// <exception cref="IllFormedException">Heaps are incomatible.</exception> |
| | | 90 | | public static Heap SuperHeap(LocalEnvironment gamma, Heap heap1, Heap heap2) |
| | 6 | 91 | | => heap1.SubHeap(gamma, heap2) |
| | 6 | 92 | | ? heap2 |
| | 6 | 93 | | : heap2.SubHeap(gamma, heap1) |
| | 6 | 94 | | ? heap1 |
| | 6 | 95 | | : throw new IllFormedException(heap1, $"Incompatible branch heaps in if expression: {heap2} |
| | | 96 | | /// <summary> |
| | | 97 | | /// Checks if the heap has a binding for the <paramref name="location"/> and returns the binding at that locatio |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="location">Location to check for.</param> |
| | | 100 | | /// <param name="binding">Binding at that location.</param> |
| | | 101 | | /// <returns><see langword="true"/> iff there exists a matching location binding.</returns> |
| | | 102 | | public bool TryGetBinding(Location location, [NotNullWhen(true)] out BlockType[]? binding) |
| | 115 | 103 | | => locationBindings.TryGetValue(location, out binding); |
| | | 104 | | /// <summary> |
| | | 105 | | /// Creates a new instance of an empty heap. |
| | | 106 | | /// </summary> |
| | 48 | 107 | | public Heap() : this(new LocationBinding[] { }) { } |
| | | 108 | | /// <summary> |
| | | 109 | | /// Creates a new instance of a heap with a single location <paramref name="binding"/>. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="binding">Single binding on the heap (if not abstract the heap can not be well formed).</param> |
| | 18 | 112 | | public Heap(LocationBinding binding) : this(new LocationBinding[] { binding }) { } |
| | | 113 | | /// <summary> |
| | | 114 | | /// Create a heap from a location -> blocktype[] enumeration. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="heap">Input to copy from.</param> |
| | 324 | 117 | | public Heap(IEnumerable<KeyValuePair<Location, BlockType[]>> heap) { |
| | 162 | 118 | | locationBindings = new Dictionary<Location, BlockType[]>(heap); |
| | 162 | 119 | | } |
| | | 120 | | /// <summary> |
| | | 121 | | /// Create a heap from an existing heap. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="heap">Input to copy from.</param> |
| | 0 | 124 | | public Heap(Heap heap) : this(heap.locationBindings) { } |
| | | 125 | | /// <summary> |
| | | 126 | | /// Locations that are bound in the heap. |
| | | 127 | | /// Same as <see cref="GetBindings(GlobalEnvironment, out IEnumerable{KeyValuePair{string, string}})"/> except t |
| | | 128 | | /// </summary> |
| | 51 | 129 | | public IEnumerable<LocationBinding> BoundLocations => locationBindings.Select(i => new LocationBinding(i.Key, i. |
| | | 130 | | /// <inheritdoc cref="IHeapElement.GetBindings(GlobalEnvironment, out IEnumerable{KeyValuePair{string, string}}) |
| | 6 | 131 | | public IEnumerable<LocationBinding> GetBindings(GlobalEnvironment phi, out IEnumerable<KeyValuePair<string, stri |
| | 6 | 132 | | links = Enumerable.Empty<KeyValuePair<string, string>>(); |
| | 6 | 133 | | return BoundLocations; |
| | 6 | 134 | | } |
| | | 135 | | /// <summary> |
| | | 136 | | /// Checks if the heap is empty and returns <see langword="true"/> iff it is. |
| | | 137 | | /// </summary> |
| | 109 | 138 | | public bool Empty => locationBindings.Count == 0; |
| | | 139 | | private readonly IDictionary<Location, BlockType[]> locationBindings; |
| | | 140 | | /// <summary> |
| | | 141 | | /// Checks if the heap contains purely abstract bindings. |
| | | 142 | | /// </summary> |
| | 17 | 143 | | public bool IsAbstract() => locationBindings.All(i => i.Key.Abstract); |
| | | 144 | | /// <summary> |
| | | 145 | | /// Create a heap from the current heap with locations that the <paramref name="predicate"/> evaluates to <see l |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name="predicate">Predicate to apply to the locations.</param> |
| | 244 | 148 | | public Heap Filter(Func<Location, bool> predicate) => new Heap(locationBindings.Where(i => predicate(i.Key))); |
| | | 149 | | /// <summary> |
| | | 150 | | /// Calculated the heap after subtracting the <paramref name="rhs"/> from the <paramref name="lhs"/>. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="lhs">Heap to subtract from.</param> |
| | | 153 | | /// <param name="rhs">Heap that is subtracted from the <paramref name="lhs"/>.</param> |
| | | 154 | | /// <returns><see langword="true"/> iff the rest was calculated.</returns> |
| | | 155 | | /// <exception cref="ArgumentNullException"><paramref name="lhs"/> or <paramref name="rhs"/> is <see langword="n |
| | | 156 | | /// <exception cref="InvalidOperationException"><paramref name="rhs"/> is not a sub-heap of <paramref name="lhs" |
| | 0 | 157 | | public static Heap operator /(Heap lhs, Heap rhs) { |
| | | 158 | | // bad core: keys in subheap that are not in the "super"-heap |
| | 0 | 159 | | var badCore = rhs.locationBindings.Keys.Except(lhs.locationBindings.Keys).ToList(); |
| | 0 | 160 | | if (badCore.Count > 0) { |
| | 0 | 161 | | var l = badCore.First(); |
| | 0 | 162 | | throw new InvalidOperationException($"Heap is not a subheap: {l} is not bound in left heap!"); |
| | | 163 | | } |
| | | 164 | | // union core: keys in both heaps, needed to check for equality of bindings |
| | 0 | 165 | | var intersectionCore = rhs.locationBindings.Keys.ToHashSet(); |
| | 0 | 166 | | var intersection = lhs.Filter(i => intersectionCore.Contains(i)); |
| | 0 | 167 | | if (intersection != rhs) |
| | 0 | 168 | | throw new InvalidOperationException($"Heap is not a subheap: Key Intersection is not consistend!"); |
| | | 169 | | // good core: keys only in the super-heap |
| | 0 | 170 | | var goodCore = lhs.locationBindings.Keys.Except(rhs.locationBindings.Keys).ToHashSet(); |
| | 0 | 171 | | return lhs.Filter(i => goodCore.Contains(i)); |
| | 0 | 172 | | } |
| | | 173 | | /// <summary> |
| | | 174 | | /// Try to get the rest of the heap after subtracting the <paramref name="heap"/> from this heap. |
| | | 175 | | /// If the input is a sub-heap of this heap the difference is calculated as <paramref name="rest"/> <see langwor |
| | | 176 | | /// otherwise the function returns <see langword="false"/>. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <param name="heap">Heap that is subtracted from this heap.</param> |
| | | 179 | | /// <param name="rest">Difference between the heaps, or <see langword="null"/></param> |
| | | 180 | | /// <exception cref="ArgumentNullException"><paramref name="heap"/> is <see langword="null"/>.</exception> |
| | | 181 | | /// <returns><see langword="true"/> iff the rest was calculated.</returns> |
| | 0 | 182 | | public bool TryGetRest(Heap heap, [NotNullWhen(true)] out Heap? rest) { |
| | 0 | 183 | | try { |
| | 0 | 184 | | rest = this / heap; |
| | 0 | 185 | | return true; |
| | 0 | 186 | | } catch (InvalidOperationException) { rest = null; } |
| | 0 | 187 | | return false; |
| | 0 | 188 | | } |
| | | 189 | | /// <summary> |
| | | 190 | | /// Checks if the current heap is a subheap of the <paramref name="other"/> heap in the given environment. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <param name="gamma">Environment to check the subheap in.</param> |
| | | 193 | | /// <param name="other">Other heap to check against.</param> |
| | | 194 | | /// <returns><see langword="true"/> iff the current heap is a subheap of the <paramref name="other"/> heap.</ret |
| | 34 | 195 | | public bool SubHeap(LocalEnvironment gamma, Heap other) { |
| | 34 | 196 | | VerbConsole.WriteLine(VerbosityLevel.Default, "<:-Heap"); |
| | 34 | 197 | | if (locationBindings.Count != other.locationBindings.Count) return false; |
| | 34 | 198 | | var keys = locationBindings.Keys.ToHashSet(); |
| | 34 | 199 | | if (!keys.SetEquals(other.locationBindings.Keys.ToHashSet())) return false; |
| | 202 | 200 | | foreach (var key in keys) |
| | 50 | 201 | | if (!SubBlock(gamma, locationBindings[key], other.locationBindings[key])) return false; |
| | 34 | 202 | | return true; |
| | 34 | 203 | | } |
| | | 204 | | /// <summary> |
| | | 205 | | /// Checks if the block <paramref name="left"/> is a subtype of the <paramref name="right"/> block in the given |
| | | 206 | | /// </summary> |
| | | 207 | | /// <param name="gamma">Environment to check the subtype in.</param> |
| | | 208 | | /// <param name="left">Current block to check with.</param> |
| | | 209 | | /// <param name="right">Other block to check against.</param> |
| | | 210 | | /// <returns><see langword="true"/> iff the current block is a subtype of the <paramref name="right"/> type.</re |
| | 65 | 211 | | public static bool SubBlock(LocalEnvironment gamma, BlockType[] left, BlockType[] right) { |
| | 65 | 212 | | var gammaOut = new LocalEnvironment(gamma); |
| | 65 | 213 | | if (left.Length != right.Length) return false; |
| | 350 | 214 | | for (int i = 0; i < left.Length; i++) { |
| | 74 | 215 | | var b1 = left[i]; |
| | 74 | 216 | | var b2 = right[i]; |
| | 74 | 217 | | if (b1.Index != b2.Index) return false; |
| | 75 | 218 | | if (!b1.Type.SubType(gammaOut, b2.Type)) return false; |
| | 91 | 219 | | if (b1.Index is SingletonIndex idx) { |
| | 18 | 220 | | VerbConsole.WriteLine(VerbosityLevel.Default, "<:-Field"); |
| | 18 | 221 | | gammaOut[OffsetVar(idx.Offset)] = b1.Type; |
| | 73 | 222 | | } else VerbConsole.WriteLine(VerbosityLevel.Default, "<:-Array"); |
| | 73 | 223 | | } |
| | 64 | 224 | | VerbConsole.WriteLine(VerbosityLevel.Default, "<:-Block-Empty"); |
| | 64 | 225 | | return true; |
| | 65 | 226 | | } |
| | | 227 | | /// <summary> |
| | | 228 | | /// Checks if the heap is well-formed and raises an exception if it is ill-formed. |
| | | 229 | | /// </summary> |
| | | 230 | | /// <param name="gamma">Environemt to check the heap with.</param> |
| | | 231 | | /// <exception cref="IllFormedException">Heap is ill-formed.</exception> |
| | 108 | 232 | | public void WellFormed(LocalEnvironment gamma) { |
| | 141 | 233 | | if (Empty) VerbConsole.WriteLine(VerbosityLevel.Default, "WF-Empty"); |
| | 765 | 234 | | foreach (var locationBinding in locationBindings) { |
| | 147 | 235 | | var l = locationBinding.Key; |
| | 147 | 236 | | var b = locationBinding.Value; |
| | 252 | 237 | | if (l.Abstract) { |
| | 105 | 238 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Abstract: {l}"); |
| | | 239 | | // Check ~l not already on the heap (currenlty enforced by design) |
| | 105 | 240 | | WellFormedAbstract(gamma, b); |
| | 147 | 241 | | } else { |
| | 42 | 242 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Concrete: {l}"); |
| | | 243 | | // Check l_j not already on the heap (currenlty enforced by design) |
| | 42 | 244 | | if (!ContainsAbstract(l.Name)) |
| | 0 | 245 | | throw new IllFormedException(l, $"Heap must contain binding to abstract location {l.Name}!"); |
| | 42 | 246 | | WellFormedConcrete(gamma, b); |
| | 42 | 247 | | } |
| | 147 | 248 | | } |
| | 108 | 249 | | } |
| | | 250 | | /// <summary> |
| | | 251 | | /// Checks if the heap contains a binding to the abstract location named <paramref name="name"/>. |
| | | 252 | | /// </summary> |
| | | 253 | | /// <param name="name">Name of the location to check.</param> |
| | | 254 | | /// <returns><see langword="true"/> iff the abstract location exists.</returns> |
| | 93 | 255 | | public bool ContainsAbstract(string name) => locationBindings.ContainsKey(new Location(name, true)); |
| | | 256 | | /// <summary> |
| | | 257 | | /// Checks if the heap contains a binding to the concrete location named <paramref name="name"/>. |
| | | 258 | | /// </summary> |
| | | 259 | | /// <param name="name">Name of the location to check.</param> |
| | | 260 | | /// <returns><see langword="true"/> iff the concrete location exists.</returns> |
| | 0 | 261 | | public bool ContainsConcrete(string name) => locationBindings.ContainsKey(new Location(name, false)); |
| | | 262 | | /// <summary> |
| | | 263 | | /// Check if the abstract block <paramref name="b"/> is well formed in the environment <paramref name="gammaIn"/ |
| | | 264 | | /// </summary> |
| | | 265 | | /// <param name="gammaIn">Environment to check the block in.</param> |
| | | 266 | | /// <param name="b">Block to check.</param> |
| | 105 | 267 | | private void WellFormedAbstract(LocalEnvironment gammaIn, BlockType[] b) { |
| | 105 | 268 | | var gamma = new LocalEnvironment(gammaIn); |
| | 615 | 269 | | for (int i = 0; i < b.Length; i++) { |
| | 135 | 270 | | var binding = b[i]; |
| | 135 | 271 | | binding.Type.WellFormed(gamma, this); |
| | 135 | 272 | | CheckIndexOverlap(b, i); |
| | 135 | 273 | | switch (binding.Index) { |
| | | 274 | | case SingletonIndex idx: |
| | 62 | 275 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Field: {binding.Type}"); |
| | 62 | 276 | | gamma[OffsetVar(idx.Offset)] = binding.Type; |
| | 62 | 277 | | break; |
| | | 278 | | default: |
| | 73 | 279 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Array: {binding.Type}"); |
| | 73 | 280 | | break; |
| | | 281 | | } |
| | 135 | 282 | | } |
| | 105 | 283 | | } |
| | | 284 | | /// <summary> |
| | | 285 | | /// Gets a string that represents the variable containing the block offset in the environment. |
| | | 286 | | /// </summary> |
| | | 287 | | /// <param name="offset">Offset to represent.</param> |
| | | 288 | | /// <returns>Variable name</returns> |
| | 177 | 289 | | public static string OffsetVar(int offset) => $"@{offset}"; |
| | | 290 | | /// <summary> |
| | | 291 | | /// Check if the index of the current binding overlaps with previous bindings in the location. |
| | | 292 | | /// </summary> |
| | | 293 | | /// <param name="b">Array of bindings in the location.</param> |
| | | 294 | | /// <param name="i">Index of current binding in the location</param> |
| | 186 | 295 | | private static void CheckIndexOverlap(BlockType[] b, int i) { |
| | 186 | 296 | | var binding = b[i]; |
| | 186 | 297 | | var bindingSize = binding.Type.Size; |
| | 489 | 298 | | for (int j = 0; j < i; j++) { |
| | 39 | 299 | | var domB = b[j]; |
| | 39 | 300 | | try { |
| | 39 | 301 | | if (binding.Index.CollidesWith(bindingSize, domB.Index, domB.Type.Size)) |
| | 0 | 302 | | throw new IllFormedException(binding.Index, $"Index collides with binding at {domB.Index}!"); |
| | 39 | 303 | | } catch (NotImplementedException e) { |
| | 0 | 304 | | throw new IllFormedException(binding.Index, $"Cannot safely reason about the index overlapping. Deta |
| | | 305 | | } |
| | 39 | 306 | | } |
| | 186 | 307 | | } |
| | | 308 | | /// <summary> |
| | | 309 | | /// Check if the concrete block <paramref name="b"/> is well formed in the environment <paramref name="gamma"/>. |
| | | 310 | | /// </summary> |
| | | 311 | | /// <param name="gamma">Environment to check the block in.</param> |
| | | 312 | | /// <param name="b">Block to check.</param> |
| | 42 | 313 | | private void WellFormedConcrete(LocalEnvironment gamma, BlockType[] b) { |
| | 237 | 314 | | for (int i = 0; i < b.Length; i++) { |
| | 51 | 315 | | var binding = b[i]; |
| | 51 | 316 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-ConcBlock: {binding.Type}"); |
| | 51 | 317 | | binding.Type.WellFormed(gamma, this); |
| | 51 | 318 | | CheckIndexOverlap(b, i); |
| | 51 | 319 | | } |
| | 42 | 320 | | } |
| | | 321 | | /// <inheritdoc/> |
| | | 322 | | public Heap Replace(Substitutor sub) |
| | 47 | 323 | | => new Heap(locationBindings.Select(i |
| | 145 | 324 | | => new KeyValuePair<Location, BlockType[]>( |
| | 145 | 325 | | i.Key.Replace(sub), |
| | 267 | 326 | | i.Value.Select(i => i.Replace(sub)).ToArray()))); |
| | | 327 | | /// <inheritdoc/> |
| | 8 | 328 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 8 | 329 | | var locations = BoundLocations.GetEnumerator(); |
| | 8 | 330 | | if (!locations.MoveNext()) { yield return "emp"; yield break; } |
| | 642 | 331 | | foreach (var tk in locations.Current.Tokens(args)) yield return tk; |
| | 16 | 332 | | while (locations.MoveNext()) |
| | 708 | 333 | | foreach (var tk in StringFormatterToken.Indented(args.IndentHeapListOperator, |
| | 358 | 334 | | () => PrintLocationBinding(args, locations.Current))) yield return tk; |
| | 8 | 335 | | } |
| | | 336 | | |
| | 8 | 337 | | private static IEnumerable<StringFormatterToken> PrintLocationBinding(NanoCSourceFormat args, LocationBinding lo |
| | 40 | 338 | | for (int i = 0; i < args.NewlinesBeforeBindingListOperator; i++) yield return new NewLineToken(); |
| | 8 | 339 | | yield return args.HeapBindingListSeparator; |
| | 954 | 340 | | foreach (var tk in location.Tokens(args)) yield return tk; |
| | 8 | 341 | | } |
| | | 342 | | /// <summary> |
| | | 343 | | /// Concatenates the <paramref name="left"/> and <paramref name="right"/> heap and returns |
| | | 344 | | /// </summary> |
| | | 345 | | /// <param name="left"></param> |
| | | 346 | | /// <param name="right"></param> |
| | | 347 | | /// <returns>Returns the concatenated heap.</returns> |
| | | 348 | | /// <exception cref="ArgumentException">Heap bindings overlap.</exception> |
| | 21 | 349 | | public static Heap operator *(Heap left, Heap right) { |
| | 21 | 350 | | var intersection = left.locationBindings.Keys.Intersect(right.locationBindings.Keys).ToList(); |
| | 21 | 351 | | if (intersection.Count > 0) |
| | 0 | 352 | | throw new ArgumentException($"Heaps overlap in locations {string.Join(", ", intersection)}!"); |
| | 21 | 353 | | return new Heap(left.locationBindings.Concat(right.locationBindings)); |
| | 21 | 354 | | } |
| | | 355 | | /// <summary> |
| | | 356 | | /// Adds the location <paramref name="binding"/> to the <paramref name="heap"/>. |
| | | 357 | | /// </summary> |
| | | 358 | | /// <param name="heap">Heap to add to.</param> |
| | | 359 | | /// <param name="binding">Location binding to add.</param> |
| | | 360 | | /// <returns>Returns the concatenated heap.</returns> |
| | | 361 | | /// <exception cref="ArgumentException">Heap bindings overlap.</exception> |
| | 8 | 362 | | public static Heap operator *(Heap heap, LocationBinding binding) { |
| | 8 | 363 | | if (heap.locationBindings.Keys.Contains(binding.Location)) |
| | 0 | 364 | | throw new ArgumentException($"Cannot append binding to {string.Join(", ", binding.Location)}!"); |
| | 8 | 365 | | return new Heap(heap.locationBindings.Append(new KeyValuePair<Location, BlockType[]>(binding.Location, bindi |
| | 8 | 366 | | } |
| | | 367 | | #region Equality checks |
| | | 368 | | /// <inheritdoc/> |
| | 0 | 369 | | public override bool Equals(object? obj) => Equals(obj as Heap); |
| | | 370 | | /// <inheritdoc/> |
| | 69 | 371 | | public bool Equals(Heap? other) => !(other is null) && HeapComparer.Default.Equals(locationBindings, other.locat |
| | | 372 | | /// <inheritdoc/> |
| | 0 | 373 | | public override int GetHashCode() => HashCode.Combine(locationBindings); |
| | | 374 | | /// <inheritdoc/> |
| | 21 | 375 | | public static bool operator ==(Heap? left, Heap? right) => EqualityComparer<Heap?>.Default.Equals(left, right); |
| | | 376 | | /// <inheritdoc/> |
| | 21 | 377 | | public static bool operator !=(Heap? left, Heap? right) => !(left == right); |
| | | 378 | | #endregion |
| | | 379 | | } |
| | | 380 | | } |