Jsm.Control.While.cs 2.9 KB

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