コード例 #1
0
        public void TestIsTrueAlwaysFalseFunction()
        {
            var expr  = AlwaysFalseFunction.CreateAlwaysFalseFunctionCall();
            var scope = AchievementScriptInterpreter.GetGlobalScope();

            ParseErrorExpression error;

            Assert.That(expr.IsTrue(scope, out error), Is.False);
            Assert.That(error, Is.Null);
        }
コード例 #2
0
        internal static ExpressionBase InvertExpression(ExpressionBase expression)
        {
            // logical inversion
            var condition = expression as ConditionalExpression;

            if (condition != null)
            {
                var newConditions = new List <ExpressionBase>(condition._conditions.Count);
                foreach (var oldCondition in condition._conditions)
                {
                    newConditions.Add(InvertExpression(oldCondition));
                }

                switch (condition.Operation)
                {
                case ConditionalOperation.Not:
                    // !(!A) => A
                    return(newConditions[0]);

                case ConditionalOperation.And:
                    // !(A && B) => !A || !B
                    return(new ConditionalExpression(ConditionalOperation.Or, newConditions));

                case ConditionalOperation.Or:
                    // !(A || B) => !A && !B
                    return(new ConditionalExpression(ConditionalOperation.And, newConditions));

                default:
                    throw new NotImplementedException("Unsupported condition operation");
                }
            }

            // comparative inversion
            var comparison = expression as ComparisonExpression;

            if (comparison != null)
            {
                // !(A == B) => A != B, !(A < B) => A >= B, ...
                return(new ComparisonExpression(
                           comparison.Left,
                           ComparisonExpression.GetOppositeComparisonOperation(comparison.Operation),
                           comparison.Right));
            }

            // boolean constant
            var boolean = expression as BooleanConstantExpression;

            if (boolean != null)
            {
                return(new BooleanConstantExpression(!boolean.Value));
            }

            // special handling for built-in functions
            var function = expression as FunctionCallExpression;

            if (function != null && function.Parameters.Count() == 0)
            {
                if (function.FunctionName.Name == "always_true")
                {
                    return(AlwaysFalseFunction.CreateAlwaysFalseFunctionCall());
                }

                if (function.FunctionName.Name == "always_false")
                {
                    return(AlwaysTrueFunction.CreateAlwaysTrueFunctionCall());
                }
            }

            // unsupported inversion
            return(new ParseErrorExpression("! operator cannot be applied to " + expression.Type, expression));
        }