IfSentence.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. *
  3. * @author seagle
  4. * @date 2024-7-22
  5. * @Description if语句处理
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace MECF.Framework.UI.Core.DxfScript
  13. {
  14. public class IfSentence:Sentence
  15. {
  16. private Express ifExpr = null;
  17. BlockSentence ifBlock = null;
  18. BlockSentence elseBlock = null;
  19. public override void Create(StringReader reader)
  20. {
  21. KeywordType kw = reader.Next();
  22. if (kw != KeywordType.KW_IF)
  23. {
  24. throw new Exception("SetSentence: if expected");
  25. }
  26. kw = reader.Next();
  27. if (kw != KeywordType.KW_ROUND_LEFT)
  28. {
  29. throw new Exception("Round left expected in if sentence");
  30. }
  31. ifExpr = new Express();
  32. ifExpr.Create(reader);
  33. kw = reader.Next();
  34. if (kw != KeywordType.KW_ROUND_RIGHT)
  35. {
  36. throw new Exception("Round right expected in if sentence");
  37. }
  38. kw = reader.Next();
  39. if (kw != KeywordType.KW_BRACE_LEFT)
  40. {
  41. throw new Exception("Brace left expected in if sentence");
  42. }
  43. ifBlock = new BlockSentence();
  44. ifBlock.Create(reader);
  45. kw = reader.Next();
  46. if(kw== KeywordType.KW_ELSE)
  47. {
  48. kw = reader.Next();
  49. if (kw != KeywordType.KW_BRACE_LEFT)
  50. {
  51. throw new Exception("Brace left expected in if-else sentence");
  52. }
  53. elseBlock = new BlockSentence();
  54. elseBlock.Create(reader);
  55. }
  56. else
  57. {
  58. if(kw!= KeywordType.KW_EOF)
  59. {
  60. reader.rollback(kw);
  61. }
  62. }
  63. }
  64. public override void Execute()
  65. {
  66. ifExpr.Compute();
  67. if (ifExpr.Root.Var.BoolValue)
  68. {
  69. if(ifBlock != null)
  70. {
  71. ifBlock.Execute();
  72. if (ifBlock.ShouldReturn)
  73. {
  74. ShouldReturn = true;
  75. ReturnVar = ifBlock.ReturnVar;
  76. return;
  77. }
  78. }
  79. }
  80. else
  81. {
  82. if (elseBlock != null)
  83. {
  84. elseBlock.Execute();
  85. if (elseBlock.ShouldReturn)
  86. {
  87. ShouldReturn = true;
  88. ReturnVar = elseBlock.ReturnVar;
  89. return;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }