Jsm.Control.While.Executer.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using OpenVIII.Fields.Scripts.Instructions;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace OpenVIII.Fields.Scripts
  5. {
  6. public static partial class Jsm
  7. {
  8. public static partial class Control
  9. {
  10. public sealed partial class While
  11. {
  12. private sealed class Executer : IScriptExecuter
  13. {
  14. private readonly WhileSegment _seg;
  15. public Executer(WhileSegment seg)
  16. {
  17. _seg = seg;
  18. }
  19. public IEnumerable<IAwaitable> Execute(IServices services)
  20. {
  21. while (CanExecute(_seg.Jpf, services))
  22. {
  23. IEnumerable<IJsmInstruction> executable = _seg.GetBodyInstructions();
  24. IScriptExecuter executer = ExecutableSegment.GetExecuter(executable);
  25. foreach (IAwaitable result in executer.Execute(services))
  26. yield return result;
  27. // Skip one iteration to give control to other operations.
  28. yield return SpinAwaitable.Instance;
  29. }
  30. }
  31. private Boolean CanExecute(JPF jpf, IServices services)
  32. {
  33. foreach (var condition in jpf.Conditions)
  34. {
  35. if (!condition.Boolean(services))
  36. return false;
  37. }
  38. return true;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }