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();
|
|
|
|
|
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; }
|
|
|
|
|
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();
|
|
|
|
|
|
2024-02-10 23:03:57 -06:00
|
|
|
sb.AppendJoin(lineEnding, Exports.Select(i => i.ToString()));
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.AppendJoin(lineEnding, Imports.Select(i => i.ToString()));
|
2024-01-31 18:43:04 -06:00
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.Append(lineEnding);
|
2024-02-11 20:38:19 -06:00
|
|
|
sb.AppendJoin(lineEnding, Directives.Where(d => d is not (IImportDirective or ExportDirective)).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
|
|
|
}
|