internal static CseObject Parse(CseObject leftOperand, CseObject rightOperand, ArithType arithType) {
dynamic result = null;
dynamic leftOpValue = leftOperand.Value;
dynamic rightOpValue = rightOperand.Value;
string methOpName = null;
try {
switch (arithType) {
case ArithType.ADD:
methOpName = OpOverloadNames.ADD;
result = leftOpValue + rightOpValue;
break;
case ArithType.DIV:
methOpName = OpOverloadNames.DIV;
result = leftOpValue / rightOpValue;
break;
case ArithType.MOD:
methOpName = OpOverloadNames.MOD;
result = leftOpValue % rightOpValue;
break;
case ArithType.MUL:
methOpName = OpOverloadNames.MUL;
result = leftOpValue * rightOpValue;
break;
case ArithType.POW:
result = Math.Pow(leftOpValue, rightOpValue);
break;
case ArithType.SUB:
methOpName = OpOverloadNames.SUB;
result = leftOpValue - rightOpValue;
break;
}
return new CseObject(result);
}
catch {
if (methOpName == null)
throw new CseLogicException(CseLogicExceptionType.ARITH_EXCEPTION, leftOpValue, rightOpValue);
MethodInfo leftOpMeth = leftOpValue.GetType().GetMethod(methOpName, OpOverloadNames.Flags);
MethodInfo rightOpMeth = rightOpValue.GetType().GetMethod(methOpName, OpOverloadNames.Flags);
if (leftOpMeth == null && rightOpMeth == null)
throw new CseLogicException(CseLogicExceptionType.ARITH_EXCEPTION, leftOpValue, rightOpValue);
else if (leftOpMeth != null) {
try {
result = new CseObject(leftOpMeth.Invoke(leftOpValue, new object[] { leftOpValue, rightOpValue }));
}
catch {
throw new CseLogicException(CseLogicExceptionType.ARITH_EXCEPTION, leftOpValue, rightOpValue);
}
}
else if (rightOpMeth != null) {
try {
result = new CseObject(rightOpMeth.Invoke(rightOpValue, new object[] { leftOpValue, rightOpValue }));
}
catch {
throw new CseLogicException(CseLogicExceptionType.ARITH_EXCEPTION, leftOpValue, rightOpValue);
}
}
else {
throw new CseLogicException(CseLogicExceptionType.ARITH_EXCEPTION, leftOpValue, rightOpValue);
}
}
return new CseObject(result);
}