| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace NanoCLang.Entities { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides an abstract superclass for all valid NanoC expressions. |
| | | 7 | | /// This class also implements the well formedness and type checking/inference mechanisms for expressions. |
| | | 8 | | /// </summary> |
| | | 9 | | public abstract class Expression : Base { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Infer the world of the expression and throw an exception if it is ill-formed. |
| | | 12 | | /// Additionally fixes the resulting world to this entity. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="phi">Global Environemt to check the expression with.</param> |
| | | 15 | | /// <param name="gamma">Local Environemt to check the expression with.</param> |
| | | 16 | | /// <param name="heap">Input heap of the expression.</param> |
| | | 17 | | /// <exception cref="IllFormedException">Expression is ill-formed.</exception> |
| | 208 | 18 | | public World InferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) => FixedWorld = DoInferWorld(p |
| | | 19 | | /// <summary> |
| | | 20 | | /// Infer the world of the expression and throw an exception if it is ill-formed. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="phi">Global Environemt to check the expression with.</param> |
| | | 23 | | /// <param name="gamma">Local Environemt to check the expression with.</param> |
| | | 24 | | /// <param name="heap">Input heap of the expression.</param> |
| | | 25 | | /// <exception cref="IllFormedException">Expression is ill-formed.</exception> |
| | | 26 | | protected abstract World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap); |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the world that was fixed to this expression during type-checking. |
| | | 29 | | /// </summary> |
| | 237 | 30 | | public World? FixedWorld { get; protected set; } |
| | | 31 | | /// <summary> |
| | | 32 | | /// Typecheck the expression with the <paramref name="world"/> and throw an exception if it is ill-formed. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="phi">Global Environemt to check the expression with.</param> |
| | | 35 | | /// <param name="gamma">Local Environemt to check the expression with.</param> |
| | | 36 | | /// <param name="heap">Input heap of the expression.</param> |
| | | 37 | | /// <param name="world">World to typecheck with.</param> |
| | | 38 | | /// <exception cref="IllFormedException">Expression is ill-formed.</exception> |
| | 1 | 39 | | public virtual void CheckType(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap, World world) { |
| | 1 | 40 | | var inferred = InferWorld(phi, gamma, heap); |
| | 1 | 41 | | if (!inferred.SubWorld(gamma, world)) throw new IllFormedException(this, $"This is not of world {world}!"); |
| | 1 | 42 | | } |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets an enumeration of required function calls. Can be used to check if a function is recursive. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <returns>Enumeration of called functions.</returns> |
| | | 47 | | public abstract IEnumerable<string> RequiredFunctions(); |
| | | 48 | | /// <inheritdoc/> |
| | 0 | 49 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) => Tokens((NanoCSourceFormat)args); |
| | | 50 | | } |
| | | 51 | | } |