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

52
src/AstBuilder.cs

@ -213,7 +213,7 @@ namespace Decompiler @@ -213,7 +213,7 @@ namespace Decompiler
}
if (typeDef.Fields.Count > 0) {
astType.Children.Add(new IdentifierExpression("\n"));
astType.Children.Add(new IdentifierExpression("\r\n"));
}
// Add events
@ -227,7 +227,7 @@ namespace Decompiler @@ -227,7 +227,7 @@ namespace Decompiler
}
if (typeDef.Events.Count > 0) {
astType.Children.Add(new IdentifierExpression("\n"));
astType.Children.Add(new IdentifierExpression("\r\n"));
}
// Add properties
@ -257,7 +257,22 @@ namespace Decompiler @@ -257,7 +257,22 @@ namespace Decompiler
}
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
@ -269,29 +284,34 @@ namespace Decompiler @@ -269,29 +284,34 @@ namespace Decompiler
astMethod.TypeReference = new Ast.TypeReference(methodDef.ReturnType.ReturnType.FullName);
astMethod.Modifier = ConvertModifiers(methodDef);
foreach(ParameterDefinition paramDef in 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.Parameters.AddRange(MakeParameters(methodDef.Parameters));
astMethod.Body = AstMetodBodyBuilder.CreateMetodBody(methodDef);
astType.Children.Add(astMethod);
astType.Children.Add(new IdentifierExpression("\n"));
astType.Children.Add(new IdentifierExpression("\r\n"));
}
if (astType.Children.Last is IdentifierExpression) {
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 @@ -459,6 +459,11 @@ namespace Decompiler
target = new Ast.IdentifierExpression(cecilMethod.DeclaringType.FullName);
}
// TODO: Constructors are ignored
if (cecilMethod.Name == ".ctor") {
return MakeComment("// Constructor");
}
// TODO: Hack, detect properties properly
if (cecilMethod.Name.StartsWith("get_")) {
return new Ast.MemberReferenceExpression(target, cecilMethod.Name.Remove(0, 4));

9
src/Transforms/Ast/RemoveDeadLabels.cs

@ -11,6 +11,15 @@ namespace Decompiler.Transforms.Ast @@ -11,6 +11,15 @@ namespace Decompiler.Transforms.Ast
List<string> usedLabels = new List<string>();
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)
{
collectingUsedLabels = true;

2
src/Transforms/Ast/RemoveGotos.cs

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

Loading…
Cancel
Save