Пример #1
0
        private void revert(Node currentNode, Node leftChild, Token leftToken)
        {
            currentNode.LeftChild = leftChild.LeftChild;
            var rightChild = currentNode.RightChild;

            rightChild.Token.Id = IdGenerator.GetIdGenerator();

            var node = new Node
            {
                Token      = (Token)leftToken.Clone(),
                LeftChild  = leftChild.RightChild,
                RightChild = RightChild
            };

            node.Token.Id          = IdGenerator.GetIdGenerator();
            currentNode.RightChild = node;
        }
Пример #2
0
 private bool optimize()
 {
     if (LeftChild != null &&
         LeftChild.Depth > RightChild.Depth + 1)
     {
         if (Token is PlusToken || Token is MultiplyToken)
         {
             reduce(this, LeftChild);
             return(true);
         }
         if (LeftChild.Token is DivideToken)
         {
             revert(this, LeftChild, new MultiplyToken(IdGenerator.GetIdGenerator()));
             return(true);
         }
         if (LeftChild.Token is MinusToken)
         {
             revert(this, LeftChild, new MultiplyToken(IdGenerator.GetIdGenerator()));
             return(true);
         }
     }
     return(false);
 }
        public IEnumerable <Token> Scan(string expression)
        {
            _reader = new StringReader(expression);
            try
            {
                var tokens = new List <Token>();
                while (_reader.Peek() != -1)
                {
                    var c = (char)_reader.Peek();
                    if (Char.IsWhiteSpace(c))
                    {
                        _reader.Read();
                        continue;
                    }

                    if (Char.IsDigit(c) || c == '.')
                    {
                        var nr = ParseNumber();
                        tokens.Add(new NumberConstantToken(nr));
                    }
                    else if (c == '-')
                    {
                        tokens.Add(new MinusToken(IdGenerator.GetIdGenerator()));
                        _reader.Read();
                    }
                    else if (c == '+')
                    {
                        tokens.Add(new PlusToken(IdGenerator.GetIdGenerator()));
                        _reader.Read();
                    }
                    else if (c == '*')
                    {
                        tokens.Add(new MultiplyToken(IdGenerator.GetIdGenerator()));
                        _reader.Read();
                    }
                    else if (c == '/')
                    {
                        tokens.Add(new DivideToken(IdGenerator.GetIdGenerator()));
                        _reader.Read();
                    }
                    else if (c == '(')
                    {
                        tokens.Add(new OpenParenthesisToken(IdGenerator.GetIdGenerator()));
                        _reader.Read();
                    }
                    else if (c == ')')
                    {
                        tokens.Add(new ClosedParenthesisToken(IdGenerator.GetIdGenerator()));
                        _reader.Read();
                    }
                    else if (char.IsLetter(c))
                    {
                        var variable = ParseVariable();
                        tokens.Add(new NumberConstantToken(variable));
                    }
                    else
                    {
                        throw new Exception("Unknown character in expression: " + c);
                    }
                }

                return(tokens);
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                Console.ReadLine();

                return(new List <Token>());
            }
        }
Пример #4
0
 public override object Clone()
 {
     return(new MultiplyToken(IdGenerator.GetIdGenerator()));
 }
Пример #5
0
 public override object Clone()
 {
     return(new PlusToken(IdGenerator.GetIdGenerator()));
 }
Пример #6
0
 public override object Clone()
 {
     return(new ClosedParenthesisToken(IdGenerator.GetIdGenerator()));
 }