Browse Source

Restore loop initializer

pull/1/head^2
David Srbecký 18 years ago
parent
commit
34c8127a9b
  1. 1
      src/AstBuilder.cs
  2. 13
      src/Transforms/Ast/RemoveEmptyElseBody.cs
  3. 12
      src/Transforms/Ast/RestoreLoop.cs

1
src/AstBuilder.cs

@ -30,6 +30,7 @@ namespace Decompiler @@ -30,6 +30,7 @@ namespace Decompiler
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveEmptyElseBody(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RestoreLoop(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveDeadLabels(), null);
astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveEmptyElseBody(), null);
astCompileUnit.AcceptVisitor(csOutVisitor, null);

13
src/Transforms/Ast/RemoveEmptyElseBody.cs

@ -8,6 +8,19 @@ namespace Decompiler.Transforms.Ast @@ -8,6 +8,19 @@ namespace Decompiler.Transforms.Ast
{
public class RemoveEmptyElseBody: AbstractAstTransformer
{
public override object VisitBlockStatement(BlockStatement blockStatement, object data)
{
for(int i = 0; i < blockStatement.Children.Count; i++) {
if (blockStatement.Children[i] is Statement &&
((Statement)blockStatement.Children[i]).IsNull)
{
blockStatement.Children.RemoveAt(i);
i--;
}
}
return base.VisitBlockStatement(blockStatement, data);
}
public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data)
{
base.VisitIfElseStatement(ifElseStatement, data);

12
src/Transforms/Ast/RestoreLoop.cs

@ -12,6 +12,18 @@ namespace Decompiler.Transforms.Ast @@ -12,6 +12,18 @@ namespace Decompiler.Transforms.Ast
{
base.VisitForStatement(forStatement, data);
// Restore loop initializer
if (forStatement.Initializers.Count == 0) {
int myIndex = forStatement.Parent.Children.IndexOf(forStatement);
if (myIndex - 1 >= 0) {
LocalVariableDeclaration varDeclr = forStatement.Parent.Children[myIndex - 1] as LocalVariableDeclaration;
if (varDeclr != null) {
forStatement.Parent.Children[myIndex - 1] = Statement.Null;
forStatement.Initializers.Add(varDeclr);
}
}
}
// Restore loop condition
if (forStatement.Condition.IsNull &&
forStatement.EmbeddedStatement.Children.Count >= 3)

Loading…
Cancel
Save