Example #1
0
        private OperatorDeclarationNode ParseOperator(Token token, ParseNodeList attributes, Modifiers modifiers, ParseNode type) {
            NextToken();        // operator
            TokenType operatorKind = EatOverloadableOperator();

            OperatorDeclarationNode tree = new OperatorDeclarationNode(
                token,
                attributes,
                modifiers,
                operatorKind,
                type,
                ParseParensFormalParameterList(),
                ParseBlockOpt());

            switch (tree.operatorTokenType) {
                // unary or binary operators
                case TokenType.Plus:
                case TokenType.Minus:
                    if (tree.Parameters.Count != 1 && tree.Parameters.Count != 2) {
                        ReportError(ParseError.WrongNumberOfArgsToOperator, tree.token);
                    }
                    break;

                // unary operators
                case TokenType.Bang:
                case TokenType.Tilde:
                case TokenType.PlusPlus:
                case TokenType.MinusMinus:
                case TokenType.True:
                case TokenType.False:
                    if (tree.Parameters.Count != 1) {
                        ReportError(ParseError.WrongNumberOfArgsToUnaryOperator, tree.token);
                    }
                    break;

                // binary operators
                case TokenType.Star:
                case TokenType.Slash:
                case TokenType.Percent:
                case TokenType.Ampersand:
                case TokenType.Bar:
                case TokenType.Hat:
                case TokenType.ShiftLeft:
                case TokenType.ShiftRight:
                case TokenType.EqualEqual:
                case TokenType.NotEqual:
                case TokenType.Greater:
                case TokenType.GreaterEqual:
                case TokenType.Less:
                case TokenType.LessEqual:
                    if (tree.Parameters.Count != 2) {
                        ReportError(ParseError.WrongNumberOfArgsToBinnaryOperator, tree.token);
                    }
                    break;

                default:
                    break;
            }

            return tree;
        }
Example #2
0
        private OperatorDeclarationNode ParseConversionOperator(Token token, ParseNodeList attributes, Modifiers modifiers) {
            TokenType implicitExplicit = NextToken().Type;   // implicit/explicit
            Eat(TokenType.Operator);
            OperatorDeclarationNode tree = new OperatorDeclarationNode(
                token,
                attributes,
                modifiers,
                implicitExplicit,
                ParseType(),
                ParseParensFormalParameterList(),
                ParseBlockOpt());

            if (tree.Parameters.Count != 1) {
                ReportError(ParseError.ConversionMustHaveOneParam, tree.token);
            }

            return tree;
        }