ExpressNode.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. *
  3. * @author seagle
  4. * @date 2024-7-10
  5. * @Description 表达式结点
  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 delegate void UserFunction(ExpressNode curr, bool OnlyCheckDataType = false);
  15. public class ExpressNode
  16. {
  17. public KeywordType NodeType { get; set;}
  18. public Variable Var { get; set; }
  19. public UserFunction UserFunc;
  20. public bool Closed { get; set; }
  21. public ExpressNode FirstChild, LastChild, NextBrother, PrevBrother, Father;
  22. public ExpressNode(Variable var,UserFunction userFunc,KeywordType nodeType )
  23. {
  24. Var = var;
  25. UserFunc = userFunc;
  26. NodeType = nodeType;
  27. FirstChild = LastChild = NextBrother = PrevBrother = Father = null;
  28. Closed = false;
  29. }
  30. public int ComparePrior(KeywordType kw)
  31. {
  32. int prior1 = getOperatorPrior(NodeType);
  33. int prior2 = getOperatorPrior(kw);
  34. if (prior1 < 0 || prior2 < 0)
  35. {
  36. throw new Exception("Can not compare prior [" + NodeType + "] and [" + kw + "]");
  37. }
  38. if (prior1 < prior2)
  39. {
  40. return -1;
  41. }
  42. else if (prior1 > prior2)
  43. {
  44. return 1;
  45. }
  46. else
  47. {
  48. if ((NodeType == KeywordType.KW_OPERATOR_PLUS && kw == KeywordType.KW_OPERATOR_PLUS) ||
  49. (NodeType == KeywordType.KW_OPERATOR_MULTIPLE && kw == KeywordType.KW_OPERATOR_MULTIPLE) ||
  50. (NodeType == KeywordType.KW_OPERATOR_AND && kw == KeywordType.KW_OPERATOR_AND) ||
  51. (NodeType == KeywordType.KW_OPERATOR_OR && kw == KeywordType.KW_OPERATOR_OR) ||
  52. (NodeType == KeywordType.KW_OPERATOR_BIT_AND && kw == KeywordType.KW_OPERATOR_BIT_AND) ||
  53. (NodeType == KeywordType.KW_OPERATOR_BIT_OR && kw == KeywordType.KW_OPERATOR_BIT_OR) ||
  54. (NodeType == KeywordType.KW_OPERATOR_COMMA && kw == KeywordType.KW_OPERATOR_COMMA))
  55. {
  56. return 0;
  57. }
  58. else
  59. {
  60. return 1;
  61. }
  62. }
  63. }
  64. //运算符优先级
  65. private static int getOperatorPrior(KeywordType Operator)
  66. {
  67. switch (Operator)
  68. {
  69. case KeywordType.KW_FUNCTION:
  70. case KeywordType.KW_ROUND_LEFT:
  71. return 0;
  72. case KeywordType.KW_OPERATOR_COMMA:
  73. return 1;
  74. case KeywordType.KW_OPERATOR_OR:
  75. return 2;
  76. case KeywordType.KW_OPERATOR_AND:
  77. return 3;
  78. case KeywordType.KW_OPERATOR_NOT:
  79. return 4;
  80. case KeywordType.KW_OPERATOR_GT:
  81. case KeywordType.KW_OPERATOR_GE:
  82. case KeywordType.KW_OPERATOR_LT:
  83. case KeywordType.KW_OPERATOR_LE:
  84. case KeywordType.KW_OPERATOR_EQUAL:
  85. case KeywordType.KW_OPERATOR_NOT_EQUAL:
  86. return 5;
  87. case KeywordType.KW_OPERATOR_PLUS:
  88. case KeywordType.KW_OPERATOR_MINUS:
  89. return 6;
  90. case KeywordType.KW_OPERATOR_MULTIPLE:
  91. case KeywordType.KW_OPERATOR_DEVIDE:
  92. return 7;
  93. case KeywordType.KW_OPERATOR_NEGATIVE:
  94. return 8;
  95. case KeywordType.KW_OPERATOR_BIT_OR:
  96. return 9;
  97. case KeywordType.KW_OPERATOR_BIT_AND:
  98. return 10;
  99. case KeywordType.KW_OPERATOR_BIT_NOT:
  100. return 11;
  101. case KeywordType.KW_OPERATOR_MOVE_LEFT:
  102. case KeywordType.KW_OPERATOR_MOVE_RIGHT:
  103. return 12;
  104. default:
  105. return -1;
  106. }
  107. }
  108. }
  109. }