Пример #1
0
        public NonTerninalExp CreateCommand(List <Token> tokens)
        {
            var closeParenthesisIndex = TokenHelper.GetIndexPairParanthesis(tokens, 1);
            var comma = tokens.IndexOf(tokens.First(token => token.TokenType == TokenType.Comma));

            var firstParam  = TerminalExpressionAnalyser.Analyse(tokens.GetRange(2, comma - 2));
            var secondParam = TerminalExpressionAnalyser.Analyse(tokens.GetRange(comma + 1,
                                                                                 closeParenthesisIndex - comma - 1));
            NonTerninalExp command;

            switch (tokens[0].TokenType)
            {
            case TokenType.CreateDir: command = new CreateDirCommand(firstParam, secondParam); break;

            case TokenType.CreateTxtFile: command = new CreateTxtFileCommand(firstParam, secondParam); break;

            case TokenType.Move: command = new MoveCommand(firstParam, secondParam); break;

            case TokenType.Copy: command = new CopyCommand(firstParam, secondParam); break;

            case TokenType.Find: command = new FindCommand(firstParam, secondParam); break;

            case TokenType.Rename: command = new RenameCommand(firstParam, secondParam); break;

            case TokenType.WriteToFile: command = new WriteToFileCommand(firstParam, secondParam); break;

            case TokenType.AppendToFile: command = new AppendToFileCommand(firstParam, secondParam); break;

            default: command = new CreateDirCommand(firstParam, secondParam); break;
            }
            command.SetNext(SyntaxAnalyser.Analyse(tokens.GetRange(closeParenthesisIndex + 1,
                                                                   tokens.Count - closeParenthesisIndex - 1)));
            return(command);
        }
Пример #2
0
        public NonTerninalExp CreateCommand(List <Token> tokens)
        {
            var closeParenthesisIndex = TokenHelper.GetIndexPairParanthesis(tokens, 1);
            var exp = TerminalExpressionAnalyser.Analyse(tokens.GetRange(2, closeParenthesisIndex - 2));

            NonTerninalExp command;

            switch (tokens[0].TokenType)
            {
            case TokenType.ConsoleLog: command = new ConsoleLogCommand(exp); break;

            case TokenType.Delete: command = new DeleteCommand(exp); break;

            case TokenType.LogAllItems: command = new LogAllItemsCommand(exp); break;

            case TokenType.ReadAndLog: command = new ReadAndLogCommand(exp); break;

            case TokenType.ClearFile: command = new ClearFileCommand(exp); break;

            default: command = new ConsoleLogCommand(exp); break;
            }

            command.SetNext(SyntaxAnalyser.Analyse(tokens.GetRange(closeParenthesisIndex + 1,
                                                                   tokens.Count - closeParenthesisIndex - 1)));
            return(command);
        }
Пример #3
0
        public NonTerninalExp CreateCommand(List <Token> tokens)
        {
            var closeParenthesisIndex = TokenHelper.GetIndexPairParanthesis(tokens, 1);

            var exp     = TerminalExpressionAnalyser.Analyse(tokens.GetRange(2, closeParenthesisIndex - 2));
            var command = new ConditionCommand(exp);

            var closeBracketIndex = TokenHelper.GetIndexPairBracket(tokens, closeParenthesisIndex + 1);

            command.SetPositiveNext(SyntaxAnalyser.Analyse(tokens.GetRange(closeParenthesisIndex + 1,
                                                                           tokens.Count - closeParenthesisIndex - 1)));
            if (tokens[closeBracketIndex + 1].TokenType == TokenType.Else)
            {
                if (tokens[closeBracketIndex + 2].TokenType != TokenType.If)
                {
                    ConditionCommand com = new ConditionCommand(new StringConstExp("true"));
                    com.SetPositiveNext(SyntaxAnalyser.Analyse(tokens.GetRange(closeBracketIndex + 2,
                                                                               tokens.Count - closeBracketIndex - 2)));
                    com.SetNegativeNext(com.GetPositiveNext().GetNext());
                    command.SetNegativeNext(com);
                }
                else
                {
                    command.SetNegativeNext(SyntaxAnalyser.Analyse(tokens.GetRange(closeBracketIndex + 2,
                                                                                   tokens.Count - closeBracketIndex - 2)));
                }
            }
            else
            {
                command.SetNegativeNext(SyntaxAnalyser.Analyse(tokens.GetRange(closeBracketIndex + 1,
                                                                               tokens.Count - closeBracketIndex - 1)));
            }
            return(command);
        }
Пример #4
0
        public NonTerninalExp CreateCommand(List <Token> tokens)
        {
            var semicolonIndex = tokens.FindIndex(a => a.TokenType == TokenType.Semicolon);
            var exp            = TerminalExpressionAnalyser.Analyse(tokens.GetRange(2, semicolonIndex - 1));
            var command        = new AssignmentCommand(tokens[0].Value, exp);

            command.SetNext(SyntaxAnalyser.Analyse(tokens.GetRange(semicolonIndex,
                                                                   tokens.Count - semicolonIndex)));
            return(command);
        }
Пример #5
0
        public NonTerninalExp CreateCommand(List <Token> tokens)
        {
            List <String> variableNamesList = new List <String>();
            String        type = tokens[0].TokenType == TokenType.Int ? "int" : "string";

            variableNamesList.Add(tokens[1].Value);
            int indexEndVariables = 1;

            for (var i = 1; tokens[i + 1].TokenType == TokenType.Comma; i += 2)
            {
                variableNamesList.Add(tokens[i + 2].Value);
                indexEndVariables = i + 2;
            }

            TerminalExp exp;

            if (tokens[indexEndVariables + 1].TokenType == TokenType.Assignment)
            {
                exp = TerminalExpressionAnalyser.Analyse(tokens.GetRange(indexEndVariables + 2, 1));
            }
            else if (type == "int")
            {
                exp = new NumericConstExp(0);
            }
            else
            {
                exp = new StringConstExp("");
            }

            List <CreateVariableCommand> listCreatesCommand = new List <CreateVariableCommand>();

            foreach (var name in variableNamesList)
            {
                listCreatesCommand.Add(new CreateVariableCommand(type, name, exp));
            }

            for (int i = 0; i < listCreatesCommand.Count - 1; i++)
            {
                listCreatesCommand[i].SetNext(listCreatesCommand[i + 1]);
            }

            var semicolonIndex = tokens.FindIndex(a => a.TokenType == TokenType.Semicolon);

            listCreatesCommand[listCreatesCommand.Count - 1].SetNext(
                SyntaxAnalyser.Analyse(tokens.GetRange
                                           (semicolonIndex, tokens.Count - semicolonIndex)));

            return(listCreatesCommand[0]);
        }
Пример #6
0
        public NonTerninalExp CreateCommand(List <Token> tokens)
        {
            var closeParenthesisIndex = TokenHelper.GetIndexPairParanthesis(tokens, 1);

            var exp     = TerminalExpressionAnalyser.Analyse(tokens.GetRange(2, closeParenthesisIndex - 2));
            var command = new ConditionCommand(exp);

            var             closeBracketsIndex = TokenHelper.GetIndexPairBracket(tokens, closeParenthesisIndex + 1);
            BracketsCommand insideCom          = (BracketsCommand)SyntaxAnalyser.Analyse(tokens.GetRange(
                                                                                             closeParenthesisIndex + 1, tokens.Count - closeParenthesisIndex - 1));

            insideCom.SetNext(command);

            command.SetPositiveNext(insideCom);
            command.SetNegativeNext(SyntaxAnalyser.Analyse(tokens.GetRange(
                                                               closeBracketsIndex + 1, tokens.Count - closeBracketsIndex - 1)));

            return(command);
        }