/// <summary> /// Evaluates the operator. /// </summary> /// <returns>The operator.</returns> /// <param name="signature">Signature.</param> /// <param name="lhs">Lhs.</param> /// <param name="rhs">Rhs.</param> new public static GeneticBool EvaluateOperator(OperatorSignature signature, GeneticObject lhs, GeneticObject rhs) { GeneticObject.ValidateOperatorCall(signature, typeof(GeneticBool), lhs, rhs); switch (signature.Value) { case "<": return(GeneticBool.OperatorLessThan((GeneticInt)lhs, (GeneticInt)rhs)); case ">": return(GeneticBool.OperatorGreaterThan((GeneticInt)lhs, (GeneticInt)rhs)); case "==": return(GeneticBool.OperatorEquals(lhs, rhs)); case "!=": return(GeneticBool.OperatorNotEquals(lhs, rhs)); case "&&": return(GeneticBool.OperatorAnd((GeneticBool)lhs, (GeneticBool)rhs)); case "||": return(GeneticBool.OperatorAnd((GeneticBool)lhs, (GeneticBool)rhs)); } throw new InvalidOperationException(string.Format( "Did not recognize operator with signature {0} with return type {1}", signature.Value, signature.ReturnType)); }
protected static void ValidateOperatorCall( OperatorSignature signature, Type expectedReturnType, GeneticObject lhs, GeneticObject rhs) { if (signature.ReturnType != expectedReturnType) { throw new InvalidOperationException(string.Format( "Expecting type {0} got type {1}", expectedReturnType, signature.ReturnType)); } }
private static GeneticBool OperatorNotEquals(GeneticObject lhs, GeneticObject rhs) { if (lhs is GeneticInt && rhs is GeneticInt) { return(GeneticBool.OperatorNotEquals((GeneticInt)lhs, (GeneticInt)rhs)); } if (lhs is GeneticBool && rhs is GeneticBool) { return(GeneticBool.OperatorNotEquals((GeneticBool)lhs, (GeneticBool)rhs)); } throw new InvalidOperationException("Could not cast arguemnts as expected"); }
/// <summary> /// Evaluates the operator. /// </summary> /// <returns>The operator.</returns> /// <param name="signature">Signature.</param> /// <param name="lhs">Lhs.</param> /// <param name="rhs">Rhs.</param> public static GeneticObject EvaluateOperator(OperatorSignature signature, GeneticObject lhs, GeneticObject rhs) { if (signature.ReturnType == typeof(GeneticInt)) { return(GeneticInt.EvaluateOperator(signature, lhs, rhs)); } if (signature.ReturnType == typeof(GeneticBool)) { return(GeneticBool.EvaluateOperator(signature, lhs, rhs)); } throw new InvalidOperationException(string.Format( "{0} is missing implementation support for an operator {1}", signature.ReturnType, signature.Value)); }
/// <summary> /// Evaluates the operator. /// </summary> /// <returns>The operator.</returns> /// <param name="signature">Signature.</param> /// <param name="lhs">Lhs.</param> /// <param name="rhs">Rhs.</param> new public static GeneticInt EvaluateOperator(OperatorSignature signature, GeneticObject lhs, GeneticObject rhs) { GeneticObject.ValidateOperatorCall(signature, typeof(GeneticInt), lhs, rhs); switch (signature.Value) { case "+": return(GeneticInt.OperatorPlus((GeneticInt)lhs, (GeneticInt)rhs)); case "-": return(GeneticInt.OperatorMinus((GeneticInt)lhs, (GeneticInt)rhs)); case "*": return(GeneticInt.OperatorMultiply((GeneticInt)lhs, (GeneticInt)rhs)); case "/": return(GeneticInt.OperatorDivide((GeneticInt)lhs, (GeneticInt)rhs)); } throw new InvalidOperationException(string.Format( "Did not recognize operator with signature {0} with return type {1}", signature.Value, signature.ReturnType)); }