0

So far i can take info from a textbox and draw shapes based on it, but i'm trying to allow the user to create variables within the textbox.

the program currently successfully splits text and draws multiple shapes using basic commands like:

circle 50 
square 100,100

but i am trying to expand the program to allow for variables to be defined in the text box and then use them to draw shapes. This is what i would like to be able to run in the text box which would in turn draw two circles:

 num = 50
circle num
num = num + 50
circle num

And this is what i currently have which splits the text and uses each part as parameters for the shapes which are then used to used to draw the lines/shapes etc.

 private void button1_Click(object sender, EventArgs e)
        {
            String[] Lines = ProgramWindow.Text.Split('\n');
            int NumberOfCommands = Lines.Length;          
            {
                for (int i = 0; i <= NumberOfCommands - 1; i++)
                {
                    String VariableName = "";
                    int VariableValue = 0; 
                    String[] Input = Lines[i].ToLower().Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);
                    String Command = Input[0];
                    int[] Parameters = Input.Length <= 1 ? new int[0] : Input[1].Split(',').Select(item => int.Parse(item)).ToArray();
                    int X = Parameters.Length >= 1 ? Parameters[0] : 0;
                    int Y = Parameters.Length >= 2 ? Parameters[1] : 0;
                    Console.WriteLine(Command);
                    Console.WriteLine(X);
                    Console.WriteLine(Y);
                    Commands(Command, X, Y, VariableName, VariableValue);
                    Refresh();
                }
            }

            void Commands(string Command, int X, int Y, String VariableName, int VariableValue)
            {                                                 
                if (X.Equals("="))
                {
                    VariableName = Command;
                    VariableValue = Y;
                }
                else if (Command.Equals("circle"))
                {
                    if (Checked == true)
                    {
                        MyCanvas.FillCircle(X);
                    }
                    else if (VariableValue > 0)
                    {
                        MyCanvas.DrawCircle(VariableValue);
                    }
                    else
                        MyCanvas.DrawCircle(X);


IronMike
  • 15
  • 2
  • 3
    You are trying to create a primitive programming language. If you are serious about this project, you should learn about language parsers that would allow you to generate an abstract syntax tree. – Kirk Woll Jan 07 '22 at 16:15
  • This is a fun idea. It seems like you are trying read the syntax of a custom language (that you create). That is pretty complicated. You could give the user more direction (and consequently you too) by creating multiple text boxes (one for command, one for number value, one for math symbol) – thewallrus Jan 07 '22 at 16:21
  • Also, I would say a problem is on line 4 of your for loop `String Command = Input[0];` Your command isn't always the first element in the array. – thewallrus Jan 07 '22 at 16:24
  • See [this](https://stackoverflow.com/a/58585275/14171304) for example. – dr.null Jan 07 '22 at 16:55
  • Be aware of the question: [What is the best scripting language to embed in a C# desktop application?](https://stackoverflow.com/questions/137933/what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application) (Maybe IronMike needs [IronPython](https://ironpython.net/)? lol) – Wyck Jan 07 '22 at 19:23
  • If you narrow down your abilities to a certain point, some simple parsing rules (using for instance even regular expressions) may be enough. If you want to allow more complex constructs (I remember you were talking about "loops" and other things yesterday, but even balanced parentheses are not that easy) you probably will need a serious parser, and generate an abstract syntax tree. But that's nothing that can be answered in a stackoverflow question (because some of these things are pretty heavy stuff in computer science) – derpirscher Jan 08 '22 at 13:29
  • Depending on the purpose of your project (ie you just want to learn something new, vs you need this done for work) maybe you are better off with @Wyck 's suggestion to embed something already existing ... – derpirscher Jan 08 '22 at 13:31

0 Answers0