Ejemplo n.º 1
0
        public static INumerable BuildNumericTernary(List <Token> tokens)
        {
            IBoolable condition = BoolableBuilder.Build(GetTernaryCondition(tokens));

            if (condition.IsNull())
            {
                return(null);
            }

            INumerable confirmationCase = NumerableBuilder.Build(GetTernaryConfirmation(tokens));

            if (confirmationCase.IsNull())
            {
                return(null);
            }

            INumerable negationCase = NumerableBuilder.Build(GetTernaryNegation(tokens));

            if (negationCase.IsNull())
            {
                return(null);
            }

            return(new NumericTernary(condition, confirmationCase, negationCase));
        }
Ejemplo n.º 2
0
        public static ISubcommand BuildWhere(List <Token> tokens)
        {
            IBoolable iboo = BoolableBuilder.Build(tokens);

            if (iboo.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In list declaration there is something wrong with expression: where.");
            }
            else
            {
                return(new Where(iboo));
            }
        }
Ejemplo n.º 3
0
        private static IBoolable BuildExpression(List <Token> tokens)
        {
            // turn list of tokens into list of BoolExpressionElements
            // they are in usual infix notation
            // when this is done, their order is changed to Reverse Polish Notation
            // meanwhile check, if it all can be represented as simple one negated IBoolable
            // finally build BoolExpression

            List <IBoolExpressionElement> infixList = new List <IBoolExpressionElement>();
            List <Token> currentTokens   = new List <Token>();
            bool         readingFunction = false;
            Token        previousToken   = new Token(TokenType.Null);

            // first, merge many tokens into fewer number of IBoolables
            foreach (Token tok in tokens)
            {
                bool actionDone = false;

                if (TokenGroups.IsLogicSign(tok.GetTokenType()))
                {
                    if (readingFunction)
                    {
                        if (Brackets.AllBracketsClosed(currentTokens))
                        {
                            IBoolable ibo = BoolableBuilder.Build(currentTokens);
                            if (!ibo.IsNull())
                            {
                                infixList.Add(ibo);
                            }
                            else
                            {
                                return(null);
                            }
                            currentTokens.Clear();
                            readingFunction = false;
                            infixList.Add(new BoolExpressionOperator(GetBEOT(tok.GetTokenType())));
                        }
                        else
                        {
                            currentTokens.Add(tok);
                        }
                    }
                    else
                    {
                        if (currentTokens.Count > 0)
                        {
                            IBoolable ibo = BoolableBuilder.Build(currentTokens);
                            if (!ibo.IsNull())
                            {
                                infixList.Add(ibo);
                            }
                            else
                            {
                                return(null);
                            }
                            currentTokens.Clear();
                        }
                        infixList.Add(new BoolExpressionOperator(GetBEOT(tok.GetTokenType())));
                    }
                    actionDone = true;
                }

                if (tok.GetTokenType().Equals(TokenType.BracketOn))
                {
                    if (readingFunction)
                    {
                        currentTokens.Add(tok);
                    }
                    else
                    {
                        if (currentTokens.Count == 1 && previousToken.GetTokenType().Equals(TokenType.Variable))
                        {
                            currentTokens.Add(tok);
                            readingFunction = true;
                        }
                        else
                        {
                            if (currentTokens.Count > 0)
                            {
                                IBoolable ibo = BoolableBuilder.Build(currentTokens);
                                if (!ibo.IsNull())
                                {
                                    infixList.Add(ibo);
                                }
                                else
                                {
                                    return(null);
                                }
                                currentTokens.Clear();
                            }
                            infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOn));
                        }
                    }
                    actionDone = true;
                }

                if (tok.GetTokenType().Equals(TokenType.BracketOff))
                {
                    if (readingFunction)
                    {
                        if (Brackets.AllBracketsClosed(currentTokens))
                        {
                            IBoolable ibo = BoolableBuilder.Build(currentTokens);
                            if (!ibo.IsNull())
                            {
                                infixList.Add(ibo);
                            }
                            else
                            {
                                return(null);
                            }
                            currentTokens.Clear();

                            readingFunction = false;
                            infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOff));
                        }
                        else
                        {
                            currentTokens.Add(tok);
                        }
                    }
                    else
                    {
                        if (currentTokens.Count > 0)
                        {
                            IBoolable ibo = BoolableBuilder.Build(currentTokens);
                            if (!ibo.IsNull())
                            {
                                infixList.Add(ibo);
                            }
                            else
                            {
                                return(null);
                            }
                            currentTokens.Clear();
                        }
                        infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOff));
                    }
                    actionDone = true;
                }

                if (!actionDone)
                {
                    currentTokens.Add(tok);
                }

                previousToken = tok;
            }

            if (currentTokens.Count > 0)
            {
                IBoolable ibo = BoolableBuilder.Build(currentTokens);
                if (!ibo.IsNull())
                {
                    infixList.Add(ibo);
                }
                else
                {
                    return(null);
                }
            }

            // try to build negation of one boolable
            if (infixList.Count == 2 && (infixList[0] is BoolExpressionOperator) && (infixList[1] is IBoolable) &&
                (infixList[0] as BoolExpressionOperator).GetOperatorType().Equals(BoolExpressionOperatorType.Not))
            {
                return(new NegatedBoolable(infixList[1] as IBoolable));
            }

            // check if value of infixlist can be computed (check order of elements)
            if (!CheckExpressionComputability(infixList))
            {
                throw new SyntaxErrorException("ERROR! Wrong syntax of logic expression.");
            }

            // if everything is right, finally build BoolExpression in RPN
            return(new BoolExpression(ReversePolishNotation(infixList)));
        }
Ejemplo n.º 4
0
        public static INumerable Build(List <Token> tokens)
        {
            // try to build Boolable
            IBoolable ibo = BoolableBuilder.Build(tokens);

            if (!ibo.IsNull())
            {
                return(ibo as INumerable);
            }

            // remove first and last bracket if it is there
            while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) &&
                   !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal))
            {
                List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList();
                tokensCopy.RemoveAt(tokens.Count - 1);
                tokensCopy.RemoveAt(0);
                tokens = tokensCopy;
            }

            // try to build simple one-token Numerable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.Number))
                    {
                        return(new NumericVariableRefer(str));
                    }
                    else
                    {
                        // try to build reference to element of time variable
                        INumerable inum = BuildTimeVariableRefer(tokens[0]);
                        if (!inum.IsNull())
                        {
                            return(inum);
                        }
                    }
                }
                if (tokens[0].GetTokenType().Equals(TokenType.NumericConstant))
                {
                    return(new NumericConstant(tokens[0].GetNumericContent()));
                }
            }

            // try to build numeric ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                INumerable inum = TernaryBuilder.BuildNumericTernary(tokens);
                if (!inum.IsNull())
                {
                    return(inum);
                }
            }

            // try to build "count" and "count inside"
            if (tokens[0].GetTokenType().Equals(TokenType.Count))
            {
                INumerable inum = BuildCount(tokens);
                if (!inum.IsNull())
                {
                    return(inum);
                }
            }

            // try to build numeric function
            if (Functions.IsPossibleFunction(tokens))
            {
                INumerable inu = NumericFunction.Build(tokens);
                if (!inu.IsNull())
                {
                    return(inu);
                }
            }

            // try to build expression: many elements with operators +, -, *, /, %
            if (TokenGroups.ContainsArithmeticTokensOutsideBrackets(tokens))
            {
                return(BuildExpression(tokens));
            }
            else
            {
                return(null);
            }
        }