示例#1
0
        Node SimpleFactor()
        {
            // SimpFactor := '(' Expr ')'
            //            or '|' Expr '|'
            //            or Number
            //            or Identifier
            //            or '-' Factor

            Node      result;
            TokenType token = scanner.Token;

            if (token == TokenType.TokenLParen)
            {
                scanner.Accept();
                result = Expression();
                if (scanner.Token == TokenType.TokenRParen)
                {
                    scanner.Accept();
                }
                else
                {
                    status = Status.Error;
                }
            }
            else if (token == TokenType.TokenTube)
            {
                scanner.Accept();
                result = new AbsoluteValueNode(Expression());
                if (scanner.Token == TokenType.TokenTube)
                {
                    scanner.Accept();
                }
                else
                {
                    status = Status.Error;
                }
            }
            else if (token == TokenType.TokenNumber)
            {
                result = new NumberNode(new Number(scanner.Number));
                scanner.Accept();
            }
            else if (token == TokenType.TokenIdentifer)
            {
                result = new UnknownNode(scanner.Symbol);
                scanner.Accept();
            }
            else if (token == TokenType.TokenMinus)
            {
                scanner.Accept();
                result = new MultiNode(new NumberNode(new Number(-1)), Factor());
            }
            else
            {
                status = Status.Error;
                result = null;
            }

            return(result);
        }
示例#2
0
        public override SimplifyResult Replace(out Node node)
        {
            if (Children.Count == 1)
            {
                node = Children[0];
                return(new SimplifyResult(true));
            }
            else if (Children.Count == 0)
            {
                node = new NumberNode(Number.One);
                return(new SimplifyResult(true));
            }

            foreach (Node n in Children)
            {
                if (typeof(NumberNode).IsInstanceOfType(n) && ((NumberNode)n).Value.N == 0)
                {
                    node = n;                     // zero
                    return(new SimplifyResult(true, true));
                }
                else if (typeof(SumNode).IsInstanceOfType(n))
                {
                    List <Node> l = new List <Node>();
                    foreach (Node child in ((SumNode)n).Children)
                    {
                        List <Node> ll = new List <Node>(Children);
                        for (int i = 0; i < ll.Count; i++)
                        {
                            if (ll[i] == n)
                            {
                                ll.RemoveAt(i);
                                break;
                            }
                        }
                        ll.Add(child);
                        l.Add(new MultiNode(ll));
                    }

                    node = new SumNode(l);
                    return(new SimplifyResult(true, true));
                }
            }

            node = null;
            return(new SimplifyResult(false));
        }
示例#3
0
 public override SimplifyResult Replace(out Node node)
 {
     if (Children.Count == 1)
     {
         node = Children[0];
         return(new SimplifyResult(true));
     }
     else if (Children.Count == 0)
     {
         node = new NumberNode(Number.Zero);
         return(new SimplifyResult(true));
     }
     else
     {
         node = null;
         return(new SimplifyResult(false));
     }
 }
示例#4
0
 public override SimplifyResult Replace(out Node node)
 {
     if (typeof(NumberNode).IsInstanceOfType(Child))
     {
         NumberNode n = (NumberNode)Child;
         if (n.Value < Number.Zero)
         {
             node = new NumberNode(Number.Zero - n.Value);
         }
         else
         {
             node = n;
         }
         return(new SimplifyResult(true, true));
     }
     else
     {
         node = null; return(new SimplifyResult(false));
     }
 }
示例#5
0
        public override SimplifyResult Replace(out Node node)
        {
            if (Children.Count == 0)
            {
                node = new NumberNode(Number.One);
                return(new SimplifyResult(true));
            }

            node = Children[0];
            for (int i = 1; i < Children.Count; i++)
            {
                if (Sign[i])
                {
                    node = new MultiNode(Children[i], node);
                }
                else
                {
                    node = new DivideNode(node, Children[i]);
                }
            }
            return(new SimplifyResult(true));
        }
示例#6
0
        public override SimplifyResult Replace(out Node node)
        {
            if (typeof(NumberNode).IsInstanceOfType(RChild) ||
                ((NumberNode)RChild).Value.M == 1)
            {
                NumberNode n = (NumberNode)RChild;

                if (n.Value.N == 0)
                {
                    node = new NumberNode(Number.One);
                    return(new SimplifyResult(true));
                }
                else
                {
                    MultiNode result = new MultiNode();
                    long      x      = Math.Abs(n.Value.N);
                    for (long i = 0; i < x; i++)
                    {
                        result.Children.Add(LChild);
                    }

                    if (n.Value.N < 0)
                    {
                        node = new DivideNode(new NumberNode(Number.One), result);
                    }
                    else
                    {
                        node = result;
                    }
                    return(new SimplifyResult(true, true));
                }
            }
            else
            {
                node = null;
                return(new SimplifyResult(false));
            }
        }