2014-11-02 50 views
0

我在文本框中製作一種自定義腳本語言。如何在C#中實現自定義if語句?

例如: <NAME1><NAME2><FAM>並執行它時,標籤被變換到從文件,數據庫或用戶輸入來從字段中的數據。

在給出的示例結果是:

Elliot, John or Gleave. 

這是好現在。我得出結論,我需要陳述。

下面是一些例子:

<IF NAME1="George"> 
<DoStuff> 
<ENDIF> 

,我使用正在尋找從字符串指標和substringing它們的方法。 你能給我一些想法或指導如何使自定義if語句?

編輯:我設法做了我想做的事。所以如果有人想編輯,建議或者其他,請在此分享。請告訴我。

public static string ReplaceIfStatement(string script, CResultData resultData) 
{ 
    string workingScript = script; 
    string manipulatedScript = workingScript.Clone() as string; // storing the script in temp variable for further checks 

    workingScript = CStringFunctions.ReplaceAll(workingScript, "\r", string.Empty); // ReplaceAll uses the string.Replace() method 
    workingScript = CStringFunctions.ReplaceAll(workingScript, "\t", string.Empty); 

    string ifStartFieldName = "<IF "; 
    string ifEndFieldName = "<ENDIF>"; 

    workingScript = CStringFunctions.ReplaceAll(workingScript, "<IF\n", ifStartFieldName); 

    bool repeat = true; 

    while (repeat) 
    { 
     int ifStartIndexOpenTag = workingScript.IndexOf(ifStartFieldName); 
     int ifEndIndexOpenTag = workingScript.IndexOf(ifEndFieldName); 

     if (ifStartIndexOpenTag != -1 
      || ifEndIndexOpenTag != -1) 
     { 
      int ifStartIndexCloseTag = CStringFunctions.IndexOfString(
       workingScript, 
       ">", 
       ifStartIndexOpenTag + ifStartFieldName.Length + 1); //IndexOfString() uses the IndexOf() 

      int ifEndIndexCloseTag = CStringFunctions.IndexOfString(
       workingScript, 
       ">", 
       ifEndIndexOpenTag + ifStartFieldName.Length + 1); 

      if (ifStartIndexCloseTag != -1) 
      { 
       string ifStatement = CStringFunctions.SubstrString(
        workingScript, 
        ifStartIndexOpenTag + ifStartFieldName.Length, 
        ifStartIndexCloseTag - (ifStartIndexOpenTag + ifStartFieldName.Length)); 

       ifStatement = CStringFunctions.ReplaceAll(ifStatement, " ", string.Empty); 

       string rightSideIfStatement = CStringFunctions.SubstrString(
        ifStatement, 
        1, 
        CStringFunctions.IndexOfString(ifStatement, "=") - 1); 

       string leftSideIfStatement = CStringFunctions.SubstrString(
        ifStatement, 
        CStringFunctions.IndexOfString(ifStatement, "=") + 1, 
        CStringFunctions.IndexOfString(ifStatement, ")") 
        - CStringFunctions.IndexOfString(ifStatement, "=") - 1); 

       CResultData ifResultData = resultData.Clone(); // here is the class containing all props, ex: NAME1, NAME2 or FAM 

       foreach (PropertyInfo property in ifResultData.GetType().GetProperties()) 
       { 
        if (rightSideIfStatement == property.Name.ToUpper() 
         && leftSideIfStatement == (string)property.GetValue(ifResultData, null)) // In this statement compare the property name in the <IF> and the <IF (statement)> 
        { 
         //if the compare is true i remove the <IF (statement)> and the <ENDIF> from the script 
         manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
          workingScript, 
          ifStartIndexOpenTag, 
          ifStartIndexCloseTag + 1, 
          string.Empty); 

         ifEndIndexOpenTag = CStringFunctions.IndexOfString(manipulatedScript, ifEndFieldName); 
         ifEndIndexCloseTag = CStringFunctions.IndexOfString(
          manipulatedScript, 
          ">", 
          ifEndIndexOpenTag + ifStartFieldName.Length + 1); 

         manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
          manipulatedScript, 
          ifEndIndexOpenTag, 
          ifEndIndexCloseTag + 1, 
          string.Empty); 

         workingScript = manipulatedScript; 

         return workingScript; 
        } 
       } 

       if (manipulatedScript != null && manipulatedScript.Length == workingScript.Length) // When the IF statement is false 
       //here I compare the parameter script with the temp script if there are changes to remove from the script begging from <IF (statement)> to <ENDIF> 
       { 
        manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
         workingScript, 
         ifStartIndexOpenTag, 
         ifEndIndexCloseTag + 1, 
         string.Empty); 

        workingScript = manipulatedScript; 

        return workingScript; 
       } 
      } 
      else 
      { 
       repeat = false; 
      } 
     } 
     else 
     { 
      repeat = false; 
     } 
    } 

    workingScript = manipulatedScript; 

    return workingScript; 
} 
+1

你的意思是'定製if語句'。與通常的c#'if'語句有什麼不同? – 2014-11-02 09:18:33

+0

在給出的例子中,我解釋了我想要的。 。 – 2014-11-02 09:22:23

+5

你的問題不清楚。我知道你正在嘗試實現一種腳本語言,但仍然很難理解你需要什麼幫助。 – Rotem 2014-11-02 09:23:21

回答

3

如果您打算按照上面提供的標記樣式編寫自定義腳本語言,那麼您最好使其符合XML規範。

See this example of parsing XML.

假設使用XML和上述語法,

var document = new XMLDocument(); 
document.Load(xmlFilePath); 

var documentNodes = document.DocumentElement.SelectNodes("path/from/root/to/stuff/of/interest"); 

foreach (var node in documentNodes) 
{ 
    var name1NodeValue = node.SelectSingleNode("NAME1").InnerText; 
    var ifNode = node.SelectSingleNode("IF"); 
    if (ifNode.Attributes["NAME1"].Value.Equals(name1NodeValue) { 
     //Look for the node of interest. I just hard-coded doStuff for the sake of brevity. 
     doStuff(); 
    } 
} 

不是一個完美的例子,但應該足以讓你去。