| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using System;
- using System.Globalization;
- using System.Text.RegularExpressions;
- using IwbZero.IwbBase;
- using IwbZero.ToolCommon.StringModel;
- namespace IwbZero.Expr
- {
- public class ExprOperator : IIwbId
- {
- public string Id { get; set; }
- public string Symbol { get; }
- public int Level { get; }
- public int LeftOperands { get; }
- public ExprOperator(string pcOperator, string pcSymbol, int piLevel, int piLeftOps)
- {
- Id = pcOperator;
- Symbol = pcSymbol;
- Level = piLevel;
- LeftOperands = piLeftOps;
- }
- public bool IsSymbolMatched(string pcString)
- {
- bool result = false;
- int length = Symbol.Length;
- if (pcString.Length >= length)
- {
- result = (pcString.Substring(0, length).ToUpper() == Symbol.ToUpper());
- }
- return result;
- }
- public ExprObject Calculate(ExprObject poVar1, ExprObject poVar2)
- {
- ExprObject result = null;
- if (Id != null)
- {
- // ReSharper disable once PossibleInvalidCastException
- switch (Id)
- {
- case "+":
- {
- result = PerformAdd(poVar1, poVar2);
- break;
- }
- case "-":
- {
- result = PerformSubtract(poVar1, poVar2);
- break;
- }
- case "*":
- {
- result = PerformMultiply(poVar1, poVar2);
- break;
- }
- case "/":
- {
- result = PerformDivision(poVar1, poVar2);
- break;
- }
- case "%":
- {
- result = PerformMod(poVar1, poVar2);
- break;
- }
- case ">":
- {
- int num = PerformCompare(poVar1, poVar2);
- result = new ExprObject((num > 0) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case ">=":
- {
- int num = PerformCompare(poVar1, poVar2);
- result = new ExprObject((num >= 0) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "<":
- {
- int num = PerformCompare(poVar1, poVar2);
- result = new ExprObject((num < 0) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "<=":
- {
- int num = PerformCompare(poVar1, poVar2);
- result = new ExprObject((num <= 0) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "=":
- {
- int num = PerformCompare(poVar1, poVar2);
- result = new ExprObject((num == 0) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "!=":
- {
- int num = PerformCompare(poVar1, poVar2);
- result = new ExprObject((num != 0) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "and":
- case "&&":
- {
- result = new ExprObject((poVar1.Expr.StrToBool() && poVar2.Expr.StrToBool()) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "or":
- case "||":
- {
- result = new ExprObject((poVar1.Expr.StrToBool() || poVar2.Expr.StrToBool()) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "not":
- case "!":
- {
- result = new ExprObject((!poVar2.Expr.StrToBool()) ? "TRUE" : "FALSE", ExprElements.Value);
- break;
- }
- case "like":
- {
- result = PerformLikeCompare(poVar1, poVar2);
- break;
- }
- }
- }
- return result;
- }
- private ExprObject PerformAdd(ExprObject poVar1, ExprObject poVar2)
- {
- ExprObject result;
- ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
- ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
- if (exprTypes == ExprTypes.DateAdd && (exprTypes2 == ExprTypes.Date || exprTypes2 == ExprTypes.String))
- {
- DateTime poDateTime = EvalExpr.AddDays(poVar2.Expr.StrToDt(), poVar1.Expr);
- result = new ExprObject(poDateTime.DtToStr(), ExprElements.Value);
- }
- else
- {
- if ((exprTypes == ExprTypes.Date || exprTypes == ExprTypes.String) && exprTypes2 == ExprTypes.DateAdd)
- {
- DateTime poDateTime2 = EvalExpr.AddDays(poVar1.Expr.StrToDt(), poVar2.Expr);
- result = new ExprObject(poDateTime2.DtToStr(), ExprElements.Value);
- }
- else
- {
- if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
- {
- result = new ExprObject((poVar1.Expr.ValI() + poVar2.Expr.ValI()).ToString(), ExprElements.Value);
- }
- else
- {
- if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
- {
- result = new ExprObject((poVar1.Expr.ValD() + poVar2.Expr.ValD()).ToString(CultureInfo.InvariantCulture), ExprElements.Value);
- }
- else
- {
- result = new ExprObject(poVar1.Expr + poVar2.Expr, ExprElements.String);
- }
- }
- }
- }
- return result;
- }
- private ExprObject PerformSubtract(ExprObject poVar1, ExprObject poVar2)
- {
- ExprObject result;
- ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
- ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
- if (exprTypes == ExprTypes.Date && exprTypes2 == ExprTypes.DateAdd)
- {
- DateTime poDateTime = EvalExpr.AddDays(poVar1.Expr.StrToDt(), "-" + poVar2.Expr);
- result = new ExprObject(poDateTime.DtToStr(), ExprElements.Value);
- }
- else
- {
- if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
- {
- result = new ExprObject((poVar1.Expr.ValI() - poVar2.Expr.ValI()).ToString(), ExprElements.Value);
- }
- else
- {
- if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
- {
- result = new ExprObject((poVar1.Expr.ValD() - poVar2.Expr.ValD()).ToString(CultureInfo.InvariantCulture), ExprElements.Value);
- }
- else
- {
- result = new ExprObject(poVar1.Expr + "-" + poVar2.Expr, ExprElements.String);
- }
- }
- }
- return result;
- }
- private ExprObject PerformDivision(ExprObject poVar1, ExprObject poVar2)
- {
- ExprObject result;
- ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
- ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
- if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
- {
- int num = poVar1.Expr.ValI();
- int num2 = poVar2.Expr.ValI();
- if (num2 != 0)
- {
- num /= num2;
- }
- result = new ExprObject(num.ToString(), ExprElements.Value);
- }
- else
- {
- if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
- {
- decimal num3 = poVar1.Expr.ValD();
- decimal num4 = poVar2.Expr.ValD();
- if (num4 != 0m)
- {
- num3 /= num4;
- }
- result = new ExprObject(num3.ToString(CultureInfo.InvariantCulture), ExprElements.Value);
- }
- else
- {
- result = new ExprObject(poVar1.Expr + "/" + poVar2.Expr, ExprElements.String);
- }
- }
- return result;
- }
- private ExprObject PerformMod(ExprObject poVar1, ExprObject poVar2)
- {
- int num = poVar1.Expr.ValI();
- int num2 = poVar2.Expr.ValI();
- int num3 = 0;
- if (num2 != 0)
- {
- num3 = num % num2;
- }
- return new ExprObject(num3.ToString(), ExprElements.String);
- }
- private ExprObject PerformMultiply(ExprObject poVar1, ExprObject poVar2)
- {
- ExprObject result;
- ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
- ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
- if (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Integer)
- {
- int num = poVar1.Expr.ValI();
- int num2 = poVar2.Expr.ValI();
- num *= num2;
- result = new ExprObject(num.ToString(), ExprElements.Value);
- }
- else
- {
- if ((exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Decimal) || (exprTypes == ExprTypes.Decimal && exprTypes2 == ExprTypes.Integer) || (exprTypes == ExprTypes.Integer && exprTypes2 == ExprTypes.Decimal))
- {
- decimal num3 = poVar1.Expr.ValD();
- decimal num4 = poVar2.Expr.ValD();
- num3 *= num4;
- result = new ExprObject(num3.ToString(CultureInfo.InvariantCulture), ExprElements.Value);
- }
- else
- {
- result = new ExprObject(poVar1.Expr + "*" + poVar2.Expr, ExprElements.String);
- }
- }
- return result;
- }
- private int PerformCompare(ExprObject poVar1, ExprObject poVar2)
- {
- int result;
- ExprTypes exprTypes = EvalExpr.GetExprTypes(poVar1);
- ExprTypes exprTypes2 = EvalExpr.GetExprTypes(poVar2);
- if ((exprTypes == ExprTypes.Date && exprTypes2 == ExprTypes.Date) || (exprTypes == ExprTypes.Date && exprTypes2 == ExprTypes.String) || (exprTypes == ExprTypes.String && exprTypes2 == ExprTypes.Date))
- {
- result = poVar1.Expr.StrToDt().CompareTo(poVar2.Expr.StrToDt());
- }
- else
- {
- 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))
- {
- result = poVar1.Expr.ValD().CompareTo(poVar2.Expr.ValD());
- }
- else
- {
- result = string.Compare(poVar1.Expr.UAndT(), poVar2.Expr.UAndT(), StringComparison.Ordinal);
- }
- }
- return result;
- }
- private ExprObject PerformLikeCompare(ExprObject poVar1, ExprObject poVar2)
- {
- string pcExpr = "FALSE";
- string expr = poVar1.Expr;
- string text = poVar2.Expr.Trim().Replace(".", "\\.").Replace("_", ".").Replace("*", ".*");
- Match match = Regex.Match(expr, text);
- if (match != null && match.Index == 0 && match.Length == expr.Length)
- {
- pcExpr = "TRUE";
- }
- return new ExprObject(pcExpr, ExprElements.Value);
- }
- }
- }
|