2024-02-08 10:50:57 -06:00
|
|
|
using System.Collections.Generic;
|
2024-02-04 22:09:21 -06:00
|
|
|
using System.Diagnostics;
|
2024-01-29 21:42:06 -06:00
|
|
|
using System.Linq;
|
2024-01-31 16:58:28 -06:00
|
|
|
using System.Text;
|
2024-01-29 21:42:06 -06:00
|
|
|
|
2024-01-31 09:21:30 -06:00
|
|
|
namespace Microsoft.Iris.Asm.Models;
|
2024-01-29 21:42:06 -06:00
|
|
|
|
2024-02-04 22:09:21 -06:00
|
|
|
[DebuggerDisplay("{Name} " + DebuggerDisplay)]
|
2024-02-10 23:03:57 -06:00
|
|
|
public record Label(string Name) : CodeItem
|
2024-01-29 21:42:06 -06:00
|
|
|
{
|
|
|
|
|
public override string ToString() => $"{Name}:";
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 20:38:19 -06:00
|
|
|
public record QualifiedTypeName(string NamespacePrefix, string TypeName) : AsmItem
|
|
|
|
|
{
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
if (NamespacePrefix is null)
|
|
|
|
|
return TypeName;
|
|
|
|
|
else
|
|
|
|
|
return $"{NamespacePrefix}:{TypeName}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 23:03:57 -06:00
|
|
|
public record Program
|
2024-01-29 21:42:06 -06:00
|
|
|
{
|
2024-02-10 23:03:57 -06:00
|
|
|
public Program(IEnumerable<IBodyItem> body)
|
|
|
|
|
{
|
|
|
|
|
Body = body.Cached();
|
|
|
|
|
|
|
|
|
|
Directives = body.OfType<IDirective>().Cached();
|
2025-01-27 19:15:30 -06:00
|
|
|
DataTableDirective = Directives.OfType<SharedDataTableDirective>().SingleOrDefault();
|
2024-02-10 23:03:57 -06:00
|
|
|
Imports = Directives.OfType<IImportDirective>().Cached();
|
|
|
|
|
Exports = Directives.OfType<ExportDirective>().Cached();
|
|
|
|
|
|
|
|
|
|
Code = body.OfType<ICodeItem>().Cached();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IBodyItem> Body { get; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IDirective> Directives { get; }
|
2025-01-27 19:15:30 -06:00
|
|
|
public SharedDataTableDirective DataTableDirective { get; }
|
2024-02-10 23:03:57 -06:00
|
|
|
public IEnumerable<IImportDirective> Imports { get; }
|
|
|
|
|
public IEnumerable<ExportDirective> Exports { get; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ICodeItem> Code { get; }
|
|
|
|
|
|
2024-01-31 16:58:28 -06:00
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2024-01-31 18:43:04 -06:00
|
|
|
const string lineEnding = "\r\n";
|
|
|
|
|
const string indent = " ";
|
2024-01-31 16:58:28 -06:00
|
|
|
StringBuilder sb = new();
|
|
|
|
|
|
2025-01-27 19:15:30 -06:00
|
|
|
if (DataTableDirective is not null)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(DataTableDirective);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Exports.Any())
|
|
|
|
|
{
|
|
|
|
|
sb.AppendJoin(lineEnding, Exports.Select(i => i.ToString()));
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Imports.Any())
|
|
|
|
|
{
|
|
|
|
|
var sortedImports = Imports
|
|
|
|
|
.OrderBy(import => import.Type switch
|
|
|
|
|
{
|
|
|
|
|
"ns" => 0,
|
|
|
|
|
"type" => 1,
|
|
|
|
|
"ctor" => 2,
|
|
|
|
|
"mthd" => 3,
|
|
|
|
|
"mbrs" => 4,
|
|
|
|
|
_ => int.MaxValue
|
|
|
|
|
})
|
|
|
|
|
.Select(i => i.ToString());
|
|
|
|
|
|
|
|
|
|
sb.AppendJoin(lineEnding, sortedImports);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var otherDirectives = Directives.Where(d => d is not (IImportDirective or ExportDirective or SharedDataTableDirective));
|
|
|
|
|
if (otherDirectives.Any())
|
|
|
|
|
{
|
|
|
|
|
sb.AppendJoin(lineEnding, otherDirectives.Select(i => i.ToString()));
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
}
|
2024-01-31 18:43:04 -06:00
|
|
|
|
2024-02-10 23:03:57 -06:00
|
|
|
foreach (var bodyItem in Code)
|
2024-01-31 18:43:04 -06:00
|
|
|
{
|
|
|
|
|
if (bodyItem is Instruction)
|
|
|
|
|
sb.Append(indent);
|
|
|
|
|
|
|
|
|
|
sb.Append(bodyItem.ToString());
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
}
|
2024-01-31 16:58:28 -06:00
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2024-01-29 21:42:06 -06:00
|
|
|
}
|