private static int ElementLength(Opcode opcode, OpcodeEncodingElement encoding, dynamic value) { switch (encoding) { case OpcodeEncodingElement.Byte: return(1); case OpcodeEncodingElement.Short: return(2); case OpcodeEncodingElement.MessageId: return(3); case OpcodeEncodingElement.Int: return(4); case OpcodeEncodingElement.Address: return(2); case OpcodeEncodingElement.JumpOffset: return(4); case OpcodeEncodingElement.NumberArgument: return(NumberLength((NumberSpec)value)); case OpcodeEncodingElement.AddressArray: return(1 + 2 * ((ImmutableArray <ushort>)value).Length); case OpcodeEncodingElement.NumberArray: return(1 + ((ImmutableArray <NumberSpec>)value).Select(NumberLength).Sum()); case OpcodeEncodingElement.JumpOffsetArray: return(1 + 4 * ((ImmutableArray <int>)value).Length); case OpcodeEncodingElement.String: return(1 + StringLength(opcode, (string)value) + 1); case OpcodeEncodingElement.LongString: return(2 + StringLength(opcode, (string)value) + 1); case OpcodeEncodingElement.StringArray: return(1 + ((ImmutableArray <string>)value).Select(_ => StringLength(opcode, _) + 1).Sum() + 1); case OpcodeEncodingElement.BitmappedNumberArguments: return(1 + ((ImmutableArray <NumberSpec>)value).Select(_ => _.Value == 0 ? 0 : NumberLength(_)) .Sum()); case OpcodeEncodingElement.PostfixNotationExpression: return(RpneLength((PostfixExpression)value)); case OpcodeEncodingElement.Opcode65: return(Opcode65Length((Opcode65)value)); default: throw new ArgumentOutOfRangeException(nameof(encoding), encoding, null); } }
private string FormatElement(Opcode opcode, int index, OpcodeEncodingElement element, dynamic data) { return(element switch { OpcodeEncodingElement.Byte => $"{data}b", OpcodeEncodingElement.Short => $"{data}s", OpcodeEncodingElement.Address => FormatNumber(NumberSpec.FromAddress(data)), OpcodeEncodingElement.Int => $"{data}i", OpcodeEncodingElement.String => $"S{FormatString(data)}", OpcodeEncodingElement.LongString => $"L{FormatString(data)}", OpcodeEncodingElement.NumberArgument => FormatNumber(data), OpcodeEncodingElement.JumpOffset => FormatJumpOffset(data), OpcodeEncodingElement.AddressArray => $"a[{string.Join(",", ((ImmutableArray<ushort>) data).Select(NumberSpec.FromAddress).Select(FormatNumber))}]", OpcodeEncodingElement.StringArray => $"S[{string.Join(",", ((ImmutableArray<string>)data).Select(FormatString))}]", OpcodeEncodingElement.NumberArray => $"n[{string.Join(",", ((ImmutableArray<NumberSpec>) data).Select(FormatNumber))}]", OpcodeEncodingElement.JumpOffsetArray => $"j[{string.Join(",", ((ImmutableArray<int>) data).Select(FormatJumpOffset))}]", OpcodeEncodingElement.PostfixNotationExpression => $"rpne{{{FormatPostfixExpression(data)}}}", OpcodeEncodingElement.BitmappedNumberArguments => $"bmn[{string.Join(",", ((ImmutableArray<NumberSpec>) data).Select(FormatNumber))}]", OpcodeEncodingElement.MessageId => $"{data}mi", _ => throw new ArgumentOutOfRangeException() });