Esempio n. 1
0
        public override AstNode Visit(PrefixOperation node)
        {
            // Begin the node.
            builder.BeginNode(node);

            // Get the expressions.
            Expression variableExpr = node.GetVariable();

            // Get the types.
            IChelaType variableType = variableExpr.GetNodeType();
            IChelaType coercionType = node.GetCoercionType();

            // Get the variable.
            Variable variable = (Variable)variableExpr.GetNodeValue();
            variableExpr.Accept(this);

            // Duplicate the variable twice reference.
            DuplicateReference(node, variable);
            DuplicateReference(node, variable);

            // Read the variable.
            Cast(node, variable, variableType, coercionType);

            // Send the right operand.
            if(coercionType.IsPointer())
            {
                builder.CreateLoadUInt32(1);
            }
            else
            {
                builder.CreateLoadInt32(1);
                if(ChelaType.GetIntType() != coercionType)
                    Cast(node, null, ChelaType.GetIntType(), coercionType);
            }

            // Perform the operation
            switch(node.GetOperation())
            {
            case PrefixOperation.Increment:
                builder.CreateAdd();
                break;
            case PrefixOperation.Decrement:
                builder.CreateSub();
                break;
            default:
                Error(node, "invalid prefix operation.");
                break;
            }

            // Now, perform the assignment.
            PerformAssignment(node, variable);

            // Set the node value.
            node.SetNodeValue(variable);

            return builder.EndNode();
        }
Esempio n. 2
0
        public override AstNode Visit(PrefixOperation node)
        {
            // Get the variable.
            Expression variableExpr = node.GetVariable();

            // Visit the variable.
            variableExpr.Accept(this);

            // Check the types.
            IChelaType variableType = variableExpr.GetNodeType();

            // The variable must be a reference.
            if(!variableType.IsReference())
                Error(node, "trying to set something that isn't a reference.");

            // Check the operation type.
            IChelaType coercionType = DeReferenceType(variableType);
            if(coercionType.IsConstant())
                Error(node, "cannot modify constants.");

            // TODO: Add operator overloading.
            if(!coercionType.IsInteger() && !coercionType.IsFloat() && !coercionType.IsPointer())
                Error(node, "increment/decrement operator expected an integer/float/pointer variable.");

            // Set the node types.
            node.SetCoercionType(coercionType);
            node.SetNodeType(variableExpr.GetNodeType());

            return node;
        }