public override Node GetBody(Node[] argumentValues)
 {
     return(TernaryOperatorNode.Conditional(TernaryOperatorNode.GreaterThan(argumentValues[0], argumentValues[1]), argumentValues[0], argumentValues[1]));
 }
Exemple #2
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);
        }