2024-02-04 21:44:53 -06:00
|
|
|
using Microsoft.Iris.Asm.Models;
|
|
|
|
|
using Sprache;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Iris.Asm;
|
|
|
|
|
|
|
|
|
|
partial class Lexer
|
|
|
|
|
{
|
|
|
|
|
private static IResult<IBodyItem> ParseBodyItem(IInput input)
|
|
|
|
|
{
|
|
|
|
|
input = ConsumeWhitespace(input);
|
2024-02-04 22:09:21 -06:00
|
|
|
var line = input.Line;
|
|
|
|
|
var column = input.Column;
|
2024-02-04 21:44:53 -06:00
|
|
|
|
2024-02-05 09:23:46 -06:00
|
|
|
var directiveResult = ParseDirective(input);
|
|
|
|
|
if (directiveResult.WasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
input = directiveResult.Remainder;
|
|
|
|
|
var directive = directiveResult.Value;
|
|
|
|
|
if (directive is not IBodyItem directiveBodyItem)
|
|
|
|
|
return Result.Failure<IBodyItem>(input, "Invalid code", [$"The {directive.Identifier} directive cannot be placed in the body of a program"]);
|
|
|
|
|
|
|
|
|
|
directive.Line = line;
|
|
|
|
|
directive.Column = column;
|
|
|
|
|
return Result.Success(directiveBodyItem, input);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 21:37:12 -06:00
|
|
|
var identifierResult = Identifier.Invoke(input);
|
2024-02-04 21:44:53 -06:00
|
|
|
if (!identifierResult.WasSuccessful)
|
2024-02-05 09:23:46 -06:00
|
|
|
return Result.Failure<IBodyItem>(input, "Invalid code", []);
|
2024-02-04 21:44:53 -06:00
|
|
|
|
|
|
|
|
input = identifierResult.Remainder;
|
|
|
|
|
var identifier = identifierResult.Value;
|
|
|
|
|
IBodyItem bodyItem;
|
|
|
|
|
|
|
|
|
|
if (!input.AtEnd && input.Current == ':')
|
|
|
|
|
{
|
|
|
|
|
input = input.Advance();
|
2024-02-04 22:09:21 -06:00
|
|
|
bodyItem = new Label(identifier)
|
|
|
|
|
{
|
|
|
|
|
Line = line, Column = column,
|
|
|
|
|
};
|
2024-02-04 21:44:53 -06:00
|
|
|
input = StatementEnd(input).Remainder;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
List<Operand> operands = new();
|
|
|
|
|
var endOfInstructionResult = StatementEnd(input);
|
|
|
|
|
input = endOfInstructionResult.Remainder;
|
|
|
|
|
|
|
|
|
|
if (!endOfInstructionResult.WasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
|
|
|
|
|
var operandsResult = Parse.Ref(() => AlphanumericText).DelimitedBy(Parse.Char(',').Token())(input);
|
|
|
|
|
input = operandsResult.Remainder;
|
2024-02-06 12:55:46 -06:00
|
|
|
|
|
|
|
|
var opCode = InstructionSet.MnemonicToOpCode(identifier);
|
|
|
|
|
var schema = InstructionSet.InstructionSchema[opCode];
|
|
|
|
|
|
|
|
|
|
int schemaIndex = 0;
|
|
|
|
|
foreach (var operandContent in operandsResult.Value)
|
|
|
|
|
{
|
|
|
|
|
var operandType = schema[schemaIndex++];
|
|
|
|
|
|
|
|
|
|
object operandValue = operandType switch
|
|
|
|
|
{
|
|
|
|
|
OperandDataType.Byte => byte.Parse(operandContent),
|
|
|
|
|
OperandDataType.UInt16 => ushort.Parse(operandContent),
|
|
|
|
|
OperandDataType.UInt32 => uint.Parse(operandContent),
|
|
|
|
|
OperandDataType.Int32 => int.Parse(operandContent),
|
|
|
|
|
OperandDataType.Bytes or _ => operandContent,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
operands.Add(new(operandValue, operandType, operandContent)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-04 21:44:53 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 22:09:21 -06:00
|
|
|
bodyItem = new Instruction(identifier, operands)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = column,
|
|
|
|
|
};
|
2024-02-04 21:44:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.Success(bodyItem, input);
|
|
|
|
|
}
|
|
|
|
|
}
|