diff --git a/xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs b/xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs index f189f08ac..62e5fef61 100644 --- a/xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs +++ b/xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs @@ -36,7 +36,7 @@ namespace xFunc.Maths.Analyzers.TypeAnalyzers /// public class TypeAnalyzer : ITypeAnalyzer { - private ResultTypes CheckArgument([NotNull] IExpression exp, ResultTypes result) + private ResultTypes CheckArgument([NotNull] IExpression? exp, ResultTypes result) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -44,7 +44,7 @@ private ResultTypes CheckArgument([NotNull] IExpression exp, ResultTypes result) return result; } - private ResultTypes CheckNumericConversion([NotNull] UnaryExpression exp) + private ResultTypes CheckNumericConversion([NotNull] UnaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -58,7 +58,7 @@ private ResultTypes CheckNumericConversion([NotNull] UnaryExpression exp) }; } - private ResultTypes CheckTrigonometric([NotNull] UnaryExpression exp) + private ResultTypes CheckTrigonometric([NotNull] UnaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -74,7 +74,7 @@ private ResultTypes CheckTrigonometric([NotNull] UnaryExpression exp) }; } - private ResultTypes CheckInverseTrigonometric([NotNull] UnaryExpression exp) + private ResultTypes CheckInverseTrigonometric([NotNull] UnaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -90,7 +90,7 @@ private ResultTypes CheckInverseTrigonometric([NotNull] UnaryExpression exp) }; } - private ResultTypes CheckStatistical([NotNull] DifferentParametersExpression exp) + private ResultTypes CheckStatistical([NotNull] DifferentParametersExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -117,7 +117,7 @@ private ResultTypes CheckStatistical([NotNull] DifferentParametersExpression exp return ResultTypes.Number; } - private ResultTypes AnalyzeRelational([NotNull] BinaryExpression exp) + private ResultTypes AnalyzeRelational([NotNull] BinaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -128,15 +128,15 @@ private ResultTypes AnalyzeRelational([NotNull] BinaryExpression exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, ResultTypes.Undefined) or - (ResultTypes.Number, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.Number) or - (ResultTypes.AngleNumber, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.AngleNumber) or - (ResultTypes.PowerNumber, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.PowerNumber) or - (ResultTypes.Number, ResultTypes.Number) or - (ResultTypes.AngleNumber, ResultTypes.AngleNumber) or - (ResultTypes.PowerNumber, ResultTypes.PowerNumber) + (ResultTypes.Number, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.Number) or + (ResultTypes.AngleNumber, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.AngleNumber) or + (ResultTypes.PowerNumber, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.PowerNumber) or + (ResultTypes.Number, ResultTypes.Number) or + (ResultTypes.AngleNumber, ResultTypes.AngleNumber) or + (ResultTypes.PowerNumber, ResultTypes.PowerNumber) => ResultTypes.Boolean, (_, ResultTypes.Number) => ResultTypes.Number.ThrowForLeft(leftResult), @@ -152,7 +152,7 @@ private ResultTypes AnalyzeRelational([NotNull] BinaryExpression exp) }; } - private ResultTypes AnalyzeLogical([NotNull] BinaryExpression exp) + private ResultTypes AnalyzeLogical([NotNull] BinaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -163,9 +163,9 @@ private ResultTypes AnalyzeLogical([NotNull] BinaryExpression exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, ResultTypes.Undefined) or - (ResultTypes.Boolean, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.Boolean) or - (ResultTypes.Boolean, ResultTypes.Boolean) + (ResultTypes.Boolean, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.Boolean) or + (ResultTypes.Boolean, ResultTypes.Boolean) => ResultTypes.Boolean, (_, ResultTypes.Boolean) => ResultTypes.Boolean.ThrowForLeft(leftResult), @@ -175,7 +175,7 @@ private ResultTypes AnalyzeLogical([NotNull] BinaryExpression exp) }; } - private ResultTypes AnalyzeLogicalAndBitwise([NotNull] BinaryExpression exp) + private ResultTypes AnalyzeLogicalAndBitwise([NotNull] BinaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -186,7 +186,7 @@ private ResultTypes AnalyzeLogicalAndBitwise([NotNull] BinaryExpression exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, @@ -202,7 +202,7 @@ private ResultTypes AnalyzeLogicalAndBitwise([NotNull] BinaryExpression exp) }; } - private ResultTypes AnalyzeEquality([NotNull] BinaryExpression exp) + private ResultTypes AnalyzeEquality([NotNull] BinaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -213,18 +213,18 @@ private ResultTypes AnalyzeEquality([NotNull] BinaryExpression exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, ResultTypes.Undefined) or - (ResultTypes.Number, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.Number) or - (ResultTypes.Boolean, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.Boolean) or - (ResultTypes.AngleNumber, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.AngleNumber) or - (ResultTypes.PowerNumber, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.PowerNumber) or - (ResultTypes.Number, ResultTypes.Number) or - (ResultTypes.Boolean, ResultTypes.Boolean) or - (ResultTypes.AngleNumber, ResultTypes.AngleNumber) or - (ResultTypes.PowerNumber, ResultTypes.PowerNumber) + (ResultTypes.Number, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.Number) or + (ResultTypes.Boolean, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.Boolean) or + (ResultTypes.AngleNumber, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.AngleNumber) or + (ResultTypes.PowerNumber, ResultTypes.Undefined) or + (ResultTypes.Undefined, ResultTypes.PowerNumber) or + (ResultTypes.Number, ResultTypes.Number) or + (ResultTypes.Boolean, ResultTypes.Boolean) or + (ResultTypes.AngleNumber, ResultTypes.AngleNumber) or + (ResultTypes.PowerNumber, ResultTypes.PowerNumber) => ResultTypes.Boolean, (_, ResultTypes.Number) => ResultTypes.Number.ThrowForLeft(leftResult), @@ -243,7 +243,7 @@ private ResultTypes AnalyzeEquality([NotNull] BinaryExpression exp) }; } - private ResultTypes AnalyzeBinaryAssign([NotNull] VariableBinaryExpression exp) + private ResultTypes AnalyzeBinaryAssign([NotNull] VariableBinaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -259,7 +259,7 @@ ResultTypes.Undefined or ResultTypes.Number }; } - private ResultTypes AnalyzeShift([NotNull] BinaryExpression exp) + private ResultTypes AnalyzeShift([NotNull] BinaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -270,9 +270,9 @@ private ResultTypes AnalyzeShift([NotNull] BinaryExpression exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.Number) or - (ResultTypes.Number, ResultTypes.Undefined) or - (ResultTypes.Number, ResultTypes.Number) + (ResultTypes.Undefined, ResultTypes.Number) or + (ResultTypes.Number, ResultTypes.Undefined) or + (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, (ResultTypes.Number, _) => ResultTypes.Number.ThrowForRight(rightResult), @@ -281,7 +281,7 @@ private ResultTypes AnalyzeShift([NotNull] BinaryExpression exp) }; } - private ResultTypes AngleConversion([NotNull] UnaryExpression exp) + private ResultTypes AngleConversion([NotNull] UnaryExpression? exp) { if (exp is null) ArgNull(ExceptionArgument.exp); @@ -335,31 +335,31 @@ public virtual ResultTypes Analyze(Add exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, (ResultTypes.Number, ResultTypes.AngleNumber) or - (ResultTypes.AngleNumber, ResultTypes.Number) or - (ResultTypes.AngleNumber, ResultTypes.AngleNumber) + (ResultTypes.AngleNumber, ResultTypes.Number) or + (ResultTypes.AngleNumber, ResultTypes.AngleNumber) => ResultTypes.AngleNumber, (ResultTypes.Number, ResultTypes.PowerNumber) or - (ResultTypes.PowerNumber, ResultTypes.Number) or - (ResultTypes.PowerNumber, ResultTypes.PowerNumber) + (ResultTypes.PowerNumber, ResultTypes.Number) or + (ResultTypes.PowerNumber, ResultTypes.PowerNumber) => ResultTypes.PowerNumber, (ResultTypes.Number, ResultTypes.ComplexNumber) or - (ResultTypes.ComplexNumber, ResultTypes.Number) or - (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) + (ResultTypes.ComplexNumber, ResultTypes.Number) or + (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) => ResultTypes.ComplexNumber, (ResultTypes.Vector, ResultTypes.Vector) => ResultTypes.Vector, (ResultTypes.Matrix, ResultTypes.Matrix) => ResultTypes.Matrix, (ResultTypes.String, _) or - (_, ResultTypes.String) + (_, ResultTypes.String) => ResultTypes.String, (_, ResultTypes.Number) => ResultTypes.Number.ThrowForLeft(leftResult), @@ -434,24 +434,24 @@ public virtual ResultTypes Analyze(Div exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, (ResultTypes.Number, ResultTypes.AngleNumber) or - (ResultTypes.AngleNumber, ResultTypes.Number) or - (ResultTypes.AngleNumber, ResultTypes.AngleNumber) + (ResultTypes.AngleNumber, ResultTypes.Number) or + (ResultTypes.AngleNumber, ResultTypes.AngleNumber) => ResultTypes.AngleNumber, (ResultTypes.Number, ResultTypes.PowerNumber) or - (ResultTypes.PowerNumber, ResultTypes.Number) or - (ResultTypes.PowerNumber, ResultTypes.PowerNumber) + (ResultTypes.PowerNumber, ResultTypes.Number) or + (ResultTypes.PowerNumber, ResultTypes.PowerNumber) => ResultTypes.PowerNumber, (ResultTypes.Number, ResultTypes.ComplexNumber) or - (ResultTypes.ComplexNumber, ResultTypes.Number) or - (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) + (ResultTypes.ComplexNumber, ResultTypes.Number) or + (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) => ResultTypes.ComplexNumber, (_, ResultTypes.Number) => ResultTypes.Number.ThrowForLeft(leftResult), @@ -645,7 +645,7 @@ public virtual ResultTypes Analyze(Log exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, @@ -669,9 +669,9 @@ public virtual ResultTypes Analyze(Mod exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.Number) or - (ResultTypes.Number, ResultTypes.Undefined) or - (ResultTypes.Number, ResultTypes.Number) + (ResultTypes.Undefined, ResultTypes.Number) or + (ResultTypes.Number, ResultTypes.Undefined) or + (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, (ResultTypes.Undefined, _) => ResultTypes.Number.ThrowForRight(rightResult), @@ -695,36 +695,36 @@ public virtual ResultTypes Analyze(Mul exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, (ResultTypes.Number, ResultTypes.AngleNumber) or - (ResultTypes.AngleNumber, ResultTypes.Number) or - (ResultTypes.AngleNumber, ResultTypes.AngleNumber) + (ResultTypes.AngleNumber, ResultTypes.Number) or + (ResultTypes.AngleNumber, ResultTypes.AngleNumber) => ResultTypes.AngleNumber, (ResultTypes.Number, ResultTypes.PowerNumber) or - (ResultTypes.PowerNumber, ResultTypes.Number) or - (ResultTypes.PowerNumber, ResultTypes.PowerNumber) + (ResultTypes.PowerNumber, ResultTypes.Number) or + (ResultTypes.PowerNumber, ResultTypes.PowerNumber) => ResultTypes.PowerNumber, (ResultTypes.Number, ResultTypes.ComplexNumber) or - (ResultTypes.ComplexNumber, ResultTypes.Number) or - (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) + (ResultTypes.ComplexNumber, ResultTypes.Number) or + (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) => ResultTypes.ComplexNumber, (ResultTypes.Vector, ResultTypes.Number) or - (ResultTypes.Number, ResultTypes.Vector) or - (ResultTypes.Vector, ResultTypes.Vector) + (ResultTypes.Number, ResultTypes.Vector) or + (ResultTypes.Vector, ResultTypes.Vector) => ResultTypes.Vector, (ResultTypes.Matrix, ResultTypes.Number) or - (ResultTypes.Matrix, ResultTypes.Vector) or - (ResultTypes.Number, ResultTypes.Matrix) or - (ResultTypes.Vector, ResultTypes.Matrix) or - (ResultTypes.Matrix, ResultTypes.Matrix) + (ResultTypes.Matrix, ResultTypes.Vector) or + (ResultTypes.Number, ResultTypes.Matrix) or + (ResultTypes.Vector, ResultTypes.Matrix) or + (ResultTypes.Matrix, ResultTypes.Matrix) => ResultTypes.Matrix, (_, ResultTypes.Number) => ResultTypes.Number.ThrowForLeft(leftResult), @@ -778,8 +778,8 @@ public virtual ResultTypes Analyze(ToNumber exp) return result switch { ResultTypes.Undefined or - ResultTypes.AngleNumber or - ResultTypes.PowerNumber + ResultTypes.AngleNumber or + ResultTypes.PowerNumber => ResultTypes.Number, _ => ResultTypes.AngleNumber.ThrowFor(result), }; @@ -797,12 +797,12 @@ public virtual ResultTypes Analyze(Pow exp) return (leftResult, rightResult) switch { (ResultTypes.ComplexNumber, ResultTypes.Undefined) or - (ResultTypes.ComplexNumber, ResultTypes.Number) or - (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) + (ResultTypes.ComplexNumber, ResultTypes.Number) or + (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) => ResultTypes.ComplexNumber, (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Number, ResultTypes.Number) @@ -824,9 +824,9 @@ public virtual ResultTypes Analyze(Root exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, ResultTypes.Undefined) or - (ResultTypes.Undefined, ResultTypes.Number) or - (ResultTypes.Number, ResultTypes.Undefined) or - (ResultTypes.Number, ResultTypes.Number) + (ResultTypes.Undefined, ResultTypes.Number) or + (ResultTypes.Number, ResultTypes.Undefined) or + (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Undefined, (ResultTypes.Undefined, _) => ResultTypes.Number.ThrowForRight(rightResult), @@ -875,24 +875,24 @@ public virtual ResultTypes Analyze(Sub exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Number, ResultTypes.Number) => ResultTypes.Number, (ResultTypes.Number, ResultTypes.AngleNumber) or - (ResultTypes.AngleNumber, ResultTypes.Number) or - (ResultTypes.AngleNumber, ResultTypes.AngleNumber) + (ResultTypes.AngleNumber, ResultTypes.Number) or + (ResultTypes.AngleNumber, ResultTypes.AngleNumber) => ResultTypes.AngleNumber, (ResultTypes.Number, ResultTypes.PowerNumber) or - (ResultTypes.PowerNumber, ResultTypes.Number) or - (ResultTypes.PowerNumber, ResultTypes.PowerNumber) + (ResultTypes.PowerNumber, ResultTypes.Number) or + (ResultTypes.PowerNumber, ResultTypes.PowerNumber) => ResultTypes.PowerNumber, (ResultTypes.Number, ResultTypes.ComplexNumber) or - (ResultTypes.ComplexNumber, ResultTypes.Number) or - (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) + (ResultTypes.ComplexNumber, ResultTypes.Number) or + (ResultTypes.ComplexNumber, ResultTypes.ComplexNumber) => ResultTypes.ComplexNumber, (ResultTypes.Vector, ResultTypes.Vector) => ResultTypes.Vector, @@ -993,6 +993,7 @@ public virtual ResultTypes Analyze(Convert exp) return valueResult switch { + ResultTypes.Undefined or ResultTypes.Number => ResultTypes.Undefined, ResultTypes.AngleNumber => ResultTypes.AngleNumber, ResultTypes.PowerNumber => ResultTypes.PowerNumber, @@ -1093,7 +1094,7 @@ public virtual ResultTypes Analyze(DotProduct exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Vector, ResultTypes.Vector) => ResultTypes.Number, @@ -1116,7 +1117,7 @@ public virtual ResultTypes Analyze(CrossProduct exp) return (leftResult, rightResult) switch { (ResultTypes.Undefined, _) or - (_, ResultTypes.Undefined) + (_, ResultTypes.Undefined) => ResultTypes.Undefined, (ResultTypes.Vector, ResultTypes.Vector) => ResultTypes.Vector, diff --git a/xFunc.Tests/Analyzers/TypeAnalyzerTests/StandardTests.cs b/xFunc.Tests/Analyzers/TypeAnalyzerTests/StandardTests.cs index 9e440b704..cb39c8f74 100644 --- a/xFunc.Tests/Analyzers/TypeAnalyzerTests/StandardTests.cs +++ b/xFunc.Tests/Analyzers/TypeAnalyzerTests/StandardTests.cs @@ -856,6 +856,18 @@ public void TestStringExpression() Test(exp, ResultTypes.String); } + [Fact] + public void TestConvertVariable() + { + var exp = new Convert( + new Converter(), + new Variable("x"), + new StringExpression("rad") + ); + + Test(exp, ResultTypes.Undefined); + } + [Fact] public void TestConvert() {