Esempio n. 1
0
        public static IBoolable BuildIn(List <Token> tokens)
        {
            int index = tokens.TakeWhile(x => !x.GetTokenType().Equals(TokenType.In)).Count();

            if (index == 0 || index == tokens.Count - 1)
            {
                return(null);
            }

            List <Token> leftTokens  = tokens.GetRange(0, index);
            List <Token> rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1);

            IStringable istr = StringableBuilder.Build(leftTokens);

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

            IListable ilis = ListableBuilder.Build(rightTokens);

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

            return(new In(istr, ilis));
        }
Esempio n. 2
0
        public static IListable BuildListTernary(List <Token> tokens)
        {
            IBoolable condition = BoolableBuilder.Build(GetTernaryCondition(tokens));

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

            IListable confirmationCase = ListableBuilder.Build(GetTernaryConfirmation(tokens));

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

            IListable negationCase = ListableBuilder.Build(GetTernaryNegation(tokens));

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

            return(new ListTernary(condition, confirmationCase, negationCase));
        }
Esempio n. 3
0
        private static INumerable BuildCountInside(List <Token> tokens)
        {
            int index = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Inside);

            if (index == tokens.Count - 1)
            {
                throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent location.");
            }

            if (index == 0)
            {
                throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent list of elements.");
            }

            IListable ilist = ListableBuilder.Build(tokens.Take(index).ToList());

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

            IStringable istr = StringableBuilder.Build(tokens.Skip(index + 1).ToList());

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

            return(new CountInside(ilist, istr));
        }
Esempio n. 4
0
        private static INumerable BuildCount(List <Token> tokens)
        {
            List <Token> laterTokens = tokens.Skip(1).ToList();

            if (laterTokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Expression 'count' do not contain all necessary information.");
            }

            if (TokenGroups.ContainsTokenOutsideBrackets(laterTokens, TokenType.Inside))
            {
                return(BuildCountInside(laterTokens));
            }
            else
            {
                IListable ilist = ListableBuilder.Build(laterTokens);
                if (ilist.IsNull())
                {
                    return(null);
                }
                else
                {
                    return(new Count(ilist));
                }
            }
        }
Esempio n. 5
0
        public static ISubcommand BuildWith(List <Token> tokens, bool negated)
        {
            string name = negated ? "without" : "with";

            IListable ilis = ListableBuilder.Build(tokens);

            if (ilis.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In list declaration there is something wrong with expression: " + name + ".");
            }
            else
            {
                return(new With(ilis, negated));
            }
        }
Esempio n. 6
0
        private static IBoolable BuildComparison(List <Token> tokens)
        {
            int index = tokens.TakeWhile(x => !TokenGroups.IsComparingSign(x.GetTokenType())).Count();

            if (index == 0 || index == tokens.Count - 1)
            {
                return(null);
            }

            ComparisonType type        = GetComparingToken(tokens);
            List <Token>   leftTokens  = tokens.GetRange(0, index);
            List <Token>   rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1);
            IListable      leftL       = ListableBuilder.Build(leftTokens);
            IListable      rightL      = ListableBuilder.Build(rightTokens);

            if (leftL.IsNull() || rightL.IsNull())
            {
                return(null);
            }

            if (leftL is INumerable && rightL is INumerable)
            {
                return(new NumericComparison(leftL as INumerable, rightL as INumerable, type));
            }

            if (leftL is ITimeable && rightL is ITimeable)
            {
                return(new TimeComparison(leftL as ITimeable, rightL as ITimeable, type));
            }

            if (leftL is IStringable && rightL is IStringable)
            {
                return(new Uroboros.syntax.expressions.bools.comparisons.StringComparison(leftL as IStringable, rightL as IStringable, type));
            }

            if (leftL is IListable && rightL is IListable)
            {
                return(new ListComparison(leftL as IListable, rightL as IListable, type));
            }

            return(null);
        }
Esempio n. 7
0
        private static IListable BuildListed(List <Token> tokens)
        {
            List <Token>     currentTokens = new List <Token>();
            List <IListable> elements      = new List <IListable>();
            int level = 0;

            for (int i = 0; i < tokens.Count; i++)
            {
                if (tokens[i].GetTokenType().Equals(TokenType.BracketOn))
                {
                    level++;
                }
                if (tokens[i].GetTokenType().Equals(TokenType.BracketOff))
                {
                    level--;
                }

                if (tokens[i].GetTokenType().Equals(TokenType.Comma) && level == 0)
                {
                    if (currentTokens.Count > 0)
                    {
                        IListable ilist = ListableBuilder.Build(currentTokens);
                        currentTokens.Clear();
                        if (ilist.IsNull())
                        {
                            return(null);
                        }
                        else
                        {
                            elements.Add(ilist);
                        }
                    }
                }
                else
                {
                    currentTokens.Add(tokens[i]);
                }
            }

            if (currentTokens.Count > 0)
            {
                IListable ilist = ListableBuilder.Build(currentTokens);
                if (ilist.IsNull())
                {
                    return(null);
                }
                else
                {
                    elements.Add(ilist);
                }
            }

            if (elements.Count == 0)
            {
                return(null);
            }

            if (elements.All(e => e is IStringable))
            {
                if (elements.All(e => e is StringConstant))
                {
                    return(new ListConstant(elements.Select(e => e.ToString()).ToList()));
                }
                else
                {
                    return(new ListedStringables(elements.Select(e => e as IStringable).ToList()));
                }
            }
            else
            {
                if (elements.All(e => e is StringConstant || e is ListConstant))
                {
                    return(new ListConstant(elements.Select(e => e.ToString()).ToList()));
                }
                else
                {
                    return(new ListedListables(elements));
                }
            }
        }
Esempio n. 8
0
        private static IListable BuildSmallArrowFunction(List <Token> tokens)
        {
            bool         unique    = false;
            List <Token> leftSide  = new List <Token>();
            List <Token> rightSide = new List <Token>();
            bool         pastTo    = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.SmallArrow))
                {
                    pastTo = true;
                }
                else
                {
                    if (pastTo)
                    {
                        rightSide.Add(tok);
                    }
                    else
                    {
                        leftSide.Add(tok);
                    }
                }
            }

            if (leftSide.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Left side of Small Arrow Function is empty.");
            }
            if (rightSide.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Right side of Small Arrow Function is empty.");
            }

            if (rightSide.First().GetTokenType().Equals(TokenType.Unique))
            {
                unique = true;
                rightSide.RemoveAt(0);
                if (rightSide.Count == 0)
                {
                    throw new SyntaxErrorException("ERROR! Right side of Small Arrow Function contains only one word: unique.");
                }
            }

            IListable ilist = ListableBuilder.Build(leftSide);

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

            IStringable istr = StringableBuilder.Build(rightSide);

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

            return(new SmallArrow(ilist, istr, unique));
        }