// 解释器 public void Interpret(PlayContext context) { if (string.IsNullOrWhiteSpace(context.PlayText)) { return; } var playKey = context.PlayText.Substring(0, 1); context.PlayText = context.PlayText.Substring(2); var playValue = Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" ", StringComparison.Ordinal))); context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ", StringComparison.Ordinal) + 1); Excute(playKey, playValue); }
static void Main(string[] args) { PlayContext context = new PlayContext(); context.PlayText = "T 500 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 "; Expression expression = null; try { while (!string.IsNullOrWhiteSpace(context.PlayText)) { string str = context.PlayText.Substring(0, 1); switch (str) { case "O": expression = new Scale(); break; case "T": expression = new Speed(); break; case "C": case "D": case "E": case "F": case "G": case "A": case "B": case "P": expression = new Note(); break; } expression.Interpret(context); } } catch (Exception exception) { Console.WriteLine(exception.Message); } Console.ReadKey(true); }