Jsm.Control.If.Executer.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using OpenVIII.Fields.Scripts.Instructions;
  2. using System.Collections.Generic;
  3. namespace OpenVIII.Fields.Scripts
  4. {
  5. public static partial class Jsm
  6. {
  7. #region Classes
  8. public static partial class Control
  9. {
  10. #region Classes
  11. public sealed partial class If
  12. {
  13. #region Classes
  14. private sealed class Executor : IScriptExecutor
  15. {
  16. #region Fields
  17. private readonly If _aggregator;
  18. #endregion Fields
  19. #region Constructors
  20. public Executor(If aggregator) => _aggregator = aggregator;
  21. #endregion Constructors
  22. #region Methods
  23. public bool CanExecute(JPF jpf, IServices services)
  24. {
  25. foreach (var condition in jpf.Conditions)
  26. {
  27. if (!condition.Boolean(services))
  28. return false;
  29. }
  30. return true;
  31. }
  32. public IEnumerable<IAwaitable> Execute(IServices services)
  33. {
  34. var executable = GetExecutableInstructions(services);
  35. if (executable == null)
  36. yield break;
  37. var executer = ExecutableSegment.GetExecuter(executable);
  38. foreach (var result in executer.Execute(services))
  39. yield return result;
  40. }
  41. private IEnumerable<IJsmInstruction> GetExecutableInstructions(IServices services)
  42. {
  43. if (CanExecute(_aggregator.IfRange.Jpf, services))
  44. return _aggregator.IfRange.GetBodyInstructions();
  45. foreach (var elseIf in _aggregator.ElseIfRanges)
  46. {
  47. if (CanExecute(elseIf.Jpf, services))
  48. {
  49. return elseIf.GetBodyInstructions();
  50. }
  51. }
  52. return _aggregator.ElseRange?.GetBodyInstructions();
  53. }
  54. #endregion Methods
  55. }
  56. #endregion Classes
  57. }
  58. #endregion Classes
  59. }
  60. #endregion Classes
  61. }
  62. }