< Summary

Class:NanoCLang.Entities.VariableExpression
Assembly:NanoCLang
File(s):C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Entities\Expression\PureExpression\VariableExpression.cs
Covered lines:23
Uncovered lines:4
Coverable lines:27
Total lines:65
Line coverage:85.1% (23 of 27)
Covered branches:6
Total branches:8
Branch coverage:75% (6 of 8)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
get_Name()100%1100%
op_Implicit(...)100%10%
WellFormed(...)50%280%
Replace(...)100%2100%
Clone()100%1100%
ToArithmetic(...)100%1100%
ToBoolean(...)100%1100%
DoInferType(...)50%2100%
Tokens()100%1100%
Equals(...)100%1100%
Equals(...)100%2100%
GetHashCode()100%1100%
op_Equality(...)100%10%
op_Inequality(...)100%10%

File(s)

C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Entities\Expression\PureExpression\VariableExpression.cs

#LineLine coverage
 1using Microsoft.Z3;
 2using NanoCLang.Environemnts;
 3using System;
 4using System.Collections.Generic;
 5
 6namespace NanoCLang.Entities {
 7    /// <summary>
 8    /// Provides a class for variable literal expression.
 9    /// </summary>
 10    public class VariableExpression : PureExpression, IEquatable<VariableExpression?>, ICloneable {
 11        /// <summary>
 12        /// Creates a new instance of a variable literal with the given <paramref name="name"/>.
 13        /// </summary>
 14        /// <param name="name">Name that the literal refers to.</param>
 501415        public VariableExpression(string name) {
 250716            Name = name;
 250717        }
 18        /// <summary>
 19        /// Name that the literal refers to.
 20        /// </summary>
 639421        public string Name { get; }
 22        /// <summary>
 23        /// Converts a string to its literal variable expression.
 24        /// </summary>
 25        /// <param name="name">Name of the variable.</param>
 026        public static implicit operator VariableExpression(string name) => new VariableExpression(name);
 27        /// <inheritdoc/>
 25628        public override void WellFormed(LocalEnvironment gamma) {
 25629            VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Var: {Name}");
 25630            if (!gamma.ContainsKey(Name))
 031                throw new IllFormedException(this, $"Reference to undeclared variable {Name}!");
 25632        }
 33        /// <inheritdoc/>
 34        public override PureExpression Replace(Substitutor rep)
 100335            => rep.Variables.TryGetValue(Name, out var substitute) ? substitute : (PureExpression)Clone();
 36        /// <inheritdoc/>
 46137        public virtual object Clone() => new VariableExpression(Name);
 38        /// <inheritdoc/>
 139939        public override ArithExpr ToArithmetic(LocalEnvironment gamma, NanoCSMT smt) => smt.Context.MkIntConst(Name);
 40        /// <inheritdoc/>
 1441        public override BoolExpr ToBoolean(LocalEnvironment gamma, NanoCSMT smt) => smt.Context.MkBoolConst(Name);
 42        /// <inheritdoc/>
 62443        protected override Type DoInferType(LocalEnvironment gamma) {
 62444            return !gamma.TryGetValue(Name, out var type)
 62445                ? throw new IllFormedException(this, $"Variable {Name} is not bound in the local environment!")
 124846                : new RefinedType(type, v => EqualExpression(v, this));
 62447        }
 48        /// <inheritdoc/>
 171549        public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) {
 171550            yield return Name;
 171551        }
 52        #region Equality checks
 53        /// <inheritdoc/>
 33554        public override bool Equals(object? obj) => Equals(obj as VariableExpression);
 55        /// <inheritdoc/>
 33556        public bool Equals(VariableExpression? other) => !(other is null) && Name == other.Name;
 57        /// <inheritdoc/>
 858        public override int GetHashCode() => System.HashCode.Combine(Name);
 59        /// <inheritdoc/>
 060        public static bool operator ==(VariableExpression? left, VariableExpression? right) => EqualityComparer<Variable
 61        /// <inheritdoc/>
 062        public static bool operator !=(VariableExpression? left, VariableExpression? right) => !(left == right);
 63        #endregion
 64    }
 65}