| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a superclass for all program entites. |
| | | 8 | | /// </summary> |
| | | 9 | | public abstract class Program : Base { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates the default environment that programs are type checked with. |
| | | 12 | | /// This may contain NanoC support functions or standard libraries as well as includes from functions. |
| | | 13 | | /// </summary> |
| | 10 | 14 | | public GlobalEnvironment DefaultEnvironemt => new GlobalEnvironment(); |
| | | 15 | | /// <summary> |
| | | 16 | | /// Checks if the rogram is well-formed and raises an exception if it is ill-formed. |
| | | 17 | | /// If it is well-formed it returns the type of the program. |
| | | 18 | | /// Uses the <see cref="DefaultEnvironemt"/> to check the program. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <exception cref="IllFormedException">Program is ill-formed.</exception> |
| | | 21 | | /// <returns>Type of the program entity.</returns> |
| | 10 | 22 | | public virtual World WellFormed() => WellFormed(DefaultEnvironemt); |
| | | 23 | | /// <summary> |
| | | 24 | | /// Checks if the program is well-formed and raises an exception if it is ill-formed. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="phi">Environemt to check the program with.</param> |
| | | 27 | | /// <exception cref="IllFormedException">Program is ill-formed.</exception> |
| | | 28 | | /// <returns>Type of the program entity.</returns> |
| | | 29 | | public abstract World WellFormed(GlobalEnvironment phi); |
| | 1 | 30 | | private static readonly string[] includes = new string[] { |
| | 1 | 31 | | "stdlib.h" /* allocation expressions */, |
| | 1 | 32 | | "stdint.h" /* fixed width integer types */, |
| | 1 | 33 | | }; |
| | | 34 | | /// <summary> |
| | | 35 | | /// Print the program header if it is not yet printed. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="args">Arguments that include whether the header was already printed.</param> |
| | | 38 | | /// <returns>Tokens that represent the header.</returns> |
| | 35 | 39 | | protected static IEnumerable<StringFormatterToken> PrintHeader(CSourceFormat args) { |
| | 64 | 40 | | if (args.HeaderPrinted) yield break; |
| | 54 | 41 | | foreach (var include in includes) yield return $"#include <{include}>\n"; |
| | 6 | 42 | | yield return "\n"; |
| | 6 | 43 | | args.HeaderPrinted = true; |
| | 6 | 44 | | } |
| | | 45 | | } |
| | | 46 | | } |