Skip to content

Commit

Permalink
lingo: minor adjustments to the source generation
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulusParssinen committed Feb 22, 2024
1 parent 40455bd commit 3c50e8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
13 changes: 6 additions & 7 deletions Shockky.SourceGeneration.Tests/InstructionGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum OPCode : byte
[OP] Return = 0x01,
}
""";
string expected = """
string expectedSyntaxForReturn = """
namespace Shockky.Lingo.Instructions;
[global::System.CodeDom.Compiler.GeneratedCode("InstructionGenerator", <ASSEMBLY_VERSION>)]
Expand All @@ -33,12 +33,11 @@ public sealed class Return : IInstruction
/// <inheritdoc />
public OPCode OP => OPCode.Return;
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
[global::System.Diagnostics.DebuggerBrowsable(global::System.Diagnostics.DebuggerBrowsableState.Never)]
public int Immediate { get; set; }
public int GetSize()
{
return sizeof(OPCode);
}
public int GetSize() => sizeof(OPCode);
public void WriteTo(global::Shockky.IO.ShockwaveWriter output)
{
Expand All @@ -49,7 +48,7 @@ public void WriteTo(global::Shockky.IO.ShockwaveWriter output)

VerifyGenerateSources(source,
[new InstructionGenerator()],
("Shockky.Lingo.Instructions.Return.g.cs", expected));
("Shockky.Lingo.Instructions.Return.g.cs", expectedSyntaxForReturn));
}

[Fact]
Expand Down Expand Up @@ -98,7 +97,7 @@ public void WriteTo(global::Shockky.IO.ShockwaveWriter output)
output.Write(op + 0x40);
output.Write((ushort)Immediate);
}
else
else
{
output.Write(op + 0x80);
output.Write(Immediate);
Expand Down
27 changes: 17 additions & 10 deletions Shockky.SourceGeneration/InstructionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,32 @@ internal static void WriteInstructionSyntax(IndentedTextWriter writer,
writer.WriteLine($$"""
/// <inheritdoc />
public OPCode OP => OPCode.{{instruction.Name}};
public int Immediate { get; set; }
""", true);
writer.WriteLine();

// If the OP has no immediate, hide it from debugger and editor on the concrete implementation.
if (!hasImmediate)
{
writer.WriteLine("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]");
writer.WriteLine("[global::System.Diagnostics.DebuggerBrowsable(global::System.Diagnostics.DebuggerBrowsableState.Never)]");
}
writer.WriteLine("public int Immediate { get; set; }");

writer.WriteLine();
writer.WriteLine("public int GetSize()");
using (var getSizeBlock = writer.WriteBlock())

if (hasImmediate)
{
if (hasImmediate)
{
writer.WriteLine("""
writer.WriteLine("""
public int GetSize()
{
uint imm = (uint)Immediate;
if (imm <= byte.MaxValue) return sizeof(OPCode) + 1;
else if (imm <= ushort.MaxValue) return sizeof(OPCode) + 2;
else return sizeof(OPCode) + 4;
""", true);
}
else writer.WriteLine("return sizeof(OPCode);");
}
""", true);
}
else writer.WriteLine("public int GetSize() => sizeof(OPCode);");

writer.WriteLine();
writer.WriteLine("public void WriteTo(global::Shockky.IO.ShockwaveWriter output)");
Expand Down

0 comments on commit 3c50e8b

Please sign in to comment.