Example #1
0
        private void buildNewPriority()
        {
            OperatorNode lastOperand = null;

            if (!_reader.Current().OperatorType.IsUnaryOperator())
            {
                /*
                 *  current node is  binary operator
                 *
                 */
                if (_currentNode.Operands.Count > 1)
                {
                    lastOperand = (OperatorNode)_currentNode.Operands[1];
                    _currentNode.Operands.RemoveAt(1);
                }

                /*
                 *  _current node is unary operator
                 */
                else
                {
                    lastOperand = (OperatorNode)_currentNode.Operands[0];
                    _currentNode.Operands.RemoveAt(0);
                }
            }

            _reader.Back();//back one token

            var node = new AstNodeBuilder(_reader, _block, _interceptChars, lastOperand).Build();

            _currentNode.Operands.Add(node);
        }
Example #2
0
        public CallNode Build()
        {
            var callNode = OperatorNodeFactory.CreateCall(_block);

            IList <OperatorNode> parameters = new List <OperatorNode>();

            bool isParameterCompleted = false;

            while (_reader.HasNext())
            {
                var node = new AstNodeBuilder(_reader, _block, _interceptChars).Build();

                if (node != null)
                {
                    parameters.Add(node);
                }

                if (_reader.Current().OperatorType == OperatorType.RightParenthesis)
                {
                    isParameterCompleted = true;

                    break;
                }
            }

            if (!isParameterCompleted)
            {
                throwError("");
            }

            callNode.Operands.AddRange(parameters);

            return(callNode);
        }
Example #3
0
        private Expression buildReturnExpression(BreakableBlock block)
        {
            var expNode = new AstNodeBuilder(_reader, block, _interceptChars).Build();

            var retNode = OperatorNodeFactory.CreateReturn(block);

            retNode.Operands.Add(expNode);

            var exp = new Expression(block);

            exp.Root = retNode;

            return(exp);
        }
Example #4
0
        public TernaryOperatorNode Build()
        {
            TernaryOperatorNode ternaryNode = new TernaryOperatorNode();

            while (_reader.HasNext())
            {
                var node = new AstNodeBuilder(_reader, _block, _interceptChars).Build();

                if (node == null)
                {
                    throwError("ternary empty expression is not allowed!");
                }

                /*
                 * expression end
                 *
                 */
                if (_reader.Current().Value == ";")
                {
                    if (ternaryNode.Operands.Count == 2)//more two
                    {
                        throwError("ternary expression incorrect!");
                    }
                    else if (ternaryNode.Operands.Count == 0)// only  one
                    {
                        throwError("ternary expression incorrect!");
                    }
                    else
                    {
                        ternaryNode.Operands.Add(node);

                        return(ternaryNode);
                    }
                }

                /*
                 * start a new ternary builder
                 *
                 */
                else if (_reader.Current().Value == "?")
                {
                    if (!node.OutputType.IsBool())
                    {
                        throwError("before '?' require a expression which output is bool! ");
                    }

                    var ternaryNode2 = new TerbaryBuilder(_reader, _block).Build();
                    ternaryNode2.Operands.Insert(0, node);

                    ternaryNode.Operands.Add(ternaryNode2);

                    if (_reader.Current().Value == ";")
                    {
                        if (ternaryNode.Operands.Count != 2)
                        {
                            throwError("incompleted ternary exception!");
                        }

                        return(ternaryNode);
                    }
                }
                else if (_reader.Current().Value == ":")
                {
                    if (ternaryNode.Operands.Count == 1)
                    {
                        ternaryNode.Operands.Add(node);

                        return(ternaryNode);
                    }
                    else
                    {
                        ternaryNode.Operands.Add(node);
                    }
                }
            }

            throwError("incompleted ternary expression!");

            return(ternaryNode);
        }
        public DeclareExpression Build()
        {
            var expression = new DeclareExpression(_block);

            var node = new DeclareOperator(_block);

            while (_reader.HasNext())
            {
                _reader.Next();

                var token = _reader.Current();

                /*
                 *  In this loop,tokens type  can only be ',', 'identifer',';','='
                 *  ',',';','=', requires previous token must be an  Identifier
                 *
                 */

                if (token.TokenType == TokenType.Identifier)
                {
                    node.Operands.Add(new OperandNode(new JString(token.Value)));
                }
                else if (token.OperatorType == OperatorType.Coma)//mutiple  variable declaration
                {
                    requirePreviousIsIdentifier();

                    continue;
                }
                else if (token.OperatorType == OperatorType.ExpressionEnd)//declaration ends
                {
                    requirePreviousIsIdentifier();

                    node.DoCheck();

                    expression.Root = node;

                    return(expression);
                }
                else if (token.OperatorType == OperatorType.Assignment)// this expression is a  declare-assignment expression
                {
                    requirePreviousIsIdentifier();

                    var assign = new DeclareAsignmentNode(_block);

                    var astNode = new AstNodeBuilder(_reader, _block, _intercptChars).Build();

                    node.DoCheck();

                    assign.Operands = node.Operands;

                    assign.Operands.Add(astNode);

                    expression.Root = assign;

                    return(expression);
                }
                else
                {
                    //invalid token type
                    throwError("");
                }
            }
            // expression incomplete
            throwError("");

            return(null);
        }