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

4

4 に答える 4

1

あなたの質問に答えて:はい。

質問を編集したので...

XMLを解析する必要があります。できれば、.NETに付属のライブラリを使用してください。次に、XMLツリーをたどり、アクションが関連付けられている各ノードを実行します。

アプリの変数を直接公開したくない場合があります。代わりに、XMLファイルで操作できる実行状態を定義する必要があります。たとえば、変数とその値の辞書を作成できます。次に、<action>タグを取得したら、var属性を確認し、辞書で変数を検索してから、タグの内容で指定されている値に変更します。

これは簡単な作業ではありません。言語通訳を書くことは必ずしも難しいことではありません(これは基本的にあなたがしていることです)。しかし、それが理にかなっているようにあなたの言語を設計するのは難しいかもしれません。また、(表示される)式が埋め込まれている場合は、式パーサーが必要になることもわかります。繰り返しになりますが、これらは「簡単に」構築できますが、経験のない人にとっては、最初にいくつかの調査を行う必要があります。実世界の構文解析手法について知らないと、非常に複雑で、遅く、壊れたものを簡単に構築してしまう可能性があります。

式の解析については、LL(1)パーサー、特に再帰下降を調べてください。これは、理解と実装が最も簡単です。

XML入力を評価するには、ツリーをウォークする再帰的アルゴリズムが必要です。これは、再帰下降パーサーに似ています。実際、この2つは、詳細を除いてほとんど同じです。

何かがうまくいったら、特定の問題について、それほど広範囲に質問するのではなく、実際に質問する必要があります。

別の編集:変数に辞書を使用します。

于 2012-10-24T02:34:15.113 に答える
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

于 2012-10-24T02:50:29.210 に答える
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

于 2012-10-24T03:05:11.220 に答える
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.

于 2012-10-24T03:13:57.277 に答える