Jsm.Control.If.IfSegment.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 If
  13. {
  14. private sealed class IfSegment : ExecutableSegment
  15. {
  16. private readonly If _aggregator;
  17. public IfSegment(Int32 from, Int32 to, If aggregator)
  18. : base(from, to)
  19. {
  20. _aggregator = aggregator;
  21. }
  22. public override void ToString(StringBuilder sb)
  23. {
  24. sb.Append("if(");
  25. sb.Append((JPF)_list[0]);
  26. sb.AppendLine(")");
  27. FormatBranch(sb, _list.Skip(1));
  28. }
  29. public override void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services)
  30. {
  31. sw.Append("if(");
  32. Jpf.Format(sw, formatterContext, services);
  33. sw.AppendLine(")");
  34. FormatBranch(sw, formatterContext, services, GetBodyInstructions());
  35. foreach (var item in _aggregator.EnumerateElseBlocks())
  36. item.Format(sw, formatterContext, services);
  37. }
  38. public override IScriptExecuter GetExecuter()
  39. {
  40. return new Executer(_aggregator);
  41. }
  42. public JPF Jpf => ((JPF)_list[0]);
  43. public IEnumerable<IJsmInstruction> GetBodyInstructions()
  44. {
  45. return _list.Skip(1);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }