Browse Source

Idioms "i++" and "i += k"

pull/1/head^2
David Srbecký 18 years ago
parent
commit
bacab7a00b
  1. 66
      bin/Debug/output.cs
  2. 1
      src/AstBuilder.cs
  3. 30
      src/Transforms/Ast/Idioms.cs
  4. 12
      src/Transforms/Ast/RestoreLoop.cs

66
bin/Debug/output.cs

@ -31,8 +31,8 @@ namespace Reversi
} }
public void SetForNewGame() public void SetForNewGame()
{ {
for (int i = 0; i < 8; i = i + 1) { for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j = j + 1) { for (int j = 0; j < 8; j++) {
squares.Set(i, j, Empty); squares.Set(i, j, Empty);
safeDiscs.Set(i, j, 0); safeDiscs.Set(i, j, 0);
} }
@ -50,13 +50,13 @@ namespace Reversi
public void MakeMove(int color, int row, int col) public void MakeMove(int color, int row, int col)
{ {
squares.Set(row, col, color); squares.Set(row, col, color);
for (int i = -1; i <= 1; i = i + 1) { for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j = j + 1) { for (int j = -1; j <= 1; j++) {
if ((i || j) && IsOutflanking(color, row, col, i, j)) { if ((i || j) && IsOutflanking(color, row, col, i, j)) {
int k = row + i; int k = row + i;
for (int l = col + j; squares.Get(k, l) == -color; l = l + j) { for (int l = col + j; squares.Get(k, l) == -color; l += j) {
squares.Set(k, l, color); squares.Set(k, l, color);
k = k + i; k += i;
} }
} }
} }
@ -65,8 +65,8 @@ namespace Reversi
} }
public bool HasAnyValidMove(int color) public bool HasAnyValidMove(int color)
{ {
for (int i = 0; i < 8; i = i + 1) { for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j = j + 1) { for (int j = 0; j < 8; j++) {
if (IsValidMove(color, i, j)) { if (IsValidMove(color, i, j)) {
return 1; return 1;
} }
@ -79,8 +79,8 @@ namespace Reversi
if (squares.Get(row, col) != Empty) { if (squares.Get(row, col) != Empty) {
return 0; return 0;
} }
for (int i = -1; i <= 1; i = i + 1) { for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j = j + 1) { for (int j = -1; j <= 1; j++) {
if ((i || j) && IsOutflanking(color, row, col, i, j)) { if ((i || j) && IsOutflanking(color, row, col, i, j)) {
return 1; return 1;
} }
@ -91,10 +91,10 @@ namespace Reversi
public int GetValidMoveCount(int color) public int GetValidMoveCount(int color)
{ {
int i = 0; int i = 0;
for (int j = 0; j < 8; j = j + 1) { for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k = k + 1) { for (int k = 0; k < 8; k++) {
if (IsValidMove(color, j, k)) { if (IsValidMove(color, j, k)) {
i = i + 1; i++;
} }
} }
} }
@ -103,8 +103,8 @@ namespace Reversi
private bool IsOutflanking(int color, int row, int col, int dr, int dc) private bool IsOutflanking(int color, int row, int col, int dr, int dc)
{ {
int i = row + dr; int i = row + dr;
for (int j = col + dc; i >= 0 && i < 8 && j >= 0 && j < 8 && squares.Get(i, j) == -color; j = j + dc) { for (int j = col + dc; i >= 0 && i < 8 && j >= 0 && j < 8 && squares.Get(i, j) == -color; j += dc) {
i = i + dr; i += dr;
} }
if (i < 0 || i > 7 || j < 0 || j > 7 || i - dr == row && j - dc == col || squares.Get(i, j) != color) { if (i < 0 || i > 7 || j < 0 || j > 7 || i - dr == row && j - dc == col || squares.Get(i, j) != color) {
return 0; return 0;
@ -122,8 +122,8 @@ namespace Reversi
blackSafeCount = 0; blackSafeCount = 0;
for (bool V_2 = 1; V_2;) { for (bool V_2 = 1; V_2;) {
V_2 = 0; V_2 = 0;
for (int i = 0; i < 8; i = i + 1) { for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j = j + 1) { for (int j = 0; j < 8; j++) {
if (squares.Get(i, j) != Empty && !safeDiscs.Get(i, j) && !IsOutflankable(i, j)) { if (squares.Get(i, j) != Empty && !safeDiscs.Get(i, j) && !IsOutflankable(i, j)) {
safeDiscs.Set(i, j, 1); safeDiscs.Set(i, j, 1);
V_2 = 1; V_2 = 1;
@ -132,13 +132,13 @@ namespace Reversi
} }
} }
i = 0; i = 0;
for (; i < 8; i = i + 1) { for (; i < 8; i++) {
j = 0; j = 0;
for (; j < 8; j = j + 1) { for (; j < 8; j++) {
bool V_5 = 0; bool V_5 = 0;
if (squares.Get(i, j) != Empty) { if (squares.Get(i, j) != Empty) {
for (int k = -1; k <= 1; k = k + 1) { for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l = l + 1) { for (int l = -1; l <= 1; l++) {
if ((k || l) && i + k >= 0 && i + k < 8 && j + l >= 0 && j + l < 8 && squares.Get(i + k, j + l) == Empty) { if ((k || l) && i + k >= 0 && i + k < 8 && j + l >= 0 && j + l < 8 && squares.Get(i + k, j + l) == Empty) {
V_5 = 1; V_5 = 1;
} }
@ -202,7 +202,7 @@ namespace Reversi
bool V_5 = 0; bool V_5 = 0;
bool V_4 = 0; bool V_4 = 0;
bool V_6 = 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++) {
if (squares.Get(row, k) == Empty) { if (squares.Get(row, k) == Empty) {
V_3 = 1; V_3 = 1;
} }
@ -213,7 +213,7 @@ namespace Reversi
} }
} }
k = col + 1; k = col + 1;
for (; k < 8 && !V_4; k = k + 1) { for (; k < 8 && !V_4; k++) {
if (squares.Get(row, k) == Empty) { if (squares.Get(row, k) == Empty) {
V_4 = 1; V_4 = 1;
} }
@ -230,7 +230,7 @@ namespace Reversi
V_4 = 0; V_4 = 0;
V_5 = 0; V_5 = 0;
V_6 = 0; V_6 = 0;
for (int j = 0; j < row && !V_3; j = j + 1) { for (int j = 0; j < row && !V_3; j++) {
if (squares.Get(j, col) == Empty) { if (squares.Get(j, col) == Empty) {
V_3 = 1; V_3 = 1;
} }
@ -241,7 +241,7 @@ namespace Reversi
} }
} }
j = row + 1; j = row + 1;
for (; j < 8 && !V_4; j = j + 1) { for (; j < 8 && !V_4; j++) {
if (squares.Get(j, col) == Empty) { if (squares.Get(j, col) == Empty) {
V_4 = 1; V_4 = 1;
} }
@ -260,7 +260,7 @@ namespace Reversi
V_6 = 0; V_6 = 0;
j = row - 1; j = row - 1;
k = col - 1; k = col - 1;
for (; j >= 0 && k >= 0 && !V_3; k = k - 1) { for (; j >= 0 && k >= 0 && !V_3; k--) {
if (squares.Get(j, k) == Empty) { if (squares.Get(j, k) == Empty) {
V_3 = 1; V_3 = 1;
} }
@ -269,11 +269,11 @@ namespace Reversi
V_5 = 1; V_5 = 1;
} }
} }
j = j - 1; j--;
} }
j = row + 1; j = row + 1;
k = col + 1; k = col + 1;
for (; j < 8 && k < 8 && !V_4; k = k + 1) { for (; j < 8 && k < 8 && !V_4; k++) {
if (squares.Get(j, k) == Empty) { if (squares.Get(j, k) == Empty) {
V_4 = 1; V_4 = 1;
} }
@ -282,7 +282,7 @@ namespace Reversi
V_6 = 1; V_6 = 1;
} }
} }
j = j + 1; j++;
} }
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 1;
@ -293,7 +293,7 @@ namespace Reversi
V_6 = 0; V_6 = 0;
j = row - 1; j = row - 1;
k = col + 1; k = col + 1;
for (; j >= 0 && k < 8 && !V_3; k = k + 1) { for (; j >= 0 && k < 8 && !V_3; k++) {
if (squares.Get(j, k) == Empty) { if (squares.Get(j, k) == Empty) {
V_3 = 1; V_3 = 1;
} }
@ -302,11 +302,11 @@ namespace Reversi
V_5 = 1; V_5 = 1;
} }
} }
j = j - 1; j--;
} }
j = row + 1; j = row + 1;
k = col - 1; k = col - 1;
for (; j < 8 && k >= 0 && !V_4; k = k - 1) { for (; j < 8 && k >= 0 && !V_4; k--) {
if (squares.Get(j, k) == Empty) { if (squares.Get(j, k) == Empty) {
V_4 = 1; V_4 = 1;
} }
@ -315,7 +315,7 @@ namespace Reversi
V_6 = 1; V_6 = 1;
} }
} }
j = j + 1; j++;
} }
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 1;

1
src/AstBuilder.cs

@ -42,6 +42,7 @@ namespace Decompiler
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveParenthesis(), null); astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveParenthesis(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveParenthesis(), null); astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveParenthesis(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.SimplifyTypeReferences(), null); astCompileUnit.AcceptVisitor(new Transforms.Ast.SimplifyTypeReferences(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.Idioms(), null);
} }
if (Options.ReduceAstLoops) { if (Options.ReduceAstLoops) {
astCompileUnit.AcceptVisitor(new Transforms.Ast.RestoreLoop(), null); astCompileUnit.AcceptVisitor(new Transforms.Ast.RestoreLoop(), null);

30
src/Transforms/Ast/Idioms.cs

@ -31,5 +31,35 @@ namespace Decompiler.Transforms.Ast
return null; return null;
} }
public override object VisitAssignmentExpression(AssignmentExpression assignment, object data)
{
IdentifierExpression ident = assignment.Left as IdentifierExpression;
BinaryOperatorExpression binary = assignment.Right as BinaryOperatorExpression;
if (ident != null && binary != null) {
IdentifierExpression binaryLeft = binary.Left as IdentifierExpression;
if (binaryLeft != null &&
binaryLeft.Identifier == ident.Identifier) {
if (binary.Right is PrimitiveExpression &&
1.Equals((binary.Right as PrimitiveExpression).Value)) {
if (binary.Op == BinaryOperatorType.Add) {
ReplaceCurrentNode(new UnaryOperatorExpression(ident, UnaryOperatorType.PostIncrement));
}
if (binary.Op == BinaryOperatorType.Subtract) {
ReplaceCurrentNode(new UnaryOperatorExpression(ident, UnaryOperatorType.PostDecrement));
}
} else {
if (binary.Op == BinaryOperatorType.Add) {
ReplaceCurrentNode(new AssignmentExpression(ident, AssignmentOperatorType.Add, binary.Right));
}
if (binary.Op == BinaryOperatorType.Subtract) {
ReplaceCurrentNode(new AssignmentExpression(ident, AssignmentOperatorType.Subtract, binary.Right));
}
}
return null;
}
}
return null;
}
} }
} }

12
src/Transforms/Ast/RestoreLoop.cs

@ -61,13 +61,11 @@ namespace Decompiler.Transforms.Ast
if (forStatement.EmbeddedStatement.Children.Count > 0 && if (forStatement.EmbeddedStatement.Children.Count > 0 &&
forStatement.Iterator.Count == 0) forStatement.Iterator.Count == 0)
{ {
ExpressionStatement lastStmt = forStatement.EmbeddedStatement.Children[forStatement.EmbeddedStatement.Children.Count - 1] as ExpressionStatement; ExpressionStatement lastStmt = forStatement.EmbeddedStatement.Children.Last as ExpressionStatement;
if (lastStmt != null) { if (lastStmt != null &&
AssignmentExpression assign = lastStmt.Expression as AssignmentExpression; (lastStmt.Expression is AssignmentExpression || lastStmt.Expression is UnaryOperatorExpression)) {
if (assign != null) { lastStmt.Remove();
lastStmt.Remove(); forStatement.Iterator.Add(lastStmt);
forStatement.Iterator.Add(lastStmt);
}
} }
} }

Loading…
Cancel
Save