Example #1
0
        /// <summary>
        /// Executes commands.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CommandLineInterface_KeyDown(object sender, KeyEventArgs e)
        {
            string[] commandLines = CommandLineInterface.Lines;         // Get each line from the CommandLineInterface and separate them.
            string[] allLines     = ProgramArea.Lines;                  // Get each line from the ProgramArea too.
            var      parser       = new CommandParser(this.varCommand); // Uses the CommandParser() to parse input.


            if (e.KeyCode == Keys.Enter || commandLines.Contains("run"))
            {
                varCommand.Clear();
                methodCommand.Reset();      // everytime loop refreshes - prevents leftovers

                List <String> allCommands = allLines.ToList <String>();
                allCommands.AddRange(commandLines);

                // Do something for each line.
                for (int i = 0; i < allCommands.Count; i++)
                {
                    try
                    {
                        String line = allCommands[i];

                        // If it's the "draw to" line...
                        if (line.Trim().ToLower().Contains("draw to"))
                        {
                            // Get the parts past the command.
                            var tokens = parser.TokenizeCommand(line, "draw to");
                            // If they are both numbers...
                            if (int.TryParse(tokens[0].Trim(), out int x) &&
                                int.TryParse(tokens[1].Trim(), out int y))
                            {
                                // Draw a line to the indicated position.
                                Canvas.DrawTo(x, y);
                            }
                        }
                        else if (line.Trim().ToLower().Contains("rect"))
                        {
                            // Get the parts past the command.
                            var tokens = parser.TokenizeCommand(line, "rect");
                            // If they are both numbers...
                            if (int.TryParse(tokens[0].Trim(), out int t) &&
                                int.TryParse(tokens[1].Trim(), out int v))
                            {
                                // Draw to the indicated position.
                                Canvas.DrawRectangle(t, v);
                            }
                        }
                        else if (line.Trim().ToLower().Contains("circle"))
                        {
                            // Get the parts past the command.
                            var tokens = parser.TokenizeCommand(line, "circle");
                            // If they are both numbers...
                            if (float.TryParse(tokens[0].Trim(), out float R))
                            {
                                // Draw to the indicated position.
                                Canvas.DrawCircle(R);
                            }
                        }
                        else if (line.Trim().ToLower().Contains("triangle"))
                        {
                            // Get the parts past the command.
                            var tokens = parser.TokenizeCommand(line, "triangle");

                            // If they are both numbers...
                            if (int.TryParse(tokens[0].Trim(), out int T) &&
                                int.TryParse(tokens[1].Trim(), out int T2))
                            {
                                // Draw to the indicated position.
                                Canvas.DrawTriangle(T, T2);
                            }
                        }
                        else if (line.Trim().ToLower().Contains("move to"))
                        {
                            // Get the parts past the command.
                            var tokens = parser.TokenizeCommand(line, "move to");
                            // If they are both numbers...
                            if (int.TryParse(tokens[0].Trim(), out int move) &&
                                int.TryParse(tokens[1].Trim(), out int move2))
                            {
                                // Draw to the indicated position.
                                Canvas.MovePen(move, move2);
                            }
                        }
                        else if (line.Trim().ToLower().Contains("pen colour"))
                        {
                            var tokens = parser.TokenizeCommand(line, "pen colour");
                            // to ensure its a string...
                            if (tokens.Length > 0)
                            {
                                var colour = tokens[0].Trim();
                                Canvas.PenColour(colour);
                            }
                        }
                        else if (line.Trim().ToLower().Contains("fill pen"))
                        {
                            var tokens = parser.TokenizeCommand(line, "fill pen");
                            // to ensure its a string...
                            if (tokens.Length > 0)
                            {
                                var mode = tokens[0].Trim();
                                Canvas.FillPen(mode);
                            }
                        }
                        else if (line.Trim().ToLower().Equals("clear"))
                        {
                            Canvas.Clear();
                        }

                        else if (line.Trim().ToLower().Equals("reset"))
                        {
                            Canvas.Reset();
                        }
                        else if (line.Equals("run") || line.Equals(""))     // "" is when return is pressed
                        {
                            Console.WriteLine("no error to display...");    // Both are correct inputs
                        }
                        else if (line.Trim().ToLower().Contains("loop for "))
                        {
                            loopCommand.SetStartLineNo(i);
                            var tokens = parser.TokenizeCommand(line.Trim().ToLower(), "loop for");

                            loopCommand.SetLoopCount(varCommand.Parse(tokens[0]));

                            i = loopCommand.EndLoop(allCommands, i);
                        }
                        else if (line.Trim().ToLower().Contains("end for"))
                        {
                            i = loopCommand.getStartLineNo() - 1;
                        }
                        else if (line.Contains(" = ") || line.Contains(" + ") || line.Contains(" - "))
                        {
                            varCommand.Parse(line);
                        }
                        else if (line.Trim().ToLower().Contains("if "))
                        {
                            var      split_tokens = line.Trim().ToLower().Substring("if".Length);
                            String[] tokens       = split_tokens.Trim().ToLower().Split('=');

                            if (varCommand.Parse(tokens[0]) != varCommand.Parse(tokens[1]))
                            {
                                i = ifElseCommands.IfCommand(allCommands);      // i Is the loop we are in and executes them all within if
                            }
                            else
                            {
                                ifElseCommands.Reset();
                            }
                        }
                        else if (line.Trim().ToLower().Contains("endif"))
                        {
                            // SKIP
                        }
                        else if (line.Trim().ToLower().Contains("method "))
                        {
                            i = methodCommand.RegisterMethod(allCommands, i);
                        }
                        else if (line.Trim().ToLower().Contains("endmethod"))
                        {
                            i = methodCommand.ReturnMethod(allCommands);
                        }
                        else if (line.Trim().ToLower().Contains("(") &&
                                 line.Trim().ToLower().Contains(")"))       // () specifies method
                        {
                            i = methodCommand.CallMethod(allCommands, line.Trim().ToLower(), i);
                        }
                        else
                        {
                            InvalidInput();
                        }
                    }
                    catch (Exception)    // When wrong input entered
                    {
                        InvalidInput();
                    }
                }
                CommandLineInterface.Text = ""; // Clears the CLI
                Refresh();                      // Ensures the user input is generated stryt after
            }
        }