| | | 1 | | using System.Collections.Generic; |
| | | 2 | | |
| | | 3 | | namespace NanoCLang.Entities { |
| | | 4 | | /// <summary> |
| | | 5 | | /// <see cref="Base"/> provides a superclass for all NanoC entities. |
| | | 6 | | /// All NanoC entities implement a conversion to a token stream to create a generic source code formatter. |
| | | 7 | | /// This can also be used to transpile the syntax. |
| | | 8 | | /// </summary> |
| | | 9 | | public abstract class Base : ITokenStream<StringFormatterToken, NanoCSourceFormat>, ITokenStream<StringFormatterToke |
| | | 10 | | /// <summary> |
| | | 11 | | /// Formats the code using the <see cref="StringFormatter"/> and returns it. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <returns>String representation of the entity.</returns> |
| | 1343 | 14 | | public override string ToString() => new StringFormatter().ToString(Tokens(new NanoCSourceFormat())); |
| | | 15 | | /// <summary> |
| | | 16 | | /// Translates the entity to C code using the <see cref="StringFormatter"/> and returns it. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <returns>String representation of the entity.</returns> |
| | 0 | 19 | | public virtual string ToCString() => new StringFormatter().ToString(Tokens(new CSourceFormat())); |
| | | 20 | | /// <inheritdoc cref="ITokenStream{T, A}.Tokens(A)"/> |
| | | 21 | | public abstract IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args); |
| | | 22 | | /// <summary> |
| | | 23 | | /// Converts the entity into it's C language equivalent string token stream. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="args">Formatter arguments.</param> |
| | | 26 | | /// <returns>C token stream</returns> |
| | 0 | 27 | | public virtual IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) => throw new System.InvalidProgramEx |
| | | 28 | | #region Equality checks |
| | | 29 | | /// <summary> |
| | | 30 | | /// Checks if the entities are equal. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="left">Left hand operand.</param> |
| | | 33 | | /// <param name="right">Right hand operand.</param> |
| | | 34 | | /// <returns><see langword="true"/> iff <paramref name="left"/> is equal to <paramref name="right"/>.</returns> |
| | 806 | 35 | | public static bool operator ==(Base left, Base right) => EqualityComparer<Base?>.Default.Equals(left, right); |
| | | 36 | | /// <summary> |
| | | 37 | | /// Checks if the entities are unequal. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="left">Left hand operand.</param> |
| | | 40 | | /// <param name="right">Right hand operand.</param> |
| | | 41 | | /// <returns><see langword="true"/> iff <paramref name="left"/> is not equal to <paramref name="right"/>.</retur |
| | 114 | 42 | | public static bool operator !=(Base left, Base right) => !(left == right); |
| | | 43 | | /// <inheritdoc/> |
| | | 44 | | public abstract override bool Equals(object? obj); |
| | | 45 | | /// <inheritdoc/> |
| | | 46 | | public abstract override int GetHashCode(); |
| | | 47 | | #endregion |
| | | 48 | | } |
| | | 49 | | } |