Browse Source

Added class constructors

pull/1/head^2
David Srbecký 18 years ago
parent
commit
b89018a214
  1. 65
      bin/Debug/output.cs
  2. 52
      src/AstBuilder.cs
  3. 5
      src/AstMetodBodyBuilder.cs
  4. 9
      src/Transforms/Ast/RemoveDeadLabels.cs
  5. 2
      src/Transforms/Ast/RemoveGotos.cs

65
bin/Debug/output.cs

@ -15,7 +15,7 @@ namespace Reversi
private int whiteSafeCount; private int whiteSafeCount;
private System.Int32[,] squares; private System.Int32[,] squares;
private System.Boolean[,] safeDiscs; private System.Boolean[,] safeDiscs;
public int BlackCount { public int BlackCount {
get { return blackCount; } get { return blackCount; }
} }
@ -37,7 +37,46 @@ namespace Reversi
public int WhiteSafeCount { public int WhiteSafeCount {
get { return whiteSafeCount; } get { return whiteSafeCount; }
} }
public Board()
{
// Constructor
squares = new System.Int32[,](8, 8);
safeDiscs = new System.Boolean[,](8, 8);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares[i, j] = Empty;
safeDiscs[i, j] = 0;
}
}
UpdateCounts();
}
public Board(Reversi.Board board)
{
// Constructor
squares = new System.Int32[,](8, 8);
safeDiscs = new System.Boolean[,](8, 8);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares[i, j] = board.squares[i, j];
safeDiscs[i, j] = board.safeDiscs[i, j];
}
}
blackCount = board.blackCount;
whiteCount = board.whiteCount;
emptyCount = board.emptyCount;
blackSafeCount = board.blackSafeCount;
whiteSafeCount = board.whiteSafeCount;
}
private static Board()
{
Black = -1;
Empty = 0;
White = 1;
}
public void SetForNewGame() public void SetForNewGame()
{ {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
@ -52,12 +91,12 @@ namespace Reversi
squares[4, 4] = White; squares[4, 4] = White;
UpdateCounts(); UpdateCounts();
} }
public int GetSquareContents(int row, int col) public int GetSquareContents(int row, int col)
{ {
return squares[row, col]; return squares[row, col];
} }
public void MakeMove(int color, int row, int col) public void MakeMove(int color, int row, int col)
{ {
squares[row, col] = color; squares[row, col] = color;
@ -74,7 +113,7 @@ namespace Reversi
} }
UpdateCounts(); UpdateCounts();
} }
public bool HasAnyValidMove(int color) public bool HasAnyValidMove(int color)
{ {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
@ -86,7 +125,7 @@ namespace Reversi
} }
return false; return false;
} }
public bool IsValidMove(int color, int row, int col) public bool IsValidMove(int color, int row, int col)
{ {
if (squares[row, col] != Empty) { if (squares[row, col] != Empty) {
@ -101,7 +140,7 @@ namespace Reversi
} }
return false; return false;
} }
public int GetValidMoveCount(int color) public int GetValidMoveCount(int color)
{ {
int i = 0; int i = 0;
@ -114,7 +153,7 @@ namespace Reversi
} }
return i; return i;
} }
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;
@ -126,7 +165,7 @@ namespace Reversi
} }
return true; return true;
} }
private void UpdateCounts() private void UpdateCounts()
{ {
blackCount = 0; blackCount = 0;
@ -177,18 +216,18 @@ namespace Reversi
} }
if (safeDiscs[i, j]) { if (safeDiscs[i, j]) {
whiteSafeCount++; whiteSafeCount++;
goto BasicBlock_327; goto BasicBlock_381;
} else { } else {
goto BasicBlock_327; goto BasicBlock_381;
} }
} }
emptyCount++; emptyCount++;
} }
BasicBlock_327: BasicBlock_381:
} }
} }
} }
private bool IsOutflankable(int row, int col) private bool IsOutflankable(int row, int col)
{ {
int i = squares[row, col]; int i = squares[row, col];

52
src/AstBuilder.cs

@ -213,7 +213,7 @@ namespace Decompiler
} }
if (typeDef.Fields.Count > 0) { if (typeDef.Fields.Count > 0) {
astType.Children.Add(new IdentifierExpression("\n")); astType.Children.Add(new IdentifierExpression("\r\n"));
} }
// Add events // Add events
@ -227,7 +227,7 @@ namespace Decompiler
} }
if (typeDef.Events.Count > 0) { if (typeDef.Events.Count > 0) {
astType.Children.Add(new IdentifierExpression("\n")); astType.Children.Add(new IdentifierExpression("\r\n"));
} }
// Add properties // Add properties
@ -257,7 +257,22 @@ namespace Decompiler
} }
if (typeDef.Properties.Count > 0) { if (typeDef.Properties.Count > 0) {
astType.Children.Add(new IdentifierExpression("\n")); astType.Children.Add(new IdentifierExpression("\r\n"));
}
// Add constructors
foreach(MethodDefinition methodDef in typeDef.Constructors) {
Ast.ConstructorDeclaration astMethod = new Ast.ConstructorDeclaration(
methodDef.Name,
ConvertModifiers(methodDef),
new List<ParameterDeclarationExpression>(MakeParameters(methodDef.Parameters)),
new List<AttributeSection>()
);
astMethod.Body = AstMetodBodyBuilder.CreateMetodBody(methodDef);
astType.Children.Add(astMethod);
astType.Children.Add(new IdentifierExpression("\r\n"));
} }
// Add methods // Add methods
@ -269,29 +284,34 @@ namespace Decompiler
astMethod.TypeReference = new Ast.TypeReference(methodDef.ReturnType.ReturnType.FullName); astMethod.TypeReference = new Ast.TypeReference(methodDef.ReturnType.ReturnType.FullName);
astMethod.Modifier = ConvertModifiers(methodDef); astMethod.Modifier = ConvertModifiers(methodDef);
foreach(ParameterDefinition paramDef in methodDef.Parameters) { astMethod.Parameters.AddRange(MakeParameters(methodDef.Parameters));
Ast.ParameterDeclarationExpression astParam = new Ast.ParameterDeclarationExpression(
new Ast.TypeReference(paramDef.ParameterType.FullName),
paramDef.Name
);
if (paramDef.IsIn && !paramDef.IsOut) astParam.ParamModifier = ParameterModifiers.In;
if (!paramDef.IsIn && paramDef.IsOut) astParam.ParamModifier = ParameterModifiers.Out;
if (paramDef.IsIn && paramDef.IsOut) astParam.ParamModifier = ParameterModifiers.Ref;
astMethod.Parameters.Add(astParam);
}
astMethod.Body = AstMetodBodyBuilder.CreateMetodBody(methodDef); astMethod.Body = AstMetodBodyBuilder.CreateMetodBody(methodDef);
astType.Children.Add(astMethod); astType.Children.Add(astMethod);
astType.Children.Add(new IdentifierExpression("\n")); astType.Children.Add(new IdentifierExpression("\r\n"));
} }
if (astType.Children.Last is IdentifierExpression) { if (astType.Children.Last is IdentifierExpression) {
astType.Children.Last.Remove(); astType.Children.Last.Remove();
} }
} }
IEnumerable<Ast.ParameterDeclarationExpression> MakeParameters(ParameterDefinitionCollection paramCol)
{
foreach(ParameterDefinition paramDef in paramCol) {
Ast.ParameterDeclarationExpression astParam = new Ast.ParameterDeclarationExpression(
new Ast.TypeReference(paramDef.ParameterType.FullName),
paramDef.Name
);
if (paramDef.IsIn && !paramDef.IsOut) astParam.ParamModifier = ParameterModifiers.In;
if (!paramDef.IsIn && paramDef.IsOut) astParam.ParamModifier = ParameterModifiers.Out;
if (paramDef.IsIn && paramDef.IsOut) astParam.ParamModifier = ParameterModifiers.Ref;
yield return astParam;
}
}
} }
} }

5
src/AstMetodBodyBuilder.cs

@ -459,6 +459,11 @@ namespace Decompiler
target = new Ast.IdentifierExpression(cecilMethod.DeclaringType.FullName); target = new Ast.IdentifierExpression(cecilMethod.DeclaringType.FullName);
} }
// TODO: Constructors are ignored
if (cecilMethod.Name == ".ctor") {
return MakeComment("// Constructor");
}
// TODO: Hack, detect properties properly // TODO: Hack, detect properties properly
if (cecilMethod.Name.StartsWith("get_")) { if (cecilMethod.Name.StartsWith("get_")) {
return new Ast.MemberReferenceExpression(target, cecilMethod.Name.Remove(0, 4)); return new Ast.MemberReferenceExpression(target, cecilMethod.Name.Remove(0, 4));

9
src/Transforms/Ast/RemoveDeadLabels.cs

@ -11,6 +11,15 @@ namespace Decompiler.Transforms.Ast
List<string> usedLabels = new List<string>(); List<string> usedLabels = new List<string>();
bool collectingUsedLabels; bool collectingUsedLabels;
public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
{
collectingUsedLabels = true;
base.VisitConstructorDeclaration(constructorDeclaration, data);
collectingUsedLabels = false;
base.VisitConstructorDeclaration(constructorDeclaration, data);
return null;
}
public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data) public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
{ {
collectingUsedLabels = true; collectingUsedLabels = true;

2
src/Transforms/Ast/RemoveGotos.cs

@ -58,7 +58,7 @@ namespace Decompiler.Transforms.Ast
} }
// End of method // End of method
if (lastStmt is ReturnStatement && if (lastStmt is ReturnStatement &&
blockStatement.Parent is MethodDeclaration && (blockStatement.Parent is MethodDeclaration || blockStatement.Parent is ConstructorDeclaration) &&
((ReturnStatement)lastStmt).Expression.IsNull) ((ReturnStatement)lastStmt).Expression.IsNull)
{ {
lastStmt.Remove(); lastStmt.Remove();

Loading…
Cancel
Save