Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,


I want to make an interpreter but i´m having some problems with code.

Interpreter have to interpret when the user declares a var and recognize that the user want to declare a var.

Example:

user writes in the text-box2 -- "y = 2", or "r = 2"

I want that the interpreter create a variable with the value(string) given by user and assign the value(int, float, etc) given by user.

I have this code but it didn´t work:

C#
private void button1_Click(object sender, EventArgs e)
        {
            string in1 = textBox2.Text;
            string in2 = textBox2.Text;
            float conv;
            if (textBox2.Text == String.Format("{0} = {1}", in1, in2))
            {
                in1 = in2;
                conv = Convert.ToInt32(in2);
                textBox1.Text = String.Format("{0} = {1}", in1, in2);
            }

        }


and then the value of var (example: y") appears in the text-box1

example : text-box2 user writes -- "y = 3"

then, in text-box1 will appear this: "var created: y = 3"



I wonder if anyone can help me on this. :)
Posted
Updated 30-Sep-13 9:24am
v4
Comments
idenizeni 30-Sep-13 15:43pm    
There are many things wrong here. Why are you doing this, may be there is a better approach to what you are trying to do.

If you expect 'y = 2' as input then your if condition will always be false as you would be trying to match 'y = 2' against 'y = 2 = y = 2', which clearly do not match.

Also, the line...
conv = Convert.ToInt32(in2);
would generate an error as 'y = 2' would not convert to a float. You would need to parse out the number and only pass it to the ToInt32() function.
MCY 30-Sep-13 17:04pm    
You should note that by this approach (correcting the mistakes in the comment before this one, of course), only strings you could parse would be simple equities such as x = a.

1 solution

The code which reads two variables from text boxes is not an interpreter. You did not explain how it should work and why.
But your code makes no sense, as it looks like you mixed up variable name, string representations of the number (in your case, immediate constants declared in your input language, and the values themselves.

Let's see. If in.Text contains something like "x = 3", you should first parse it into "x" and "3", and check if "=" is really the assignment operator. Anyway, comparing in1 and in2 does not give you anything at all. Why would the user enter two identical strings in two different text boxes? But in your code, this is the main case. Besides, why getting textBox2.Text again, if you already got in2? And next statement in1 = in2; simply makes an assignment which looses previous value of in2. Why doing that if you have in2.

Overall, the impression is that you are not ready to write any interpreter, not even any UI. Please understand, I'm not trying to hurt you, I'm trying to give you a useful practical advice. You first need to gain some understanding and experience solving simple problems, better by writing simple console-only applications and reading documentation.

—SA
 
Share this answer
 
Comments
Ron Beyer 30-Sep-13 17:59pm    
5'd, having implemented 2 scripting systems myself (one graphical and unlike anything out there), its not something for the novice to undertake. Understanding the elements of scripting systems before diving into the code would help greatly here, and there are a number of articles on CodeProject that could bring him down that path.
Sergey Alexandrovich Kryukov 30-Sep-13 18:12pm    
Thank you, Ron.
—SA
Manfred Rudolf Bihy 30-Sep-13 18:55pm    
Good advice, SA! When I've read the question before your answer I was not sure how to respond to this absolute lack of understanding what an interpreter actually does. Explaining something like that is totally out of scope for Q&A. I do appreciate your reconciliation abilities in your communication with the OP though.

Cheers!
Sergey Alexandrovich Kryukov 30-Sep-13 19:38pm    
Thank you, Manfred. Frankly, I'm still not sure how to respond. It's really awkward to discuss one's abilities, because it's easy to make a mistake, but sometimes this can be the way to evoke some sober view on the problem and finally help to save from some waste of time and bigger frustration. I think it would be safe to state that the abilities can grow with effort and spent time, so insufficient ability is not a final personal ceiling; the attitudes and determination are more important...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900