Jsm.ExecutableSegment.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. namespace FF8
  4. {
  5. public static partial class Jsm
  6. {
  7. public class ExecutableSegment : Segment, IJsmInstruction
  8. {
  9. public ExecutableSegment(Int32 from, Int32 to)
  10. : base(from, to)
  11. {
  12. }
  13. public virtual IScriptExecuter GetExecuter()
  14. {
  15. return GetExecuter(_list);
  16. }
  17. internal static IScriptExecuter GetExecuter(IEnumerable<IJsmInstruction> instructions)
  18. {
  19. return new Executer(instructions);
  20. }
  21. private sealed class Executer : IScriptExecuter
  22. {
  23. private readonly IEnumerable<IJsmInstruction> _list;
  24. public Executer(IEnumerable<IJsmInstruction> list)
  25. {
  26. _list = list;
  27. }
  28. public IEnumerable<IAwaitable> Execute(IServices services)
  29. {
  30. foreach (IJsmInstruction instr in _list)
  31. {
  32. if (instr is JsmInstruction singleInstruction)
  33. {
  34. yield return singleInstruction.Execute(services);
  35. }
  36. else if (instr is ExecutableSegment segment)
  37. {
  38. // TODO: Change recursion to the loop
  39. IScriptExecuter nested = segment.GetExecuter();
  40. foreach (IAwaitable result in nested.Execute(services))
  41. yield return result;
  42. }
  43. else
  44. {
  45. throw new NotSupportedException($"Cannot execute instruction [{instr}] of type [{instr.GetType()}].");
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }