Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,23 +287,18 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,

if (enumType is null && op.Name is ">>" or ">>>" or "<<")
{
if (!TypeUtils.IsInteger(lhs.Type) && TypeUtils.IsInteger(rhs.Type))
Error.NonApplicableBinaryOperator(op, lhs.Type, rhs.Type);

if (rhs.Type == typeof(sbyte)
|| rhs.Type == typeof(byte)
|| rhs.Type == typeof(short)
|| rhs.Type == typeof(ushort))
rhs = Expression.Convert(rhs, typeof(int));
//
// The predefined shift operators take an int, uint, long, or ulong value and an int count,
// so an operand of a smaller integral type, including char, is converted to int, as in C#.
//
var lhsType = lhs.Type;
var rhsType = rhs.Type;

if (rhs.Type != typeof(int))
Error.NonApplicableBinaryOperator(op, lhs.Type, rhs.Type);
lhs = ApplyUnaryNumericPromotion(lhs);
rhs = ApplyUnaryNumericPromotion(rhs);

if (lhs.Type == typeof(sbyte)
|| lhs.Type == typeof(byte)
|| lhs.Type == typeof(short)
|| lhs.Type == typeof(ushort))
lhs = Expression.Convert(lhs, typeof(int));
if (!TypeUtils.IsInteger(lhs.Type) || rhs.Type != typeof(int))
Error.NonApplicableBinaryOperator(op, lhsType, rhsType);

if (op.Name == ">>>")
{
Expand Down Expand Up @@ -344,6 +339,26 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
return result;
}

private static Expression ApplyUnaryNumericPromotion(Expression operand)
{
//
// 12.4.7.2 Unary numeric promotions
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12472-unary-numeric-promotions
//
// Unary numeric promotion consists of converting operands of type sbyte, byte, short, ushort, or char to type int.
//
var type = operand.Type;

if (type == typeof(sbyte)
|| type == typeof(byte)
|| type == typeof(short)
|| type == typeof(ushort)
|| type == typeof(char))
return Expression.Convert(operand, typeof(int));

return operand;
}

private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression lhs, ref Expression rhs)
{
//
Expand Down
7 changes: 2 additions & 5 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,8 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
operand = Expression.Convert(operand, typeof(long));
break;

case (not UnaryType.Convert, _) when underlyingType == typeof(byte):
case (not UnaryType.Convert, _) when underlyingType == typeof(sbyte):
case (not UnaryType.Convert, _) when underlyingType == typeof(short):
case (not UnaryType.Convert, _) when underlyingType == typeof(ushort):
operand = Expression.Convert(operand, typeof(int));
case (not UnaryType.Convert, _):
operand = ApplyUnaryNumericPromotion(operand);
break;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ Unary operator '!' cannot be applied to operand of type 'System.Byte'.
!DayOfWeek.Friday
Unary operator '!' cannot be applied to operand of type 'System.DayOfWeek'.

!'a'
Unary operator '!' cannot be applied to operand of type 'System.Char'.

#######################################
# Shift operators

'a' << 1L
Operator '<<' cannot be applied to operands of type 'System.Char' and 'System.Int64'.

1 << 1u
Operator '<<' cannot be applied to operands of type 'System.Int32' and 'System.UInt32'.

1.5 >> 1
Operator '>>' cannot be applied to operands of type 'System.Double' and 'System.Int32'.

1m >>> 1
Operator '>>>' cannot be applied to operands of type 'System.Decimal' and 'System.Int32'.

#######################################
# Conditional operator branches without a common type

Expand Down
10 changes: 10 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public void EvaluateExpression(string expression, object result) =>
public void Evaluate_NegatedUInt32_ReturnsNegatedInt64(string expression, long expected) =>
Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf<long>().And.EqualTo(expected));

[TestCase("-'a'", -97)]
[TestCase("+'a'", 97)]
[TestCase("~'a'", -98)]
[TestCase("'a' << 1", 194)]
[TestCase("'a' >> 1", 48)]
[TestCase("'a' >>> 1", 48)]
[TestCase("1 << 'a'", 2)]
public void Evaluate_CharOperand_PromotesToInt32(string expression, int expected) =>
Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf<int>().And.EqualTo(expected));

[TestCase("Sum(Numbers)", 6)]
[TestCase("Count(Words)", 2)]
[TestCase("IsWords(Words)", true)]
Expand Down
Loading