| | | 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 ternary expressions. |
| | | 9 | | /// </summary> |
| | | 10 | | public class TernaryExpression : Expression, IEquatable<TernaryExpression?> { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a new ternary expression instance. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="condition">Ternary condition expression.</param> |
| | | 15 | | /// <param name="then">Expression to evaluate if the <paramref name="condition"/> is truthy.</param> |
| | | 16 | | /// <param name="else">Expression to evaluate if the <paramref name="condition"/> is falsy.</param> |
| | 28 | 17 | | public TernaryExpression(PureExpression condition, Expression then, Expression @else) { |
| | 14 | 18 | | Condition = condition; |
| | 14 | 19 | | Then = then; |
| | 14 | 20 | | Else = @else; |
| | 14 | 21 | | } |
| | | 22 | | /// <summary> |
| | | 23 | | /// Condition that decides the evaluation of the bodies. |
| | | 24 | | /// </summary> |
| | 43 | 25 | | public PureExpression Condition { get; } |
| | | 26 | | /// <summary> |
| | | 27 | | /// Expression to evaluate if the <see cref="Condition"/> is truthy. |
| | | 28 | | /// </summary> |
| | 31 | 29 | | public Expression Then { get; } |
| | | 30 | | /// <summary> |
| | | 31 | | /// Expression to evaluate if the <see cref="Condition"/> is falsy. |
| | | 32 | | /// </summary> |
| | 31 | 33 | | public Expression Else { get; } |
| | | 34 | | /// <inheritdoc/> |
| | 6 | 35 | | protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) { |
| | 6 | 36 | | var condType = Condition.InferType(gamma); |
| | 6 | 37 | | if (!(condType.BaseType is IntegerType condBase)) |
| | 0 | 38 | | throw new IllFormedException(Condition, $"Condition is not boolean nor integral in ternary!"); |
| | 6 | 39 | | var thenGuard = PureExpression.UnqualExpression(Condition, new IntegerConstant(0, condBase.Size)); |
| | 6 | 40 | | var thenWorld = gamma.Guarded(thenGuard, |
| | 12 | 41 | | gamma => Then.InferWorld(phi, gamma, heap)); |
| | 6 | 42 | | var elseGuard = PureExpression.EqualExpression(Condition, new IntegerConstant(0, condBase.Size)); |
| | 6 | 43 | | var elseWorld = gamma.Guarded(elseGuard, |
| | 12 | 44 | | gamma => Else.InferWorld(phi, gamma, heap)); |
| | | 45 | | |
| | 6 | 46 | | try { |
| | 6 | 47 | | var returnWorld = new World( |
| | 6 | 48 | | new RefinedType(BasicType.SuperType(thenWorld.Type.BaseType, elseWorld.Type.BaseType), |
| | 6 | 49 | | v => (thenGuard & thenWorld.Type.Refinement) | (elseGuard & elseWorld.Type.Refinement)), |
| | 6 | 50 | | Heap.SuperHeap(gamma, thenWorld.Heap, elseWorld.Heap)); |
| | 6 | 51 | | returnWorld.WellFormed(gamma); |
| | | 52 | | |
| | 12 | 53 | | if (gamma.Guarded(thenGuard, gamma => thenWorld.SubWorld(gamma, returnWorld)) |
| | 12 | 54 | | && gamma.Guarded(elseGuard, gamma => elseWorld.SubWorld(gamma, returnWorld))) |
| | 6 | 55 | | return returnWorld; |
| | 0 | 56 | | } catch (IllFormedException) { } |
| | | 57 | | |
| | | 58 | | // Fallback to default, less sophisticated world checking |
| | 0 | 59 | | return thenWorld.SubWorld(gamma, elseWorld) |
| | 0 | 60 | | ? elseWorld |
| | 0 | 61 | | : elseWorld.SubWorld(gamma, thenWorld) |
| | 0 | 62 | | ? thenWorld |
| | 0 | 63 | | : throw new IllFormedException(Else, "Two branches of the ternary must return the same type!"); |
| | 6 | 64 | | } |
| | | 65 | | /// <inheritdoc/> |
| | 6 | 66 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 6 | 67 | | yield return "if"; |
| | 12 | 68 | | if (args.NewlinesAfterIf <= 0) yield return " "; |
| | 0 | 69 | | else for (int i = 0; i < args.NewlinesAfterIf; i++) yield return new NewLineToken(); |
| | 156 | 70 | | foreach (var tk in Condition.Tokens(args)) yield return tk; |
| | 6 | 71 | | if (args.NewlinesBeforeThen <= 0) yield return " "; |
| | 30 | 72 | | else for (int i = 0; i < args.NewlinesBeforeThen; i++) yield return new NewLineToken(); |
| | 6 | 73 | | yield return "then"; |
| | 270 | 74 | | foreach (var tk in StringFormatterToken.Indented(args.IfBodyIndent, () => PrintBody(Then, args.NewlinesAfter |
| | 6 | 75 | | if (args.NewlinesBeforeElse <= 0) yield return " "; |
| | 30 | 76 | | else for (int i = 0; i < args.NewlinesBeforeElse; i++) yield return new NewLineToken(); |
| | 6 | 77 | | yield return "else"; |
| | 102 | 78 | | foreach (var tk in StringFormatterToken.Indented(args.IfBodyIndent, () => PrintBody(Else, args.NewlinesAfter |
| | 6 | 79 | | } |
| | 12 | 80 | | private IEnumerable<StringFormatterToken> PrintBody(Expression expression, int newlines, NanoCSourceFormat args) |
| | 24 | 81 | | if (newlines <= 0) yield return " "; |
| | 0 | 82 | | else for (int i = 0; i < newlines; i++) yield return new NewLineToken(); |
| | 252 | 83 | | foreach (var tk in expression.Tokens(args)) yield return tk; |
| | 12 | 84 | | } |
| | | 85 | | /// <inheritdoc/> |
| | 5 | 86 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 5 | 87 | | yield return "if ("; |
| | 5 | 88 | | args.PureRequiresReturn = false; |
| | 132 | 89 | | foreach (var tk in Condition.Tokens(args)) yield return tk; |
| | 5 | 90 | | yield return ") {"; |
| | 5 | 91 | | yield return new NewLineToken(); |
| | 5 | 92 | | args.PureRequiresReturn = true; |
| | 198 | 93 | | foreach (var tk in Then.Tokens(args)) yield return tk; |
| | 5 | 94 | | yield return ";"; |
| | 5 | 95 | | yield return new NewLineToken(); |
| | 5 | 96 | | yield return "} else {"; |
| | 5 | 97 | | yield return new NewLineToken(); |
| | 5 | 98 | | args.PureRequiresReturn = true; |
| | 108 | 99 | | foreach (var tk in Else.Tokens(args)) yield return tk; |
| | 5 | 100 | | yield return ";"; |
| | 5 | 101 | | yield return new NewLineToken(); |
| | 5 | 102 | | yield return "}"; |
| | 5 | 103 | | } |
| | | 104 | | /// <inheritdoc/> |
| | 0 | 105 | | public override IEnumerable<string> RequiredFunctions() => Then.RequiredFunctions().Concat(Else.RequiredFunction |
| | | 106 | | #region Equality checks |
| | | 107 | | /// <inheritdoc/> |
| | 7 | 108 | | public override bool Equals(object? obj) => Equals(obj as TernaryExpression); |
| | | 109 | | /// <inheritdoc/> |
| | 7 | 110 | | public bool Equals(TernaryExpression? other) => !(other is null) && EqualityComparer<PureExpression>.Default.Equ |
| | | 111 | | /// <inheritdoc/> |
| | 0 | 112 | | public override int GetHashCode() => HashCode.Combine(Condition, Then, Else); |
| | | 113 | | /// <inheritdoc/> |
| | 0 | 114 | | public static bool operator ==(TernaryExpression? left, TernaryExpression? right) => EqualityComparer<TernaryExp |
| | | 115 | | /// <inheritdoc/> |
| | 0 | 116 | | public static bool operator !=(TernaryExpression? left, TernaryExpression? right) => !(left == right); |
| | | 117 | | #endregion |
| | | 118 | | } |
| | | 119 | | } |