Skip to content

Commit

Permalink
Fix #211 - Extract properties from IExpression to separate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Jan 20, 2019
1 parent da59693 commit 9c24961
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 182 deletions.
24 changes: 0 additions & 24 deletions xFunc.Maths/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,30 +912,6 @@ public IExpression Parent
}
}

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
public int MinParameters => current.MinParameters;

/// <summary>
/// Gets the maximum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
public int MaxParameters => current.MaxParameters;

/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
public int ParametersCount => current.ParametersCount;

#endregion

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/BinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace xFunc.Maths.Expressions
/// <summary>
/// The base class for binary operations.
/// </summary>
public abstract class BinaryExpression : IExpression
public abstract class BinaryExpression : IFunctionExpression
{

/// <summary>
Expand Down
24 changes: 0 additions & 24 deletions xFunc.Maths/Expressions/ComplexNumbers/ComplexNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,6 @@ public IExpression Clone()
/// </summary>
public IExpression Parent { get; set; }

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
public int MinParameters => 0;

/// <summary>
/// Gets the maximum count of parameters.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
public int MaxParameters => -1;

/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
public int ParametersCount => 0;

}

}
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Define.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace xFunc.Maths.Expressions
/// <summary>
/// Represents the Define operation.
/// </summary>
public class Define : IExpression
public class Define : IFunctionExpression
{

private IExpression key;
Expand Down
24 changes: 0 additions & 24 deletions xFunc.Maths/Expressions/DelegateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,6 @@ public IExpression Clone()
/// </summary>
public IExpression Parent { get; set; }

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
public int MinParameters => 1;

/// <summary>
/// Gets the maximum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
public int MaxParameters => 1;

/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
public int ParametersCount => 1;

}

}
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/DifferentParametersExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace xFunc.Maths.Expressions
/// <summary>
/// The base class for expressions with different number of parameters.
/// </summary>
public abstract class DifferentParametersExpression : IExpression
public abstract class DifferentParametersExpression : IFunctionExpression
{

/// <summary>
Expand Down
22 changes: 0 additions & 22 deletions xFunc.Maths/Expressions/IExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,6 @@ public interface IExpression
/// </summary>
IExpression Parent { get; set; }

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
int MinParameters { get; }
/// <summary>
/// Gets the maximum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
int MaxParameters { get; }
/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
int ParametersCount { get; }

}

}
52 changes: 52 additions & 0 deletions xFunc.Maths/Expressions/IFunctionExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2012-2018 Dmitry Kischenko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using xFunc.Maths.Analyzers;
using xFunc.Maths.Analyzers.Formatters;

namespace xFunc.Maths.Expressions
{

/// <summary>
/// Defines methods to work with expressions.
/// </summary>
public interface IFunctionExpression : IExpression
{

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
int MinParameters { get; }
/// <summary>
/// Gets the maximum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
int MaxParameters { get; }
/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
int ParametersCount { get; }

}

}
24 changes: 0 additions & 24 deletions xFunc.Maths/Expressions/LogicalAndBitwise/Bool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,6 @@ public IExpression Clone()
/// </summary>
public IExpression Parent { get; set; }

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
public int MinParameters => 0;

/// <summary>
/// Gets the maximum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
public int MaxParameters => -1;

/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
public int ParametersCount => 0;

/// <summary>
/// Gets the value of this expression.
/// </summary>
Expand Down
27 changes: 23 additions & 4 deletions xFunc.Maths/Expressions/Matrices/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,18 @@ public override IExpression[] Arguments
{
if (value != null && value.Length > 0)
{
var size = value[0].ParametersCount;

if (value.Any(exp => exp.ParametersCount != size))
var vectors = new Vector[value.Length];
for (var i = 0; i < vectors.Length; i++)
{
var vector = value[i] as Vector;
if (vector == null)
throw new MatrixIsInvalidException();

vectors[i] = vector;
}

var size = vectors[0].ParametersCount;
if (vectors.Any(exp => exp.ParametersCount != size))
throw new MatrixIsInvalidException();
}

Expand All @@ -253,7 +262,17 @@ public override IExpression[] Arguments
/// <value>
/// The size of vectors.
/// </value>
public int SizeOfVectors => m_arguments[0].ParametersCount;
public int SizeOfVectors
{
get
{
var value = m_arguments[0] as Vector;
if (value == null)
throw new MatrixIsInvalidException();

return value.ParametersCount;
}
}

/// <summary>
/// Gets the minimum count of parameters.
Expand Down
24 changes: 0 additions & 24 deletions xFunc.Maths/Expressions/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,30 +154,6 @@ public IExpression Clone()
/// </summary>
public IExpression Parent { get; set; }

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
public int MinParameters => 0;

/// <summary>
/// Gets the maximum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
public int MaxParameters => -1;

/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
public int ParametersCount => 0;

}

}
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/UnaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace xFunc.Maths.Expressions
/// <summary>
/// The abstract base class that represents the unary operation.
/// </summary>
public abstract class UnaryExpression : IExpression
public abstract class UnaryExpression : IFunctionExpression
{

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Undefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace xFunc.Maths.Expressions
/// <summary>
/// Represents the Undefice operation.
/// </summary>
public class Undefine : IExpression
public class Undefine : IFunctionExpression
{

private IExpression key;
Expand Down
24 changes: 0 additions & 24 deletions xFunc.Maths/Expressions/Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,6 @@ public IExpression Clone()
/// </summary>
public IExpression Parent { get; set; }

/// <summary>
/// Gets the minimum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The minimum count of parameters.
/// </value>
public int MinParameters => 0;

/// <summary>
/// Gets the maximum count of parameters. -1 - Infinity.
/// </summary>
/// <value>
/// The maximum count of parameters.
/// </value>
public int MaxParameters => -1;

/// <summary>
/// Gets the count of parameters.
/// </summary>
/// <value>
/// The count of parameters.
/// </value>
public int ParametersCount => 0;

}

}
6 changes: 3 additions & 3 deletions xFunc.Maths/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ private IEnumerable<IExpression> ConvertTokensToExpressions(IEnumerable<IToken>
if (exp == null)
throw new ParserException(Resource.ErrorWhileParsingTree);

if (token is FunctionToken t)
if (token is FunctionToken t && exp is IFunctionExpression funcExp)
{
if (t.CountOfParams < exp.MinParameters)
if (t.CountOfParams < funcExp.MinParameters)
throw new ParserException(Resource.LessParams);
if (exp.MaxParameters != -1 && t.CountOfParams > exp.MaxParameters)
if (funcExp.MaxParameters != -1 && t.CountOfParams > funcExp.MaxParameters)
throw new ParserException(Resource.MoreParams);
}

Expand Down
6 changes: 2 additions & 4 deletions xFunc.Tests/Analyzers/TypeAnalyzerTests/MatrixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ public void TestMatrixUndefinedElement()
[Fact]
public void TestMatrixNotVectorElement()
{
DifferentParametersExpression exp = new Matrix(2, 2)
Assert.Throws<MatrixIsInvalidException>(() => new Matrix(2, 2)
{
Arguments = new[] { new Number(2) }
};

TestDiffParamException(exp);
});
}

[Fact]
Expand Down

0 comments on commit 9c24961

Please sign in to comment.