Пример #1
0
        public void Build()
        {
            while (_reader.HasNext())
            {
                _reader.Next();
                var token = _reader.Current();

                switch (token.TokenType)
                {
                case TokenType.Keyword:

                    if (_reader.Current().Value == Keywords.FOR)
                    {
                        var block = new ForBlockBuilder(_reader, _block).Build();

                        Block.Children.Add(block);
                    }
                    else if (_reader.Current().Value == Keywords.IF)
                    {
                        var block = new IfBlockBuilder(_reader, _block).Build();

                        Block.Children.Add(block);
                    }
                    else if (_reader.Current().Value == Keywords.FOREACH)
                    {
                        var block = new ForeachBuilder(_reader, _block).Build();

                        Block.Children.Add(block);
                    }
                    else if (_reader.Current().Value == Keywords.DO)
                    {
                        var block = new DoWhileScopeBuilder(_reader, _block).Build();

                        Block.Children.Add(block);
                    }
                    else if (_reader.Current().Value == Keywords.WHILE)
                    {
                        var block = new WhileBlockBuilder(_reader, _block).Build();

                        Block.Children.Add(block);
                    }
                    else if (_reader.Current().Value == Keywords.TRY)
                    {
                        var block = new TryCatchFinallyBlockBuilder(_reader, _block).Build();

                        Block.Children.Add(block);
                    }
                    else
                    {
                        throwError("");
                    }

                    break;

                case TokenType.Operator:

                    switch (_reader.Current().OperatorType)
                    {
                    /*
                     *  variable declare
                     */
                    case OperatorType.Declare:

                        var decExpression = new DeclareExpressionBuilder(_reader, _block).Build();

                        Block.Children.Add(decExpression);

                        break;

                    /*
                     * Function define expression is all right in this scope
                     *
                     */

                    case OperatorType.Function:

                        var function = new FunctionBuilder(_reader, _block).Build();

                        var funcDefineExp = new FunctionDefineExpression(_block);

                        funcDefineExp.Root = new FunctionDefineNode(_block);

                        funcDefineExp.Root.Operands.Add(new OperandNode(function));

                        Block.Children.Add(funcDefineExp);

                        break;

                    /*
                     *  New expression
                     */
                    case OperatorType.NewInstance:
                    /*
                     * Increament  expression
                     *
                     */
                    case OperatorType.Increment:
                    case OperatorType.Decrement:

                        _reader.Back();

                        var crementExpression = new ExpressionBuilder(_reader, false, _block).Build();

                        Block.Children.Add(crementExpression);

                        break;

                    /*
                     * Expression can not start with operator ,excepts  increment(++) ,decrement(--) ,var ,new ,function
                     *
                     */

                    default:

                        throwError("");

                        break;
                    }

                    break;

                /*
                 * Back one token ,to build excuting expression
                 *
                 */

                case TokenType.Identifier:

                    _reader.Back();

                    var expression = new ExpressionBuilder(_reader, true, _block).Build();

                    Block.Children.Add(expression);

                    break;

                /*
                 * Expression can  not start with those kinds of token
                 * number,null,bool,string
                 *
                 */
                case TokenType.String:
                case TokenType.Number:
                case TokenType.Bool:
                case TokenType.Null:
                default:

                    throwError("");

                    break;
                }
            }
        }
Пример #2
0
        public OrderdedBlock Build()
        {
            OrderdedBlock orderingBlock = new OrderdedBlock(_block);

            throwErrorIfHasNoNextOrNext($"incompleted {_parentName} block;");

            /*
             * to check ,the block is single expression or mutiple expression
             *
             */

            if (_reader.Current().OperatorType == OperatorType.LeftBrace)
            {
                while (_reader.HasNext())
                {
                    _reader.Next();

                    var token = _reader.Current();

                    switch (token.TokenType)
                    {
                    case TokenType.Keyword:

                        if (_reader.Current().Value == Keywords.FOR)
                        {
                            var block = new ForBlockBuilder(_reader, orderingBlock).Build();

                            orderingBlock.Children.Add(block);
                        }
                        else if (_reader.Current().Value == Keywords.IF)
                        {
                            var block = new IfBlockBuilder(_reader, orderingBlock).Build();

                            orderingBlock.Children.Add(block);
                        }
                        else if (_reader.Current().Value == Keywords.FOREACH)
                        {
                            var block = new ForeachBuilder(_reader, orderingBlock).Build();

                            orderingBlock.Children.Add(block);
                        }
                        else if (_reader.Current().Value == Keywords.DO)
                        {
                            var block = new DoWhileScopeBuilder(_reader, orderingBlock).Build();

                            orderingBlock.Children.Add(block);
                        }
                        else if (_reader.Current().Value == Keywords.WHILE)
                        {
                            var block = new WhileBlockBuilder(_reader, orderingBlock).Build();

                            orderingBlock.Children.Add(block);
                        }
                        else if (_reader.Current().Value == Keywords.TRY)
                        {
                            var block = new TryCatchFinallyBlockBuilder(_reader, orderingBlock).Build();

                            orderingBlock.Children.Add(block);
                        }
                        else
                        {
                            throwUnexceptedError();
                        }

                        break;

                    /*
                     *  the operators that  a expression  can start with
                     *  new;
                     *  break;
                     *  continue;
                     *  throw;
                     *  ++;
                     *  --;
                     *  new;
                     *  var;
                     *  identifier;
                     *
                     *  function operator is not allowed ,it's just allowed in topiest block
                     */

                    case TokenType.Operator:

                        switch (_reader.Current().OperatorType)
                        {
                        case OperatorType.Break:

                            orderingBlock.Children.Add(buildContinueOrBreakExpression(false));

                            throwIfBlockNotFinish();

                            return(orderingBlock);

                        case OperatorType.Continue:

                            orderingBlock.Children.Add(buildContinueOrBreakExpression(true));

                            throwIfBlockNotFinish();

                            return(orderingBlock);

                        case OperatorType.Declare:

                            var decExpression = new DeclareExpressionBuilder(_reader, orderingBlock).Build();

                            orderingBlock.Children.Add(decExpression);

                            break;

                        case OperatorType.Return:

                            orderingBlock.Children.Add(buildReturnExpression(orderingBlock));

                            throwIfBlockNotFinish();

                            return(orderingBlock);

                        //block finishd
                        case OperatorType.RightBrace:

                            return(orderingBlock);

                        case OperatorType.Increment:
                        case OperatorType.Decrement:

                            _reader.Back();

                            var expression0 = new ExpressionBuilder(_reader, true, orderingBlock).Build();
                            orderingBlock.Children.Add(expression0);

                            break;

                        default:

                            throwUnexceptedError();

                            break;
                        }

                        break;

                    case TokenType.Identifier:

                        _reader.Back();

                        var expression = new ExpressionBuilder(_reader, true, orderingBlock).Build();

                        orderingBlock.Children.Add(expression);

                        break;

                    case TokenType.String:
                    case TokenType.Number:
                    case TokenType.Bool:
                    case TokenType.Null:
                    default:

                        throwUnexceptedError();

                        break;
                    }
                }
            }
            else
            {
                if (_reader.Current().OperatorType == OperatorType.Continue)
                {
                    orderingBlock.Children.Add(buildContinueOrBreakExpression(true));

                    return(orderingBlock);
                }
                else if (_reader.Current().OperatorType == OperatorType.Break)
                {
                    orderingBlock.Children.Add(buildContinueOrBreakExpression(false));

                    return(orderingBlock);
                }
                else if (_reader.Current().OperatorType == OperatorType.Return)
                {
                    orderingBlock.Children.Add(buildReturnExpression(orderingBlock));

                    return(orderingBlock);
                }
                else
                {
                    _reader.Back();

                    var expression = new ExpressionBuilder(_reader, true, orderingBlock).Build();

                    orderingBlock.Children.Add(expression);

                    return(orderingBlock);
                }
            }

            throwError($"{_parentName} block is incompleted;");

            return(orderingBlock);
        }