ExprOperator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using IwbZero.IwbBase;
  5. using IwbZero.ToolCommon.StringModel;
  6. namespace IwbZero.Expr
  7. {
  8. public class ExprOperator : IIwbId
  9. {
  10. public string Id { get; set; }
  11. public string Symbol { get; }
  12. public int Level { get; }
  13. public int LeftOperands { get; }
  14. public ExprOperator(string pcOperator, string pcSymbol, int piLevel, int piLeftOps)
  15. {
  16. Id = pcOperator;
  17. Symbol = pcSymbol;
  18. Level = piLevel;
  19. LeftOperands = piLeftOps;
  20. }
  21. public bool IsSymbolMatched(string pcString)
  22. {
  23. bool result = false;
  24. int length = Symbol.Length;
  25. if (pcString.Length >= length)
  26. {
  27. result = (pcString.Substring(0, length).ToUpper() == Symbol.ToUpper());
  28. }
  29. return result;
  30. }
  31. public ExprObject Calculate(ExprObject poVar1, ExprObject poVar2)
  32. {
  33. ExprObject result = null;
  34. if (Id != null)
  35. {
  36. // ReSharper disable once PossibleInvalidCastException
  37. switch (Id)
  38. {
  39. case "+":
  40. {
  41. result = PerformAdd(poVar1, poVar2);
  42. break;
  43. }
  44. case "-":
  45. {
  46. result = PerformSubtract(poVar1, poVar2);
  47. break;
  48. }
  49. case "*":
  50. {
  51. result = PerformMultiply(poVar1, poVar2);
  52. break;
  53. }
  54. case "/":
  55. {
  56. result = PerformDivision(poVar1, poVar2);
  57. break;
  58. }
  59. case "%":
  60. {
  61. result = PerformMod(poVar1, poVar2);
  62. break;
  63. }
  64. case ">":
  65. {
  66. int num = PerformCompare(poVar1, poVar2);
  67. result = new ExprObject((num > 0) ? "TRUE" : "FALSE", ExprElements.Value);
  68. break;
  69. }
  70. case ">=":
  71. {
  72. int num = PerformCompare(poVar1, poVar2);
  73. result = new ExprObject((num >= 0) ? "TRUE" : "FALSE", ExprElements.Value);
  74. break;
  75. }
  76. case "<":
  77. {
  78. int num = PerformCompare(poVar1, poVar2);
  79. result = new ExprObject((num < 0) ? "TRUE" : "FALSE", ExprElements.Value);
  80. break;
  81. }
  82. case "<=":
  83. {
  84. int num = PerformCompare(poVar1, poVar2);
  85. result = new ExprObject((num <= 0) ? "TRUE" : "FALSE", ExprElements.Value);
  86. break;
  87. }
  88. case "=":
  89. {
  90. int num = PerformCompare(poVar1, poVar2);
  91. result = new ExprObject((num == 0) ? "TRUE" : "FALSE", ExprElements.Value);
  92. break;
  93. }
  94. case "!=":
  95. {
  96. int num = PerformCompare(poVar1, poVar2);
  97. result = new ExprObject((num != 0) ? "TRUE" : "FALSE", ExprElements.Value);
  98. break;
  99. }
  100. case "and":
  101. case "&&":
  102. {
  103. result = new ExprObject((poVar1.Expr.StrToBool() && poVar2.Expr.StrToBool()) ? "TRUE" : "FALSE", ExprElements.Value);
  104. break;
  105. }
  106. case "or":
  107. case "||":
  108. {
  109. result = new ExprObject((poVar1.Expr.StrToBool() || poVar2.Expr.StrToBool()) ? "TRUE" : "FALSE", ExprElements.Value);
  110. break;
  111. }
  112. case "not":
  113. case "!":
  114. {
  115. result = new ExprObject((!poVar2.Expr.StrToBool()) ? "TRUE" : "FALSE", ExprElements.Value);
  116. break;
  117. }
  118. case "like":
  119. {
  120. result = PerformLikeCompare(poVar1, poVar2);
  121. break;
  122. }
  123. }
  124. }
  125. return result;
  126. }
  127. private ExprObject PerformAdd(ExprObject poVar1, ExprObject poVar2)
  128. {
  129. ExprObject result;
  130. ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
  131. ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
  132. if (exprTypes == ExprTypes.DateAdd && (exprTypes2 == ExprTypes.Date || exprTypes2 == ExprTypes.String))
  133. {
  134. DateTime poDateTime = EvalExpr.AddDays(poVar2.Expr.StrToDt(), poVar1.Expr);
  135. result = new ExprObject(poDateTime.DtToStr(), ExprElements.Value);
  136. }
  137. else
  138. {
  139. if ((exprTypes == ExprTypes.Date || exprTypes == ExprTypes.String) && exprTypes2 == ExprTypes.DateAdd)
  140. {
  141. DateTime poDateTime2 = EvalExpr.AddDays(poVar1.Expr.StrToDt(), poVar2.Expr);
  142. result = new ExprObject(poDateTime2.DtToStr(), ExprElements.Value);
  143. }
  144. else
  145. {
  146. if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
  147. {
  148. result = new ExprObject((poVar1.Expr.ValI() + poVar2.Expr.ValI()).ToString(), ExprElements.Value);
  149. }
  150. else
  151. {
  152. if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
  153. {
  154. result = new ExprObject((poVar1.Expr.ValD() + poVar2.Expr.ValD()).ToString(CultureInfo.InvariantCulture), ExprElements.Value);
  155. }
  156. else
  157. {
  158. result = new ExprObject(poVar1.Expr + poVar2.Expr, ExprElements.String);
  159. }
  160. }
  161. }
  162. }
  163. return result;
  164. }
  165. private ExprObject PerformSubtract(ExprObject poVar1, ExprObject poVar2)
  166. {
  167. ExprObject result;
  168. ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
  169. ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
  170. if (exprTypes == ExprTypes.Date && exprTypes2 == ExprTypes.DateAdd)
  171. {
  172. DateTime poDateTime = EvalExpr.AddDays(poVar1.Expr.StrToDt(), "-" + poVar2.Expr);
  173. result = new ExprObject(poDateTime.DtToStr(), ExprElements.Value);
  174. }
  175. else
  176. {
  177. if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
  178. {
  179. result = new ExprObject((poVar1.Expr.ValI() - poVar2.Expr.ValI()).ToString(), ExprElements.Value);
  180. }
  181. else
  182. {
  183. if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
  184. {
  185. result = new ExprObject((poVar1.Expr.ValD() - poVar2.Expr.ValD()).ToString(CultureInfo.InvariantCulture), ExprElements.Value);
  186. }
  187. else
  188. {
  189. result = new ExprObject(poVar1.Expr + "-" + poVar2.Expr, ExprElements.String);
  190. }
  191. }
  192. }
  193. return result;
  194. }
  195. private ExprObject PerformDivision(ExprObject poVar1, ExprObject poVar2)
  196. {
  197. ExprObject result;
  198. ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
  199. ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
  200. if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
  201. {
  202. int num = poVar1.Expr.ValI();
  203. int num2 = poVar2.Expr.ValI();
  204. if (num2 != 0)
  205. {
  206. num /= num2;
  207. }
  208. result = new ExprObject(num.ToString(), ExprElements.Value);
  209. }
  210. else
  211. {
  212. if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
  213. {
  214. decimal num3 = poVar1.Expr.ValD();
  215. decimal num4 = poVar2.Expr.ValD();
  216. if (num4 != 0m)
  217. {
  218. num3 /= num4;
  219. }
  220. result = new ExprObject(num3.ToString(CultureInfo.InvariantCulture), ExprElements.Value);
  221. }
  222. else
  223. {
  224. result = new ExprObject(poVar1.Expr + "/" + poVar2.Expr, ExprElements.String);
  225. }
  226. }
  227. return result;
  228. }
  229. private ExprObject PerformMod(ExprObject poVar1, ExprObject poVar2)
  230. {
  231. int num = poVar1.Expr.ValI();
  232. int num2 = poVar2.Expr.ValI();
  233. int num3 = 0;
  234. if (num2 != 0)
  235. {
  236. num3 = num % num2;
  237. }
  238. return new ExprObject(num3.ToString(), ExprElements.String);
  239. }
  240. private ExprObject PerformMultiply(ExprObject poVar1, ExprObject poVar2)
  241. {
  242. ExprObject result;
  243. ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
  244. ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
  245. if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
  246. {
  247. int num = poVar1.Expr.ValI();
  248. int num2 = poVar2.Expr.ValI();
  249. num *= num2;
  250. result = new ExprObject(num.ToString(), ExprElements.Value);
  251. }
  252. else
  253. {
  254. if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
  255. {
  256. decimal num3 = poVar1.Expr.ValD();
  257. decimal num4 = poVar2.Expr.ValD();
  258. num3 *= num4;
  259. result = new ExprObject(num3.ToString(CultureInfo.InvariantCulture), ExprElements.Value);
  260. }
  261. else
  262. {
  263. result = new ExprObject(poVar1.Expr + "*" + poVar2.Expr, ExprElements.String);
  264. }
  265. }
  266. return result;
  267. }
  268. private int PerformCompare(ExprObject poVar1, ExprObject poVar2)
  269. {
  270. int result;
  271. ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
  272. ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
  273. if ((exprTypes == ExprTypes.Date && exprTypes2 == ExprTypes.Date) || (exprTypes == ExprTypes.Date && exprTypes2 == ExprTypes.String) || (exprTypes == ExprTypes.String && exprTypes2 == ExprTypes.Date))
  274. {
  275. result = poVar1.Expr.StrToDt().CompareTo(poVar2.Expr.StrToDt());
  276. }
  277. else
  278. {
  279. if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer))
  280. {
  281. result = poVar1.Expr.ValD().CompareTo(poVar2.Expr.ValD());
  282. }
  283. else
  284. {
  285. result = string.Compare(poVar1.Expr.UAndT(), poVar2.Expr.UAndT(), StringComparison.Ordinal);
  286. }
  287. }
  288. return result;
  289. }
  290. private ExprObject PerformLikeCompare(ExprObject poVar1, ExprObject poVar2)
  291. {
  292. string pcExpr = "FALSE";
  293. string expr = poVar1.Expr;
  294. string text = poVar2.Expr.Trim().Replace(".", "\\.").Replace("_", ".").Replace("*", ".*");
  295. Match match = Regex.Match(expr, text);
  296. if (match != null && match.Index == 0 && match.Length == expr.Length)
  297. {
  298. pcExpr = "TRUE";
  299. }
  300. return new ExprObject(pcExpr, ExprElements.Value);
  301. }
  302. }
  303. }