Jsm.Control.While.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace FF8
  6. {
  7. public static partial class Jsm
  8. {
  9. public static partial class Control
  10. {
  11. public sealed partial class While : IJsmControl
  12. {
  13. private readonly List<JsmInstruction> _instructions;
  14. private readonly WhileSegment _segment;
  15. public While(List<JsmInstruction> instructions, Int32 from, Int32 to)
  16. {
  17. _instructions = instructions;
  18. _segment = new WhileSegment(from, to);
  19. _segment.Add(_instructions[from]);
  20. }
  21. public override String ToString()
  22. {
  23. StringBuilder sb = new StringBuilder();
  24. sb.Append("while(");
  25. sb.Append((JPF)_instructions[_segment.From]);
  26. sb.AppendLine(")");
  27. sb.AppendLine("{");
  28. for (Int32 i = _segment.From + 1; i < _segment.To; i++)
  29. {
  30. JsmInstruction instruction = _instructions[i];
  31. sb.Append('\t').AppendLine(instruction.ToString());
  32. }
  33. sb.AppendLine("}");
  34. return base.ToString();
  35. }
  36. public IEnumerable<Segment> EnumerateSegments()
  37. {
  38. yield return _segment;
  39. }
  40. private sealed class WhileSegment : ExecutableSegment
  41. {
  42. public WhileSegment(Int32 from, Int32 to)
  43. : base(from, to)
  44. {
  45. }
  46. public override void ToString(StringBuilder sb)
  47. {
  48. sb.Append("while(");
  49. sb.Append((JPF)_list[0]);
  50. sb.AppendLine(")");
  51. FormatBranch(sb, _list.Skip(1));
  52. }
  53. public override void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services)
  54. {
  55. sw.Append("while(");
  56. ((JPF)_list[0]).Format(sw, formatterContext, services);
  57. sw.AppendLine(")");
  58. FormatBranch(sw, formatterContext, services, _list.Skip(1));
  59. }
  60. public override IScriptExecuter GetExecuter()
  61. {
  62. return new Executer(this);
  63. }
  64. public JPF Jpf => ((JPF)_list[0]);
  65. public IEnumerable<IJsmInstruction> GetBodyInstructions()
  66. {
  67. return _list.Skip(1);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }