OperatorCode.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. namespace NTERA.EmuEra.Game.EraEmu.GameData.Expression
  3. {
  4. internal enum OperatorCode
  5. {
  6. //単項 > "*/%" > "+-" > 比較 > ビット演算 > 論理演算 > 代入
  7. //xx > 90 > 80 > 65,60(不等価優先) > 50 > 40 > xx
  8. //優先順は本家に準拠
  9. NULL = 0,
  10. __PRIORITY_MASK__ = 0xFF,
  11. __UNARY__ = 0x10000,//単項
  12. __BINARY__ = 0x20000,//2項
  13. __TERNARY__ = 0x40000,//3項
  14. __UNARY_AFTER__ = 0x80000,//後置単項
  15. Plus = 0x0100 + 0x80 | __UNARY__ | __BINARY__,//"+"単項可
  16. Minus = 0x0200 + 0x80 | __UNARY__ | __BINARY__,//"-"
  17. Mult = 0x0300 + 0x90 | __BINARY__,//"*"
  18. Div = 0x0400 + 0x90 | __BINARY__,//"/"
  19. Mod = 0x0500 + 0x90 | __BINARY__,//"%"
  20. Equal = 0x0600 + 0x60 | __BINARY__,//"=="
  21. Greater = 0x0700 + 0x65 | __BINARY__,//">"
  22. Less = 0x0800 + 0x65 | __BINARY__,//"<"
  23. GreaterEqual = 0x0900 + 0x65 | __BINARY__,//">="
  24. LessEqual = 0x0A00 + 0x65 | __BINARY__,//"<="
  25. NotEqual = 0x0B00 + 0x60 | __BINARY__,//"!="
  26. Increment = 0x2000 | __UNARY__ | __UNARY_AFTER__,//"++"
  27. Decrement = 0x2100 | __UNARY__ | __UNARY_AFTER__,//"--"
  28. And = 0x0C00 + 0x40 | __BINARY__,//"&&"
  29. Or = 0x0D00 + 0x40 | __BINARY__,//"||"
  30. Xor = 0x1500 + 0x40 | __BINARY__,//"^^"
  31. Nand = 0x1600 + 0x40 | __BINARY__,//"!&"
  32. Nor = 0x1700 + 0x40 | __BINARY__,//"!^"
  33. BitAnd = 0x0E00 + 0x50 | __BINARY__,//"&"
  34. BitOr = 0x0F00 + 0x50 | __BINARY__,//"|"
  35. BitXor = 0x1000 + 0x50 | __BINARY__,//"^"、優先順位は&と|の中間。
  36. Not = 0x1100 | __UNARY__,//"!"単項
  37. BitNot = 0x1200 | __UNARY__,//"~"単項
  38. RightShift = 0x1300 + 0x70 | __BINARY__,//">>"
  39. LeftShift = 0x1400 + 0x70 | __BINARY__,//"<<"
  40. Ternary_a = 0x1800 + 0x05 | __TERNARY__,//"?"、三項演算子
  41. Ternary_b = 0x1900 + 0x10 | __TERNARY__,//"#"、三項演算子区切り":"が使えないのでかわり
  42. Assignment = 0x0100 + 0xFE,//"="
  43. AssignmentStr = 0x0200 + 0xFE//"'="
  44. }
  45. internal static class OperatorManager
  46. {
  47. static readonly Dictionary<string, OperatorCode> opDictionary;
  48. static OperatorManager()
  49. {
  50. opDictionary = new Dictionary<string, OperatorCode>();
  51. opDictionary.Add("+", OperatorCode.Plus);
  52. opDictionary.Add("-", OperatorCode.Minus);
  53. opDictionary.Add("*", OperatorCode.Mult);
  54. opDictionary.Add("/", OperatorCode.Div);
  55. opDictionary.Add("%", OperatorCode.Mod);
  56. opDictionary.Add("==", OperatorCode.Equal);
  57. opDictionary.Add(">", OperatorCode.Greater);
  58. opDictionary.Add("<", OperatorCode.Less);
  59. opDictionary.Add(">=", OperatorCode.GreaterEqual);
  60. opDictionary.Add("<=", OperatorCode.LessEqual);
  61. opDictionary.Add("!=", OperatorCode.NotEqual);
  62. opDictionary.Add("&&", OperatorCode.And);
  63. opDictionary.Add("||", OperatorCode.Or);
  64. opDictionary.Add("^^", OperatorCode.Xor);
  65. opDictionary.Add("!&", OperatorCode.Nand);
  66. opDictionary.Add("!|", OperatorCode.Nor);
  67. opDictionary.Add("&", OperatorCode.BitAnd);
  68. opDictionary.Add("|", OperatorCode.BitOr);
  69. opDictionary.Add("!", OperatorCode.Not);
  70. opDictionary.Add("^", OperatorCode.BitXor);
  71. opDictionary.Add("~", OperatorCode.BitNot);
  72. opDictionary.Add("?", OperatorCode.Ternary_a);
  73. opDictionary.Add("#", OperatorCode.Ternary_b);
  74. opDictionary.Add(">>", OperatorCode.RightShift);
  75. opDictionary.Add("<<", OperatorCode.LeftShift);
  76. opDictionary.Add("++", OperatorCode.Increment);
  77. opDictionary.Add("--", OperatorCode.Decrement);
  78. opDictionary.Add("=", OperatorCode.Assignment);
  79. opDictionary.Add("'=", OperatorCode.AssignmentStr);
  80. }
  81. public static string ToOperatorString(OperatorCode op)
  82. {
  83. if (op == OperatorCode.NULL)
  84. return "";
  85. foreach(KeyValuePair<string, OperatorCode> pair in opDictionary)
  86. {
  87. if(op == pair.Value)
  88. return pair.Key;
  89. }
  90. return "";
  91. }
  92. public static bool IsUnary(OperatorCode type)
  93. {
  94. return ((type & OperatorCode.__UNARY__) == OperatorCode.__UNARY__);
  95. }
  96. public static bool IsUnaryAfter(OperatorCode type)
  97. {
  98. return ((type & OperatorCode.__UNARY_AFTER__) == OperatorCode.__UNARY_AFTER__);
  99. }
  100. public static bool IsBinary(OperatorCode type)
  101. {
  102. return ((type & OperatorCode.__BINARY__) == OperatorCode.__BINARY__);
  103. }
  104. public static bool IsTernary(OperatorCode type)
  105. {
  106. return ((type & OperatorCode.__TERNARY__) == OperatorCode.__TERNARY__);
  107. }
  108. /// <summary>
  109. /// 大きい方が優先度が高い。 '&' < '+' < '*'等
  110. /// </summary>
  111. /// <param name="type"></param>
  112. /// <returns></returns>
  113. public static int GetPriority(OperatorCode type)
  114. {
  115. return (int)(type & OperatorCode.__PRIORITY_MASK__);
  116. }
  117. }
  118. }