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); }
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; }