示例#1
0
        public override AstNode Visit(UnaryOperation node)
        {
            // Remove the nop expression.
            if(node.GetOperation() == UnaryOperation.OpNop)
                return node.GetExpression();

            // Process the base expression.
            Expression expr = node.GetExpression();
            expr = (Expression)expr.Accept(this);
            node.SetExpression(expr);

            // Get the node type.
            IChelaType nodeType = node.GetNodeType();
            if(!nodeType.IsConstant())
                return node;

            // Perform the base coercion.
            ConstantValue baseConstant = (ConstantValue)expr.GetNodeValue();
            IChelaType baseType = expr.GetNodeType();
            IChelaType baseCoercion = node.GetCoercionType();
            if(baseType != baseCoercion)
                baseConstant = baseConstant.Cast(baseCoercion);

            // Perform the constant operation.
            ConstantValue res = null;
            switch(node.GetOperation())
            {
            case UnaryOperation.OpNop:
                res = baseConstant;
                break;
            case UnaryOperation.OpNeg:
                res = -baseConstant;
                break;
            case UnaryOperation.OpNot:
                res = !baseConstant;
                break;
            case UnaryOperation.OpBitNot:
                res = ~baseConstant;
                break;
            }

            return res.ToAstNode(node.GetPosition());
        }