private void CheckPairForNull(OperandPair pair, string opName) { if (pair.Left == null || pair.Right == null) { throw new InvalidOperationException(GetMessage(opName, pair)); } }
private static string GetMessage(string op, OperandPair pair) { string t1 = pair.Left == null ? "<null>" : KOSNomenclature.GetKOSName(pair.Left.GetType()); string t2 = pair.Right == null ? "<null>" : KOSNomenclature.GetKOSName(pair.Right.GetType()); return(string.Format("Cannot perform the operation: {0} On Structures {1} and {2}", op, t1, t2)); }
public static Calculator GetCalculator(OperandPair operandPair) { var scalarCount = 0; var stringCount = 0; var specialCount = 0; var boolCount = 0; if (operandPair.Left is ScalarValue) { scalarCount++; } if (operandPair.Left is StringValue) { stringCount++; } if (operandPair.Left is ISuffixed) { specialCount++; } if (operandPair.Left is BooleanValue) { boolCount++; } if (operandPair.Right is ScalarValue) { scalarCount++; } if (operandPair.Right is StringValue) { stringCount++; } if (operandPair.Right is ISuffixed) { specialCount++; } if (operandPair.Right is BooleanValue) { boolCount++; } if (scalarCount == 2) { return(calculatorScalar ?? (calculatorScalar = new CalculatorScalar())); } if (stringCount > 0) { return(calculatorString ?? (calculatorString = new CalculatorString())); } if (boolCount > 0) { return(calculatorBool ?? (calculatorBool = new CalculatorBool())); } if (specialCount > 0) { return(calculatorStructure ?? (calculatorStructure = new CalculatorStructure())); } throw new NotImplementedException(string.Format("Can't operate types {0} and {1}", operandPair.Left.GetType(), operandPair.Right.GetType())); }
private bool TryCoerceImplicit(OperandPair pair, out OperandPair resultPair) { bool couldCoerce = false; object newLeft; object newRight; if (pair.LeftType == pair.RightType) { resultPair = null; // Since the types are already the same, we can't coerce them to be the same. // Otherwise, some types will act as if they have been coerced because of // other implict conversions. return(false); } MethodInfo convert2 = pair.LeftType.GetMethod("op_Implicit", FLAGS | BindingFlags.ExactBinding, null, new[] { pair.RightType }, null); if (convert2 != null) { couldCoerce = true; newRight = InvokeWithCorrectExceptions(convert2, null, new[] { pair.Right }); } else { newRight = pair.Right; } MethodInfo convert1 = pair.RightType.GetMethod("op_Implicit", FLAGS | BindingFlags.ExactBinding, null, new[] { pair.LeftType }, null); if (convert1 != null) { couldCoerce = true; newLeft = InvokeWithCorrectExceptions(convert1, null, new[] { pair.Left }); } else { newLeft = pair.Left; } resultPair = new OperandPair(newLeft, newRight); return(couldCoerce); }
public override object Power(OperandPair pair) { CheckPairForNull(pair, "Power"); object result; if (TryInvokeExplicit(pair, "op_ExclusiveOr", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(Power(resultPair)); } throw new KOSException(GetMessage("^", pair)); }
private bool TryInvokeExplicit(OperandPair pair, string methodName, out object result) { MethodInfo method1 = pair.LeftType.GetMethod(methodName, FLAGS, null, new[] { pair.LeftType, pair.RightType }, null); if (method1 != null) { result = InvokeWithCorrectExceptions(method1, null, new[] { pair.Left, pair.Right }); return(true); } MethodInfo method2 = pair.RightType.GetMethod(methodName, FLAGS, null, new[] { pair.LeftType, pair.RightType }, null); if (method2 != null) { result = InvokeWithCorrectExceptions(method2, null, new[] { pair.Left, pair.Right }); return(true); } result = null; return(false); }
public override object Multiply(OperandPair pair) { CheckPairForNull(pair, "Multiply"); object result; if (TryInvokeExplicit(pair, "op_Multiply", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(Multiply(resultPair)); } throw new KOSException(GetMessage("*", pair)); }
public override object LessThanEqual(OperandPair pair) { CheckPairForNull(pair, "LessThanEqual"); object result; if (TryInvokeExplicit(pair, "op_LessThanEqual", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(LessThanEqual(resultPair)); } throw new KOSException(GetMessage("<=", pair)); }
public override object Equal(OperandPair pair) { CheckPairForNull(pair, "Equal"); object result; if (TryInvokeExplicit(pair, "op_Equality", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(Equal(pair)); } return(pair.Left.Equals(pair.Right)); }
public override object GreaterThan(OperandPair pair) { CheckPairForNull(pair, "GreaterThan"); object result; if (TryInvokeExplicit(pair, "op_GreaterThan", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(GreaterThan(resultPair)); } throw new KOSException(GetMessage(">", pair)); }
public override object Add(OperandPair pair) { CheckPairForNull(pair, "Add"); object result; if (TryInvokeExplicit(pair, "op_Addition", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(Add(resultPair)); } throw new KOSException(GetMessage("+", pair)); }
public override object Divide(OperandPair pair) { CheckPairForNull(pair, "Divide"); object result; if (TryInvokeExplicit(pair, "op_Division", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(Divide(resultPair)); } throw new KOSException(GetMessage("/", pair)); }
public override object Subtract(OperandPair pair) { CheckPairForNull(pair, "Subtract"); object result; if (TryInvokeExplicit(pair, "op_Subtraction", out result)) { return(result); } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return(Subtract(resultPair)); } throw new KOSException(GetMessage("-", pair)); }
public override object Divide(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "divide", "by"); }
public override object GreaterThan(OperandPair pair) { int compareNum = string.Compare(pair.Left.ToString(), pair.Right.ToString(), StringComparison.OrdinalIgnoreCase); return(compareNum > 0); }
public override object Add(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "add", "to"); }
public override object Multiply(OperandPair pair) { return(ScalarValue.Create(pair.Left) * ScalarValue.Create(pair.Right)); }
public override object Subtract(OperandPair pair) { return(ScalarValue.Create(pair.Left) - ScalarValue.Create(pair.Right)); }
public override object Add(OperandPair pair) { return(new StringValue(string.Concat(pair.Left, pair.Right))); }
public override object Equal(OperandPair pair) { return(string.Equals(pair.Left.ToString(), pair.Right.ToString(), StringComparison.OrdinalIgnoreCase)); }
public override object LessThanEqual(OperandPair pair) { int compareNum = string.Compare(pair.Left.ToString(), pair.Right.ToString(), StringComparison.OrdinalIgnoreCase); return(compareNum <= 0); }
public override object Subtract(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "subtract", "from"); }
public override object GreaterThan(OperandPair pair) { return(ScalarValue.Create(pair.Left) > ScalarValue.Create(pair.Right)); }
public override object Power(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "exponentiate", "by"); }
public override object Equal(OperandPair pair) { return(new BooleanValue(Convert.ToBoolean(pair.Left) == Convert.ToBoolean(pair.Right))); }
public override object LessThanEqual(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "ordinate", "<="); }
public override object NotEqual(OperandPair pair) { return(ScalarValue.Create(pair.Left) != ScalarValue.Create(pair.Right)); }
public override object Add(OperandPair pair) { return(ScalarValue.Create(pair.Left) + ScalarValue.Create(pair.Right)); }
public override object LessThanEqual(OperandPair pair) { return(ScalarValue.Create(pair.Left) <= ScalarValue.Create(pair.Right)); }
public override object Multiply(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "multiply", "by"); }