Jsm.Control.Builder.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using OpenVIII.Fields.Scripts.Instructions;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace OpenVIII.Fields.Scripts
  5. {
  6. public static partial class Jsm
  7. {
  8. #region Classes
  9. public static partial class Control
  10. {
  11. #region Classes
  12. public sealed class Builder
  13. {
  14. #region Fields
  15. private readonly List<JsmInstruction> _instructions;
  16. private readonly ProcessedJumps _processed = new ProcessedJumps();
  17. private readonly List<IJsmControl> _result = new List<IJsmControl>();
  18. private JPF _begin;
  19. private int _index;
  20. #endregion Fields
  21. #region Constructors
  22. private Builder(List<JsmInstruction> instructions) => _instructions = instructions;
  23. #endregion Constructors
  24. #region Methods
  25. public static IReadOnlyList<IJsmControl> Build(List<JsmInstruction> instructions) => new Builder(instructions).Make();
  26. public IReadOnlyList<IJsmControl> Make()
  27. {
  28. for (_index = 0; _index < _instructions.Count; _index++)
  29. {
  30. if (TryMakeGotoOrSkip())
  31. continue;
  32. if (TryMakeWhile())
  33. continue;
  34. if (TryMakeIf())
  35. continue;
  36. throw new InvalidProgramException($"Cannot recognize the logical block: {_begin}");
  37. }
  38. return _result;
  39. }
  40. private void AddIfElseBranches(If control, JPF jpf, JMP jmp)
  41. {
  42. var endOfBlock = jmp.Index;
  43. while (true)
  44. {
  45. var newJpfIndex = jpf.Index;
  46. if (!(_instructions[newJpfIndex] is JPF newJpf) || newJpf.Index > endOfBlock)
  47. {
  48. control.AddElse(jpf.Index, endOfBlock);
  49. return;
  50. }
  51. if (!(_instructions[newJpf.Index - 1] is JMP newJmp))
  52. {
  53. if (newJpf.Index == endOfBlock)
  54. {
  55. // if-elseif without jmp
  56. _processed.Process(newJpf);
  57. control.AddIf(newJpfIndex, newJpf.Index);
  58. }
  59. else
  60. {
  61. // if-else without jmp
  62. control.AddElse(jpf.Index, endOfBlock);
  63. }
  64. return;
  65. }
  66. // Isn't our jump
  67. if (newJmp.Index != endOfBlock)
  68. {
  69. control.AddElse(jpf.Index, endOfBlock);
  70. return;
  71. }
  72. jpf = newJpf;
  73. jmp = newJmp;
  74. _processed.Process(jpf);
  75. _processed.TryProcess(jmp);
  76. control.AddIf(newJpfIndex, jpf.Index);
  77. if (jpf.Index == endOfBlock)
  78. return;
  79. }
  80. }
  81. private bool TryMakeGotoOrSkip()
  82. {
  83. var instruction = _instructions[_index];
  84. if (instruction is JMP @goto && _processed.TryProcess(@goto))
  85. {
  86. var control = new Goto(_instructions, _index, @goto.Index);
  87. _result.Add(control);
  88. return true;
  89. }
  90. if (!(instruction is JPF jpf))
  91. return true;
  92. if (!_processed.TryProcess(jpf))
  93. return true;
  94. _begin = jpf;
  95. return false;
  96. }
  97. private bool TryMakeIf()
  98. {
  99. var jpf = _begin;
  100. var control = new If(_instructions, _index, _begin.Index);
  101. _result.Add(control);
  102. if (!(_instructions[_begin.Index - 1] is JMP jmp))
  103. {
  104. // There is no JMP instruction. Simple if {}
  105. return true;
  106. }
  107. if (jmp.Index == jpf.Index)
  108. {
  109. // It isn't our jump, but an nested if. If { nested if{}<-}
  110. return true;
  111. }
  112. if (jmp.Index < jpf.Index)
  113. {
  114. // It isn't our jump, but an nested loop. If { nested while{}<-}
  115. return true;
  116. }
  117. if (jmp.Index < _index)
  118. {
  119. // It isn't our jump, but an nested goto. If { nested goto l;<-}
  120. return true;
  121. }
  122. _processed.Process(jmp);
  123. AddIfElseBranches(control, jpf, jmp);
  124. return true;
  125. }
  126. private bool TryMakeWhile()
  127. {
  128. if (_instructions[_begin.Index - 1] is JMP jmp && jmp.Index == _index)
  129. {
  130. _processed.TryProcess(jmp);
  131. _result.Add(new While(_instructions, _index, _begin.Index));
  132. return true;
  133. }
  134. return false;
  135. }
  136. #endregion Methods
  137. }
  138. #endregion Classes
  139. }
  140. #endregion Classes
  141. }
  142. }