2

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?)

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • (after edit) Yeah, much better question. – JayC Oct 24 '12 at 02:40
  • Does the file have to be xml? What kind of syntax are you looking for in the file? If neither one of those questions has strong requirements, I would look at hosting the IronPython or Powershell runtimes. Currently, c# has no built in "eval" capabilities. – Mike Zboray Oct 24 '12 at 02:51
  • I'm making my own syntax for this thing, it won't be as complex as the example in the question. I could use XML if it will be easier, I just need to know if I can do things I need with my variables with this. Btw thanks for telling me how it's called, what I'm trying to do. – user1306322 Oct 24 '12 at 02:53

4 Answers4

1

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 the var attribute, 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.

siride
  • 200,666
  • 4
  • 41
  • 62
  • You'd have to use a dictionary of actions, not variables. As far as I know it's impossible to refer to a particular (local) variable by some dictionary mechanism (say, to assign something to it). – JayC Oct 24 '12 at 02:43
  • @JayC: even if you did want to actually modify real variables in the app, you can have a dictionary linking variable name to its reflection object. Of course, using closures is probably good enough. – siride Oct 24 '12 at 02:46
0

For a very simple case where you have a small and fixed set of keyword/syntax for defining the action, I will recommend that you just write a custom parser and use Reflection to access the field/property that you are targeting and custom code on the actual operation. You can leverage on the use of Action<> and Fun<> delegates to isolate/reuse the code implementation for action with clear evaluation path.

But if you are looking at a more complex scenario, then I will recommend that you start looking into DSL. My knowledge in DSL is very limited so I can't say much, but certainly there is a fair bit of learning curve involved. Something like meta# might be a good starting point to abstract away some of the complexity

Fadrian Sudaman
  • 6,405
  • 21
  • 29
0

Sounds like a custom config section will help you. Take a look at http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx

Retired_User
  • 1,595
  • 11
  • 17
  • Ok, I've read you question again and seems like I misunderstood it, so please nevermind – Retired_User Oct 24 '12 at 03:10
  • 1
    You want basically add scripting support to your app (write instructions to a text file and make your application to follow them). Take a look at http://stackoverflow.com/questions/260/adding-scripting-functionality-to-net-applications – Retired_User Oct 24 '12 at 03:12
0

I think this screams for application scripting, but binding all the app vars to script is a lot of work (not mentioning learning a new language).

So the best is to use .NET Runtime Compilation feature, then your xml-like file whould be a simple C# file that will be loaded, compiled and executed at runtime and better yet you can make it reference all your app vars very easily.

Majid Max
  • 509
  • 1
  • 4
  • 10