Skip to content

Commit

Permalink
⚖️ Make sure pre-packed values are added in comments to generated sou…
Browse files Browse the repository at this point in the history
…rces (#1176)
  • Loading branch information
eduherminio authored Nov 18, 2024
1 parent 404e7dd commit adffc4f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
48 changes: 25 additions & 23 deletions src/Lynx.Generator/GeneratedPackGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ private static bool IsGeneratedPackAttribute(SyntaxNode syntaxNode) =>

private static Model? GetModel(GeneratorAttributeSyntaxContext context)
{
var value = ExtractPackedValueFromAttribute(context.TargetNode, context.SemanticModel);
var (mg, eg) = ExtractPackedValueFromAttribute(context.TargetNode, context.SemanticModel);

if (value != Model.DefaultValue)
if (mg != Model.DefaultShortValue && eg != Model.DefaultShortValue)
{
var classSymbol = context.TargetSymbol.ContainingSymbol;

return new Model(
Namespace: classSymbol.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)) ?? string.Empty,
ClassName: classSymbol.Name,
PropertyName: context.TargetSymbol.Name[1..],
PackedValue: value);
MG: mg,
EG: eg);
}

return null;
Expand All @@ -73,22 +74,22 @@ private static bool IsGeneratedPackAttribute(SyntaxNode syntaxNode) =>
/// Heavily inspired by https://andrewlock.net/creating-a-source-generator-part-4-customising-generated-code-with-marker-attributes/
/// Code in https://github.com/andrewlock/blog-examples/tree/c35edf1c1f0e1f9adf84c215e2ce7ab644b374f5/NetEscapades.EnumGenerators4
/// </summary>
static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, SemanticModel semanticModel)
static (short MG, short EG) ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, SemanticModel semanticModel)
{
const short DefaultShortValue = 32_323;
(short, short)defaultResult = (Model.DefaultShortValue, Model.DefaultShortValue);

short mg = DefaultShortValue;
short eg = DefaultShortValue;
short mg = Model.DefaultShortValue;
short eg = Model.DefaultShortValue;

if (semanticModel.GetDeclaredSymbol(variableDeclarationSyntax) is not IFieldSymbol variableSymbol)
{
return Model.DefaultValue;
return defaultResult;
}

INamedTypeSymbol? ourAttribute = semanticModel.Compilation.GetTypeByMetadataName(OurAttribute);
if (ourAttribute is null)
{
return Model.DefaultValue;
return defaultResult;
}

// Loop through all of the attributes on the enum until we find the [EnumExtensions] attribute
Expand All @@ -103,7 +104,7 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax,
// More than one of our attributes -> invalid for our use case
if(group.Count() > 1)
{
return Model.DefaultValue;
return defaultResult;
}

AttributeData attributeData = group.First();
Expand All @@ -118,14 +119,14 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax,
{
if (arg.Kind == TypedConstantKind.Error)
{
return Model.DefaultValue;
return defaultResult;
}
}

// Make sure both arguments are present
if (args.Length != 2 || args[0].IsNull || args[1].IsNull)
{
return Model.DefaultValue;
return defaultResult;
}

mg = (short)args[0].Value!;
Expand All @@ -142,7 +143,7 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax,
// Make sure we don't have any errors, otherwise don't do generation
if (typedConstant.Kind == TypedConstantKind.Error)
{
return Model.DefaultValue;
return defaultResult;
}
else
{
Expand All @@ -151,12 +152,12 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax,
{
case "mg":
mg = typedConstant.IsNull
? DefaultShortValue
? Model.DefaultShortValue
: (short)typedConstant.Value!;
break;
case "eg":
eg = typedConstant.IsNull
? DefaultShortValue
? Model.DefaultShortValue
: (short)typedConstant.Value!;
break;
}
Expand All @@ -167,12 +168,12 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax,
break;
}

if (mg == DefaultShortValue || eg == DefaultShortValue)
if (mg == Model.DefaultShortValue || eg == Model.DefaultShortValue)
{
return Model.DefaultValue;
return defaultResult;
}

return Utils.Pack(mg, eg);
return (mg, eg);
}
}

Expand Down Expand Up @@ -203,9 +204,10 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Mode
}
#pragma warning restore S2589 // Boolean expressions should not be gratuitous

sb.AppendLine($$"""
public const int {{model.PropertyName}} = {{model.PackedValue}};
""");
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("}");
Expand All @@ -216,8 +218,8 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Mode
}
}

private sealed record Model(string Namespace, string ClassName, string PropertyName, int PackedValue)
private sealed record Model(string Namespace, string ClassName, string PropertyName, short MG, short EG)
{
public const int DefaultValue = 999_999_999;
public const short DefaultShortValue = 32_323;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

static partial class EvaluationParams
{
public const int IsolatedPawnPenalty = -917521;
public const int OpenFileRookBonus = 131112;
public const int SemiOpenFileRookBonus = 524303;
public const int SemiOpenFileKingPenalty = 327656;
public const int OpenFileKingPenalty = 131007;
public const int KingShieldBonus = -196585;
public const int BishopPairBonus = 4718622;
public const int PieceProtectedByPawnBonus = 983052;
public const int PieceAttackedByPawnPenalty = -2162735;
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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

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

static partial class TestClass
{
public const int TestConstant = 131071;
public const int TestConstant = 131071; // Pack( -1, 2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ namespace Lynx.Generator;

static partial class TestClass
{
public const int TestConstant = -131073;
public const int TestConstant = -131073; // Pack( -1, -2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ namespace Lynx.Generator;

static partial class TestClass
{
public const int TestConstant = 131073;
public const int TestConstant = 131073; // Pack( 1, 2)
}

0 comments on commit adffc4f

Please sign in to comment.