Exemplo n.º 1
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));
                }
            }
        }
Exemplo n.º 2
0
        public static ICommand Build(List <Token> tokens, bool forced)
        {
            switch (tokens.First().GetTokenType())
            {
            case TokenType.Copy:
            {
                if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.To))
                {
                    if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.As))
                    {
                        return(InterpreterCoreToAs.Build(tokens, forced));
                    }
                    else
                    {
                        return(InterpreterCoreTo.Build(tokens, forced));
                    }
                }
                else
                {
                    return(InterpreterCore.Build(tokens));
                }
            }

            case TokenType.Cut:
            {
                if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.To))
                {
                    if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.As))
                    {
                        return(InterpreterCoreToAs.Build(tokens, forced));
                    }
                    else
                    {
                        return(InterpreterCoreTo.Build(tokens, forced));
                    }
                }
                else
                {
                    return(InterpreterCore.Build(tokens));
                }
            }

            case TokenType.CreateDirectory:
            {
                return(InterpreterCore.BuildCreate(tokens, forced, true));
            }

            case TokenType.CreateFile:
            {
                return(InterpreterCore.BuildCreate(tokens, forced, false));
            }

            case TokenType.Delete:
            {
                return(InterpreterCore.Build(tokens));
            }

            case TokenType.Drop:
            {
                return(InterpreterCore.Build(tokens));
            }

            case TokenType.Hide:
            {
                return(InterpreterCore.Build(tokens));
            }

            case TokenType.Lock:
            {
                return(InterpreterCore.Build(tokens));
            }

            case TokenType.Open:
            {
                return(InterpreterCore.Build(tokens));
            }

            case TokenType.Move:
            {
                if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.To))
                {
                    if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.As))
                    {
                        return(InterpreterCoreToAs.Build(tokens, forced));
                    }
                    else
                    {
                        return(InterpreterCoreTo.Build(tokens, forced));
                    }
                }
                else
                {
                    throw new SyntaxErrorException("ERROR! Move command do not contain destination directory.");
                }
            }

            case TokenType.Reaccess:
            {
                if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To)))
                {
                    return(InterpreterCoreToTime.Build(tokens));
                }
                else
                {
                    throw new SyntaxErrorException("ERROR! Reaccess command do not contain definition of new time.");
                }
            }

            case TokenType.Recreate:
            {
                if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To)))
                {
                    return(InterpreterCoreToTime.Build(tokens));
                }
                else
                {
                    throw new SyntaxErrorException("ERROR! Recreate command do not contain definition of new time.");
                }
            }

            case TokenType.Remodify:
            {
                if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To)))
                {
                    return(InterpreterCoreToTime.Build(tokens));
                }
                else
                {
                    throw new SyntaxErrorException("ERROR! Remodify command do not contain definition of new time.");
                }
            }

            case TokenType.Rename:
            {
                if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To)))
                {
                    return(InterpreterCoreTo.Build(tokens, forced));
                }
                else
                {
                    throw new SyntaxErrorException("ERROR! Rename command do not contain definition of new name.");
                }
            }

            case TokenType.Unhide:
            {
                return(InterpreterCore.Build(tokens));
            }

            case TokenType.Unlock:
            {
                return(InterpreterCore.Build(tokens));
            }
            }
            return(null);
        }
Exemplo n.º 3
0
        public static IBoolable Build(List <Token> tokens)
        {
            /// INITIAL CHECKING

            // check is is empty
            if (tokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Variable declaration is empty.");
            }

            // check if contains not allowed tokens
            Token wwtok = TokenGroups.WrongTokenInExpression(tokens);

            if (!wwtok.GetTokenType().Equals(TokenType.Null))
            {
                return(null);
            }

            // check brackets
            if (!Brackets.CheckCorrectness(tokens))
            {
                return(null);
            }

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

            // check is is empty again after removing brackets
            if (tokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Variable declaration is empty.");
            }

            /// BOOL BUILDING

            // try to build simple one-element Boolable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.Bool))
                    {
                        return(new BoolVariableRefer(str));
                    }
                    else
                    {
                        return(null);
                    }
                }
                if (tokens[0].GetTokenType().Equals(TokenType.BoolConstant))
                {
                    if (tokens[0].GetContent().Equals("true"))
                    {
                        return(new BoolConstant(true));
                    }
                    else
                    {
                        return(new BoolConstant(false));
                    }
                }
            }

            // try to build IN function
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.In))
            {
                IBoolable iboo = BuildIn(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build LIKE function
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Like))
            {
                IBoolable iboo = BuildLike(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build BETWEEN function
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.And) &&
                TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Between))
            {
                IBoolable iboo = BuildBetween(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build time comparison IS AFTER/IS BEFORE
            if (ContainsOneTimeComparingToken(tokens))
            {
                IBoolable iboo = BuildTimeComparison(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build comparison = != > < >= <=
            if (ContainsOneComparingToken(tokens))
            {
                IBoolable iboo = BuildComparison(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build bool ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                IBoolable iboo = TernaryBuilder.BuildBoolTernary(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build bool function
            if (Functions.IsPossibleFunction(tokens))
            {
                IBoolable iboo = BoolFunction.Build(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build expression: many elements with operators or, and, xor, not
            if (ContainsLogicTokens(tokens))
            {
                return(BuildExpression(tokens));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        public static IListable Build(List <Token> tokens)
        {
            // try to build Stringable
            IStringable ist = StringableBuilder.Build(tokens);

            if (!ist.IsNull())
            {
                return(ist as IListable);
            }

            // 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 'empty list'
            if (tokens.Count == 2 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.Variable) &&
                tokens[0].GetContent().ToLower().Equals("empty") && tokens[1].GetContent().ToLower().Equals("list"))
            {
                return(new EmptyList());
            }

            // try to build small arrow function
            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.SmallArrow)).Count() == 1)
            {
                IListable smallArrow = BuildSmallArrowFunction(tokens);
                if (!smallArrow.IsNull())
                {
                    return(smallArrow);
                }
            }

            string str = tokens[0].GetContent();

            // try to build list variable reference - just one word and it is a name
            if (tokens.Count == 1 && tokens[0].GetTokenType().Equals(TokenType.Variable) && InterVariables.GetInstance().Contains(str, InterVarType.List))
            {
                return(new ListVariableRefer(str));
            }

            // try to build list expression
            if (InterVariables.GetInstance().Contains(str, InterVarType.List))
            {
                IListable listEx = BuildListExpression(tokens, str);
                if (!listEx.IsNull())
                {
                    return(listEx);
                }
            }

            // try to build list ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                IListable ilist = TernaryBuilder.BuildListTernary(tokens);
                if (!ilist.IsNull())
                {
                    return(ilist);
                }
            }

            // try to build listed lists/strings: many Listables/Stringables divided by commas
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma))
            {
                IListable listed = BuildListed(tokens);
                if (!listed.IsNull())
                {
                    return(listed);
                }
            }

            throw new SyntaxErrorException("ERROR! Unknown error in code syntax.");
        }
Exemplo n.º 5
0
        public static IStringable Build(List <Token> tokens)
        {
            // try to build Numerable
            INumerable inu = NumerableBuilder.Build(tokens);

            if (!inu.IsNull())
            {
                return(inu as IStringable);
            }

            // try to build Timeable
            ITimeable itim = TimeableBuilder.Build(tokens);

            if (!itim.IsNull())
            {
                return(itim as IStringable);
            }

            // 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 Stringable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.String))
                    {
                        return(new StringVariableRefer(str));
                    }
                    else
                    {
                        // try to build reference to date or clock time
                        IStringable istr = BuildTimeVariableRefer(tokens[0]);
                        if (!istr.IsNull())
                        {
                            return(istr);
                        }
                    }
                }
                if (tokens[0].GetTokenType().Equals(TokenType.StringConstant))
                {
                    return(new StringConstant(tokens[0].GetContent()));
                }
            }

            //try to build string function
            if (Functions.IsPossibleFunction(tokens))
            {
                IStringable istr = StringFunction.Build(tokens);
                if (!istr.IsNull())
                {
                    return(istr);
                }
            }

            // try to build string ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                IStringable istr = TernaryBuilder.BuildStringTernary(tokens);
                if (!istr.IsNull())
                {
                    return(istr);
                }
            }

            // try to build reference to n-th element of list of strings
            if (tokens.Count > 3 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.SquareBracketOn) &&
                tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.SquareBracketOff))
            {
                IStringable istr = BuildListElement(tokens);
                if (!istr.IsNull())
                {
                    return(istr);
                }
            }

            // try to build concatenated string -> text merged by +
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Plus) && !TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma))
            {
                return(BuildConcatenated(tokens));
            }
            else
            {
                return(null);
            }
        }