private static bool ShallowEq(LiteralExpr expr1, LiteralExpr expr2) { if (!TriggerUtils.SameNullity(expr1.Value, expr2.Value) || (expr1.Value != null && !expr1.Value.Equals(expr2.Value))) { return(false); } if (expr1 is StringLiteralExpr && expr2 is StringLiteralExpr) { return(ShallowEq((StringLiteralExpr)expr1, (StringLiteralExpr)expr2)); } else if (expr1 is CharLiteralExpr && expr2 is CharLiteralExpr) { return(ShallowEq((CharLiteralExpr)expr1, (CharLiteralExpr)expr2)); } else if (expr1 is StaticReceiverExpr && expr2 is StaticReceiverExpr) { return(ShallowEq((StaticReceiverExpr)expr1, (StaticReceiverExpr)expr2)); } else { return(expr1.GetType() == expr2.GetType()); // LiteralExpr is not abstract } }
/// <summary> /// Evalutate an expression tree /// </summary> /// <param name="expt"></param> /// <returns></returns> protected bool EvaluateGuardTree(ExpressionTree expt) { Contract.Requires(expt != null); // if the node is leaf, cast it to bool and return if (expt.IsLeaf()) { var lit = EvaluateLeaf(expt) as LiteralExpr; return(lit?.Value is bool && (bool)lit.Value); } // left branch only if (expt.LChild != null && expt.RChild == null) { return(EvaluateGuardTree(expt.LChild)); } // if there is no more nesting resolve the expression if (expt.LChild.IsLeaf() && expt.RChild.IsLeaf()) { LiteralExpr lhs = null; LiteralExpr rhs = null; lhs = EvaluateLeaf(expt.LChild) as LiteralExpr; rhs = EvaluateLeaf(expt.RChild) as LiteralExpr; if (lhs.GetType() == rhs.GetType()) { return(false); } var bexp = tcce.NonNull(expt.Data as BinaryExpr); int res = -1; if (lhs.Value is BigInteger) { var l = (BigInteger)lhs.Value; var r = (BigInteger)rhs.Value; res = l.CompareTo(r); } else if (lhs.Value is string) { var l = (string)lhs.Value; var r = rhs.Value as string; res = String.Compare(l, r, StringComparison.Ordinal); } else if (lhs.Value is bool) { res = ((bool)lhs.Value).CompareTo((bool)rhs.Value); } switch (bexp.Op) { case BinaryExpr.Opcode.Eq: return(res == 0); case BinaryExpr.Opcode.Neq: return(res != 0); case BinaryExpr.Opcode.Ge: return(res >= 0); case BinaryExpr.Opcode.Gt: return(res > 0); case BinaryExpr.Opcode.Le: return(res <= 0); case BinaryExpr.Opcode.Lt: return(res < 0); } } else // evaluate a nested expression { BinaryExpr bexp = tcce.NonNull(expt.Data as BinaryExpr); if (bexp.Op == BinaryExpr.Opcode.And) { return(EvaluateGuardTree(expt.LChild) && EvaluateGuardTree(expt.RChild)); } if (bexp.Op == BinaryExpr.Opcode.Or) { return(EvaluateGuardTree(expt.LChild) || EvaluateGuardTree(expt.RChild)); } } return(false); }