/// <summary> /// Tries parsing a term in the given text. /// </summary> public static bool AcceptTerm(Dictionary<string, Expression> Variables, string Text, ref int Index, ref Expression Term, out int ErrorIndex) { if (AcceptLiteral(Text, ref Index, ref Term, out ErrorIndex)) return true; int cur = Index; if (AcceptString("-", Text, ref cur)) { if (AcceptTerm(Variables, Text, ref cur, ref Term, out ErrorIndex)) { Term = new UnaryExpression(Term, UnaryOperation.Negate); Index = cur; return true; } } if (AcceptString("~", Text, ref cur)) { if (AcceptTerm(Variables, Text, ref cur, ref Term, out ErrorIndex)) { Term = new UnaryExpression(Term, UnaryOperation.Complement); Index = cur; return true; } } int wordend = 0; string word = null; if (AcceptWord(Text, ref cur, ref word)) { wordend = cur; if (Variables.TryGetValue(word, out Term)) { Index = cur; return true; } AcceptExtendedWhitespace(Text, ref cur); } if (AcceptString("(", Text, ref cur)) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptExpression(Variables, Text, ref cur, ref Term, out ErrorIndex)) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptString(")", Text, ref cur)) { if (word != null) { switch (word) { case "saw": Term = new UnaryExpression(Term, UnaryOperation.Saw); break; case "sin": Term = new UnaryExpression(Term, UnaryOperation.Sine); break; case "square": Term = new UnaryExpression(Term, UnaryOperation.Square); break; case "tri": Term = new UnaryExpression(Term, UnaryOperation.Triangle); break; default: ErrorIndex = wordend; return false; } } Index = cur; return true; } ErrorIndex = cur; return false; } return false; } // Sequencer if (AcceptString("[", Text, ref cur)) { List<Expression> items = new List<Expression>(); ErrorIndex = cur; while (true) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptExpression(Variables, Text, ref cur, ref Term, out ErrorIndex)) { items.Add(Term); if (AcceptString(",", Text, ref cur)) { continue; } } break; } if (AcceptString("]", Text, ref cur) && items.Count > 0) { if (AcceptTerm(Variables, Text, ref cur, ref Term, out ErrorIndex)) { Term = new SequencerExpression(items, Term); Index = cur; return true; } } else { ErrorIndex = cur; return false; } } return false; }