| | | 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 the main call. |
| | | 9 | | /// </summary> |
| | | 10 | | public class MainCall : Program, IEquatable<MainCall?> { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a new main call that calls the main function named <paramref name="name"/> without parameters. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="name">Name of the main function.</param> |
| | 54 | 15 | | public MainCall(string name) { |
| | 27 | 16 | | Name = name; |
| | 27 | 17 | | } |
| | | 18 | | /// <summary> |
| | | 19 | | /// Name of the main function. |
| | | 20 | | /// </summary> |
| | 66 | 21 | | public string Name { get; } |
| | | 22 | | /// <inheritdoc/> |
| | 9 | 23 | | public override World WellFormed(GlobalEnvironment phi) { |
| | 9 | 24 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-MainCall: {Name}"); |
| | 9 | 25 | | if (!phi.TryGetValue(Name, out var rawschema)) |
| | 1 | 26 | | throw new IllFormedException(this, $"Main call to unknown function {Name}!"); |
| | 8 | 27 | | var schema = rawschema.Build(phi); |
| | 8 | 28 | | if (!schema.Heap.IsAbstract()) |
| | 1 | 29 | | throw new IllFormedException(this, $"Main call is invalid because {Name} operates on concrete locations! |
| | 7 | 30 | | return schema.World; |
| | 7 | 31 | | } |
| | | 32 | | /// <inheritdoc/> |
| | 8 | 33 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 8 | 34 | | yield return Name; |
| | 8 | 35 | | if (args.SpaceAfterFunctionName) yield return " "; |
| | 8 | 36 | | yield return "("; |
| | 8 | 37 | | if (args.SpaceInEmptyArgList) yield return " "; |
| | 8 | 38 | | yield return ")"; |
| | 8 | 39 | | } |
| | | 40 | | /// <inheritdoc/> |
| | 12 | 41 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) => Name is null ? Enumerable.Empty< |
| | 12 | 42 | | : Program.PrintHeader(args).Concat(new FunctionBinding( |
| | 12 | 43 | | "main", |
| | 12 | 44 | | new FunctionDefinition( |
| | 12 | 45 | | new ConcatenationExpression(new FunctionCallExpression(Name, new string[] { }), |
| | 12 | 46 | | new IntegerConstant(0, 4)), |
| | 12 | 47 | | new RawFunctionSchema( |
| | 12 | 48 | | new string[] { }, |
| | 12 | 49 | | new TypeParameter[] { }, |
| | 12 | 50 | | new IHeapElement[] { }, |
| | 12 | 51 | | new RawWorld(new IntegerType(4), new IHeapElement[] { }))), new MainCall(null!)).Tokens(args)); |
| | | 52 | | #region Equality checks |
| | | 53 | | /// <inheritdoc/> |
| | 10 | 54 | | public override bool Equals(object? obj) => Equals(obj as MainCall); |
| | | 55 | | /// <inheritdoc/> |
| | 10 | 56 | | public bool Equals(MainCall? other) => !(other is null) && Name == other.Name; |
| | | 57 | | /// <inheritdoc/> |
| | 0 | 58 | | public override int GetHashCode() => HashCode.Combine(Name); |
| | | 59 | | /// <inheritdoc/> |
| | 0 | 60 | | public static bool operator ==(MainCall? left, MainCall? right) => EqualityComparer<MainCall?>.Default.Equals(le |
| | | 61 | | /// <inheritdoc/> |
| | 0 | 62 | | public static bool operator !=(MainCall? left, MainCall? right) => !(left == right); |
| | | 63 | | #endregion |
| | | 64 | | } |
| | | 65 | | } |