Browse Source

Remove some parenthesis that are not needed due to C# operator precedence.

pull/1/head^2
David Srbecký 18 years ago
parent
commit
704a8c769e
  1. 50
      bin/Debug/output.cs
  2. 136
      src/Transforms/Ast/RemoveParenthesis.cs

50
bin/Debug/output.cs

@ -54,7 +54,7 @@ namespace Reversi @@ -54,7 +54,7 @@ namespace Reversi
for (int j = -1; j <= 1; j = (j + 1)) {
if (((i || j) && (@this.IsOutflanking(color, row, col, i, j)))) {
int k = (row + i);
for (int l = (col + j); ((@this.squares).Get(k, l)) == (-color); l = (l + j)) {
for (int l = (col + j); ((@this.squares).Get(k, l)) == -color; l = (l + j)) {
(@this.squares).Set(k, l, color);
k = (k + i);
}
@ -103,10 +103,10 @@ namespace Reversi @@ -103,10 +103,10 @@ namespace Reversi
private bool IsOutflanking(int color, int row, int col, int dr, int dc)
{
int i = (row + dr);
for (int j = (col + dc); ((((i >= 0) && (i < 8)) && (j >= 0)) && (j < 8)) && (((@this.squares).Get(i, j)) == (-color)); j = (j + dc)) {
for (int j = (col + dc); (((i >= 0 && i < 8) && j >= 0) && j < 8) && ((@this.squares).Get(i, j)) == -color; j = (j + dc)) {
i = (i + dr);
}
if (((((((i < 0) || (i > 7)) || (j < 0)) || (j > 7)) || (((i - dr) == row) && ((j - dc) == col))) || (((@this.squares).Get(i, j)) != color))) {
if ((((((i < 0 || i > 7) || j < 0) || j > 7) || i - dr == row && j - dc == col) || ((@this.squares).Get(i, j)) != color)) {
return 0;
}
return 1;
@ -124,7 +124,7 @@ namespace Reversi @@ -124,7 +124,7 @@ namespace Reversi
V_2 = 0;
for (int i = 0; i < 8; i = (i + 1)) {
for (int j = 0; j < 8; j = (j + 1)) {
if (((!(((@this.squares).Get(i, j)) == IL__ldsfld(Empty)) && (!((@this.safeDiscs).Get(i, j)))) && (!(@this.IsOutflankable(i, j))))) {
if (((!(((@this.squares).Get(i, j)) == IL__ldsfld(Empty)) && !((@this.safeDiscs).Get(i, j))) && !(@this.IsOutflankable(i, j)))) {
(@this.safeDiscs).Set(i, j, 1);
V_2 = 1;
}
@ -139,7 +139,7 @@ namespace Reversi @@ -139,7 +139,7 @@ namespace Reversi
if ((!(((@this.squares).Get(i, j)) == IL__ldsfld(Empty)))) {
for (int k = -1; k <= 1; k = (k + 1)) {
for (int l = -1; l <= 1; l = (l + 1)) {
if (((((((k || l) && ((i + k) >= 0)) && ((i + k) < 8)) && ((j + l) >= 0)) && ((j + l) < 8)) && !(((@this.squares).Get((i + k), (j + l))) != IL__ldsfld(Empty)))) {
if (((((((k || l) && i + k >= 0) && i + k < 8) && j + l >= 0) && j + l < 8) && !(((@this.squares).Get((i + k), (j + l))) != IL__ldsfld(Empty)))) {
V_5 = 1;
}
}
@ -201,56 +201,56 @@ namespace Reversi @@ -201,56 +201,56 @@ namespace Reversi
bool V_5 = 0;
bool V_4 = 0;
bool V_6 = 0;
for (int k = 0; (k < col) && (!V_3); k = (k + 1)) {
for (int k = 0; k < col && !V_3; k = (k + 1)) {
if ((!(((@this.squares).Get(row, k)) != IL__ldsfld(Empty)))) {
V_3 = 1;
}
else {
if (((((@this.squares).Get(row, k)) != i) || (!((@this.safeDiscs).Get(row, k))))) {
if ((((@this.squares).Get(row, k)) != i || !((@this.safeDiscs).Get(row, k)))) {
V_5 = 1;
}
}
}
k = (col + 1);
for (; (k < 8) && (!V_4); k = (k + 1)) {
for (; k < 8 && !V_4; k = (k + 1)) {
if ((!(((@this.squares).Get(row, k)) != IL__ldsfld(Empty)))) {
V_4 = 1;
}
else {
if (((((@this.squares).Get(row, k)) != i) || (!((@this.safeDiscs).Get(row, k))))) {
if ((((@this.squares).Get(row, k)) != i || !((@this.safeDiscs).Get(row, k)))) {
V_6 = 1;
}
}
}
if ((((V_3 && V_4) || (V_3 && V_6)) || (V_5 && V_4))) {
if (((V_3 && V_4 || V_3 && V_6) || V_5 && V_4)) {
return 1;
}
V_3 = 0;
V_4 = 0;
V_5 = 0;
V_6 = 0;
for (int j = 0; (j < row) && (!V_3); j = (j + 1)) {
for (int j = 0; j < row && !V_3; j = (j + 1)) {
if ((!(((@this.squares).Get(j, col)) != IL__ldsfld(Empty)))) {
V_3 = 1;
}
else {
if (((((@this.squares).Get(j, col)) != i) || (!((@this.safeDiscs).Get(j, col))))) {
if ((((@this.squares).Get(j, col)) != i || !((@this.safeDiscs).Get(j, col)))) {
V_5 = 1;
}
}
}
j = (row + 1);
for (; (j < 8) && (!V_4); j = (j + 1)) {
for (; j < 8 && !V_4; j = (j + 1)) {
if ((!(((@this.squares).Get(j, col)) != IL__ldsfld(Empty)))) {
V_4 = 1;
}
else {
if (((((@this.squares).Get(j, col)) != i) || (!((@this.safeDiscs).Get(j, col))))) {
if ((((@this.squares).Get(j, col)) != i || !((@this.safeDiscs).Get(j, col)))) {
V_6 = 1;
}
}
}
if ((((V_3 && V_4) || (V_3 && V_6)) || (V_5 && V_4))) {
if (((V_3 && V_4 || V_3 && V_6) || V_5 && V_4)) {
return 1;
}
V_3 = 0;
@ -259,12 +259,12 @@ namespace Reversi @@ -259,12 +259,12 @@ namespace Reversi
V_6 = 0;
j = (row - 1);
k = (col - 1);
for (; ((j >= 0) && (k >= 0)) && (!V_3); k = (k - 1)) {
for (; (j >= 0 && k >= 0) && !V_3; k = (k - 1)) {
if ((!(((@this.squares).Get(j, k)) != IL__ldsfld(Empty)))) {
V_3 = 1;
}
else {
if (((((@this.squares).Get(j, k)) != i) || (!((@this.safeDiscs).Get(j, k))))) {
if ((((@this.squares).Get(j, k)) != i || !((@this.safeDiscs).Get(j, k)))) {
V_5 = 1;
}
}
@ -272,18 +272,18 @@ namespace Reversi @@ -272,18 +272,18 @@ namespace Reversi
}
j = (row + 1);
k = (col + 1);
for (; ((j < 8) && (k < 8)) && (!V_4); k = (k + 1)) {
for (; (j < 8 && k < 8) && !V_4; k = (k + 1)) {
if ((!(((@this.squares).Get(j, k)) != IL__ldsfld(Empty)))) {
V_4 = 1;
}
else {
if (((((@this.squares).Get(j, k)) != i) || (!((@this.safeDiscs).Get(j, k))))) {
if ((((@this.squares).Get(j, k)) != i || !((@this.safeDiscs).Get(j, k)))) {
V_6 = 1;
}
}
j = (j + 1);
}
if ((((V_3 && V_4) || (V_3 && V_6)) || (V_5 && V_4))) {
if (((V_3 && V_4 || V_3 && V_6) || V_5 && V_4)) {
return 1;
}
V_3 = 0;
@ -292,12 +292,12 @@ namespace Reversi @@ -292,12 +292,12 @@ namespace Reversi
V_6 = 0;
j = (row - 1);
k = (col + 1);
for (; ((j >= 0) && (k < 8)) && (!V_3); k = (k + 1)) {
for (; (j >= 0 && k < 8) && !V_3; k = (k + 1)) {
if ((!(((@this.squares).Get(j, k)) != IL__ldsfld(Empty)))) {
V_3 = 1;
}
else {
if (((((@this.squares).Get(j, k)) != i) || (!((@this.safeDiscs).Get(j, k))))) {
if ((((@this.squares).Get(j, k)) != i || !((@this.safeDiscs).Get(j, k)))) {
V_5 = 1;
}
}
@ -305,18 +305,18 @@ namespace Reversi @@ -305,18 +305,18 @@ namespace Reversi
}
j = (row + 1);
k = (col - 1);
for (; ((j < 8) && (k >= 0)) && (!V_4); k = (k - 1)) {
for (; (j < 8 && k >= 0) && !V_4; k = (k - 1)) {
if ((!(((@this.squares).Get(j, k)) != IL__ldsfld(Empty)))) {
V_4 = 1;
}
else {
if (((((@this.squares).Get(j, k)) != i) || (!((@this.safeDiscs).Get(j, k))))) {
if ((((@this.squares).Get(j, k)) != i || !((@this.safeDiscs).Get(j, k)))) {
V_6 = 1;
}
}
j = (j + 1);
}
if ((((V_3 && V_4) || (V_3 && V_6)) || (V_5 && V_4))) {
if (((V_3 && V_4 || V_3 && V_6) || V_5 && V_4)) {
return 1;
}
return 0;

136
src/Transforms/Ast/RemoveParenthesis.cs

@ -8,15 +8,6 @@ namespace Decompiler.Transforms.Ast @@ -8,15 +8,6 @@ namespace Decompiler.Transforms.Ast
{
public class RemoveParenthesis: AbstractAstTransformer
{
Expression Deparenthesize(Expression expr)
{
if (expr is ParenthesizedExpression) {
return Deparenthesize(((ParenthesizedExpression)expr).Expression);
} else {
return expr;
}
}
public override object VisitParenthesizedExpression(ParenthesizedExpression parenthesizedExpression, object data)
{
// The following do not need to be parenthesized
@ -29,9 +20,24 @@ namespace Decompiler.Transforms.Ast @@ -29,9 +20,24 @@ namespace Decompiler.Transforms.Ast
return base.VisitParenthesizedExpression(parenthesizedExpression, data);
}
public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
public override object VisitUnaryOperatorExpression(UnaryOperatorExpression unary, object data)
{
return base.VisitBinaryOperatorExpression(binaryOperatorExpression, data);
if (GetPrecedence(unary.Expression) > GetPrecedence(unary)) {
unary.Expression = Deparenthesize(unary.Expression);
}
return base.VisitUnaryOperatorExpression(unary, data);
}
public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binary, object data)
{
int? myPrecedence = GetPrecedence(binary);
if (GetPrecedence(binary.Left) > myPrecedence) {
binary.Left = Deparenthesize(binary.Left);
}
if (GetPrecedence(binary.Right) > myPrecedence) {
binary.Right = Deparenthesize(binary.Right);
}
return base.VisitBinaryOperatorExpression(binary, data);
}
public override object VisitExpressionStatement(ExpressionStatement expressionStatement, object data)
@ -50,5 +56,113 @@ namespace Decompiler.Transforms.Ast @@ -50,5 +56,113 @@ namespace Decompiler.Transforms.Ast
{
return base.VisitInvocationExpression(invocationExpression, data);
}
Expression Deparenthesize(Expression expr)
{
if (expr is ParenthesizedExpression) {
return Deparenthesize(((ParenthesizedExpression)expr).Expression);
} else {
return expr;
}
}
int? GetPrecedence(Expression expr)
{
if (expr is ParenthesizedExpression) {
return GetPrecedence(((ParenthesizedExpression)expr).Expression);
}
UnaryOperatorExpression unary = expr as UnaryOperatorExpression;
BinaryOperatorExpression binary = expr as BinaryOperatorExpression;
// see http://msdn2.microsoft.com/en-us/library/ms173145.aspx
// Primary
// x.y
// f(x)
// a[x]
// x++
// x--
// new T(...)
// new T(...){...}
// new {...}
// new T[...]
// typeof(T)
// checked(x)
// unchecked(x)
// default (T)
// delegate {}
// Unary
// +x
if (unary != null && unary.Op == UnaryOperatorType.Plus) return 14;
// -x
if (unary != null && unary.Op == UnaryOperatorType.Minus) return 14;
// !x
if (unary != null && unary.Op == UnaryOperatorType.Not) return 14;
// ~x
if (unary != null && unary.Op == UnaryOperatorType.BitNot) return 14;
// ++x
if (unary != null && unary.Op == UnaryOperatorType.Increment) return 14;
// --x
if (unary != null && unary.Op == UnaryOperatorType.Decrement) return 14;
// (T)x
// Multiplicative
// *, ,
if (binary != null && binary.Op == BinaryOperatorType.Multiply) return 13;
// /
if (binary != null && binary.Op == BinaryOperatorType.Divide) return 13;
// %
if (binary != null && binary.Op == BinaryOperatorType.Modulus) return 13;
// Additive
// x + y
if (binary != null && binary.Op == BinaryOperatorType.Add) return 12;
// x - y
if (binary != null && binary.Op == BinaryOperatorType.Subtract) return 12;
// Shift
// x << y
// x >> y
// Relational and Type Testing
// x < y
if (binary != null && binary.Op == BinaryOperatorType.LessThan) return 10;
// x > y
if (binary != null && binary.Op == BinaryOperatorType.GreaterThan) return 10;
// x <= y
if (binary != null && binary.Op == BinaryOperatorType.LessThanOrEqual) return 10;
// x >= y
if (binary != null && binary.Op == BinaryOperatorType.GreaterThanOrEqual) return 10;
// x is T
// x as T
// Equality
// x == y
if (binary != null && binary.Op == BinaryOperatorType.Equality) return 9;
// x != y
if (binary != null && binary.Op == BinaryOperatorType.InEquality) return 9;
// Logical AND
// x & y
if (binary != null && binary.Op == BinaryOperatorType.BitwiseAnd) return 8;
// Logical XOR
// x ^ y
if (binary != null && binary.Op == BinaryOperatorType.ExclusiveOr) return 7;
// Logical OR
// x | y
if (binary != null && binary.Op == BinaryOperatorType.BitwiseOr) return 6;
// Conditional AND
// x && y
if (binary != null && binary.Op == BinaryOperatorType.LogicalAnd) return 5;
// Conditional OR
// x || y
if (binary != null && binary.Op == BinaryOperatorType.LogicalOr) return 4;
// Null coalescing
// X ?? y
// Conditional
// x ?: y : z
// Assignment or anonymous function
// =, , =>
if (expr is AssignmentExpression) return 1;
// x op= y
// (T x) => y
return null;
}
}
}

Loading…
Cancel
Save