Exemple #1
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);
            }
        }
Exemple #2
0
 internal override Expression VisitVariable(Variable variable)
 {
     return(_env.GetVariable(variable.Name.FullName));
 }