Beispiel #1
0
 public TokenList(TokenList List)
 {
     for (TokenListNode node = List.First; node != null; node = node.Next)
     {
         Add(node.Value);
     }
 }
Beispiel #2
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;
 }
Beispiel #3
0
 private static Value MakeValue( TokenList List, bool Unary )
 {
     if ( Unary )
     {
         Value result = NoValue.Value;
         if ( List.First.Value is ICanRunAtCompile )
         {
             result = ( (ICanRunAtCompile)List.First.Value ).CompileTimeOperate( (Value)List.First.Next.Value, NoValue.Value );
         }
         if ( result is NoValue )
         {
             return new Expression( (Value)List.First.Next.Value, NoValue.Value, (Operator)List.First.Value );
         }
         return result;
     } else
     {
         Value result = NoValue.Value;
         if ( List.First.Next.Value is ICanRunAtCompile )
         {
             result = ( (ICanRunAtCompile)List.First.Next.Value ).CompileTimeOperate( (Value)List.First.Value,
                                                                                    (Value)List.First.Next.Next.Value );
         }
         if ( result is NoValue )
         {
             return new Expression( (Value)List.First.Value, (Value)List.First.Next.Next.Value,
                                   (Operator)List.First.Next.Value );
         }
         return result;
     }
 }
Beispiel #4
0
        public static Value ParseInfixString(string Infix)
        {
            var tokens = new TokenList();
            IEnumerable <string> array = SplitString(Infix);

            foreach (string str in array)
            {
                switch (GetCharType(str[0]))
                {
                case ParseState.Letters:
                    if (str[0] == '"')
                    {
                        tokens.Add(new String(str.Substring(1, str.Length - 2)));
                    }
                    else
                    {
                        if (Operators.OperatorDictionary.ContainsKey(str))
                        {
                            tokens.Add((Token)Activator.CreateInstance(Operators.OperatorDictionary[str]));
                        }
                        else
                        {
                            switch (str)
                            {
                            case "true":
                                tokens.Add(new Boolean(true));
                                break;

                            case "false":
                                tokens.Add(new Boolean(false));
                                break;

                            default:
                                tokens.Add(new Identifier(str));
                                break;
                            }
                        }
                    }
                    break;

                case ParseState.Numbers:
                    tokens.Add(new Number(double.Parse(str, CultureInfo.InvariantCulture)));
                    break;

                case ParseState.Signs:
                    string str1 = str;
                    Func <BracketType, bool> fn = (X => Operators.RepresentationDictionary[X.OpeningBracket] == str1);
                    if (Operators.Brackets.Any(fn))
                    {
                        if (str1 == "[" && !(tokens.Last.Value is Identifier) && !(tokens.Last.Value is ParenthesisClosed))
                        {
                            tokens.Add(new MakeArray());
                        }
                        else
                        {
                            tokens.Add((Token)Activator.CreateInstance(Operators.Brackets.First(fn).SpecialOperator));
                        }
                        tokens.Add(new ParenthesisOpen());
                    }
                    else if (Operators.Brackets.Any(X => Operators.RepresentationDictionary[X.ClosingBracket] == str1))
                    {
                        tokens.Add(new ParenthesisClosed());
                    }
                    else
                    {
                        if (tokens.Last.Value is Identifier && str1 == "(")
                        {
                            tokens.Add(new InvokeOperator());
                            tokens.Add(new ParenthesisOpen());
                        }
                        else if (!(tokens.Last.Value is Number || tokens.Last.Value is ParenthesisClosed || tokens.Last.Value is Identifier) && str1 == "-")
                        {
                            tokens.Add(new Number(0));
                            tokens.Add(new Subtraction());
                        }
                        else if (Operators.OperatorDictionary.ContainsKey(str1))
                        {
                            tokens.Add((Operator)Activator.CreateInstance(Operators.OperatorDictionary[str1]));
                        }
                        else
                        {
                            tokens.Add(new Unidentified());
                        }
                    }
                    break;
                }
            }
            originalTokenOrder = tokens.ToLinkedList().ToList();
            Value test = Expression.CreateValueFromExpression(tokens);

            return(test);
        }
Beispiel #5
0
 public static Value ParseInfixString( string Infix )
 {
     var tokens = new TokenList();
     IEnumerable<string> array = SplitString( Infix );
     foreach ( string str in array )
     {
         switch ( GetCharType( str[ 0 ] ) )
         {
             case ParseState.Letters:
                 if ( str[ 0 ] == '"' )
                 {
                     tokens.Add( new String( str.Substring( 1, str.Length - 2 ) ) );
                 } else
                 {
                     if ( Operators.OperatorDictionary.ContainsKey( str ) )
                     {
                         tokens.Add( (Token)Activator.CreateInstance( Operators.OperatorDictionary[ str ] ) );
                     } else
                     {
                         switch ( str )
                         {
                             case "true":
                                 tokens.Add( new Boolean( true ) );
                                 break;
                             case "false":
                                 tokens.Add( new Boolean( false ) );
                                 break;
                             default:
                                 tokens.Add( new Identifier( str ) );
                                 break;
                         }
                     }
                 }
                 break;
             case ParseState.Numbers:
                 tokens.Add( new Number( double.Parse( str, CultureInfo.InvariantCulture ) ) );
                 break;
             case ParseState.Signs:
                 string str1 = str;
                 Func<BracketType, bool> fn = ( X => Operators.RepresentationDictionary[ X.OpeningBracket ] == str1 );
                 if ( Operators.Brackets.Any( fn ) )
                 {
                     if ( str1 == "[" && !( tokens.Last.Value is Identifier ) && !( tokens.Last.Value is ParenthesisClosed ) ) tokens.Add( new MakeArray() );
                     else tokens.Add( (Token)Activator.CreateInstance( Operators.Brackets.First( fn ).SpecialOperator ) );
                     tokens.Add( new ParenthesisOpen() );
                 } else if ( Operators.Brackets.Any( X => Operators.RepresentationDictionary[ X.ClosingBracket ] == str1 ) )
                 {
                     tokens.Add( new ParenthesisClosed() );
                 } else
                 {
                     if ( tokens.Last.Value is Identifier && str1 == "(" )
                     {
                         tokens.Add( new InvokeOperator() );
                         tokens.Add( new ParenthesisOpen() );
                     } else if ( !( tokens.Last.Value is Number || tokens.Last.Value is ParenthesisClosed || tokens.Last.Value is Identifier ) && str1 == "-" )
                     {
                         tokens.Add( new Number( 0 ) );
                         tokens.Add( new Subtraction() );
                     } else if ( Operators.OperatorDictionary.ContainsKey( str1 ) )
                     {
                         tokens.Add( (Operator)Activator.CreateInstance( Operators.OperatorDictionary[ str1 ] ) );
                     } else
                     {
                         tokens.Add( new Unidentified() );
                     }
                 }
                 break;
         }
     }
     originalTokenOrder = tokens.ToLinkedList().ToList();
     Value test = Expression.CreateValueFromExpression( tokens );
     return test;
 }
Beispiel #6
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);
        }
Beispiel #7
0
 public ExpressionSequence(TokenList List)
 {
     list = List;
 }
Beispiel #8
0
 public ExpressionSequence(TokenList List)
 {
     list = List;
 }