< Summary

Class:NanoCLang.Environemnts.GlobalEnvironment
Assembly:NanoCLang
File(s):C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Environemnts\GlobalEnvironment.cs
Covered lines:27
Uncovered lines:1
Coverable lines:28
Total lines:76
Line coverage:96.4% (27 of 28)
Covered branches:4
Total branches:4
Branch coverage:100% (4 of 4)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
With(...)100%2100%
AddStruct(...)100%1100%
With(...)100%1100%
With(...)100%2100%
TryGetStruct(...)100%1100%
With(...)100%1100%
ToString()100%10%

File(s)

C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Environemnts\GlobalEnvironment.cs

#LineLine coverage
 1using NanoCLang.Entities;
 2using System;
 3using System.Collections.Generic;
 4using System.Diagnostics.CodeAnalysis;
 5
 6namespace 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>
 9815        public GlobalEnvironment() : base() {
 4916            structs = new Dictionary<string, StructDefinition>();
 4917        }
 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>
 2227        public T With<T>(string name, RawFunctionSchema schema, Func<GlobalEnvironment, T> p) {
 2228            if (!TryAdd(name, schema))
 129                throw new IllFormedException(schema, $"Cannot bind function to {name} because it was already defined!");
 2130            try {
 2131                return p(this);
 2132            } finally {
 2133                Remove(name);
 2134            }
 2035        }
 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>
 1141        public void AddStruct(string name, StructDefinition def) {
 1142            structs.Add(name, def);
 1143        }
 44        /// <inheritdoc cref="With{T}(string, RawFunctionSchema, Func{GlobalEnvironment, T})"/>
 2245        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>
 655        public T With<T>(string name, StructDefinition structure, Func<GlobalEnvironment, T> p) {
 656            if (!structs.TryAdd(name, structure))
 157                throw new IllFormedException(structure, $"Cannot bind structure to {name} because it was already defined
 558            try {
 559                return p(this);
 560            } finally {
 561                structs.Remove(name);
 562            }
 463        }
 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>
 6970        public bool TryGetStruct(string name, [NotNullWhen(true)] out StructDefinition def) => structs.TryGetValue(name,
 71        /// <inheritdoc cref="With{T}(string, StructDefinition, Func{GlobalEnvironment, T})"/>
 672        public T With<T>(StructBinding def, Func<GlobalEnvironment, T> p) => With(def.Name, def.Definition, p);
 73        /// <inheritdoc/>
 074        public override string ToString() => string.Join(",", Keys);
 75    }
 76}