Jsm.Segment.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 abstract partial class Segment : IFormattableScript
  11. {
  12. #region Fields
  13. protected readonly List<IJsmInstruction> _list = new List<IJsmInstruction>();
  14. #endregion Fields
  15. #region Constructors
  16. public Segment(int from, int to)
  17. {
  18. From = from;
  19. To = to;
  20. }
  21. #endregion Constructors
  22. #region Properties
  23. public int From { get; }
  24. public int To { get; }
  25. #endregion Properties
  26. #region Methods
  27. public void Add(IJsmInstruction value) => _list.Add(value);
  28. public virtual void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services) => FormatItems(sw, formatterContext, services, _list);
  29. public override string ToString()
  30. {
  31. var sb = new StringBuilder();
  32. ToString(sb);
  33. return sb.ToString();
  34. }
  35. public virtual void ToString(StringBuilder sb)
  36. {
  37. foreach (var item in _list)
  38. sb.AppendLine(item.ToString());
  39. }
  40. protected void FormatBranch(StringBuilder sb, IEnumerable<object> items)
  41. {
  42. sb.AppendLine("{");
  43. var pos = sb.Length;
  44. AppendItems(sb, items);
  45. if (sb.Length == pos)
  46. sb.AppendLine("// do nothing");
  47. sb.AppendLine("}");
  48. }
  49. protected void FormatBranch(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext, IEnumerable<IJsmInstruction> items)
  50. {
  51. sw.AppendLine("{");
  52. sw.Indent++;
  53. var state = sw.RememberState();
  54. FormatItems(sw, formatterContext, executionContext, items);
  55. if (!state.IsChanged)
  56. {
  57. sw.AppendLine("// do nothing");
  58. }
  59. sw.Indent--;
  60. sw.AppendLine("}");
  61. }
  62. private static void AppendItems(StringBuilder sb, IEnumerable<object> items)
  63. {
  64. var position = -1;
  65. JMP lastItem = null;
  66. foreach (var item in items)
  67. {
  68. if (lastItem != null)
  69. throw new InvalidProgramException($"Unexpected jump: {lastItem}");
  70. lastItem = item as JMP;
  71. position = sb.Length;
  72. sb.Append('\t').AppendLine(item.ToString());
  73. }
  74. if (lastItem != null)
  75. sb.Length = position;
  76. }
  77. private static void FormatItems(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext, IEnumerable<IJsmInstruction> items)
  78. {
  79. foreach (var item in items)
  80. {
  81. if (item is IFormattableScript formattable)
  82. formattable.Format(sw, formatterContext, executionContext);
  83. else
  84. sw.AppendLine(item.ToString());
  85. }
  86. }
  87. #endregion Methods
  88. }
  89. #endregion Classes
  90. }
  91. }