| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for worlds. |
| | | 8 | | /// </summary> |
| | | 9 | | public class RawWorld : Base, IEquatable<RawWorld?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new world instance that gives an expression a return <paramref name="type"/> and a modified <param |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="type">Type of the world.</param> |
| | | 14 | | /// <param name="heap">Modified heap of the world.</param> |
| | | 15 | | public RawWorld(Type type, IHeapElement[] heap) { |
| | | 16 | | Type = type; |
| | | 17 | | Heap = heap; |
| | | 18 | | } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Type of the world. |
| | | 21 | | /// </summary> |
| | | 22 | | public Type Type { get; } |
| | | 23 | | /// <summary> |
| | | 24 | | /// Modified heap of the world. |
| | | 25 | | /// </summary> |
| | | 26 | | public IHeapElement[] Heap { get; } |
| | | 27 | | /// <inheritdoc/> |
| | | 28 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | | 29 | | foreach (var tk in Type.Tokens(args)) yield return tk; |
| | | 30 | | foreach (var tk in WorldOperator(args)) yield return tk; |
| | | 31 | | foreach (var tk in Heap.Tokens(args)) yield return tk; |
| | | 32 | | } |
| | | 33 | | /// <summary> |
| | | 34 | | /// Print the world operator |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="args">Formatter arguments</param> |
| | | 37 | | /// <returns>Token enumeration</returns> |
| | | 38 | | internal static IEnumerable<StringFormatterToken> WorldOperator(NanoCSourceFormat args) { |
| | | 39 | | for (int i = 0; i < args.NewlinesBeforeWorldOperator; i++) yield return new NewLineToken(); |
| | | 40 | | if (args.NewlinesBeforeWorldOperator <= 0 && args.SpaceBeforeWorldOperator) yield return " "; |
| | | 41 | | yield return "/"; |
| | | 42 | | for (int i = 0; i < args.NewlinesAfterWorldOperator; i++) yield return new NewLineToken(); |
| | | 43 | | if (args.NewlinesAfterWorldOperator <= 0 && args.SpaceAfterWorldOperator) yield return " "; |
| | | 44 | | } |
| | | 45 | | /// <summary> |
| | | 46 | | /// Creates the world represented by the raw world by expanding structures with the environment <paramref name=" |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="phi">Global environment to expand structures with.</param> |
| | | 49 | | /// <returns>Built world.</returns> |
| | | 50 | | public World Build(GlobalEnvironment phi) => new World(Type, Heap.GenerateHeap(phi)); |
| | | 51 | | #region Equality checks |
| | | 52 | | /// <inheritdoc/> |
| | | 53 | | public override bool Equals(object? obj) => Equals(obj as RawWorld); |
| | | 54 | | /// <inheritdoc/> |
| | | 55 | | public bool Equals(RawWorld? other) => !(other is null) && EqualityComparer<Type>.Default.Equals(Type, other.Typ |
| | | 56 | | /// <inheritdoc/> |
| | | 57 | | public override int GetHashCode() => HashCode.Combine(Type, Heap); |
| | | 58 | | /// <inheritdoc/> |
| | | 59 | | public static bool operator ==(RawWorld? left, RawWorld? right) => EqualityComparer<RawWorld?>.Default.Equals(le |
| | | 60 | | /// <inheritdoc/> |
| | | 61 | | public static bool operator !=(RawWorld? left, RawWorld? right) => !(left == right); |
| | | 62 | | #endregion |
| | | 63 | | } |
| | | 64 | | /// <summary> |
| | | 65 | | /// Provides a class for worlds. |
| | | 66 | | /// </summary> |
| | | 67 | | public class World : Base, IEquatable<World?> { |
| | | 68 | | /// <summary> |
| | | 69 | | /// Creates a new world instance that gives an expression a return <paramref name="type"/> and a modified <param |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="type">Type of the world.</param> |
| | | 72 | | /// <param name="heap">Modified heap of the world.</param> |
| | 340 | 73 | | public World(Type type, Heap heap) { |
| | 170 | 74 | | Type = type; |
| | 170 | 75 | | Heap = heap; |
| | 170 | 76 | | } |
| | | 77 | | /// <summary> |
| | | 78 | | /// Implicitly converts a built world into a raw world (trivial). |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="world">World to convert.</param> |
| | 21 | 81 | | public static implicit operator RawWorld(World world) => new RawWorld(world.Type, new IHeapElement[] { world.Hea |
| | | 82 | | /// <summary> |
| | | 83 | | /// Type of the world. |
| | | 84 | | /// </summary> |
| | 192 | 85 | | public Type Type { get; } |
| | | 86 | | /// <summary> |
| | | 87 | | /// Modified heap of the world. |
| | | 88 | | /// </summary> |
| | 308 | 89 | | public Heap Heap { get; } |
| | | 90 | | /// <summary> |
| | | 91 | | /// Checks if the world is well-formed and raises an exception if it is ill-formed. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <exception cref="IllFormedException">World is ill-formed.</exception> |
| | 30 | 94 | | public void WellFormed(LocalEnvironment gamma) { |
| | 30 | 95 | | VerbConsole.WriteLine(VerbosityLevel.Default, "WF-World"); |
| | 30 | 96 | | Heap.WellFormed(gamma); |
| | 30 | 97 | | Type.WellFormed(gamma, Heap); |
| | 30 | 98 | | } |
| | | 99 | | /// <inheritdoc/> |
| | 1 | 100 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 42 | 101 | | foreach (var tk in Type.Tokens(args)) yield return tk; |
| | 12 | 102 | | foreach (var tk in RawWorld.WorldOperator(args)) yield return tk; |
| | 84 | 103 | | foreach (var tk in Heap.Tokens(args)) yield return tk; |
| | 1 | 104 | | } |
| | | 105 | | /// <summary> |
| | | 106 | | /// Checks if the current world is a subworld of the <paramref name="other"/> world in the given environment. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="gamma">Environment to check the subworld in.</param> |
| | | 109 | | /// <param name="other">Other world to check against.</param> |
| | | 110 | | /// <returns><see langword="true"/> iff the current world is a subworld of the <paramref name="other"/> world.</ |
| | 13 | 111 | | public bool SubWorld(LocalEnvironment gamma, World other) { |
| | 13 | 112 | | VerbConsole.WriteLine(VerbosityLevel.Default, "<:-World"); |
| | 13 | 113 | | return Type.SubType(gamma, other.Type) && Heap.SubHeap(gamma, other.Heap); |
| | 13 | 114 | | } |
| | | 115 | | #region Equality checks |
| | | 116 | | /// <inheritdoc/> |
| | 2 | 117 | | public override bool Equals(object? obj) => Equals(obj as World); |
| | | 118 | | /// <inheritdoc/> |
| | 3 | 119 | | public bool Equals(World? other) => !(other is null) && EqualityComparer<Type>.Default.Equals(Type, other.Type) |
| | | 120 | | /// <inheritdoc/> |
| | 0 | 121 | | public override int GetHashCode() => HashCode.Combine(Type, Heap); |
| | | 122 | | /// <inheritdoc/> |
| | 0 | 123 | | public static bool operator ==(World? left, World? right) => EqualityComparer<World?>.Default.Equals(left, right |
| | | 124 | | /// <inheritdoc/> |
| | 0 | 125 | | public static bool operator !=(World? left, World? right) => !(left == right); |
| | | 126 | | #endregion |
| | | 127 | | } |
| | | 128 | | } |