2024-01-29 21:42:06 -06:00
|
|
|
using Microsoft.Iris.Markup;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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 18:50:45 -06:00
|
|
|
public record Instruction(string Mnemonic, IEnumerable<Operand> Operands) : BodyItem
|
2024-01-29 21:42:06 -06:00
|
|
|
{
|
2024-01-31 16:58:28 -06:00
|
|
|
public Instruction(OpCode opCode, OperationType? operationType, IEnumerable<Operand> Operands)
|
|
|
|
|
: this(LexerMaps.GetMnemonic(opCode, operationType), Operands)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
public Instruction(OpCode opCode, IEnumerable<Operand> Operands)
|
|
|
|
|
: this(opCode, null, Operands)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 21:42:06 -06:00
|
|
|
public OpCode OpCode => LexerMaps.MnemonicToOpCode(Mnemonic);
|
|
|
|
|
public OperationType? OperationType => LexerMaps.TryOperationMnemonicToType(Mnemonic);
|
|
|
|
|
|
2024-01-31 18:44:31 -06:00
|
|
|
public override string ToString() => Operands.Any()
|
|
|
|
|
? $"{Mnemonic} {string.Join(", ", Operands)}"
|
|
|
|
|
: Mnemonic;
|
2024-01-29 21:42:06 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 18:50:45 -06:00
|
|
|
public record Label(string Name) : BodyItem
|
2024-01-29 21:42:06 -06:00
|
|
|
{
|
|
|
|
|
public override string ToString() => $"{Name}:";
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-04 18:50:45 -06:00
|
|
|
public record Operand(object Value, string Content = null) : AsmItem
|
2024-01-29 21:42:06 -06:00
|
|
|
{
|
2024-01-30 23:30:11 -06:00
|
|
|
public override string ToString() => Content ?? Value.ToString();
|
2024-01-29 21:42:06 -06:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 22:44:48 -06:00
|
|
|
public record Program(IEnumerable<IImport> Imports, IEnumerable<IBodyItem> Body)
|
2024-01-29 21:42:06 -06:00
|
|
|
{
|
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-01-31 18:43:04 -06:00
|
|
|
sb.AppendJoin(lineEnding, Imports.Select(i => i.ToString()));
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
sb.Append(lineEnding);
|
|
|
|
|
|
|
|
|
|
foreach (var bodyItem in Body)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|