Пример #1
0
 public virtual T Visit(LogicalNot node)
 {
     return(Visit((SimpleUnaryExpression)node));
 }
Пример #2
0
 public override bool Visit(LogicalNot node)
 {
     outputCode("!", false, false);
     Visit((SimpleUnaryExpression)node);
     return(true);
 }
Пример #3
0
    private Expression ParseUnaryExpression(TokenSet followers) {
      Expression result;
      switch (this.currentToken) {
        case Token.Addition: {
            SourceLocationBuilder slb = new SourceLocationBuilder(this.scanner.CurrentSourceLocation);
            this.GetNextToken();
            result = new UnaryPlus(this.ParseSimpleExpression(followers), slb);
            break;
          }

        case Token.Not: {
            SourceLocationBuilder slb = new SourceLocationBuilder(this.scanner.CurrentSourceLocation);
            this.GetNextToken();
            result = new LogicalNot(this.ParseRelationalExpression(followers), slb);
            break;
          }

        case Token.Subtraction: {
            SourceLocationBuilder slb = new SourceLocationBuilder(this.scanner.CurrentSourceLocation);
            this.GetNextToken();
            result = new UnaryPlus(this.ParseSimpleExpression(followers), slb);
            break;
          }

        default:
          result = this.ParseSimpleExpression(followers);
          break;
      }
      return result;
    }
Пример #4
0
 public override bool Visit(LogicalNot node)
 {
     Visit((SimpleUnaryExpression)node);
     return(true);
 }
Пример #5
0
        public override Value Visit(LogicalNot node)
        {
            Value arg = traverse(node.expr);

            return(builder.BuildNot(arg));
        }
Пример #6
0
        public static IExpression Normalize(IExpression expression)
        {
            LogicalNot /*?*/ logicalNot = expression as LogicalNot;

            if (logicalNot != null)
            {
                IExpression operand = logicalNot.Operand;
                #region LogicalNot: !
                LogicalNot /*?*/ operandAsLogicalNot = operand as LogicalNot;
                if (operandAsLogicalNot != null)
                {
                    return(Normalize(operandAsLogicalNot.Operand));
                }
                #endregion
                #region BinaryOperations: ==, !=, <, <=, >, >=
                BinaryOperation /*?*/ binOp = operand as BinaryOperation;
                if (binOp != null)
                {
                    BinaryOperation /*?*/ result = null;
                    if (binOp is IEquality)
                    {
                        result = new NotEquality();
                    }
                    else if (binOp is INotEquality)
                    {
                        result = new Equality();
                    }
                    else if (binOp is ILessThan)
                    {
                        result = new GreaterThanOrEqual();
                    }
                    else if (binOp is ILessThanOrEqual)
                    {
                        result = new GreaterThan();
                    }
                    else if (binOp is IGreaterThan)
                    {
                        result = new LessThanOrEqual();
                    }
                    else if (binOp is IGreaterThanOrEqual)
                    {
                        result = new LessThan();
                    }
                    if (result != null)
                    {
                        result.LeftOperand  = Normalize(binOp.LeftOperand);
                        result.RightOperand = Normalize(binOp.RightOperand);
                        return(result);
                    }
                }
                #endregion
                #region Conditionals: &&, ||
                Conditional /*?*/ conditional = operand as Conditional;
                if (conditional != null)
                {
                    if (ExpressionHelper.IsIntegralNonzero(conditional.ResultIfTrue) ||
                        ExpressionHelper.IsIntegralZero(conditional.ResultIfFalse))
                    {
                        Conditional result = new Conditional();
                        LogicalNot  not;
                        //invert condition
                        not              = new LogicalNot();
                        not.Operand      = conditional.Condition;
                        result.Condition = Normalize(not);
                        //invert false branch and switch with true branch
                        not                 = new LogicalNot();
                        not.Operand         = conditional.ResultIfFalse;
                        result.ResultIfTrue = Normalize(not);
                        //invert true branch and switch with false branch
                        not                  = new LogicalNot();
                        not.Operand          = conditional.ResultIfTrue;
                        result.ResultIfFalse = Normalize(not);
                        //return
                        result.Type = conditional.Type;
                        return(result);
                    }
                }
                #endregion
                #region Constants: true, false
                CompileTimeConstant /*?*/ ctc = operand as CompileTimeConstant;
                if (ctc != null)
                {
                    if (ExpressionHelper.IsIntegralNonzero(ctc)) //Is true
                    {
                        var val = SetBooleanFalse(ctc);
                        if (val != null)
                        {
                            return(val);
                        }
                        else
                        {
                            return(expression);
                        }
                    }
                    else if (ExpressionHelper.IsIntegralZero(ctc)) //Is false
                    {
                        var val = SetBooleanTrue(ctc);
                        if (val != null)
                        {
                            return(val);
                        }
                        else
                        {
                            return(expression);
                        }
                    }
                }
                #endregion
            }
            return(expression);
        }
Пример #7
0
        protected override IMethodContract CreateQueryContract(State state, Action target)
        {
            var queryContract  = new MethodContract();
            var targetContract = target.Contract;

            // Add preconditions of enabled actions
            foreach (var a in state.EnabledActions)
            {
                var actionContract = a.Contract;
                if (actionContract == null)
                {
                    continue;
                }

                var preconditions = from p in actionContract.Preconditions
                                    select new Precondition
                {
                    Condition   = p.Condition,
                    Description = new CompileTimeConstant
                    {
                        Value = string.Format("Enabled action ({0})", a.Name),
                        Type  = host.PlatformType.SystemString
                    },
                    OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(p.Condition)
                };
                queryContract.Preconditions.AddRange(preconditions);
            }

            // Add negated preconditions of disabled actions
            foreach (var a in state.DisabledActions)
            {
                var actionContract = a.Contract;
                if (actionContract == null || !actionContract.Preconditions.Any())
                {
                    continue;
                }

                var preconditions = from p in actionContract.Preconditions
                                    select p.Condition;
                var joinedPreconditions = new LogicalNot
                {
                    Type    = host.PlatformType.SystemBoolean,
                    Operand = Helper.JoinWithLogicalAnd(host, preconditions.ToList(), true)
                };
                var compactPrecondition = new Precondition
                {
                    Condition = joinedPreconditions,
                    // Add the user message to identify easily each precondition
                    Description = new CompileTimeConstant
                    {
                        Value = string.Format("Disabled action ({0})", a.Name),
                        Type  = host.PlatformType.SystemString
                    },
                    // Add the string-ified version of the condition to help debugging
                    OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(joinedPreconditions)
                };
                queryContract.Preconditions.Add(compactPrecondition);
            }

            // Now the postconditions
            // Having no preconditions is the same as having the 'true' precondition
            if (targetContract == null || !targetContract.Preconditions.Any())
            {
                var post = new Postcondition
                {
                    Condition = new CompileTimeConstant
                    {
                        Type  = host.PlatformType.SystemBoolean,
                        Value = false
                    },
                    OriginalSource = "false",
                    Description    = new CompileTimeConstant {
                        Value = "Target negated precondition", Type = host.PlatformType.SystemString
                    }
                };

                queryContract.Postconditions.Add(post);
            }
            else
            {
                var exprs = (from pre in targetContract.Preconditions
                             select pre.Condition).ToList();

                var post = new Postcondition
                {
                    Condition = new LogicalNot
                    {
                        Type    = host.PlatformType.SystemBoolean,
                        Operand = Helper.JoinWithLogicalAnd(host, exprs, true)
                    },
                    OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(Helper.JoinWithLogicalAnd(host, exprs, true)),
                    Description    = new CompileTimeConstant {
                        Value = "Target negated precondition", Type = host.PlatformType.SystemString
                    }
                };
                queryContract.Postconditions.Add(post);
            }
            return(queryContract);
        }