| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for assertions. |
| | | 8 | | /// </summary> |
| | | 9 | | public class AssertionExpression : PureExpression, IEquatable<AssertionExpression?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new assertion expression instance that asserts that the <paramref name="expression"/> is truty. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="expression">Expression to evaluate.</param> |
| | 30 | 14 | | public AssertionExpression(PureExpression expression) { |
| | 15 | 15 | | Expression = expression; |
| | 15 | 16 | | } |
| | | 17 | | /// <summary> |
| | | 18 | | /// Expression to evaluate. |
| | | 19 | | /// </summary> |
| | 33 | 20 | | public PureExpression Expression { get; } |
| | | 21 | | /// <inheritdoc/> |
| | 0 | 22 | | public override void WellFormed(LocalEnvironment gamma) => Expression.WellFormed(gamma); |
| | | 23 | | /// <inheritdoc/> |
| | 7 | 24 | | protected override Type DoInferType(LocalEnvironment gamma) { |
| | 7 | 25 | | var type = Expression.InferType(gamma); |
| | 14 | 26 | | if (!type.SubType(gamma, new RefinedType(new IntegerType(type.Size), v => UnqualExpression(v, (PureExpressio |
| | 2 | 27 | | throw new IllFormedException(Expression, $"Assertion failed!"); |
| | 5 | 28 | | return IntegerType.Void; |
| | 5 | 29 | | } |
| | | 30 | | /// <inheritdoc/> |
| | 6 | 31 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 6 | 32 | | yield return "assert"; |
| | 6 | 33 | | if (args.SpaceAfterFunctionName) yield return " "; |
| | 6 | 34 | | yield return "("; |
| | 336 | 35 | | foreach (var tk in Expression.Tokens(args)) yield return tk; |
| | 6 | 36 | | yield return ")"; |
| | 6 | 37 | | } |
| | | 38 | | /// <inheritdoc/> |
| | 2 | 39 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 2 | 40 | | yield return "/* "; |
| | 150 | 41 | | foreach (var tk in Tokens((NanoCSourceFormat)args)) yield return tk; |
| | 2 | 42 | | yield return " */"; |
| | 2 | 43 | | } |
| | | 44 | | /// <inheritdoc/> |
| | 2 | 45 | | public override void CheckType(LocalEnvironment gamma, Type type) { |
| | 2 | 46 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"T-Assert: {Expression}"); |
| | 2 | 47 | | if (!(type is IntegerType i && i.IsVoid)) throw new IllFormedException(this, $"Assertion expression is of ty |
| | 2 | 48 | | var inferredType = Expression.InferType(gamma); |
| | 4 | 49 | | Expression.CheckType(gamma, new RefinedType(new IntegerType(inferredType.Size), v => UnqualExpression(v, (Pu |
| | 1 | 50 | | } |
| | | 51 | | /// <inheritdoc/> |
| | | 52 | | public override PureExpression Replace(Substitutor sub) |
| | 0 | 53 | | => new AssertionExpression(Expression.Replace(sub)); |
| | | 54 | | #region Equality checks |
| | | 55 | | /// <inheritdoc/> |
| | 6 | 56 | | public override bool Equals(object? obj) => Equals(obj as AssertionExpression); |
| | | 57 | | /// <inheritdoc/> |
| | 6 | 58 | | public bool Equals(AssertionExpression? other) => !(other is null) && EqualityComparer<PureExpression>.Default.E |
| | | 59 | | /// <inheritdoc/> |
| | 0 | 60 | | public override int GetHashCode() => System.HashCode.Combine(Expression); |
| | | 61 | | /// <inheritdoc/> |
| | 0 | 62 | | public static bool operator ==(AssertionExpression? left, AssertionExpression? right) => EqualityComparer<Assert |
| | | 63 | | /// <inheritdoc/> |
| | 0 | 64 | | public static bool operator !=(AssertionExpression? left, AssertionExpression? right) => !(left == right); |
| | | 65 | | #endregion |
| | | 66 | | } |
| | | 67 | | } |