Пример #1
0
        private void TestUnary(UnaryOperationKind kind)
        {
            string text;

            if (Operation.IsPrefixOperation(kind))
            {
                text = Operation.GetText(kind) + "$a";
            }
            else
            {
                text = "$a" + Operation.GetText(kind);
            }

            var expr = NSScript.ParseExpression(text) as UnaryExpression;

            Assert.NotNull(expr);
            Assert.Equal(SyntaxNodeKind.UnaryExpression, expr.Kind);
            Assert.Equal(kind, expr.OperationKind);

            var operand = expr.Operand as Variable;

            Assert.NotNull(operand);
            Assert.Equal("$a", operand.Name.FullName);

            Assert.Equal(text, expr.ToString());
        }
Пример #2
0
        public BoundIncDecEx(BoundReferenceExpression target, UnaryOperationKind kind)
            : base(target, new BoundLiteral(1L).WithAccess(BoundAccess.Read), Operations.IncDec)
        {
            Debug.Assert(
                kind == UnaryOperationKind.OperatorPostfixDecrement ||
                kind == UnaryOperationKind.OperatorPostfixIncrement ||
                kind == UnaryOperationKind.OperatorPrefixDecrement ||
                kind == UnaryOperationKind.OperatorPrefixIncrement);

            this.IncrementKind = kind;
        }
Пример #3
0
        public static bool IsPrefixOperation(UnaryOperationKind unaryOperationKind)
        {
            switch (unaryOperationKind)
            {
            case UnaryOperationKind.LogicalNegation:
            case UnaryOperationKind.UnaryPlus:
            case UnaryOperationKind.UnaryMinus:
                return(true);

            default:
                return(false);
            }
        }
Пример #4
0
        private ConstantValue ApplyUnaryOperation(Expression operand, UnaryOperationKind operationKind)
        {
            if (operationKind == UnaryOperationKind.LogicalNegation)
            {
                return(!Evaluate(operand));
            }

            if (operand.Kind != SyntaxNodeKind.Variable &&
                (operationKind == UnaryOperationKind.PostfixIncrement || operationKind == UnaryOperationKind.PostfixIncrement))
            {
                string op = Operation.GetText(operationKind);
                throw new InvalidOperationException($"Unary operator '{op}' can only be applied to variables.");
            }

            ConstantValue oldValue;

            string variableName = string.Empty;

            if (operand.Kind == SyntaxNodeKind.Variable)
            {
                variableName = (operand as Variable).Name.FullName;
                oldValue     = _env?.GetVariable((operand as Variable).Name.FullName);
            }
            else
            {
                oldValue = Evaluate(operand);
            }

            switch (operationKind)
            {
            case UnaryOperationKind.UnaryPlus:
                return(oldValue);

            case UnaryOperationKind.UnaryMinus:
                return(-oldValue);

            case UnaryOperationKind.PostfixIncrement:
                _env.SetVariable(variableName, oldValue++);
                return(oldValue);

            case UnaryOperationKind.PostfixDecrement:
            default:
                _env.SetVariable(variableName, oldValue--);
                return(oldValue);
            }
        }
Пример #5
0
        public static string GetText(UnaryOperationKind unaryOperationKind)
        {
            switch (unaryOperationKind)
            {
            case UnaryOperationKind.LogicalNegation:
                return("!");

            case UnaryOperationKind.UnaryPlus:
                return("+");

            case UnaryOperationKind.UnaryMinus:
                return("-");

            case UnaryOperationKind.PostfixIncrement:
                return("++");

            case UnaryOperationKind.PostfixDecrement:
                return("--");

            default:
                return(string.Empty);
            }
        }
Пример #6
0
 public static UnaryOperandKind GetUnaryOperandKind(UnaryOperationKind kind)
 {
     return (UnaryOperandKind)((int)kind & UnaryAndBinaryOperationExtensions.UnaryOperandKindMask);
 }
Пример #7
0
 public static SimpleUnaryOperationKind GetSimpleUnaryOperationKind(UnaryOperationKind kind)
 {
     return (SimpleUnaryOperationKind)((int)kind & UnaryAndBinaryOperationExtensions.SimpleUnaryOperationKindMask);
 }
Пример #8
0
 public static UnaryExpression Unary(Expression operand, UnaryOperationKind operationKind) =>
 new UnaryExpression(operand, operationKind);
Пример #9
0
 internal UnaryExpression(Expression operand, UnaryOperationKind operationKind)
 {
     Operand       = operand;
     OperationKind = operationKind;
 }
Пример #10
0
        internal static BinaryOperationKind DeriveBinaryOperationKind(UnaryOperationKind incrementKind)
        {
            switch (incrementKind)
            {
                case UnaryOperationKind.OperatorPostfixIncrement:
                case UnaryOperationKind.OperatorPrefixIncrement:
                    return BinaryOperationKind.OperatorAdd;
                case UnaryOperationKind.OperatorPostfixDecrement:
                case UnaryOperationKind.OperatorPrefixDecrement:
                    return BinaryOperationKind.OperatorSubtract;
                case UnaryOperationKind.IntegerPostfixIncrement:
                case UnaryOperationKind.IntegerPrefixIncrement:
                    return BinaryOperationKind.IntegerAdd;
                case UnaryOperationKind.IntegerPostfixDecrement:
                case UnaryOperationKind.IntegerPrefixDecrement:
                    return BinaryOperationKind.IntegerSubtract;
                case UnaryOperationKind.UnsignedPostfixIncrement:
                case UnaryOperationKind.UnsignedPrefixIncrement:
                    return BinaryOperationKind.UnsignedAdd;
                case UnaryOperationKind.UnsignedPostfixDecrement:
                case UnaryOperationKind.UnsignedPrefixDecrement:
                    return BinaryOperationKind.UnsignedSubtract;
                case UnaryOperationKind.FloatingPostfixIncrement:
                case UnaryOperationKind.FloatingPrefixIncrement:
                    return BinaryOperationKind.FloatingAdd;
                case UnaryOperationKind.FloatingPostfixDecrement:
                case UnaryOperationKind.FloatingPrefixDecrement:
                    return BinaryOperationKind.FloatingSubtract;
                case UnaryOperationKind.DecimalPostfixIncrement:
                case UnaryOperationKind.DecimalPrefixIncrement:
                    return BinaryOperationKind.DecimalAdd;
                case UnaryOperationKind.DecimalPostfixDecrement:
                case UnaryOperationKind.DecimalPrefixDecrement:
                    return BinaryOperationKind.DecimalSubtract;
                case UnaryOperationKind.EnumPostfixIncrement:
                case UnaryOperationKind.EnumPrefixIncrement:
                    return BinaryOperationKind.EnumAdd;
                case UnaryOperationKind.EnumPostfixDecrement:
                case UnaryOperationKind.EnumPrefixDecrement:
                    return BinaryOperationKind.EnumSubtract;
                case UnaryOperationKind.PointerPostfixIncrement:
                case UnaryOperationKind.PointerPrefixIncrement:
                    return BinaryOperationKind.PointerIntegerAdd;
                case UnaryOperationKind.PointerPostfixDecrement:
                case UnaryOperationKind.PointerPrefixDecrement:
                    return BinaryOperationKind.PointerIntegerSubtract;
                case UnaryOperationKind.DynamicPostfixIncrement:
                case UnaryOperationKind.DynamicPrefixIncrement:
                    return BinaryOperationKind.DynamicAdd;
                case UnaryOperationKind.DynamicPostfixDecrement:
                case UnaryOperationKind.DynamicPrefixDecrement:
                    return BinaryOperationKind.DynamicSubtract;

                default:
                    throw ExceptionUtilities.UnexpectedValue(incrementKind);
            }
        }
 public static UnaryOperandKind GetUnaryOperandKind(UnaryOperationKind kind)
 {
     return((UnaryOperandKind)((int)kind & UnaryAndBinaryOperationExtensions.UnaryOperandKindMask));
 }
 public static SimpleUnaryOperationKind GetSimpleUnaryOperationKind(UnaryOperationKind kind)
 {
     return((SimpleUnaryOperationKind)((int)kind & UnaryAndBinaryOperationExtensions.SimpleUnaryOperationKindMask));
 }
Пример #13
0
 public static bool IsPostfixOperation(UnaryOperationKind unaryOperationKind) => !IsPrefixOperation(unaryOperationKind);
Пример #14
0
 private static Precedence GetOperationPrecedence(UnaryOperationKind operationKind) => Precedence.Unary;