Merge pull request #41 from icsharpcode/vb-operator-support

VB -> C#: Expand operator support
This commit is contained in:
GrahamTheCoder 2018-03-13 09:18:02 +00:00 коммит произвёл GitHub
Родитель 62893a59d5 b4125a72a6
Коммит 120d333c62
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 86 добавлений и 3 удалений

Просмотреть файл

@ -333,6 +333,7 @@ namespace ICSharpCode.CodeConverter.CSharp
case VBasic.SyntaxKind.MultiplyExpression:
return SyntaxKind.MultiplyExpression;
case VBasic.SyntaxKind.DivideExpression:
case VBasic.SyntaxKind.IntegerDivideExpression:
return SyntaxKind.DivideExpression;
case VBasic.SyntaxKind.ModuloExpression:
return SyntaxKind.ModuloExpression;
@ -358,6 +359,14 @@ namespace ICSharpCode.CodeConverter.CSharp
return SyntaxKind.LessThanExpression;
case VBasic.SyntaxKind.LessThanOrEqualExpression:
return SyntaxKind.LessThanOrEqualExpression;
case VBasic.SyntaxKind.IsExpression:
return SyntaxKind.EqualsExpression;
case VBasic.SyntaxKind.IsNotExpression:
return SyntaxKind.NotEqualsExpression;
case VBasic.SyntaxKind.LeftShiftExpression:
return SyntaxKind.LeftShiftExpression;
case VBasic.SyntaxKind.RightShiftExpression:
return SyntaxKind.RightShiftExpression;
// assignment
case VBasic.SyntaxKind.SimpleAssignmentStatement:
return SyntaxKind.SimpleAssignmentExpression;
@ -368,8 +377,13 @@ namespace ICSharpCode.CodeConverter.CSharp
return SyntaxKind.SubtractAssignmentExpression;
case VBasic.SyntaxKind.MultiplyAssignmentStatement:
return SyntaxKind.MultiplyAssignmentExpression;
case VBasic.SyntaxKind.IntegerDivideAssignmentStatement:
case VBasic.SyntaxKind.DivideAssignmentStatement:
return SyntaxKind.DivideAssignmentExpression;
case VBasic.SyntaxKind.LeftShiftAssignmentStatement:
return SyntaxKind.LeftShiftAssignmentExpression;
case VBasic.SyntaxKind.RightShiftAssignmentStatement:
return SyntaxKind.RightShiftAssignmentExpression;
// Casts
case VBasic.SyntaxKind.CObjKeyword:
return SyntaxKind.ObjectKeyword;

Просмотреть файл

@ -98,6 +98,10 @@ namespace ICSharpCode.CodeConverter.Util
return SyntaxKind.SlashToken;
case SyntaxKind.ModuloExpression:
return SyntaxKind.PercentToken;
case SyntaxKind.LeftShiftExpression:
return SyntaxKind.LessThanLessThanToken;
case SyntaxKind.RightShiftExpression:
return SyntaxKind.GreaterThanGreaterThanToken;
// assignments
case SyntaxKind.SimpleAssignmentExpression:
return SyntaxKind.EqualsToken;
@ -105,6 +109,10 @@ namespace ICSharpCode.CodeConverter.Util
return SyntaxKind.PlusEqualsToken;
case SyntaxKind.SubtractAssignmentExpression:
return SyntaxKind.MinusEqualsToken;
case SyntaxKind.LeftShiftAssignmentExpression:
return SyntaxKind.LessThanLessThanEqualsToken;
case SyntaxKind.RightShiftAssignmentExpression:
return SyntaxKind.GreaterThanGreaterThanEqualsToken;
// unary
case SyntaxKind.UnaryPlusExpression:
return SyntaxKind.PlusToken;
@ -115,7 +123,7 @@ namespace ICSharpCode.CodeConverter.Util
case SyntaxKind.BitwiseNotExpression:
return SyntaxKind.TildeToken;
}
throw new ArgumentOutOfRangeException(nameof(op));
throw new ArgumentOutOfRangeException(nameof(op), op, null);
}
/// <summary>

Просмотреть файл

@ -43,6 +43,67 @@ class TestClass
}");
}
[Fact]
public void BinaryOperatorsIsIsNotLeftShiftRightShift()
{
TestConversionVisualBasicToCSharp(@"Class TestClass
Private bIs as Boolean = New Object Is New Object
Private bIsNot as Boolean = New Object IsNot New Object
Private bLeftShift as Integer = 1 << 3
Private bRightShift as Integer = 8 >> 3
End Class", @"class TestClass
{
private bool bIs = new object() == new object();
private bool bIsNot = new object() != new object();
private int bLeftShift = 1 << 3;
private int bRightShift = 8 >> 3;
}");
}
[Fact]
public void ShiftAssignment()
{
TestConversionVisualBasicToCSharp(@"Class TestClass
Private Sub TestMethod()
Dim x = 1
x <<= 4
x >>= 3
End Sub
End Class", @"class TestClass
{
private void TestMethod()
{
var x = 1;
x <<= 4;
x >>= 3;
}
}");
}
[Fact]
public void IntegerArithmetic()
{
TestConversionVisualBasicToCSharp(@"Class TestClass
Private Sub TestMethod()
Dim x = 6 Mod 5 \ 4 + 3 * 2
x += 1
x -= 2
x *= 3
x \= 4
End Sub
End Class", @"class TestClass
{
private void TestMethod()
{
var x = 6 % 5 / 4 + 3 * 2;
x += 1;
x -= 2;
x *= 3;
x /= 4;
}
}");
}
[Fact]
public void FullyTypeInferredEnumerableCreation()
{

Просмотреть файл

@ -145,7 +145,7 @@ using Microsoft.VisualBasic;
/// </summary>
private static bool IsTwoLineCsIfStatement(string line, string nextLine)
{
return line.Contains("if") && !nextLine.Trim().Equals("{");
return line.Contains("if ") && !nextLine.Trim().Equals("{");
}
private static bool HasNoTargetLine(string prevLine, string line, string nextLine)
@ -161,7 +161,7 @@ using Microsoft.VisualBasic;
private static bool IsFirstOfMultiLineVbIfStatement(string line)
{
return line.Trim().StartsWith("If") && line.Trim().EndsWith("Then");
return line.Trim().StartsWith("If ") && line.Trim().EndsWith("Then");
}
private static bool IsVbInheritsOrImplements(string line)