我正在用C#編寫一個非常簡單的腳本解釋器。此語言中沒有If/Else
語句或用戶可編程子例程;唯一的控制流程是goto
關鍵字。C#:在foreach中跳過行嗎?
如果我用foreach
循環逐行解析腳本,如何讓程序跳轉到GoTo()
塊中參數指定的行號?
static void Main(string[] args)
{
string testLines = "SomeCommand(34,32)\n" +
"SomeCommand(1)\n" +
"GoTo(5)\n" +
"This(\"Will\",\"Be\",\"Skipped\")\n" +
"Destination(\"OfTheGoToKeyWord\")";
Regex r = new Regex("^(?<cmd>\\w+)[(](?<params>\\S+)[)]", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
string[] lines = testLines.Split('\n');
foreach (string line in lines)
{
try
{
string[] matches = r.Split(line);
if (matches[1].Equals("GoTo"))
{
GoToLineSpecifiedByMatchesElement2();
}
}
catch (Exception)
{
}
}
}
轉換'foreach'成'for'環路和環路指數設定爲GoTo' – oleksii 2013-03-04 18:27:56
的'價值或者你也可以使用一些醜陋continue語句。除非goto允許回去。 – 2013-03-04 18:31:05