Can I somehow use a text file with contents like this:
<comment> This is something like an XML file.
<action var="myInteger"> +50
<condition> stringVar == "sometext" <action var="boolVar"> = true
.. parse it and make my app perform some actions?
The idea is to make a user-friendly (example doesn’t count) pseudocode that can change app’s variables and run methods. Problem is I don’t know how to change variables by their names.
Making a separate case for each variable name (explicitly supporting them) would be rather crazy:
switch(varName)
{
case "var1": {/* things */ break;}
case "var2": {/* things */ break;}
/* ... */
case "var9999": {/* things */ break;}
}
Edit: I think I asked the wrong question originally. (And it was Is there an easy way to work with application’s variables by executing code from text file?)
In answer to your question: yes.
Now that you’ve edited your question…
You’ll want to parse the XML, preferably using the libraries that ship with .NET. Then you walk the XML tree, executing each node that has some action associated with it.
You probably don’t want to expose your app’s variables directly. Instead, you should define some execution state that can be manipulated by the XML file. You could have, for example, a dictionary of variables and their values. Then when you get an
<action>tag, you look at thevarattribute, look up the variable in the dictionary, then change the value to be whatever the contents of the tag specify.This is not a simple task. It’s not necessarily hard to write a language interpreter (which is what you are doing, essentially). But it can be difficult to design your language so that it makes sense. You’ll also find that, if you have embedded expressions (which you appear to), then you’ll need an expression parser. Again, these are “easy” to construct, but for someone without experience, you’ll need to do some research first. You could easily end up constructing something that’s very complicated, slow and broken if you don’t know about real world parsing techniques.
For expression parsing, look into LL(1) parsers, specifically recursive descent, which is the easiest to understand and implement.
For evaluating the XML input, you’ll need a recursive algorithm that walks the tree. This will be similar to your recursive descent parser. In fact, the two are pretty much the same except for the details.
Once you’ve gotten something going, you should ask an actual question about a particular problem, instead of asking one so broad.
Another edit: use a dictionary for your variables.