Skip to content

Commit

Permalink
Merge pull request #64 from SartoxSoftware/formatting
Browse files Browse the repository at this point in the history
Add option to disable formatting and comments
  • Loading branch information
quajak authored Dec 18, 2022
2 parents fbf2111 + f4685bf commit b93409d
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 29 deletions.
26 changes: 20 additions & 6 deletions source/XSharp/XSharp/Assembler/Gen1/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ public Assembler()
mInstances.Push(this);
}

public Assembler(bool addWhitespaceWhileFlushing) : this()
public Assembler(bool aFormat = false) : this()
{
mAddWhitespaceWhileFlushing = addWhitespaceWhileFlushing;
Format = aFormat;
if (aFormat)
{
Separator = ", ";
AddSymbol = " + ";
}
else
{
Separator = ",";
AddSymbol = "+";
}
}

private static readonly Stack<Assembler> mInstances = new Stack<Assembler>();
Expand Down Expand Up @@ -217,7 +227,7 @@ public virtual void FlushText(TextWriter aOutput)
aOutput.WriteLine();
foreach (DataMember xMember in mDataMembers)
{
if (mAddWhitespaceWhileFlushing)
if (Format)
{
aOutput.Write("\t");
}
Expand All @@ -238,15 +248,15 @@ public virtual void FlushText(TextWriter aOutput)
{
var xOp = mInstructions[i];
string prefix = null;
if (mAddWhitespaceWhileFlushing)
if (Format)
{
prefix = "\t\t\t";
}
if (xOp is Label)
{
var xLabel = (Label) xOp;
aOutput.WriteLine();
if (mAddWhitespaceWhileFlushing)
if (Format)
{
prefix = "\t\t";
}
Expand All @@ -265,7 +275,11 @@ public virtual void FlushText(TextWriter aOutput)
aOutput.Flush();
}

private bool mAddWhitespaceWhileFlushing = true;
public bool Format { get; }

public string Separator { get; }

public string AddSymbol { get; }

protected virtual void BeforeFlushText(TextWriter aOutput)
{
Expand Down
52 changes: 40 additions & 12 deletions source/XSharp/XSharp/Assembler/Gen1/DataMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DataMember(string aName, string aValue, bool noConvert = false)
xBytes.CopyTo(xBytes2, 0);
xBytes2[xBytes2.Length - 1] = 0;
RawDefaultValue = xBytes2; StringValue = aValue;
}
}
}

public DataMember(string aName, string aValue, Type aType)
Expand Down Expand Up @@ -170,14 +170,22 @@ into i
{
if (IsGlobal)
{
aOutput.Write("\tglobal ");
if (aAssembler.Format)
{
aOutput.Write('\t');
}
aOutput.Write("global ");
aOutput.WriteLine(Name);

if (AdditionalNames != null && AdditionalNames.Count() > 0)
{
foreach (var xName in AdditionalNames)
{
aOutput.Write("\tglobal");
if (aAssembler.Format)
{
aOutput.Write('\t');
}
aOutput.Write("global");
aOutput.WriteLine(xName);
}
}
Expand All @@ -189,15 +197,23 @@ into i
{
foreach(var xName in AdditionalNames)
{
aOutput.WriteLine("\t" + xName + ":");
if (aAssembler.Format)
{
aOutput.Write('\t');
}
aOutput.WriteLine(xName + ":");
}
}

aOutput.Write("\t db ");
if (aAssembler.Format)
{
aOutput.Write("\t ");
}
aOutput.Write("db ");
for (int i = 0; i < (RawDefaultValue.Length - 1); i++)
{
aOutput.Write(RawDefaultValue[i]);
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
}
aOutput.Write(RawDefaultValue.Last());
}
Expand All @@ -212,7 +228,11 @@ into i
{
foreach (var xName in AdditionalNames)
{
aOutput.Write("\tglobal");
if (aAssembler.Format)
{
aOutput.Write('\t');
}
aOutput.Write("global");
aOutput.WriteLine(xName);
}
}
Expand All @@ -223,15 +243,23 @@ into i
aOutput.WriteLine(Name + ":");
foreach (var xName in AdditionalNames)
{
aOutput.WriteLine("\t" + xName + ":");
if (aAssembler.Format)
{
aOutput.Write('\t');
}
aOutput.WriteLine(xName + ":");
}
}
else
{
aOutput.Write(Name + ":");
}

aOutput.Write("\t TIMES ");
if (aAssembler.Format)
{
aOutput.Write("\t ");
}
aOutput.Write("TIMES ");
aOutput.Write(RawDefaultValue.Length);
aOutput.Write($" {Size ?? "db"} ");
aOutput.Write(RawDefaultValue[0]);
Expand Down Expand Up @@ -260,12 +288,12 @@ into i
{
return xElementRef.Name;
}
return xElementRef.Name + " + " + xElementRef.Offset;
return xElementRef.Name + aAssembler.AddSymbol + xElementRef.Offset;
};
for (int i = 0; i < (UntypedDefaultValue.Length - 1); i++)
{
aOutput.Write(xGetTextForItem(UntypedDefaultValue[i]));
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
}
aOutput.Write(xGetTextForItem(UntypedDefaultValue.Last()));
return;
Expand All @@ -279,7 +307,7 @@ into i
aOutput.Write(" ");
aOutput.Write(GetStringFromType(Type));
aOutput.Write(" ");
aOutput.Write(StringValue);
aOutput.Write(StringValue);
}
else if (Size != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public override void WriteText(XSharp.Assembler.Assembler aAssembler, System.IO.
aOutput.Write(mMnemonic);
aOutput.Write(" ");
aOutput.Write(this.GetDestinationAsString());
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
aOutput.Write(this.GetSourceAsString());
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
aOutput.Write(this.pseudoOpcode);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override void WriteText( XSharp.Assembler.Assembler aAssembler, System.IO
aOutput.Write(destination);
string source = this.GetSourceAsString();
if (!(SourceEmpty && source.Equals(""))){
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
aOutput.Write(source);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ public override void WriteText( XSharp.Assembler.Assembler aAssembler, System.IO
aOutput.Write(destination);
string source = this.GetSourceAsString();
if (!(SourceEmpty && source.Equals(""))){
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
aOutput.Write(source);
string argument = this.GetArgumentAsString();
if (!(ArgumentEmpty && argument.Equals("")))
{
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
aOutput.Write(argument);
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract class InstructionWithDestinationAndSourceAndSize : InstructionWi
IInstructionWithSize
{
// todo: do all instructions with two operands have a size?
//todo should validate Instructions or use a constructor and no set properties.
//todo should validate Instructions or use a constructor and no set properties.
protected byte mSize;

public InstructionWithDestinationAndSourceAndSize()
Expand Down Expand Up @@ -82,7 +82,7 @@ public override void WriteText(Assembler aAssembler, System.IO.TextWriter aOutpu

aOutput.Write(" ");
aOutput.Write(this.GetDestinationAsString());
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);

if (SourceIsIndirect)
{
Expand All @@ -94,4 +94,4 @@ public override void WriteText(Assembler aAssembler, System.IO.TextWriter aOutpu
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void WriteText(XSharp.Assembler.Assembler aAssembler, System.IO.
}
aOutput.Write(" ");
aOutput.Write("ST0");
aOutput.Write(", ");
aOutput.Write(aAssembler.Separator);
aOutput.Write(this.GetSourceAsString());
}
}
Expand Down
7 changes: 6 additions & 1 deletion source/XSharp/XSharp/Gen1/XS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace XSharp
[SuppressMessage("Usage", "CA1806:Do not ignore method results")]
public static partial class XS
{
public static bool AllowComments = true;

public static void Label(string labelName)
{
new Label(labelName);
Expand Down Expand Up @@ -653,7 +655,10 @@ public static void Jump(string label)

public static void Comment(string comment)
{
new Comment(comment);
if (AllowComments)
{
new Comment(comment);
}
}

public static void Call(string target)
Expand Down

0 comments on commit b93409d

Please sign in to comment.