| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides an interface that makes heap elements available to the heap constructor. |
| | | 8 | | /// If new objects inherit from this interface the function <see cref="HeapElementComparer.Equals(IHeapElement[], IH |
| | | 9 | | /// </summary> |
| | | 10 | | public interface IHeapElement : ITokenStream<StringFormatterToken, NanoCSourceFormat> { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Generates the available location bindings for the heap constructor from the current environment. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="phi">Global environment that supplies the structure definitions.</param> |
| | | 15 | | /// <param name="links">Structure location links.</param> |
| | | 16 | | /// <returns>Enumeration of generated heap bindings.</returns> |
| | | 17 | | IEnumerable<LocationBinding> GetBindings(GlobalEnvironment phi, out IEnumerable<KeyValuePair<string, string>> li |
| | | 18 | | } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Provides an extension class for heap element enumerations. |
| | | 21 | | /// </summary> |
| | | 22 | | public static class HeapElementExtensions { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Generates a new heap from the list of heap generators. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="elems">Heap generators.</param> |
| | | 27 | | /// <param name="phi">Environment to generate with.</param> |
| | | 28 | | /// <param name="links">Structure location links.</param> |
| | | 29 | | /// <returns>New heap with all bindings.</returns> |
| | | 30 | | /// <exception cref="IllFormedException">Heap is not well formed.</exception> |
| | 46 | 31 | | public static Heap GenerateHeap(this IEnumerable<IHeapElement> elems, GlobalEnvironment phi, out IEnumerable<Key |
| | 46 | 32 | | var linkList = new List<KeyValuePair<string, string>>(); |
| | 46 | 33 | | var heapBindings = new List<LocationBinding>(); |
| | 312 | 34 | | foreach (var elem in elems) { |
| | 58 | 35 | | heapBindings.AddRange(elem.GetBindings(phi, out var newLinks)); |
| | 58 | 36 | | linkList.AddRange(newLinks); |
| | 58 | 37 | | } |
| | 46 | 38 | | links = linkList; |
| | 46 | 39 | | return new Heap(heapBindings); |
| | 46 | 40 | | } |
| | | 41 | | /// <inheritdoc cref="GenerateHeap(IEnumerable{IHeapElement}, GlobalEnvironment, out IEnumerable{KeyValuePair{st |
| | | 42 | | public static Heap GenerateHeap(this IEnumerable<IHeapElement> elems, GlobalEnvironment phi) |
| | 146 | 43 | | => new Heap(elems.SelectMany(i => i.GetBindings(phi, out _))); |
| | | 44 | | /// <inheritdoc cref="ITokenStream{T, A}.Tokens(A)"/> |
| | | 45 | | public static IEnumerable<StringFormatterToken> Tokens(this IHeapElement[] elems, NanoCSourceFormat args) |
| | 45 | 46 | | => elems.Length == 0 |
| | 45 | 47 | | ? Enumerable.Repeat<StringFormatterToken>("emp", 1) |
| | 45 | 48 | | : StringFormatterToken.Separated(elems, args, args.HeapBindingListSeparator); |
| | | 49 | | } |
| | | 50 | | } |