| | | 1 | | using NanoCLang.Entities; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | |
| | | 6 | | namespace NanoCLang.Environemnts { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides a class for global environemnts mapping function names to their schema. |
| | | 9 | | /// </summary> |
| | | 10 | | public class GlobalEnvironment : Dictionary<string, RawFunctionSchema> { |
| | | 11 | | private readonly Dictionary<string, StructDefinition> structs; |
| | | 12 | | /// <summary> |
| | | 13 | | /// Creates a new instance of an empty global environment. |
| | | 14 | | /// </summary> |
| | 98 | 15 | | public GlobalEnvironment() : base() { |
| | 49 | 16 | | structs = new Dictionary<string, StructDefinition>(); |
| | 49 | 17 | | } |
| | | 18 | | /// <summary> |
| | | 19 | | /// Evaluates a function with an additional schema binding on the environment. |
| | | 20 | | /// This function allows for evaluating judgements with additional bindings on an environment without the need t |
| | | 21 | | /// </summary> |
| | | 22 | | /// <typeparam name="T">Function output to evaluate.</typeparam> |
| | | 23 | | /// <param name="name">Name of the function binding.</param> |
| | | 24 | | /// <param name="schema">Schema that the name is bound to.</param> |
| | | 25 | | /// <param name="p">Function that evaluates the body.</param> |
| | | 26 | | /// <returns>Evaluated function result.</returns> |
| | 22 | 27 | | public T With<T>(string name, RawFunctionSchema schema, Func<GlobalEnvironment, T> p) { |
| | 22 | 28 | | if (!TryAdd(name, schema)) |
| | 1 | 29 | | throw new IllFormedException(schema, $"Cannot bind function to {name} because it was already defined!"); |
| | 21 | 30 | | try { |
| | 21 | 31 | | return p(this); |
| | 21 | 32 | | } finally { |
| | 21 | 33 | | Remove(name); |
| | 21 | 34 | | } |
| | 20 | 35 | | } |
| | | 36 | | /// <summary> |
| | | 37 | | /// Bind the structure definition to the name in the global environment. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="name">Name of the defined structure.</param> |
| | | 40 | | /// <param name="def">Definition of the structure.</param> |
| | 11 | 41 | | public void AddStruct(string name, StructDefinition def) { |
| | 11 | 42 | | structs.Add(name, def); |
| | 11 | 43 | | } |
| | | 44 | | /// <inheritdoc cref="With{T}(string, RawFunctionSchema, Func{GlobalEnvironment, T})"/> |
| | 22 | 45 | | public T With<T>(FunctionBinding def, Func<GlobalEnvironment, T> p) => With(def.Name, def.Definition.Schema, p); |
| | | 46 | | /// <summary> |
| | | 47 | | /// Evaluates a function with an additional structure binding on the environment. |
| | | 48 | | /// This function allows for evaluating judgements with additional bindings on an environment without the need t |
| | | 49 | | /// </summary> |
| | | 50 | | /// <typeparam name="T">Function output to evaluate.</typeparam> |
| | | 51 | | /// <param name="name">Name of the function binding.</param> |
| | | 52 | | /// <param name="structure">Structure that the name is bound to.</param> |
| | | 53 | | /// <param name="p">Function that evaluates the body.</param> |
| | | 54 | | /// <returns>Evaluated function result.</returns> |
| | 6 | 55 | | public T With<T>(string name, StructDefinition structure, Func<GlobalEnvironment, T> p) { |
| | 6 | 56 | | if (!structs.TryAdd(name, structure)) |
| | 1 | 57 | | throw new IllFormedException(structure, $"Cannot bind structure to {name} because it was already defined |
| | 5 | 58 | | try { |
| | 5 | 59 | | return p(this); |
| | 5 | 60 | | } finally { |
| | 5 | 61 | | structs.Remove(name); |
| | 5 | 62 | | } |
| | 4 | 63 | | } |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the structure definition of the structure that was bound to the <paramref name="name"/> and outputs it. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="name">Name of the structure to look for.</param> |
| | | 68 | | /// <param name="def">Definition of the structure.</param> |
| | | 69 | | /// <returns><see langword="true"/> iff the name was defined.</returns> |
| | 69 | 70 | | public bool TryGetStruct(string name, [NotNullWhen(true)] out StructDefinition def) => structs.TryGetValue(name, |
| | | 71 | | /// <inheritdoc cref="With{T}(string, StructDefinition, Func{GlobalEnvironment, T})"/> |
| | 6 | 72 | | public T With<T>(StructBinding def, Func<GlobalEnvironment, T> p) => With(def.Name, def.Definition, p); |
| | | 73 | | /// <inheritdoc/> |
| | 0 | 74 | | public override string ToString() => string.Join(",", Keys); |
| | | 75 | | } |
| | | 76 | | } |