JPF.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. namespace OpenVIII.Fields.Scripts.Instructions
  4. {
  5. /// <summary>
  6. /// <para>Jump Forward with condition</para>
  7. /// <para>Jump forward a number of instructions given by the Argument if condition is equal to 0. Else does nothing. The condition is always popped from the stack.</para>
  8. /// </summary>
  9. /// <see cref="http://wiki.ffrtt.ru/index.php?title=FF8/Field/Script/Opcodes/003_JPF"/>
  10. public sealed class JPF : JsmInstruction, IJumpToOpcode, IFormattableScript
  11. {
  12. #region Fields
  13. private readonly List<IJsmExpression> _conditions = new List<IJsmExpression>();
  14. private int _index = -1;
  15. #endregion Fields
  16. #region Constructors
  17. public JPF(int offset, IJsmExpression condition)
  18. {
  19. Offset = offset;
  20. _conditions.Add(condition);
  21. }
  22. public JPF(int offset, IStack<IJsmExpression> stack)
  23. : this(offset,
  24. condition: stack.Pop())
  25. {
  26. }
  27. #endregion Constructors
  28. #region Properties
  29. public IReadOnlyList<IJsmExpression> Conditions => _conditions;
  30. public int Index
  31. {
  32. get
  33. {
  34. if (_index == -1)
  35. throw new ArgumentException($"{nameof(JPF)} instruction isn't indexed yet.", nameof(Index));
  36. return _index;
  37. }
  38. set =>
  39. //if (_index != -1)
  40. // throw new ArgumentException($"{nameof(JPF)} instruction has already been indexed: {_index}.", nameof(Index));
  41. _index = value;
  42. }
  43. /// <summary>
  44. /// Number of instructions to jump forward. (in Deling's editor, this is just a label)
  45. /// </summary>
  46. public int Offset { get; set; }
  47. #endregion Properties
  48. #region Methods
  49. public override void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services)
  50. {
  51. var state = sw.RememberState();
  52. var isTrue = true;
  53. foreach (var expression in _conditions)
  54. {
  55. if (expression is IConstExpression number)
  56. {
  57. if (number.Value == 0)
  58. {
  59. // We can ignore the other conditions if one of them is always false
  60. state.Cancel();
  61. sw.Append("false");
  62. return;
  63. }
  64. // We don't need to add a condition that is always true
  65. }
  66. else
  67. {
  68. isTrue = false;
  69. if (state.IsChanged)
  70. sw.Append(" && ");
  71. expression.Format(sw, formatterContext, services);
  72. }
  73. }
  74. if (isTrue)
  75. {
  76. // We can ignore conditions if all of them is always true
  77. state.Cancel();
  78. sw.Append("true");
  79. }
  80. }
  81. public void Inverse(JMP nextJmp)
  82. {
  83. if (_conditions.Count != 1)
  84. throw new NotSupportedException($"Conditional jump already merged with an other one: {this}");
  85. var expression = _conditions[0];
  86. if (!(expression is ILogicalExpression constExpression))
  87. _conditions[0] = new Jsm.Expression.CAL.LogNot(expression);
  88. else
  89. _conditions[0] = constExpression.LogicalInverse();
  90. var jmpIndex = nextJmp.Index;
  91. var jmpOffset = nextJmp.Offset;
  92. nextJmp.Index = Index;
  93. nextJmp.Offset = Offset;
  94. Index = jmpIndex;
  95. Offset = jmpOffset;
  96. }
  97. public override string ToString() => _index < 0
  98. ? $"{nameof(JPF)}[{nameof(Offset)}: {Offset}, {nameof(Conditions)}: ( {string.Join(") && (", Conditions)} )]"
  99. : $"{nameof(JPF)}[{nameof(Index)}: {Index}, {nameof(Conditions)}: ( {string.Join(") && (", Conditions)} )]";
  100. public void Union(JPF newJpf)
  101. {
  102. foreach (var cond in newJpf.Conditions)
  103. _conditions.Add(cond);
  104. }
  105. #endregion Methods
  106. }
  107. }