Jsm.Control.If.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using OpenVIII.Fields.Scripts.Instructions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace OpenVIII.Fields.Scripts
  6. {
  7. public static partial class Jsm
  8. {
  9. public static partial class Control
  10. {
  11. public sealed partial class If : IJsmControl
  12. {
  13. private readonly List<JsmInstruction> _instructions;
  14. private readonly IfSegment IfRange;
  15. private readonly List<ElseIfSegment> ElseIfRanges = new List<ElseIfSegment>();
  16. private ElseSegment ElseRange;
  17. public If(List<JsmInstruction> instructions, Int32 from, Int32 to)
  18. {
  19. _instructions = instructions;
  20. IfRange = new IfSegment(from, to, this);
  21. IfRange.Add(_instructions[from]);
  22. }
  23. public void AddIf(Int32 from, Int32 to)
  24. {
  25. ElseIfSegment elseIf = new ElseIfSegment(from, to);
  26. elseIf.Add(_instructions[from]);
  27. ElseIfRanges.Add(elseIf);
  28. }
  29. public void AddElse(Int32 from, Int32 to)
  30. {
  31. if (ElseRange != null)
  32. throw new InvalidOperationException($"Cannot replace existing else ({ElseRange}) by a new one.");
  33. ElseRange = new ElseSegment(from, to);
  34. }
  35. public override String ToString()
  36. {
  37. StringBuilder sb = new StringBuilder();
  38. FormatIf(sb, IfRange);
  39. FormatBranch(sb, IfRange);
  40. foreach (var item in ElseIfRanges)
  41. {
  42. FormatElseIf(sb, item);
  43. FormatBranch(sb, item);
  44. }
  45. if (ElseRange != null)
  46. {
  47. FormatElse(sb, ElseRange);
  48. FormatBranch(sb, ElseRange);
  49. }
  50. return sb.ToString();
  51. }
  52. public IEnumerable<Segment> EnumerateSegments()
  53. {
  54. yield return IfRange;
  55. foreach (var block in EnumerateElseBlocks())
  56. yield return block;
  57. }
  58. private IEnumerable<Segment> EnumerateElseBlocks()
  59. {
  60. foreach (var item in ElseIfRanges)
  61. yield return item;
  62. if (ElseRange != null)
  63. yield return ElseRange;
  64. }
  65. private void FormatIf(StringBuilder sb, Segment ifRange)
  66. {
  67. sb.Append("if(");
  68. sb.Append((JPF)_instructions[ifRange.From]);
  69. sb.AppendLine(")");
  70. }
  71. private void FormatElseIf(StringBuilder sb, Segment elseIfRange)
  72. {
  73. sb.Append("else if(");
  74. sb.Append((JPF)_instructions[elseIfRange.From]);
  75. sb.AppendLine(")");
  76. }
  77. private void FormatElse(StringBuilder sb, Segment elseRange)
  78. {
  79. sb.AppendLine("else");
  80. }
  81. private void FormatBranch(StringBuilder sb, Segment range)
  82. {
  83. sb.AppendLine("{");
  84. for (Int32 i = range.From + 1; i < range.To; i++)
  85. {
  86. JsmInstruction instruction = _instructions[i];
  87. sb.Append('\t').AppendLine(instruction.ToString());
  88. }
  89. sb.AppendLine("}");
  90. }
  91. }
  92. }
  93. }
  94. }