Пример #1
0
        public override Calc_node VisitLog_exp([NotNull] CalcParser.Log_expContext context)
        {
            // 生成 log 节点
            Binary_node node = new Binary_node();

            node.op = Binary_node.Operator.LOG;
            if (context.children[2] is CalcParser.ExpContext)
            {
                node.node1 = Visit(context.children[2]);
                if (context.children[4] is CalcParser.ExpContext)
                {
                    node.node2 = Visit(context.children[4]);
                }
                else
                {
                    node.node2 = null;
                }
            }
            else
            {
                node.node1 = null;
                if (context.exp(0) != null)
                {
                    node.node2 = Visit(context.exp(0));
                }
                else
                {
                    node.node2 = null;
                }
            }
            //node.node1 = Visit(context.exp(0)),
            //node.node2 = Visit(context.exp(1))

            return(node);
        }
Пример #2
0
        public override Calc_node VisitExp([NotNull] CalcParser.ExpContext context)
        {
            int i = context.ChildCount;

            if (i == 1)
            {
                //node log_exp trig_exp
                var subtree = context.GetChild(0);
                return(Visit(subtree));
            }
            else if (i == 2)
            {
                // LeftParen RightParen 空括号节点
                if (context.LeftParen() != null)
                {
                    return(new Bracket_node());
                }
                else if (context.Minus() != null)
                {
                    Calc_node absolute_val = Visit(context.children[1]);
                    absolute_val.fresult = -absolute_val.fresult;
                    absolute_val.result  = -absolute_val.result;
                    return(absolute_val);
                }
                else
                {
                    return(null);
                }
            }
            else if (i == 3)
            {
                var child = context.GetChild(1);
                if (child.ChildCount == 0)
                {
                    // + - * / ^
                    Binary_node node = new Binary_node();
                    switch (child.GetText())
                    {
                    case "+":
                        node.op = Binary_node.Operator.ADD;
                        break;

                    case "-":
                        node.op = Binary_node.Operator.MINUS;
                        break;

                    case "*":
                        node.op = Binary_node.Operator.MULTI;
                        break;

                    case "/":
                        node.op = Binary_node.Operator.DIVIDE;
                        break;

                    case "^":
                        node.op = Binary_node.Operator.EXPO;
                        break;
                    }
                    node.node1 = Visit(context.GetChild(0));
                    node.node2 = Visit(context.GetChild(2));
                    return(node);
                }
                else
                {
                    //(exp)
                    return(new Bracket_node
                    {
                        internal_node = Visit(context.GetChild(1))
                    });
                }
            }
            else
            {
                // will not be executed Unless there is a error
                throw(new ApplicationException("unexpected ChildCount of ExpContext"));
            }
        }