| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | namespace NanoCLang.Entities { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides a class for binding expressions. |
| | | 9 | | /// </summary> |
| | | 10 | | public class BindingExpression : Expression, IEquatable<BindingExpression?> { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a new instance of a binding expression that binds the <paramref name="expression"/> to the <paramref |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="name">Name of the binding.</param> |
| | | 15 | | /// <param name="expression">Expression that is bound to the <paramref name="name"/>.</param> |
| | | 16 | | /// <param name="next">Expression that is evaluated with the binding.</param> |
| | 106 | 17 | | public BindingExpression(string name, Expression expression, Expression next) { |
| | 53 | 18 | | Name = name; |
| | 53 | 19 | | Expression = expression; |
| | 53 | 20 | | Next = next; |
| | 53 | 21 | | } |
| | | 22 | | /// <summary> |
| | | 23 | | /// Name that the <see cref="Expression"/> is bound to. |
| | | 24 | | /// </summary> |
| | 127 | 25 | | public string Name { get; } |
| | | 26 | | /// <summary> |
| | | 27 | | /// Expression that is bound to the <see cref="Name"/>. |
| | | 28 | | /// </summary> |
| | 161 | 29 | | public Expression Expression { get; } |
| | | 30 | | /// <summary> |
| | | 31 | | /// Expression that is evaluated with the binding. |
| | | 32 | | /// </summary> |
| | 137 | 33 | | public Expression Next { get; } |
| | | 34 | | /// <inheritdoc/> |
| | 26 | 35 | | protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) { |
| | 26 | 36 | | VerbConsole.WriteLine(VerbosityLevel.Default, "T-Let"); |
| | 26 | 37 | | var expressionWorld = Expression.InferWorld(phi, gamma, heap); |
| | 26 | 38 | | return gamma.With(Name, expressionWorld.Type, |
| | 52 | 39 | | gamma => Next.InferWorld(phi, gamma, expressionWorld.Heap)); |
| | 24 | 40 | | } |
| | | 41 | | /// <inheritdoc/> |
| | 25 | 42 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 25 | 43 | | yield return "let"; |
| | 25 | 44 | | yield return " "; |
| | 25 | 45 | | yield return Name; |
| | 50 | 46 | | if (args.SpaceBeforeBindingAssignment) yield return " "; |
| | 25 | 47 | | yield return "="; |
| | 50 | 48 | | if (args.SpaceAfterBindingAssignment) yield return " "; |
| | 600 | 49 | | foreach (var tk in Expression.Tokens(args)) yield return tk; |
| | 25 | 50 | | yield return " "; |
| | 25 | 51 | | yield return "in"; |
| | 25 | 52 | | if (args.NewlinesAfterBindingExpression <= 0) yield return " "; |
| | 125 | 53 | | else for (int i = 0; i < args.NewlinesAfterBindingExpression; i++) yield return new NewLineToken(); |
| | 4017 | 54 | | foreach (var tk in Next.Tokens(args)) yield return tk; |
| | 25 | 55 | | } |
| | | 56 | | /// <inheritdoc/> |
| | 24 | 57 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 24 | 58 | | var boundType = Expression.FixedWorld?.Type; |
| | 24 | 59 | | if (boundType is null) throw new InvalidOperationException("Expression must have fixed world when translatin |
| | 192 | 60 | | foreach (var tk in boundType.Tokens(args)) yield return tk; |
| | 24 | 61 | | yield return " "; |
| | 24 | 62 | | yield return Name; |
| | 48 | 63 | | if (args.SpaceBeforeBindingAssignment) yield return " "; |
| | 24 | 64 | | yield return "="; |
| | 48 | 65 | | if (args.SpaceAfterBindingAssignment) yield return " "; |
| | 24 | 66 | | args.PureRequiresReturn = false; |
| | 861 | 67 | | foreach (var tk in Expression.Tokens(args)) yield return tk; |
| | 24 | 68 | | yield return ";"; |
| | 24 | 69 | | yield return new NewLineToken(); |
| | 24 | 70 | | args.PureRequiresReturn = true; |
| | 5031 | 71 | | foreach (var tk in Next.Tokens(args)) yield return tk; |
| | 24 | 72 | | yield return ";"; |
| | 24 | 73 | | } |
| | | 74 | | /// <inheritdoc/> |
| | 10 | 75 | | public override IEnumerable<string> RequiredFunctions() => Expression.RequiredFunctions().Concat(Next.RequiredFu |
| | | 76 | | #region Equality checks |
| | | 77 | | /// <inheritdoc/> |
| | 26 | 78 | | public override bool Equals(object? obj) => Equals(obj as BindingExpression); |
| | | 79 | | /// <inheritdoc/> |
| | 26 | 80 | | public bool Equals(BindingExpression? other) => !(other is null) && Name == other.Name && EqualityComparer<Expre |
| | | 81 | | /// <inheritdoc/> |
| | 0 | 82 | | public override int GetHashCode() => HashCode.Combine(Name, Expression, Next); |
| | | 83 | | /// <inheritdoc/> |
| | 0 | 84 | | public static bool operator ==(BindingExpression? left, BindingExpression? right) => EqualityComparer<BindingExp |
| | | 85 | | /// <inheritdoc/> |
| | 0 | 86 | | public static bool operator !=(BindingExpression? left, BindingExpression? right) => !(left == right); |
| | | 87 | | #endregion |
| | | 88 | | } |
| | | 89 | | } |