Exemple #1
0
        public static IBoolable BuildLisStr(string name, List <Argument> args)
        {
            if (args.Count != 2)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 arguments: one list and one text.");
            }

            IListable   ilis = ListableBuilder.Build(args[0].tokens);
            IStringable istr = StringableBuilder.Build(args[1].tokens);

            if (ilis.IsNull())
            {
                throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as list.");
            }
            if (istr.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as text.");
            }

            if (name.Equals("contain") || name.Equals("contains"))
            {
                return(new FuncContain(ilis, istr));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
        public static ICommand BuildCreateUsual(List <Token> tokens, bool forced, bool directory)
        {
            if (tokens.Count == 0)
            {
                if (directory)
                {
                    return(new CreateDirectory(new StringVariableRefer("this"), forced));
                }
                else
                {
                    return(new CreateFile(new StringVariableRefer("this"), forced));
                }
            }
            IListable ilist = ListableBuilder.Build(tokens);


            if (ilist.IsNull())
            {
                throw new SyntaxErrorException("ERROR! There are is something wrong with " + (directory ? "directory" : "file")
                                               + " creation command syntax.");
            }
            if (directory)
            {
                return(new CreateDirectory(ilist, forced));
            }
            else
            {
                return(new CreateFile(ilist, forced));
            }
        }
        public static ICommand Build(List <Token> tokens, bool forced)
        {
            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.To)).Count() > 1)
            {
                throw new SyntaxErrorException("ERROR! In " + GetName(type) + " command keyword 'to' occurs too many times.");
            }

            List <Token> part1  = new List <Token>();
            List <Token> part2  = new List <Token>();
            bool         pastTo = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.To))
                {
                    pastTo = true;
                }
                else
                {
                    if (pastTo)
                    {
                        part2.Add(tok);
                    }
                    else
                    {
                        part1.Add(tok);
                    }
                }
            }

            if (part2.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " is too short and do not contain all necessary information.");
            }

            IStringable expression2 = StringableBuilder.Build(part2);

            if (expression2.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second part of command " + GetName(type) + " cannot be read as text.");
            }

            if (part1.Count == 0)
            {
                return(BuildSimple(type, expression2, forced));
            }
            else
            {
                IListable expression1 = ListableBuilder.Build(part1);
                if (expression1.IsNull())
                {
                    throw new SyntaxErrorException("ERROR! First part of command " + GetName(type) + " cannot be read as list.");
                }
                return(BuildComplex(type, expression1, expression2, forced));
            }
        }
Exemple #4
0
        public static IStringable BuildLis(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 list argument.");
            }

            IListable ilis = ListableBuilder.Build(args[0].tokens);

            if (ilis.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as list.");
            }

            if (name.Equals("commonbeginning"))
            {
                return(new FuncCommonbeginning(ilis));
            }
            else if (name.Equals("commonending"))
            {
                return(new FuncCommonending(ilis));
            }
            else if (name.Equals("concatenate") || name.Equals("concatenated"))
            {
                return(new FuncConcatenate(ilis));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
Exemple #5
0
        public static ICommand Build(List <Token> tokens, bool forced)
        {
            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            int toIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.To);
            int asIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.As);

            if (asIndex < toIndex)
            {
                return(null);
            }
            if (toIndex == asIndex - 1)
            {
                throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of destination directory.");
            }
            if (asIndex == tokens.Count - 1)
            {
                throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of new name for file/directory.");
            }

            List <Token> listTokens        = tokens.Take(toIndex).ToList();
            List <Token> destinationTokens = tokens.GetRange(toIndex + 1, asIndex - toIndex - 1);
            List <Token> nameTokens        = tokens.Skip(asIndex + 1).ToList();

            IStringable destination = StringableBuilder.Build(destinationTokens);

            if (destination.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of destination directory cannot be read as text.");
            }
            IStringable name = StringableBuilder.Build(nameTokens);

            if (name.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of new name for file/directory cannot be read as text.");
            }

            if (listTokens.Count == 0)
            {
                return(BuildSimple(type, destination, name, forced));
            }
            else
            {
                IListable list = ListableBuilder.Build(listTokens);
                if (list.IsNull())
                {
                    throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of list of files and directories is not correct.");
                }
                return(BuildComplex(type, list, destination, name, forced));
            }
        }
Exemple #6
0
        public static ICommand Build(List <Token> tokens)
        {
            if (tokens.Count == 0)
            {
                return(new Print(new StringVariableRefer("this")));
            }
            IListable ilist = ListableBuilder.Build(tokens);

            if (ilist.IsNull())
            {
                throw new SyntaxErrorException("ERROR! There are is something wrong with print command.");
            }
            else
            {
                return(new Print(ilist));
            }
        }
Exemple #7
0
        public static IBoolable BuildLis(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 list argument.");
            }

            IListable ilis = ListableBuilder.Build(args[0].tokens);

            if (ilis.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as list.");
            }

            if (name.Equals("emptylist") || name.Equals("listisempty"))
            {
                return(new FuncEmptylist(ilis));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
        public static ICommand Build(List <Token> tokens)
        {
            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            if (tokens.Count == 0)
            {
                return(BuildSimple(type));
            }

            IListable ilist = ListableBuilder.Build(tokens);

            if (ilist.IsNull())
            {
                throw new SyntaxErrorException("ERROR! There are is something wrong with elements declaration in " + GetName(type) + " command.");
            }
            else
            {
                return(BuildComplex(type, ilist));
            }
        }
Exemple #9
0
        public static INumerable BuildLis(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 list argument.");
            }

            IListable ilis = ListableBuilder.Build(args[0].tokens);

            if (ilis.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as list.");
            }

            else if (name.Equals("lengthofshortest"))
            {
                return(new FuncLengthofshortest(ilis));
            }
            else if (name.Equals("lengthoflongest"))
            {
                return(new FuncLengthoflongest(ilis));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
        public static ICommand BuildCreateFrom(List <Token> tokens, bool forced, bool directory)
        {
            List <Token> part1    = new List <Token>();
            List <Token> part2    = new List <Token>();
            bool         pastFrom = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.From))
                {
                    pastFrom = true;
                }
                else
                {
                    if (pastFrom)
                    {
                        part2.Add(tok);
                    }
                    else
                    {
                        part1.Add(tok);
                    }
                }
            }
            if (part2.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Source in " + (directory ? "directory" : "file")
                                               + " creation command is empty.");
            }

            IListable   ilist;
            IStringable istring = StringableBuilder.Build(part2);

            if (part1.Count == 0)
            {
                ilist = new StringVariableRefer("this");
            }
            else
            {
                ilist = ListableBuilder.Build(part1);
            }

            if (ilist.IsNull())
            {
                throw new SyntaxErrorException("ERROR! There are is something wrong with new " + (directory ? "directory" : "file") +
                                               " name in " + (directory ? "directory" : "file") + " creation command syntax.");
            }
            if (istring.IsNull())
            {
                throw new SyntaxErrorException("ERROR! There are is something wrong with source " + (directory ? "directory" : "file") +
                                               " name in " + (directory ? "directory" : "file") + " creation command syntax.");
            }

            if (directory)
            {
                return(new CreateDirectoryFrom(istring, ilist, forced));
            }
            else
            {
                return(new CreateFileFrom(istring, ilist, forced));
            }
        }
        public static ICommand Build(List <Token> tokens)
        {
            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            if (!tokens.Any(t => t.GetTokenType().Equals(TokenType.To)))
            {
                throw new SyntaxErrorException("ERROR! Command 'add' do not contain keyword 'to'.");
            }
            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.To)).Count() > 1)
            {
                throw new SyntaxErrorException("ERROR! In command 'add' keyword 'to' occurs too many times.");
            }

            List <Token> part1  = new List <Token>();
            List <Token> part2  = new List <Token>();
            bool         pastTo = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.To))
                {
                    pastTo = true;
                }
                else
                {
                    if (pastTo)
                    {
                        part2.Add(tok);
                    }
                    else
                    {
                        part1.Add(tok);
                    }
                }
            }

            if (part2.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Command 'add' do not contain definition for target variable.");
            }
            if (part2.Count > 2 || !part2[0].GetTokenType().Equals(TokenType.Variable))
            {
                throw new SyntaxErrorException("ERROR! Target variable in command 'add' cannot be read.");
            }

            string name = part2[0].GetContent();

            if (!InterVariables.GetInstance().ContainsChangable(name, InterVarType.List))
            {
                throw new SyntaxErrorException("ERROR! In command 'add' variable " + name + " do not exist or cannot be read as list.");
            }

            // add this
            if (part1.Count == 0)
            {
                return(new AddString(name, new StringVariableRefer("this") as IStringable));
            }

            IListable ilist = ListableBuilder.Build(part1);

            if (ilist.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In command 'add' definition for elements to add cannot be read as list.");
            }

            // turn variable to list if it was string
            if (InterVariables.GetInstance().ContainsChangable(name, InterVarType.String))
            {
                InterVariables.GetInstance().TurnToList(name);
            }

            if (ilist is IStringable)
            {
                return(new AddString(name, ilist as IStringable));
            }
            else
            {
                return(new AddList(name, ilist));
            }
        }
Exemple #12
0
        public static List <ICommand> Build(List <Token> tokens)
        {
            List <Token>    currentTokens        = new List <Token>();
            List <ICommand> commands             = new List <ICommand>();
            int             waitingArrowsEndings = 0;
            int             arrowDepth           = 0;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.Semicolon))
                {
                    if (currentTokens.Count > 0)
                    {
                        commands.Add(SingleCommandFactory.Build(currentTokens));
                        currentTokens.Clear();
                    }
                    if (arrowDepth == 0 && waitingArrowsEndings > 0)
                    {
                        waitingArrowsEndings.Times(() => commands.Add(new BracketOff()));
                        waitingArrowsEndings = 0;
                    }
                }
                else if (tok.GetTokenType().Equals(TokenType.CurlyBracketOff))
                {
                    if (currentTokens.Count > 0)
                    {
                        commands.Add(SingleCommandFactory.Build(currentTokens));
                        currentTokens.Clear();
                    }
                    commands.Add(new BracketOff());
                    InterVariables.GetInstance().BracketsDown();

                    if (waitingArrowsEndings > 0)
                    {
                        arrowDepth--;
                    }

                    if (arrowDepth == 0 && waitingArrowsEndings > 0)
                    {
                        waitingArrowsEndings.Times(() => commands.Add(new BracketOff()));
                        waitingArrowsEndings = 0;
                    }
                }
                else if (tok.GetTokenType().Equals(TokenType.CurlyBracketOn))
                {
                    if (currentTokens.Count == 0)
                    {
                        commands.Add(new EmptyOpenning());
                    }
                    else
                    {
                        TokenType first = currentTokens.First().GetTokenType();
                        if (first.Equals(TokenType.If))
                        {
                            currentTokens.RemoveAt(0);
                            if (currentTokens.Count == 0)
                            {
                                throw new SyntaxErrorException("ERROR! Expression 'if' is empty.");
                            }

                            IBoolable iboo = BoolableBuilder.Build(currentTokens);
                            if (iboo.IsNull())
                            {
                                throw new SyntaxErrorException("ERROR! There is something wrong with expression 'if'.");
                            }

                            commands.Add(new IfOpenning(iboo, commands.Count));
                            currentTokens.Clear();
                        }
                        else if (first.Equals(TokenType.Else))
                        {
                            currentTokens.RemoveAt(0);
                            if (currentTokens.Count == 0)
                            {
                                commands.Add(new ElseOpenning());
                            }
                            else
                            {
                                throw new SyntaxErrorException("ERROR! Expression 'else' contains not necessary code.");
                            }
                        }
                        else if (first.Equals(TokenType.While))
                        {
                            currentTokens.RemoveAt(0);
                            if (currentTokens.Count == 0)
                            {
                                throw new SyntaxErrorException("ERROR! Expression 'while' is empty.");
                            }

                            IBoolable iboo = BoolableBuilder.Build(currentTokens);
                            if (iboo.IsNull())
                            {
                                throw new SyntaxErrorException("ERROR! There is something wrong with expression 'while'.");
                            }

                            commands.Add(new WhileOpenning(iboo, commands.Count));
                            currentTokens.Clear();
                        }
                        else if (first.Equals(TokenType.Inside))
                        {
                            currentTokens.RemoveAt(0);
                            if (currentTokens.Count == 0)
                            {
                                throw new SyntaxErrorException("ERROR! Expression 'inside' is empty.");
                            }

                            IListable ilist = ListableBuilder.Build(currentTokens);
                            if (ilist.IsNull())
                            {
                                throw new SyntaxErrorException("ERROR! There is something wrong with expression 'inside'.");
                            }

                            commands.Add(new InsideOpenning(ilist, commands.Count));
                            currentTokens.Clear();
                        }
                        else
                        {
                            IListable ilist = ListableBuilder.Build(currentTokens);
                            if (ilist.IsNull())
                            {
                                throw new SyntaxErrorException("ERROR! There is something wrong with List Loop / Numeric Loop.");
                            }

                            if (ilist is INumerable)
                            {
                                commands.Add(new NumericLoopOpenning(ilist as INumerable, commands.Count));
                            }
                            else
                            {
                                commands.Add(new ListLoopOpenning(ilist, commands.Count));
                            }

                            currentTokens.Clear();
                        }
                    }
                    InterVariables.GetInstance().BracketsUp();

                    if (waitingArrowsEndings > 0)
                    {
                        arrowDepth++;
                    }
                }
                else if (tok.GetTokenType().Equals(TokenType.BigArrow))
                {
                    if (currentTokens.Count == 0)
                    {
                        throw new SyntaxErrorException("ERROR! Left side of Big Arrow Function is empty.");
                    }
                    else
                    {
                        IListable ilist = ListableBuilder.Build(currentTokens);
                        if (ilist.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! There is something wrong with List Loop / Numeric Loop.");
                        }

                        if (ilist is INumerable)
                        {
                            commands.Add(new NumericLoopOpenning(ilist as INumerable, commands.Count));
                        }
                        else
                        {
                            commands.Add(new ListLoopOpenning(ilist, commands.Count));
                        }

                        currentTokens.Clear();
                        waitingArrowsEndings++;
                    }
                }
                else
                {
                    currentTokens.Add(tok);
                }
            }

            if (currentTokens.Count > 0)
            {
                commands.Add(SingleCommandFactory.Build(currentTokens));
            }

            if (waitingArrowsEndings > 0)
            {
                waitingArrowsEndings.Times(() => commands.Add(new BracketOff()));
            }


            return(commands);
        }
        public static ICommand Build(List <Token> tokens)
        {
            string name = tokens[0].GetContent();

            tokens.RemoveAt(0);
            tokens.RemoveAt(0);

            if (tokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Variable " + name + " has no assigned value.");
            }

            if (name.Contains('.'))
            {
                return(BuildWithPoint(tokens, name));
            }

            if (!InterVariables.GetInstance().Contains(name))
            {
                IListable value = ListableBuilder.Build(tokens);
                if (value.IsNull())
                {
                    throw new SyntaxErrorException("ERROR! There are is something wrong with assigning value to variable " + name + ".");
                }

                if (value is IBoolable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.Bool);
                    return(new BoolDeclaration(name, (IBoolable)value));
                }
                else if (value is INumerable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.Number);
                    return(new NumericDeclaration(name, (INumerable)value));
                }
                else if (value is ITimeable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.Time);
                    return(new TimeDeclaration(name, (ITimeable)value));
                }
                else if (value is IStringable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.String);
                    return(new StringDeclaration(name, (IStringable)value));
                }
                else
                {
                    InterVariables.GetInstance().Add(name, InterVarType.List);
                    return(new ListDeclaration(name, value));
                }
            }
            else
            {
                InterVar ivar = InterVariables.GetInstance().GetVar(name);

                if (ivar.IsBool())
                {
                    IBoolable value = BoolableBuilder.Build(tokens);
                    if (value.IsNull())
                    {
                        throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be logical.");
                    }
                    return(new BoolDeclaration(name, value));
                }
                else
                {
                    if (ivar.IsNumber())
                    {
                        INumerable value = NumerableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be numeric.");
                        }
                        return(new NumericDeclaration(name, value));
                    }
                    else if (ivar.IsTime())
                    {
                        ITimeable value = TimeableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be time.");
                        }
                        return(new TimeDeclaration(name, value));
                    }
                    else if (ivar.IsString())
                    {
                        IStringable value = StringableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be text.");
                        }
                        return(new StringDeclaration(name, value));
                    }
                    else
                    {
                        IListable value = ListableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be list.");
                        }
                        return(new ListDeclaration(name, value));
                    }
                }
            }
        }