Пример #1
0
        public override ParserNode Optimize(ParserNodeContext Context)
        {
            var That = CreateThisInstanceAs <ParserNodeParent>();

            That.Parent = Parent.Optimize(Context);
            return(That);
        }
        override public ParserNode Optimize(ParserNodeContext Context)
        {
            var LeftNodeOptimized  = LeftNode.Optimize(Context);
            var RightNodeOptimized = RightNode.Optimize(Context);

            if ((LeftNodeOptimized is ParserNodeNumericLiteral) && (RightNodeOptimized is ParserNodeNumericLiteral))
            {
                var LeftNodeLiteral  = (ParserNodeNumericLiteral)LeftNodeOptimized;
                var RightNodeLiteral = (ParserNodeNumericLiteral)RightNodeOptimized;
                switch (Operator)
                {
                case "+": return(new ParserNodeNumericLiteral()
                    {
                        Value = LeftNodeLiteral.Value + RightNodeLiteral.Value,
                    });

                case "-": return(new ParserNodeNumericLiteral()
                    {
                        Value = LeftNodeLiteral.Value - RightNodeLiteral.Value,
                    });

                case "*": return(new ParserNodeNumericLiteral()
                    {
                        Value = LeftNodeLiteral.Value * RightNodeLiteral.Value,
                    });

                case "/": return(new ParserNodeNumericLiteral()
                    {
                        Value = LeftNodeLiteral.Value / RightNodeLiteral.Value,
                    });
                }
            }
            return(this);
        }