Jsm.Control.If.cs 4.0 KB

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