Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bykiev committed Jul 22, 2024
2 parents 4e744f1 + 5d6e7df commit 974a0ef
Show file tree
Hide file tree
Showing 62 changed files with 292 additions and 311 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ root = true

# 4 space indentation
[*.cs]
csharp_indent_case_contents_when_block = false
dotnet_sort_system_directives_first = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
42 changes: 19 additions & 23 deletions src/NCalc.Async/AsyncExpression.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NCalc.Cache;
using System.Diagnostics.CodeAnalysis;
using NCalc.Cache;
using NCalc.Domain;
using NCalc.Exceptions;
using NCalc.Factories;
using NCalc.Visitors;
using System.Diagnostics.CodeAnalysis;
using NCalc.Handlers;
using NCalc.Helpers;
using NCalc.Services;
using NCalc.Visitors;

namespace NCalc;

Expand All @@ -22,7 +22,7 @@ public class AsyncExpression
/// Default Value: True
/// </summary>
public static bool CacheEnabled { get; set; } = true;

/// <summary>
/// Options for the expression evaluation.
/// </summary>
Expand All @@ -40,7 +40,7 @@ public CultureInfo CultureInfo
get => Context.CultureInfo;
set => Context.CultureInfo = value;
}

protected AsyncExpressionContext Context { get; init; }

/// <summary>
Expand All @@ -52,20 +52,18 @@ public CultureInfo CultureInfo
set => Context.StaticParameters = value;
}


public IDictionary<string, AsyncExpressionParameter> DynamicParameters
{
get => Context.DynamicParameters;
set => Context.DynamicParameters = value;
}

public IDictionary<string, AsyncExpressionFunction> Functions
{
get => Context.Functions;
set => Context.Functions = value;
}



/// <summary>
/// Event triggered to handle function evaluation.
/// </summary>
Expand All @@ -74,7 +72,7 @@ public event AsyncEvaluateFunctionHandler EvaluateFunctionAsync
add => Context.AsyncEvaluateFunctionHandler += value;
remove => Context.AsyncEvaluateFunctionHandler -= value;
}

/// <summary>
/// Event triggered to handle parameter evaluation.
/// </summary>
Expand All @@ -83,7 +81,7 @@ public event AsyncEvaluateParameterHandler EvaluateParameterAsync
add => Context.AsyncEvaluateParameterHandler += value;
remove => Context.AsyncEvaluateParameterHandler -= value;
}

/// <summary>
/// Textual representation of the expression.
/// </summary>
Expand All @@ -93,13 +91,12 @@ public event AsyncEvaluateParameterHandler EvaluateParameterAsync

public Exception? Error { get; private set; }

protected ILogicalExpressionCache LogicalExpressionCache { get;}
protected ILogicalExpressionCache LogicalExpressionCache { get; }

protected ILogicalExpressionFactory LogicalExpressionFactory { get; }

protected IAsyncEvaluationService EvaluationService { get; }



private AsyncExpression(AsyncExpressionContext? context = null)
{
LogicalExpressionCache = Cache.LogicalExpressionCache.GetInstance();
Expand All @@ -121,7 +118,7 @@ public AsyncExpression(
EvaluationService = evaluationService;
LogicalExpressionFactory = factory;
}

public AsyncExpression(
LogicalExpression logicalExpression,
AsyncExpressionContext context,
Expand All @@ -135,13 +132,12 @@ public AsyncExpression(
EvaluationService = evaluationService;
LogicalExpressionFactory = factory;
}

public AsyncExpression(string expression, AsyncExpressionContext? context = null) : this(context)
{
ExpressionString = expression;
}


// ReSharper disable once RedundantOverload.Global
// Reason: False positive, ExpressionContext have implicit conversions.
public AsyncExpression(string expression) : this(expression, ExpressionOptions.None)
Expand Down Expand Up @@ -169,7 +165,7 @@ public AsyncExpression(LogicalExpression logicalExpression, ExpressionOptions op
CultureInfo? cultureInfo = null) : this(logicalExpression, new AsyncExpressionContext(options, cultureInfo))
{
}

private LogicalExpression? GetLogicalExpression()
{
if (string.IsNullOrEmpty(ExpressionString))
Expand All @@ -195,7 +191,7 @@ public AsyncExpression(LogicalExpression logicalExpression, ExpressionOptions op

return logicalExpression;
}

/// <summary>
/// Create the LogicalExpression in order to check syntax errors.
/// If errors are detected, the Error property contains the exception.
Expand All @@ -217,7 +213,7 @@ public bool HasErrors()
return true;
}
}


/// <summary>
/// Asynchronously evaluates the logical expression.
Expand All @@ -240,7 +236,7 @@ public bool HasErrors()

return await EvaluationService.EvaluateAsync(LogicalExpression!, Context);
}

private async Task<List<object?>> IterateParametersAsync()
{
var parameterEnumerators = ParametersHelper.GetEnumerators(Parameters, out var size);
Expand All @@ -260,7 +256,7 @@ public bool HasErrors()

return results;
}

/// <summary>
/// Returns a list with all parameter names from the expression.
/// </summary>
Expand All @@ -276,7 +272,7 @@ public List<string> GetParametersNames()
{
return GetParameterNames();
}

/// <summary>
/// Returns a list with all function names from the expression.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/NCalc.Async/AsyncExpressionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public record AsyncExpressionContext : ExpressionContextBase
public IDictionary<string, AsyncExpressionFunction> Functions { get; set; } =
new Dictionary<string, AsyncExpressionFunction>();


public AsyncEvaluateParameterHandler? AsyncEvaluateParameterHandler { get; set; }
public AsyncEvaluateFunctionHandler? AsyncEvaluateFunctionHandler { get; set; }

public AsyncExpressionContext()
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/NCalc.Async/AsyncExpressionFunctionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ public class AsyncExpressionFunctionData(Guid id, AsyncExpression[] arguments, A
public Guid Id { get; } = id;
private AsyncExpression[] Arguments { get; } = arguments;
public AsyncExpressionContext Context { get; } = context;

public AsyncExpression this[int index]
{
get => Arguments[index];
set => Arguments[index] = value;
}

public IEnumerator<AsyncExpression> GetEnumerator()
{
return ((IEnumerable<AsyncExpression>)Arguments).GetEnumerator();
Expand Down
10 changes: 5 additions & 5 deletions src/NCalc.Async/Handlers/AsyncFunctionArgs.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace NCalc.Handlers;

public class AsyncFunctionArgs(Guid id,AsyncExpression[] parameters) : EventArgs
public class AsyncFunctionArgs(Guid id, AsyncExpression[] parameters) : EventArgs
{
public Guid Id { get; } = id;

private object? _result;

public object? Result
Expand All @@ -15,10 +15,10 @@ public object? Result
HasResult = true;
}
}

public AsyncExpression[] Parameters { get; } = parameters;
public bool HasResult { get; private set; }

public bool HasResult { get; private set; }

public async Task<object?[]> EvaluateParametersAsync()
{
Expand Down
2 changes: 1 addition & 1 deletion src/NCalc.Async/Handlers/AsyncParameterArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace NCalc.Handlers;
public class AsyncParameterArgs(Guid id) : EventArgs
{
public Guid Id { get; } = id;

private object? _result;
public object? Result
{
Expand Down
5 changes: 2 additions & 3 deletions src/NCalc.Async/Helpers/AsyncBuiltInFunctionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace NCalc.Helpers;


public static class AsyncBuiltInFunctionHelper
{
public static async Task<object?> EvaluateAsync(string functionName, AsyncExpression[] arguments, AsyncExpressionContext context)
{
var caseSensitive = !context.Options.HasFlag(ExpressionOptions.IgnoreCaseAtBuiltInFunctions);
var comparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
var caseInsensitive = context.Options.HasFlag(ExpressionOptions.IgnoreCaseAtBuiltInFunctions);
var comparison = caseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

if (functionName.Equals("Abs", comparison))
{
Expand Down
1 change: 0 additions & 1 deletion src/NCalc.Async/Services/AsyncEvaluationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace NCalc.Services;
/// <inheritdoc cref="IAsyncEvaluationService"/>
public class AsyncEvaluationService : IAsyncEvaluationService
{

public Task<object?> EvaluateAsync(LogicalExpression expression, AsyncExpressionContext context)
{
var visitor = new AsyncEvaluationVisitor(context);
Expand Down
1 change: 0 additions & 1 deletion src/NCalc.Async/Services/IAsyncEvaluationService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NCalc.Domain;
using NCalc.Handlers;

namespace NCalc.Services;

Expand Down
6 changes: 4 additions & 2 deletions src/NCalc.Async/Visitors/AsyncEvaluationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ExtendedNumerics;
using System.Numerics;
using System.Numerics;
using System.Threading;
using ExtendedNumerics;
using NCalc.Domain;
using NCalc.Exceptions;
using NCalc.Handlers;
Expand Down Expand Up @@ -121,12 +121,14 @@ public class AsyncEvaluationVisitor(AsyncExpressionContext context) : ILogicalEx
return Math.Pow(Convert.ToDouble(await left.Value, context.CultureInfo),
Convert.ToDouble(await right.Value, context.CultureInfo));
}

case BinaryExpressionType.In:
{
var rightValue = await right.Value;
var leftValue = await left.Value;
return EvaluationHelper.In(rightValue, leftValue, context);
}

case BinaryExpressionType.NotIn:
{
var rightValue = await right.Value;
Expand Down
2 changes: 1 addition & 1 deletion src/NCalc.Core/Domain/BinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public sealed class BinaryExpression(

public override T Accept<T>(ILogicalExpressionVisitor<T> visitor)
{
return visitor.Visit(this);
return visitor.Visit(this);
}
}
2 changes: 1 addition & 1 deletion src/NCalc.Core/Domain/ValueExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ValueExpression(string value)
Value = value;
Type = ValueType.String;
}

public ValueExpression(char value)
{
Value = value;
Expand Down
8 changes: 4 additions & 4 deletions src/NCalc.Core/ExpressionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public enum ExpressionOptions
/// Specifies that no options are set.
/// </summary>
None = 1 << 0,

[Obsolete("Please use IgnoreCaseAtBuiltInFunctions")]
IgnoreCase = 1 << 1,

/// <summary>
/// Specifies case-insensitive matching for built-in functions
/// </summary>
Expand All @@ -33,7 +33,7 @@ public enum ExpressionOptions
/// When using Round(), if a number is halfway between two others, it is rounded toward the nearest number that is away from zero.
/// </summary>
RoundAwayFromZero = 1 << 4,

/// <summary>
/// Specifies the use of CaseInsensitiveComparer for comparisons.
/// </summary>
Expand Down Expand Up @@ -68,7 +68,7 @@ public enum ExpressionOptions
/// Concat values as strings instead of arithmetic addition
/// </summary>
StringConcat = 1 << 11,

/// <summary>
/// Parse single quoted strings as <see cref="char"/>
/// </summary>
Expand Down
7 changes: 3 additions & 4 deletions src/NCalc.Core/Factories/LogicalExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ LogicalExpression ILogicalExpressionFactory.Create(string expression, Expression

public static LogicalExpression Create(string expression, ExpressionOptions options = ExpressionOptions.None)
{
LogicalExpression? logicalExpression;
try
{
var parserContext = new LogicalExpressionParserContext(expression, options);
logicalExpression = LogicalExpressionParser.Parse(parserContext);
var logicalExpression = LogicalExpressionParser.Parse(parserContext);

if (logicalExpression is null)
throw new ArgumentNullException(nameof(logicalExpression));

return logicalExpression;
}
catch (Exception exception)
{
throw new NCalcParserException("Error parsing the expression.", exception);
}

return logicalExpression;
}
}
4 changes: 2 additions & 2 deletions src/NCalc.Core/Helpers/EvaluationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class EvaluationHelper
return string.Concat(leftValue, rightValue);
}
}

/// <summary>
/// Determines if the left value is contained within the right value, which must be either an enumerable or a string.
/// </summary>
Expand Down Expand Up @@ -59,7 +59,7 @@ public static bool In(object? rightValue, object? leftValue, ExpressionContextBa
"'in' operator right value must implement IEnumerable or be a string.");
}
}

/// <summary>
/// Evaluates a unary expression.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/NCalc.Core/Helpers/MathHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ExtendedNumerics;
using System.Numerics;
using System.Numerics;
using ExtendedNumerics;

namespace NCalc.Helpers;

Expand Down
Loading

0 comments on commit 974a0ef

Please sign in to comment.