static LiteralExpression GetFoldedIntegerLiteral(BinaryOperatorType @operator, long lhs, long rhs)
{
long result;
switch (@operator)
{
//arithmetic
case BinaryOperatorType.Addition:
result = lhs + rhs;
break;
case BinaryOperatorType.Subtraction:
result = lhs - rhs;
break;
case BinaryOperatorType.Multiply:
result = lhs * rhs;
break;
case BinaryOperatorType.Division:
result = lhs / rhs;
break;
case BinaryOperatorType.Modulus:
result = lhs % rhs;
break;
case BinaryOperatorType.Exponentiation:
result = (long) Math.Pow(lhs, rhs);
break;
//bitwise
case BinaryOperatorType.BitwiseOr:
result = lhs | rhs;
break;
case BinaryOperatorType.BitwiseAnd:
result = lhs & rhs;
break;
case BinaryOperatorType.ExclusiveOr:
result = lhs ^ rhs;
break;
case BinaryOperatorType.ShiftLeft:
result = lhs << (int) rhs;
break;
case BinaryOperatorType.ShiftRight:
result = lhs >> (int) rhs;
break;
//comparison
case BinaryOperatorType.LessThan:
return new BoolLiteralExpression(lhs < rhs);
case BinaryOperatorType.LessThanOrEqual:
return new BoolLiteralExpression(lhs <= rhs);
case BinaryOperatorType.GreaterThan:
return new BoolLiteralExpression(lhs > rhs);
case BinaryOperatorType.GreaterThanOrEqual:
return new BoolLiteralExpression(lhs >= rhs);
case BinaryOperatorType.Equality:
return new BoolLiteralExpression(lhs == rhs);
case BinaryOperatorType.Inequality:
return new BoolLiteralExpression(lhs != rhs);
default:
return null; //not supported
}
return new IntegerLiteralExpression(result);
}