| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for NanoC integer types. |
| | | 8 | | /// </summary> |
| | | 9 | | public class IntegerType : ArbitraryIntegerType, IEquatable<IntegerType?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a NanoC integer type. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="size">The size in bytes of the type.</param> |
| | | 14 | | /// <param name="values">The index of valid values.</param> |
| | 1842 | 15 | | public IntegerType(int size, Index values) { |
| | 921 | 16 | | Size = size; |
| | 921 | 17 | | Values = values; |
| | 921 | 18 | | } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates a new instance of a NanoC integer type with arbitrary index. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="size">The size in bytes of the type.</param> |
| | 507 | 23 | | public IntegerType(int size) : this(size, new ArbitraryIndex()) { } |
| | | 24 | | /// <summary> |
| | | 25 | | /// Size of the type in bytes. |
| | | 26 | | /// </summary> |
| | 2845 | 27 | | public override int Size { get; } |
| | | 28 | | /// <summary> |
| | | 29 | | /// Index of allowed values for the integer. |
| | | 30 | | /// </summary> |
| | 1767 | 31 | | public Index Values { get; } |
| | | 32 | | /// <summary> |
| | | 33 | | /// Checks if the type is a void type. |
| | | 34 | | /// </summary> |
| | 4 | 35 | | public bool IsVoid => Size == 0 && Values == Index.Zero; |
| | | 36 | | /// <summary> |
| | | 37 | | /// Returns a new void type. |
| | | 38 | | /// </summary> |
| | 64 | 39 | | public static IntegerType Void => new IntegerType(0, new SingletonIndex(0)); |
| | | 40 | | /// <summary> |
| | | 41 | | /// Returns a new boolean type. |
| | | 42 | | /// </summary> |
| | 155 | 43 | | public static IntegerType Boolean => new IntegerType(1, new SequenceIndex(0, 1)); |
| | | 44 | | /// <inheritdoc/> |
| | 198 | 45 | | public override void WellFormed(LocalEnvironment gamma, Heap heap) { |
| | 198 | 46 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Int: {this}"); |
| | 198 | 47 | | if (Size < 0) |
| | 1 | 48 | | throw new IllFormedException(this, $"Integer cannot have negative size {Size}!"); |
| | 197 | 49 | | } |
| | | 50 | | /// <inheritdoc/> |
| | 591 | 51 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 591 | 52 | | switch (Size) { |
| | 147 | 53 | | case 0 when Values == Index.Zero: yield return "void"; break; |
| | 828 | 54 | | case 1 when Values is ArbitraryIndex: yield return "char"; break; |
| | 669 | 55 | | case 4 when Values is ArbitraryIndex: yield return "int"; break; |
| | | 56 | | default: |
| | 64 | 57 | | yield return Size.ToString(); |
| | 64 | 58 | | yield return "["; |
| | 606 | 59 | | foreach (var tk in Values.Tokens(args)) yield return tk; |
| | 64 | 60 | | yield return "]"; |
| | 64 | 61 | | break; |
| | | 62 | | } |
| | 591 | 63 | | } |
| | | 64 | | /// <inheritdoc/> |
| | 268 | 65 | | public override object Clone() => new IntegerType(Size, (Index)Values.Clone()); |
| | 115 | 66 | | internal static StringFormatterToken GetCType(int size) => size switch { |
| | 19 | 67 | | 0 => "void", |
| | 18 | 68 | | 1 => "int8_t", |
| | 0 | 69 | | 2 => "int16_t", |
| | 78 | 70 | | 4 => "int32_t", |
| | 0 | 71 | | 8 => "int64_t", |
| | 0 | 72 | | _ => throw new InvalidProgramException($"Cannot declare a {size}-Byte integer in C!"), |
| | 115 | 73 | | }; |
| | | 74 | | /// <inheritdoc/> |
| | 186 | 75 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { yield return GetCType(Size); } |
| | | 76 | | #region Equality checks |
| | | 77 | | /// <inheritdoc/> |
| | 347 | 78 | | public override bool Equals(object? obj) => Equals(obj as IntegerType); |
| | | 79 | | /// <inheritdoc/> |
| | 347 | 80 | | public bool Equals(IntegerType? other) => !(other is null) && Size == other.Size && EqualityComparer<Index>.Defa |
| | | 81 | | /// <inheritdoc/> |
| | 0 | 82 | | public override int GetHashCode() => HashCode.Combine(Size, Values); |
| | | 83 | | /// <inheritdoc/> |
| | 0 | 84 | | public static bool operator ==(IntegerType? left, IntegerType? right) => EqualityComparer<IntegerType?>.Default. |
| | | 85 | | /// <inheritdoc/> |
| | 0 | 86 | | public static bool operator !=(IntegerType? left, IntegerType? right) => !(left == right); |
| | | 87 | | #endregion |
| | | 88 | | } |
| | | 89 | | } |