Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Improve GeneratedPack sourcegen: XML comments with mg and eg values #1177

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,6 @@ tmp/
# Verify exclusions
**/Snapshots/*.g.received.cs

# Configuration binder source generator output
# Exclude all source generators output but ours
**/Generated/*
!**/Generated/Lynx.Generator/
9 changes: 6 additions & 3 deletions src/Lynx.Generator/GeneratedPackGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,12 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Mode
}
#pragma warning restore S2589 // Boolean expressions should not be gratuitous

string leftSide = string.Format("public const int {0} = {1};", model.PropertyName, Utils.Pack(model.MG, model.EG));
sb.AppendLine($$$"""
{{{string.Format("{0,-60}", leftSide)}}}// Pack({{{string.Format("{0,4}", model.MG.ToString())}}}, {{{string.Format("{0,4}", model.EG.ToString())}}})
sb.AppendLine($$"""
/// <summary>
/// <see cref="Utils.Pack({{model.MG}}, {{model.EG}})"/>
/// </summary>
{{string.Format("public const int {0} = {1};", model.PropertyName, Utils.Pack(model.MG, model.EG))}}

""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,49 @@

static partial class EvaluationParams
{
public const int IsolatedPawnPenalty = -917521; // Pack( -17, -14)
public const int OpenFileRookBonus = 131112; // Pack( 40, 2)
public const int SemiOpenFileRookBonus = 524303; // Pack( 15, 8)
public const int SemiOpenFileKingPenalty = 327656; // Pack( -24, 5)
public const int OpenFileKingPenalty = 131007; // Pack( -65, 2)
public const int KingShieldBonus = -196585; // Pack( 23, -3)
public const int BishopPairBonus = 4718622; // Pack( 30, 72)
public const int PieceProtectedByPawnBonus = 983052; // Pack( 12, 15)
public const int PieceAttackedByPawnPenalty = -2162735; // Pack( -47, -33)
/// <summary>
/// <see cref="Utils.Pack(-17, -14)"/>
/// </summary>
public const int IsolatedPawnPenalty = -917521;

/// <summary>
/// <see cref="Utils.Pack(40, 2)"/>
/// </summary>
public const int OpenFileRookBonus = 131112;

/// <summary>
/// <see cref="Utils.Pack(15, 8)"/>
/// </summary>
public const int SemiOpenFileRookBonus = 524303;

/// <summary>
/// <see cref="Utils.Pack(-24, 5)"/>
/// </summary>
public const int SemiOpenFileKingPenalty = 327656;

/// <summary>
/// <see cref="Utils.Pack(-65, 2)"/>
/// </summary>
public const int OpenFileKingPenalty = 131007;

/// <summary>
/// <see cref="Utils.Pack(23, -3)"/>
/// </summary>
public const int KingShieldBonus = -196585;

/// <summary>
/// <see cref="Utils.Pack(30, 72)"/>
/// </summary>
public const int BishopPairBonus = 4718622;

/// <summary>
/// <see cref="Utils.Pack(12, 15)"/>
/// </summary>
public const int PieceProtectedByPawnBonus = 983052;

/// <summary>
/// <see cref="Utils.Pack(-47, -33)"/>
/// </summary>
public const int PieceAttackedByPawnPenalty = -2162735;

}
33 changes: 33 additions & 0 deletions tests/Lynx.Generator.Test/GeneratedPackTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ public partial class TestClass
return Verify(source);
}

[Test]
public Task MultipleNamespacesAndClasses()
{
const string source = @"
using namespace Lynx.Generator;

namespace Namespace1
{
public partial class TestClass1_1
{
[GeneratedPack(0, 1)]
private static readonly int _TestConstant1_1;
}

public partial class TestClass1_2
{
[GeneratedPack(2, 3)]
private static readonly int _TestConstant1_2;
}
}

namespace Namespace2
{
public partial class TestClass2_1
{
[GeneratedPack(4, 5)]
private static readonly int _TestConstant2_1;
}
}";

return Verify(source);
}

[Test]
public Task NoNamespaceImport_ShouldNotGenerateConstant()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//HintName: GeneratedPackAttribute.g.cs
using System;

namespace Lynx.Generator
{
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
internal sealed class GeneratedPackAttribute : Attribute
{
public short MG { get; }
public short EG { get; }

public GeneratedPackAttribute(short mg, short eg)
{
MG = mg;
EG = eg;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: TestClass1_1.g.cs
namespace Lynx.Generator.Namespace1;

static partial class TestClass1_1
{
/// <summary>
/// <see cref="Utils.Pack(0, 1)"/>
/// </summary>
public const int TestConstant1_1 = 65536;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: TestClass1_2.g.cs
namespace Lynx.Generator.Namespace1;

static partial class TestClass1_2
{
/// <summary>
/// <see cref="Utils.Pack(2, 3)"/>
/// </summary>
public const int TestConstant1_2 = 196610;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: TestClass2_1.g.cs
namespace Lynx.Generator.Namespace2;

static partial class TestClass2_1
{
/// <summary>
/// <see cref="Utils.Pack(4, 5)"/>
/// </summary>
public const int TestConstant2_1 = 327684;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ namespace Lynx.Generator;

static partial class TestClass
{
public const int TestConstant = -131073; // Pack( -1, -2)
/// <summary>
/// <see cref="Utils.Pack(-1, -2)"/>
/// </summary>
public const int TestConstant = -131073;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ namespace Lynx.Generator;

static partial class TestClass
{
public const int TestConstant = 131071; // Pack( -1, 2)
/// <summary>
/// <see cref="Utils.Pack(-1, 2)"/>
/// </summary>
public const int TestConstant = 131071;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ namespace Lynx.Generator;

static partial class TestClass
{
public const int TestConstant = -131073; // Pack( -1, -2)
/// <summary>
/// <see cref="Utils.Pack(-1, -2)"/>
/// </summary>
public const int TestConstant = -131073;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ namespace Lynx.Generator;

static partial class TestClass
{
public const int TestConstant = 131073; // Pack( 1, 2)
/// <summary>
/// <see cref="Utils.Pack(1, 2)"/>
/// </summary>
public const int TestConstant = 131073;

}
Loading