Skip to content

Commit

Permalink
#175 - Support rational numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Aug 25, 2023
1 parent 520b9a2 commit a7cbd2a
Show file tree
Hide file tree
Showing 17 changed files with 977 additions and 4 deletions.
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ public virtual TResult Analyze(StringExpression exp, TContext context)
public virtual TResult Analyze(Expressions.Units.Convert exp, TContext context)
=> Analyze(exp as IExpression, context);

/// <inheritdoc />
public virtual TResult Analyze(Rational exp, TContext context)
=> Analyze(exp as IExpression, context);

/// <inheritdoc />
public virtual TResult Analyze(ToRational exp, TContext context)
=> Analyze(exp as IExpression, context);

#endregion Standard

#region Matrix
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ public virtual TResult Analyze(StringExpression exp)
public virtual TResult Analyze(Expressions.Units.Convert exp)
=> Analyze(exp as IExpression);

/// <inheritdoc />
public virtual TResult Analyze(Rational exp)
=> Analyze(exp as IExpression);

/// <inheritdoc />
public virtual TResult Analyze(ToRational exp)
=> Analyze(exp as IExpression);

#endregion Standard

#region Matrix
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ public virtual string Analyze(Expressions.Units.Convert exp)
return $"convert({value}, {unit})";
}

/// <inheritdoc />
public virtual string Analyze(Rational exp)
=> ToString(exp, "{0} : {1}");

Check warning on line 292 in xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs#L292

Added line #L292 was not covered by tests

/// <inheritdoc />
public virtual string Analyze(ToRational exp)
=> ToString(exp, "torational({0})");

Check warning on line 296 in xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs#L296

Added line #L296 was not covered by tests

#endregion Standard

#region Matrix
Expand Down
16 changes: 16 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,22 @@ public interface IAnalyzer<out TResult, in TContext>
/// <returns>The result of analysis.</returns>
TResult Analyze(Expressions.Units.Convert exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <param name="context">The context.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(Rational exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <param name="context">The context.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(ToRational exp, TContext context);

#endregion Standard

#region Matrix
Expand Down
14 changes: 14 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,20 @@ public interface IAnalyzer<out TResult>
/// <returns>The result of analysis.</returns>
TResult Analyze(Expressions.Units.Convert exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(Rational exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(ToRational exp);

#endregion Standard

#region Matrix
Expand Down
5 changes: 5 additions & 0 deletions xFunc.Maths/Analyzers/TypeAnalyzers/ResultTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ public enum ResultTypes
/// The expression returns a volume number.
/// </summary>
VolumeNumber = 1 << 15,

/// <summary>
/// The returns a rational number.
/// </summary>
RationalNumber = 1 << 16,
}
44 changes: 44 additions & 0 deletions xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,50 @@ ResultTypes.Undefined or
};
}

/// <inheritdoc />
public virtual ResultTypes Analyze(Rational exp)
{
if (exp is null)
throw new ArgumentNullException(nameof(exp));

var leftResult = exp.Left.Analyze(this);
var rightResult = exp.Right.Analyze(this);

Check warning on line 1402 in xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs#L1401-L1402

Added lines #L1401 - L1402 were not covered by tests

return (leftResult, rightResult) switch
{
(ResultTypes.Undefined, _) or (_, ResultTypes.Undefined)
=> ResultTypes.Undefined,

(ResultTypes.Number, ResultTypes.Number)
=> ResultTypes.RationalNumber,

(ResultTypes.Number, _)
=> ResultTypes.Number.ThrowForRight(rightResult),

(_, ResultTypes.Number)
=> ResultTypes.Number.ThrowForLeft(leftResult),

_ => throw new ParameterTypeMismatchException(),
};

Check warning on line 1419 in xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs#L1404-L1419

Added lines #L1404 - L1419 were not covered by tests
}

/// <inheritdoc />
public virtual ResultTypes Analyze(ToRational exp)
{
if (exp is null)
throw new ArgumentNullException(nameof(exp));

var argumentResult = exp.Argument.Analyze(this);

Check warning on line 1428 in xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs#L1428

Added line #L1428 was not covered by tests

return argumentResult switch
{
ResultTypes.Undefined => ResultTypes.Undefined,
ResultTypes.Number => ResultTypes.RationalNumber,

_ => throw new ParameterTypeMismatchException(ResultTypes.Number, argumentResult),
};

Check warning on line 1436 in xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs#L1430-L1436

Added lines #L1430 - L1436 were not covered by tests
}

#endregion Standard

#region Matrix
Expand Down
59 changes: 59 additions & 0 deletions xFunc.Maths/Expressions/Rational.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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>
/// Represents the rational number expression.
/// </summary>
public class Rational : BinaryExpression
{
/// <summary>
/// Initializes a new instance of the <see cref="Rational"/> class.
/// </summary>
/// <param name="left">The left (first) operand.</param>
/// <param name="right">The right (second) operand.</param>
public Rational(IExpression left, IExpression right)
: base(left, right)

Check warning on line 19 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L19

Added line #L19 was not covered by tests
{
}

Check warning on line 21 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L21

Added line #L21 was not covered by tests

/// <summary>
/// Initializes a new instance of the <see cref="Rational"/> class.
/// </summary>
/// <param name="arguments">The list of arguments.</param>
internal Rational(ImmutableArray<IExpression> arguments)
: base(arguments)

Check warning on line 28 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L28

Added line #L28 was not covered by tests
{
}

Check warning on line 30 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L30

Added line #L30 was not covered by tests

/// <inheritdoc />
public override object Execute(ExpressionParameters? parameters)
{
var leftResult = Left.Execute(parameters);
var rightResult = Right.Execute(parameters);

Check warning on line 36 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L35-L36

Added lines #L35 - L36 were not covered by tests

return (leftResult, rightResult) switch
{
(NumberValue left, NumberValue right) => new RationalValue(left, right),

_ => throw new ResultIsNotSupportedException(this, leftResult, rightResult),
};

Check warning on line 43 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L38-L43

Added lines #L38 - L43 were not covered by tests
}

/// <inheritdoc />
protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> analyzer)
=> analyzer.Analyze(this);

Check warning on line 48 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L48

Added line #L48 was not covered by tests

/// <inheritdoc />
protected override TResult AnalyzeInternal<TResult, TContext>(
IAnalyzer<TResult, TContext> analyzer,
TContext context)
=> analyzer.Analyze(this, context);

Check warning on line 54 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L54

Added line #L54 was not covered by tests

/// <inheritdoc />
public override IExpression Clone(IExpression? left = null, IExpression? right = null)
=> new Rational(left ?? Left, right ?? Right);

Check warning on line 58 in xFunc.Maths/Expressions/Rational.cs

View check run for this annotation

Codecov / codecov/patch

xFunc.Maths/Expressions/Rational.cs#L58

Added line #L58 was not covered by tests
}

0 comments on commit a7cbd2a

Please sign in to comment.