Jsm.Control.If.Executer.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 If
  11. {
  12. private sealed class Executer : IScriptExecuter
  13. {
  14. private readonly If _aggregator;
  15. public Executer(If aggregator)
  16. {
  17. _aggregator = aggregator;
  18. }
  19. public IEnumerable<IAwaitable> Execute(IServices services)
  20. {
  21. IEnumerable<IJsmInstruction> executable = GetExecutableInstructions(services);
  22. if (executable == null)
  23. yield break;
  24. IScriptExecuter executer = ExecutableSegment.GetExecuter(executable);
  25. foreach (IAwaitable result in executer.Execute(services))
  26. yield return result;
  27. }
  28. public Boolean CanExecute(JPF jpf, IServices services)
  29. {
  30. foreach (var condition in jpf.Conditions)
  31. {
  32. if (!condition.Boolean(services))
  33. return false;
  34. }
  35. return true;
  36. }
  37. private IEnumerable<IJsmInstruction> GetExecutableInstructions(IServices services)
  38. {
  39. if (CanExecute(_aggregator.IfRange.Jpf, services))
  40. return _aggregator.IfRange.GetBodyInstructions();
  41. foreach (var elseIf in _aggregator.ElseIfRanges)
  42. {
  43. if (CanExecute(elseIf.Jpf, services))
  44. {
  45. return elseIf.GetBodyInstructions();
  46. }
  47. }
  48. return _aggregator.ElseRange?.GetBodyInstructions();
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }