-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#628 - Create CallExpression / High-Order Functions.
- Loading branch information
Showing
98 changed files
with
1,702 additions
and
962 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright (c) Dmytro Kyshchenko. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Immutable; | ||
|
||
namespace xFunc.Maths.Expressions; | ||
|
||
/// <summary> | ||
/// Represent the expression to call function. | ||
/// </summary> | ||
/// <seealso cref="Function"/> | ||
public class CallExpression : IExpression, IEquatable<CallExpression> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CallExpression"/> class. | ||
/// </summary> | ||
/// <param name="function">The expression that returns function.</param> | ||
/// <param name="parameters">The list of parameters of the function.</param> | ||
public CallExpression(IExpression function, ImmutableArray<IExpression> parameters) | ||
{ | ||
Function = function; | ||
Parameters = parameters; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(CallExpression? other) | ||
{ | ||
if (ReferenceEquals(null, other)) | ||
return false; | ||
|
||
if (ReferenceEquals(this, other)) | ||
return true; | ||
|
||
return Function.Equals(other.Function) && Parameters.SequenceEqual(other.Parameters); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) | ||
return false; | ||
|
||
if (ReferenceEquals(this, obj)) | ||
return true; | ||
|
||
if (obj.GetType() != GetType()) | ||
return false; | ||
|
||
return Equals((CallExpression)obj); | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <exception cref="NotSupportedException">Always.</exception> | ||
public object Execute() | ||
=> throw new NotSupportedException(); | ||
|
||
/// <inheritdoc /> | ||
public object Execute(ExpressionParameters? parameters) | ||
{ | ||
if (parameters is null) | ||
{ | ||
throw new ArgumentNullException(nameof(parameters)); | ||
} | ||
|
||
if (Function.Execute(parameters) is not Lambda function) | ||
{ | ||
throw new ResultIsNotSupportedException(this, Function); | ||
} | ||
|
||
var nestedScope = parameters.CreateScope(); | ||
var zip = function.Parameters.Zip(Parameters, (parameter, expression) => (parameter, expression)); | ||
foreach (var (parameter, expression) in zip) | ||
{ | ||
nestedScope[parameter] = new ParameterValue(expression.Execute(nestedScope)); | ||
} | ||
|
||
var result = function.Call(nestedScope); | ||
|
||
return result; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string ToString(IFormatter formatter) | ||
=> formatter.Analyze(this); | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
=> ToString(CommonFormatter.Instance); | ||
|
||
/// <inheritdoc /> | ||
public TResult Analyze<TResult>(IAnalyzer<TResult> analyzer) | ||
{ | ||
if (analyzer is null) | ||
throw new ArgumentNullException(nameof(analyzer)); | ||
|
||
return analyzer.Analyze(this); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public TResult Analyze<TResult, TContext>( | ||
IAnalyzer<TResult, TContext> analyzer, | ||
TContext context) | ||
{ | ||
if (analyzer is null) | ||
throw new ArgumentNullException(nameof(analyzer)); | ||
|
||
return analyzer.Analyze(this, context); | ||
} | ||
|
||
/// <summary> | ||
/// Clones this instance of the <see cref="IExpression" />. | ||
/// </summary> | ||
/// <param name="function">The expression that returns function.</param> | ||
/// <param name="parameters">The list of parameters of the function.</param> | ||
/// <returns> | ||
/// Returns the new instance of <see cref="IExpression" /> that is a clone of this instance. | ||
/// </returns> | ||
public IExpression Clone(IExpression? function = null, ImmutableArray<IExpression>? parameters = null) | ||
=> new CallExpression(function ?? Function, parameters ?? Parameters); | ||
|
||
/// <summary> | ||
/// Gets the expression that returns the function. | ||
/// </summary> | ||
public IExpression Function { get; } | ||
|
||
/// <summary> | ||
/// Gets a list of parameters of the function. | ||
/// </summary> | ||
public ImmutableArray<IExpression> Parameters { get; } | ||
} |
Oops, something went wrong.