예제 #1
0
 public TokenList(TokenList List)
 {
     for (TokenListNode node = List.First; node != null; node = node.Next)
     {
         Add(node.Value);
     }
 }
예제 #2
0
        public LinkedList <Token> ToLinkedList()
        {
            var newList = new LinkedList <Token>();

            for (TokenListNode node = first; node != null; node = node.Next)
            {
                newList.AddLast(node.Value);
            }
            return(newList);
        }
예제 #3
0
        public override List <Token> Tokenize()
        {
            var returnList = new List <Token>();

            for (TokenListNode node = list.First; node != null; node = node.Next)
            {
                returnList.AddRange(((Value)node.Value).Tokenize());
            }
            return(returnList);
        }
예제 #4
0
 public void Add(Token Token)
 {
     if (empty)
     {
         last = new TokenListNode(Token);
         first = last;
         empty = false;
     }
     else
     {
         last.Next = new TokenListNode(Token);
         TokenListNode temp = last;
         last = last.Next;
         last.Previous = temp;
     }
 }
예제 #5
0
 public void Add(Token Token)
 {
     if (empty)
     {
         last  = new TokenListNode(Token);
         first = last;
         empty = false;
     }
     else
     {
         last.Next = new TokenListNode(Token);
         TokenListNode temp = last;
         last          = last.Next;
         last.Previous = temp;
     }
 }
예제 #6
0
 /// <summary>
 /// Insert a new node between 2 other ones
 /// </summary>
 /// <param name="Node"></param>
 /// <param name="Left"></param>
 /// <param name="Right"></param>
 public void Insert(TokenListNode Node, TokenListNode Left, TokenListNode Right)
 {
     if (Left != null)
         Left.Next = Node;
     else
     {
         first = Node;
     }
     Node.Next = Right;
     Node.Previous = Left;
     if (Right != null)
         Right.Previous = Node;
     else
     {
         last = Node;
     }
 }
예제 #7
0
 /// <summary>
 /// Insert a new node between 2 other ones
 /// </summary>
 /// <param name="Node"></param>
 /// <param name="Left"></param>
 /// <param name="Right"></param>
 public void Insert(TokenListNode Node, TokenListNode Left, TokenListNode Right)
 {
     if (Left != null)
     {
         Left.Next = Node;
     }
     else
     {
         first = Node;
     }
     Node.Next     = Right;
     Node.Previous = Left;
     if (Right != null)
     {
         Right.Previous = Node;
     }
     else
     {
         last = Node;
     }
 }
예제 #8
0
 public static Value CreateValueFromExpression( TokenList List )
 {
     if ( List.First == null ) return new Array();
     //Bracket pass
     var startNode = new TokenListNode();
     int parCount = 0;
     for ( TokenListNode node = List.First ; node != null ; node = node.Next )
     {
         if ( node.Value is ParenthesisOpen )
         {
             if ( parCount == 0 )
             {
                 startNode = node;
             }
             parCount++;
         } else if ( node.Value is ParenthesisClosed )
         {
             parCount--;
             if ( parCount == 0 )
             {
                 var group = new TokenList();
                 for ( TokenListNode innerNode = startNode.Next ; innerNode != node ; innerNode = innerNode.Next )
                 {
                     group.Add( innerNode.Value );
                 }
                 Value val = CreateValueFromExpression( group );
                 List.Insert( new TokenListNode( val ), startNode.Previous, node.Next );
             }
         }
     }
     //Operator pass
     for ( int i = 1 ; i <= Operators.LowestPrecedance ; ++i )
     {
         for ( TokenListNode node = List.First ; node != null ; node = node.Next )
         {
             if ( node.Value is Operator )
             {
                 var oper = (Operator)node.Value;
                 OperatorType type = oper.Type;
                 if ( oper.Precedance == i )
                 {
                     var temp = new TokenList();
                     TokenListNode left = new TokenListNode(), right = new TokenListNode();
                     if ( oper is DotOperator ) type = OperatorType.InfixBinary;
                     switch ( type ) //Using the operators type, determines which tokens to make a value from
                     {
                         case OperatorType.InfixBinary: //A + B
                             temp.Add( node.Previous.Value );
                             temp.Add( node.Value );
                             temp.Add( node.Next.Value );
                             left = node.Previous.Previous;
                             right = node.Next.Next;
                             break;
                         case OperatorType.PrefixBinary: //+ A B
                             temp.Add( node.Next.Value );
                             temp.Add( node.Value );
                             temp.Add( node.Next.Next.Value );
                             left = node.Previous;
                             right = node.Next.Next.Next;
                             break;
                         case OperatorType.PrefixUnary: //+ A
                             temp.Add( node.Value );
                             temp.Add( node.Next.Value );
                             left = node.Previous;
                             right = node.Next.Next;
                             break;
                         case OperatorType.SufixBinary: //A B +
                             temp.Add( node.Previous.Previous.Value );
                             temp.Add( node.Value );
                             temp.Add( node.Previous.Value );
                             left = node.Previous.Previous.Previous;
                             right = node.Next;
                             break;
                         case OperatorType.SufixUnary: //A +
                             temp.Add( node.Value );
                             temp.Add( node.Previous.Value );
                             left = node.Previous.Previous;
                             right = node.Next;
                             break;
                     }
                     Value val = MakeValue( temp, ( type == OperatorType.PrefixUnary || type == OperatorType.SufixUnary ) );
                     List.Insert( new TokenListNode( val ), left, right );
                 }
             }
         }
     }
     if ( List.First.Next != null )
     {
         return new ExpressionSequence( List );
     }
     return (Value)List.First.Value;
 }
예제 #9
0
        public static Value CreateValueFromExpression(TokenList List)
        {
            if (List.First == null)
            {
                return(new Array());
            }
            //Bracket pass
            var startNode = new TokenListNode();
            int parCount  = 0;

            for (TokenListNode node = List.First; node != null; node = node.Next)
            {
                if (node.Value is ParenthesisOpen)
                {
                    if (parCount == 0)
                    {
                        startNode = node;
                    }
                    parCount++;
                }
                else if (node.Value is ParenthesisClosed)
                {
                    parCount--;
                    if (parCount == 0)
                    {
                        var group = new TokenList();
                        for (TokenListNode innerNode = startNode.Next; innerNode != node; innerNode = innerNode.Next)
                        {
                            group.Add(innerNode.Value);
                        }
                        Value val = CreateValueFromExpression(group);
                        List.Insert(new TokenListNode(val), startNode.Previous, node.Next);
                    }
                }
            }
            //Operator pass
            for (int i = 1; i <= Operators.LowestPrecedance; ++i)
            {
                for (TokenListNode node = List.First; node != null; node = node.Next)
                {
                    if (node.Value is Operator)
                    {
                        var          oper = (Operator)node.Value;
                        OperatorType type = oper.Type;
                        if (oper.Precedance == i)
                        {
                            var           temp = new TokenList();
                            TokenListNode left = new TokenListNode(), right = new TokenListNode();
                            if (oper is DotOperator)
                            {
                                type = OperatorType.InfixBinary;
                            }
                            switch (type)                               //Using the operators type, determines which tokens to make a value from
                            {
                            case OperatorType.InfixBinary:              //A + B
                                temp.Add(node.Previous.Value);
                                temp.Add(node.Value);
                                temp.Add(node.Next.Value);
                                left  = node.Previous.Previous;
                                right = node.Next.Next;
                                break;

                            case OperatorType.PrefixBinary:                                     //+ A B
                                temp.Add(node.Next.Value);
                                temp.Add(node.Value);
                                temp.Add(node.Next.Next.Value);
                                left  = node.Previous;
                                right = node.Next.Next.Next;
                                break;

                            case OperatorType.PrefixUnary:                                     //+ A
                                temp.Add(node.Value);
                                temp.Add(node.Next.Value);
                                left  = node.Previous;
                                right = node.Next.Next;
                                break;

                            case OperatorType.SufixBinary:                                     //A B +
                                temp.Add(node.Previous.Previous.Value);
                                temp.Add(node.Value);
                                temp.Add(node.Previous.Value);
                                left  = node.Previous.Previous.Previous;
                                right = node.Next;
                                break;

                            case OperatorType.SufixUnary:                                     //A +
                                temp.Add(node.Value);
                                temp.Add(node.Previous.Value);
                                left  = node.Previous.Previous;
                                right = node.Next;
                                break;
                            }
                            Value val = MakeValue(temp, (type == OperatorType.PrefixUnary || type == OperatorType.SufixUnary));
                            List.Insert(new TokenListNode(val), left, right);
                        }
                    }
                }
            }
            if (List.First.Next != null)
            {
                return(new ExpressionSequence(List));
            }
            return((Value)List.First.Value);
        }