예제 #1
0
        private static IBlock GetWhileBlock(Queue <string> commands, State state, IIntreperter intreperter)
        {
            Queue <string> instructions  = new Queue <string>();
            bool           blockFinished = false;
            int            bracketsCount = 0;

            while (commands.Count > 0 && blockFinished == false)
            {
                string line = commands.Peek();

                if (line.Contains('{'))
                {
                    bracketsCount++;
                }
                else if (line.Contains('}'))
                {
                    bracketsCount--;
                }

                instructions.Enqueue(commands.Dequeue());

                if (bracketsCount <= 0)
                {
                    blockFinished = true;
                }
            }

            IBlock whileBlock = new WhileBlock(instructions, state, intreperter);

            return(whileBlock);
        }
예제 #2
0
        private static IBlock GetGeneralBlock(Queue <string> commands, IIntreperter intreperter)
        {
            Queue <string> instructions  = new Queue <string>();
            bool           blockFinished = false;

            while (commands.Count > 0 && blockFinished == false)
            {
                string line = commands.Peek();

                if (line.StartsWith("IF") || line.StartsWith("WHILE"))
                {
                    blockFinished = true;
                }
                else if (line.Contains('}') || line == string.Empty)
                {
                    commands.Dequeue();
                }
                else
                {
                    instructions.Enqueue(commands.Dequeue());
                }
            }

            IBlock genBlock = null;

            if (instructions.Count > 0)
            {
                genBlock = new GeneralBlock(instructions, intreperter);
            }

            return(genBlock);
        }
예제 #3
0
        public WhileBlock(Queue <string> commands, State state, IIntreperter intreperter)
        {
            _intreperter = intreperter;
            _predicate   = commands.Dequeue();
            _commands    = new Queue <string>(commands);

            _blocks       = BlockGenerator.Generate(commands, state, intreperter).ToArray();
            _state        = state;
            _blockIndex   = 0;
            _currentBlock = null;
            _isFinished   = false;
            _predicateMet = null;
        }
예제 #4
0
        public IfBlock(Queue <string> commandsIf, Queue <string> commandsElse, State state, IIntreperter intreperter)
        {
            _intreperter = intreperter;
            _predicate   = commandsIf.Dequeue();
            // dequeue first line
            if (commandsElse.Count > 0)
            {
                commandsElse.Dequeue();
            }

            _blocksIf   = BlockGenerator.Generate(commandsIf, state, intreperter);
            _blocksElse = BlockGenerator.Generate(commandsElse, state, intreperter);
        }
예제 #5
0
        private static IBlock GetIfBlock(Queue <string> commands, State state, IIntreperter intreperter)
        {
            Queue <string> instructionsIf   = new Queue <string>();
            Queue <string> instructionsElse = new Queue <string>();
            bool           blockFinished    = false;
            bool           inElse           = false;
            int            bracketsCount    = 0;

            while (commands.Count > 0 && blockFinished == false)
            {
                string line = commands.Peek();

                if (line.Contains('{'))
                {
                    bracketsCount++;
                }
                else if (line.Contains('}'))
                {
                    bracketsCount--;
                }

                if (inElse)
                {
                    instructionsElse.Enqueue(commands.Dequeue());
                }
                else
                {
                    instructionsIf.Enqueue(commands.Dequeue());
                }

                if (bracketsCount <= 0)
                {
                    if (inElse || !commands.Peek().Contains("ELSE"))
                    {
                        blockFinished = true;
                    }
                    else
                    {
                        inElse = true;
                    }
                }
            }

            IBlock ifBlock = new IfBlock(instructionsIf, instructionsElse, state, intreperter);

            return(ifBlock);
        }
예제 #6
0
 public GeneralBlock(Queue <string> commands, IIntreperter intreperter)
 {
     _commands    = commands;
     _intreperter = intreperter;
 }
예제 #7
0
        internal static Queue <IBlock> Generate(Queue <string> commands, State state, IIntreperter intreperter)
        {
            Queue <IBlock> blocks = new Queue <IBlock>();

            while (commands.Count > 0)
            {
                string line = commands.Peek();

                if (line.StartsWith("IF"))
                {
                    blocks.Enqueue(GetIfBlock(commands, state, intreperter));
                }
                else if (line.StartsWith("WHILE"))
                {
                    blocks.Enqueue(GetWhileBlock(commands, state, intreperter));
                }
                else
                {
                    blocks.Enqueue(GetGeneralBlock(commands, intreperter));
                }
            }

            return(blocks);
        }