Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Jun 9, 2017
1 parent b6bf422 commit 03aaf17
Showing 1 changed file with 48 additions and 32 deletions.
80 changes: 48 additions & 32 deletions xFunc.Maths/Analyzers/TypeAnalyzer.cs
Expand Up @@ -27,7 +27,6 @@ namespace xFunc.Maths.Analyzers
{

// todo: exceptions!!!
// todo: define/undefine!!!

/// <summary>
/// Type Analyzer checks the expression tree for argument type and result type. If result type is Undefined, then Type Analyzer cannot determine the right type and bypass current expression.
Expand Down Expand Up @@ -123,6 +122,7 @@ public virtual ResultType Analyze(Add exp)
if (leftResult == ResultType.Vector && rightResult == ResultType.Vector)
return ResultType.Vector;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand All @@ -140,7 +140,7 @@ public virtual ResultType Analyze(Ceil exp)
if (result == ResultType.Number)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number, result);
}

/// <summary>
Expand All @@ -150,6 +150,7 @@ public virtual ResultType Analyze(Ceil exp)
/// <returns>The result of analysis.</returns>
public virtual ResultType Analyze(Define exp)
{
// TODO: !!!
return ResultType.Undefined;
}

Expand All @@ -160,6 +161,7 @@ public virtual ResultType Analyze(Define exp)
/// <returns>The result of analysis.</returns>
public virtual ResultType Analyze(Del exp)
{
// TODO: !!!
return ResultType.Vector;
}

Expand All @@ -179,6 +181,7 @@ public virtual ResultType Analyze(Derivative exp)
if (exp.ParametersCount == 3 && exp.Arguments[1] is Variable && exp.Arguments[2] is Number)
return ResultType.Number;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand All @@ -203,6 +206,7 @@ public virtual ResultType Analyze(Div exp)
if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand All @@ -223,7 +227,7 @@ public virtual ResultType Analyze(Exp exp)
if (result == ResultType.ComplexNumber)
return ResultType.ComplexNumber;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number | ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -240,7 +244,7 @@ public virtual ResultType Analyze(Fact exp)
if (result == ResultType.Number)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number, result);
}

/// <summary>
Expand All @@ -257,7 +261,7 @@ public virtual ResultType Analyze(Floor exp)
if (result == ResultType.Number)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number, result);
}

/// <summary>
Expand All @@ -271,10 +275,11 @@ public virtual ResultType Analyze(GCD exp)
if (results == null || results.Contains(ResultType.Undefined))
return ResultType.Undefined;

if (results.All(x => x == ResultType.Number))
return ResultType.Number;
for (var i = 0; i < results.Count; i++)
if (results[i] != ResultType.Number)
throw new DifferentParameterTypeMismatchException(ResultType.Number, results[i], i);

throw new ParameterTypeMismatchException();
return ResultType.Number;
}

/// <summary>
Expand All @@ -291,7 +296,7 @@ public virtual ResultType Analyze(Lb exp)
if (result == ResultType.Number)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number, result);
}

/// <summary>
Expand All @@ -305,10 +310,11 @@ public virtual ResultType Analyze(LCM exp)
if (results == null || results.Contains(ResultType.Undefined))
return ResultType.Undefined;

if (results.All(x => x == ResultType.Number))
return ResultType.Number;
for (var i = 0; i < results.Count; i++)
if (results[i] != ResultType.Number)
throw new DifferentParameterTypeMismatchException(ResultType.Number, results[i], i);

throw new ParameterTypeMismatchException();
return ResultType.Number;
}

/// <summary>
Expand All @@ -328,7 +334,7 @@ public virtual ResultType Analyze(Lg exp)
if (result == ResultType.ComplexNumber)
return ResultType.ComplexNumber;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number | ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -348,7 +354,7 @@ public virtual ResultType Analyze(Ln exp)
if (result == ResultType.ComplexNumber)
return ResultType.ComplexNumber;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number | ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -369,6 +375,7 @@ public virtual ResultType Analyze(Log exp)
if (leftResult == ResultType.Number && rightResult == ResultType.ComplexNumber)
return ResultType.ComplexNumber;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand All @@ -387,6 +394,7 @@ public virtual ResultType Analyze(Mod exp)
if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand Down Expand Up @@ -423,6 +431,7 @@ public virtual ResultType Analyze(Mul exp)
if (leftResult == ResultType.Vector || (leftResult == ResultType.Number || leftResult == ResultType.Matrix || leftResult == ResultType.Vector))
return ResultType.Vector;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand Down Expand Up @@ -455,6 +464,7 @@ public virtual ResultType Analyze(Pow exp)
(rightResult == ResultType.Number || rightResult == ResultType.ComplexNumber))
return ResultType.ComplexNumber;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand All @@ -473,6 +483,7 @@ public virtual ResultType Analyze(Root exp)
if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand All @@ -487,10 +498,11 @@ public virtual ResultType Analyze(Round exp)
if (results.Contains(ResultType.Undefined))
return ResultType.Undefined;

if (results.All(x => x == ResultType.Number))
return ResultType.Number;
for (var i = 0; i < results.Count; i++)
if (results[i] != ResultType.Number)
throw new DifferentParameterTypeMismatchException(ResultType.Number, results[i], i);

throw new ParameterTypeMismatchException();
return ResultType.Number;
}

/// <summary>
Expand Down Expand Up @@ -540,6 +552,7 @@ public virtual ResultType Analyze(Sub exp)
if (leftResult == ResultType.Vector && rightResult == ResultType.Vector)
return ResultType.Vector;

// TODO: !!!
throw new ParameterTypeMismatchException();
}

Expand All @@ -560,7 +573,7 @@ public virtual ResultType Analyze(UnaryMinus exp)
if (result == ResultType.ComplexNumber)
return ResultType.ComplexNumber;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number | ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -570,6 +583,7 @@ public virtual ResultType Analyze(UnaryMinus exp)
/// <returns>The result of analysis.</returns>
public virtual ResultType Analyze(Undefine exp)
{
// TODO: !!!
return ResultType.Undefined;
}

Expand Down Expand Up @@ -618,10 +632,11 @@ public virtual ResultType Analyze(Vector exp)
if (results == null || results.Contains(ResultType.Undefined))
return ResultType.Undefined;

if (results.All(x => x == ResultType.Number))
return ResultType.Vector;
for (var i = 0; i < results.Count; i++)
if (results[i] != ResultType.Number)
throw new DifferentParameterTypeMismatchException(ResultType.Number, results[i], i);

throw new ParameterTypeMismatchException();
return ResultType.Vector;
}

/// <summary>
Expand All @@ -635,10 +650,11 @@ public virtual ResultType Analyze(Matrix exp)
if (results == null || results.Contains(ResultType.Undefined))
return ResultType.Undefined;

if (results.All(x => x == ResultType.Vector))
return ResultType.Matrix;
for (var i = 0; i < results.Count; i++)
if (results[i] != ResultType.Number)
throw new DifferentParameterTypeMismatchException(ResultType.Vector, results[i], i);

throw new ParameterTypeMismatchException();
return ResultType.Matrix;
}

/// <summary>
Expand All @@ -655,7 +671,7 @@ public virtual ResultType Analyze(Determinant exp)
if (result == ResultType.Matrix)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Matrix, result);
}

/// <summary>
Expand All @@ -672,7 +688,7 @@ public virtual ResultType Analyze(Inverse exp)
if (result == ResultType.Matrix)
return ResultType.Matrix;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Matrix, result);
}

/// <summary>
Expand All @@ -689,7 +705,7 @@ public virtual ResultType Analyze(Transpose exp)
if (result == ResultType.Vector || result == ResultType.Matrix)
return ResultType.Matrix;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Vector | ResultType.Matrix, result);
}

#endregion Matrix
Expand Down Expand Up @@ -720,7 +736,7 @@ public virtual ResultType Analyze(Conjugate exp)
if (result == ResultType.ComplexNumber)
return ResultType.ComplexNumber;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -737,7 +753,7 @@ public virtual ResultType Analyze(Im exp)
if (result == ResultType.ComplexNumber)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -754,7 +770,7 @@ public virtual ResultType Analyze(Phase exp)
if (result == ResultType.ComplexNumber)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -771,7 +787,7 @@ public virtual ResultType Analyze(Re exp)
if (result == ResultType.ComplexNumber)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.ComplexNumber, result);
}

/// <summary>
Expand All @@ -788,7 +804,7 @@ public virtual ResultType Analyze(Reciprocal exp)
if (result == ResultType.ComplexNumber)
return ResultType.ComplexNumber;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.ComplexNumber, result);
}

#endregion Complex Numbers
Expand Down

0 comments on commit 03aaf17

Please sign in to comment.