JMP.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. namespace OpenVIII.Fields.Scripts.Instructions
  3. {
  4. /// <summary>
  5. /// <para>Jump</para>
  6. /// <para>Jump a number of instructions given by Argument. If Argument is negative, jumps backward.</para>
  7. /// </summary>
  8. /// <see cref="http://wiki.ffrtt.ru/index.php?title=FF8/Field/Script/Opcodes/002_JMP"/>
  9. public sealed class JMP : JsmInstruction, IJumpToOpcode
  10. {
  11. #region Fields
  12. private int _index = -1;
  13. #endregion Fields
  14. #region Constructors
  15. public JMP(int offset) => Offset = offset;
  16. public JMP(int offset, IStack<IJsmExpression> stack)
  17. : this(offset)
  18. {
  19. }
  20. #endregion Constructors
  21. #region Properties
  22. public int Index
  23. {
  24. get
  25. {
  26. if (_index == -1)
  27. throw new ArgumentException($"{nameof(JMP)} instruction isn't indexed yet.", nameof(Index));
  28. return _index;
  29. }
  30. set =>
  31. //if (_index != -1)
  32. // throw new ArgumentException($"{nameof(JMP)} instruction has already been indexed: {_index}.", nameof(Index));
  33. _index = value;
  34. }
  35. /// <summary>
  36. /// Number of instructions to jump (signed value).
  37. /// </summary>
  38. public int Offset { get; set; }
  39. #endregion Properties
  40. #region Methods
  41. public override IAwaitable Execute(IServices services) =>
  42. // This instruction is part of conditional jumps and isn't exists as is.
  43. DummyAwaitable.Instance;
  44. public override void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services)
  45. {
  46. // This instruction is part of conditional jumps and isn't exists as is.
  47. }
  48. public override string ToString() => _index < 0
  49. ? $"{nameof(JMP)}({nameof(Offset)}: {Offset})"
  50. : $"{nameof(JMP)}({nameof(Index)}: {Index})";
  51. #endregion Methods
  52. }
  53. }