| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a superclass for binary expressions. |
| | | 8 | | /// </summary> |
| | | 9 | | public abstract class BinaryExpression : PureExpression, IEquatable<BinaryExpression?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a binary operation applied to <paramref name="left"/> and <paramref name="right"/> |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="left">Left hand expression of the operator.</param> |
| | | 14 | | /// <param name="operation">Operation to evaluate for the expression.</param> |
| | | 15 | | /// <param name="right">Right hand expression of the operator.</param> |
| | 4730 | 16 | | public BinaryExpression(PureExpression left, string operation, PureExpression right) { |
| | 2365 | 17 | | Left = left; |
| | 2365 | 18 | | Operation = operation; |
| | 2365 | 19 | | Right = right; |
| | 2365 | 20 | | } |
| | | 21 | | /// <summary> |
| | | 22 | | /// Left hand expression of the operator. |
| | | 23 | | /// </summary> |
| | 3914 | 24 | | public PureExpression Left { get; } |
| | | 25 | | /// <summary> |
| | | 26 | | /// Operation to evaluate for the expression. |
| | | 27 | | /// </summary> |
| | 1693 | 28 | | public string Operation { get; } |
| | | 29 | | /// <summary> |
| | | 30 | | /// Right hand expression of the operator. |
| | | 31 | | /// </summary> |
| | 3509 | 32 | | public PureExpression Right { get; } |
| | | 33 | | /// <inheritdoc/> |
| | 172 | 34 | | public override void WellFormed(LocalEnvironment gamma) { |
| | 172 | 35 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-BinOp: {this}"); |
| | 172 | 36 | | Left.WellFormed(gamma); |
| | 172 | 37 | | Right.WellFormed(gamma); |
| | 172 | 38 | | } |
| | | 39 | | /// <inheritdoc/> |
| | | 40 | | protected abstract override Type DoInferType(LocalEnvironment gamma); |
| | | 41 | | /// <inheritdoc/> |
| | 1038 | 42 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 1038 | 43 | | yield return "("; |
| | 15408 | 44 | | foreach (var tk in Left.Tokens(args)) yield return tk; |
| | 2076 | 45 | | if (args.SpaceBeforeBinOp) yield return " "; |
| | 1038 | 46 | | yield return Operation; |
| | 2076 | 47 | | if (args.SpaceAfterBinOp) yield return " "; |
| | 17886 | 48 | | foreach (var tk in Right.Tokens(args)) yield return tk; |
| | 1038 | 49 | | yield return ")"; |
| | 1038 | 50 | | } |
| | | 51 | | /// <inheritdoc/> |
| | 29 | 52 | | protected override IEnumerable<StringFormatterToken> NoReturnTokens(CSourceFormat args) { |
| | 29 | 53 | | yield return "("; |
| | 186 | 54 | | foreach (var tk in Left.Tokens(args)) yield return tk; |
| | 58 | 55 | | if (args.SpaceBeforeBinOp) yield return " "; |
| | 29 | 56 | | yield return Operation; |
| | 58 | 57 | | if (args.SpaceAfterBinOp) yield return " "; |
| | 414 | 58 | | foreach (var tk in Right.Tokens(args)) yield return tk; |
| | 29 | 59 | | yield return ")"; |
| | 29 | 60 | | } |
| | | 61 | | /// <summary> |
| | | 62 | | /// Applies the operation on indices and gives a superset of all possible values as output index. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="left">Left hand operand.</param> |
| | | 65 | | /// <param name="right">Right hand operand.</param> |
| | | 66 | | /// <returns>Resulting index that covers all possible values.</returns> |
| | 0 | 67 | | public virtual Index IndexOperation(Index left, Index right) => new ArbitraryIndex(); |
| | | 68 | | #region Equality checks |
| | | 69 | | /// <inheritdoc/> |
| | 345 | 70 | | public override bool Equals(object? obj) => Equals(obj as BinaryExpression); |
| | | 71 | | /// <inheritdoc/> |
| | 345 | 72 | | public bool Equals(BinaryExpression? other) => !(other is null) && Operation == other.Operation && EqualityCompa |
| | | 73 | | /// <inheritdoc/> |
| | 0 | 74 | | public override int GetHashCode() => HashCode.Combine(Left, Operation, Right); |
| | | 75 | | /// <inheritdoc/> |
| | 0 | 76 | | public static bool operator ==(BinaryExpression? left, BinaryExpression? right) => EqualityComparer<BinaryExpres |
| | | 77 | | /// <inheritdoc/> |
| | 0 | 78 | | public static bool operator !=(BinaryExpression? left, BinaryExpression? right) => !(left == right); |
| | | 79 | | #endregion |
| | | 80 | | } |
| | | 81 | | } |