Пример #1
0
        private bool ParseUnaryOperator(Tokenizer tokenizer, TokenOperator token)
        {
            // Handle group
            if (token.OpType == TokenOperator.OperatorType.GroupStart)
            {
                // Consume token
                tokenizer.Next();
                return(ParseGroup(tokenizer));
            }
            else if (token.OpType == TokenOperator.OperatorType.VectorStart)
            {
                // Consume token
                tokenizer.Next();
                return(ParseVector(tokenizer, false));
            }

            bool ret = true;

            switch (token.OpType)
            {
            case TokenOperator.OperatorType.Minus:
                token = new TokenOperator(TokenOperator.OperatorType.UnaryMinus, "-");
                break;

            case TokenOperator.OperatorType.Plus:
                token = new TokenOperator(TokenOperator.OperatorType.UnaryPlus, "+");
                break;

            case TokenOperator.OperatorType.Negate:
                break;

            case TokenOperator.OperatorType.Complement:
                break;

            default:
                Error("Expected unary operator (-, +, !) but got `" + token.Text + "'", tokenizer);
                ret = false;
                break;
            }

            if (ret)
            {
                tokenizer.Next();
                ret = ParseExpression(tokenizer, token.Properties.Priority, token.Properties.LeftAssoc);
            }

            if (ret)
            {
                Instruction inst = new InstructionFunction(token.Text, Operations.LookupOperator(token.OpType, 1), 1);

                if (inst != null)
                {
                    d_instructions.Add(inst);
                }
            }

            return(ret);
        }
Пример #2
0
        private bool ParseVector(Tokenizer tokenizer, bool indexer)
        {
            Token next    = tokenizer.Peek();
            bool  loopit  = true;
            int   numargs = 0;

            if (next != null && (next is TokenOperator) && ((TokenOperator)next).OpType == TokenOperator.OperatorType.VectorEnd)
            {
                // Consume vector end
                tokenizer.Next();
                loopit = false;
            }

            while (loopit)
            {
                if (!ParseExpression(tokenizer, -1, false))
                {
                    return(false);
                }

                ++numargs;

                // See what is next
                next = tokenizer.Peek();

                if (next == null || !(next is TokenOperator))
                {
                    return(Error("Expected `operator', but got " + (next != null ? next.Text : "(nothing)"), tokenizer));
                }

                TokenOperator nextop = next as TokenOperator;

                if (nextop.OpType == TokenOperator.OperatorType.VectorEnd)
                {
                    // Consume it
                    tokenizer.Next();
                    break;
                }
                else if (nextop.OpType != TokenOperator.OperatorType.Comma)
                {
                    return(Error("Expected `,' but got " + next.Text, tokenizer));
                }

                // Consume token
                tokenizer.Next();
            }

            if (indexer)
            {
                InstructionFunction f = d_instructions[d_instructions.Count - 1] as InstructionFunction;

                if (numargs == 1 && f != null && f.Name == "..")
                {
                    d_instructions.Remove(f);
                    d_instructions.Add(new InstructionIndexRange());
                }
                else
                {
                    d_instructions.Add(new InstructionIndex(numargs));
                }
            }
            else
            {
                d_instructions.Add(new InstructionVector(numargs));
            }

            return(true);
        }