/// <summary> /// Tries parsing the selected contents of this form, displaying the appropriate messages on failure. /// </summary> private bool _ParseSelected(out Expression Expression, out SoundOptions Options) { Expression = null; Options = null; int targetindex = this._Text.SelectionStart; int targetlength = this._Text.SelectionLength; int errorindex = targetindex; if (targetlength > 0 && Parser.Parse(this._Text.Text, targetindex, targetlength, out Expression, out Options, out errorindex)) { return true; } else { this._Text.Select(errorindex, 0); return false; } }
/// <summary> /// Plays the given expression with the given options. /// </summary> private void _Play(Expression Expression, SoundOptions Options) { this._Sound.Stop(); // Just to make sure. if (this._Sound.Play(new EvaluatorStream(4096, Expression, Options, false))) { this._Update = false; this._PlayStop.Text = _StopText; } else { MessageBox.Show("Could not create sound output, check values of \"#rate\" and \"#resolution\".", MessageBoxCaption, MessageBoxButtons.OK); this._PlayStop.Text = _PlayText; } }
/// <summary> /// Combines an optree with a term using the given operator. /// </summary> public static _OpTree Combine(_OpTree Left, Operator Operator, Expression Right) { if (Left.Operator == null || Left.Operator.Precedence >= Operator.Precedence) return new _OpTree(Operator, Left, new _OpTree(Right)); else return new _OpTree(Left.Operator, Left.Left, Combine(Left.Right, Operator, Right)); }
/// <summary> /// Tries parsing the contents of this form, displaying the appropriate messages on failure. /// </summary> private bool _Parse(out Expression Expression, out SoundOptions Options) { int errorindex; if (Parser.Parse(this._Text.Text, out Expression, out Options, out errorindex)) { if (Expression != null) { return true; } else { MessageBox.Show("The result variable \"" + Parser.Result + "\" must be defined", MessageBoxCaption, MessageBoxButtons.OK); return false; } } else { this._Text.Select(errorindex, 0); return false; } }
/// <summary> /// Parses the given text (or as much as possible of it) and returns the expression for the specified target string, or false with an error index. /// </summary> public static bool Parse(string Text, int TargetIndex, int TargetLength, out Expression Expression, out SoundOptions Options, out int ErrorIndex) { Expression = null; Options = new SoundOptions(); Dictionary<string, Expression> variables = null; int index = 0; AcceptProgram(Text, ref index, ref variables, ref Options, out ErrorIndex); if (index >= TargetIndex) { index = 0; string target = Text.Substring(TargetIndex, TargetLength); AcceptExtendedWhitespace(target, ref index); if (AcceptExpression(variables, target, ref index, ref Expression, out ErrorIndex)) { AcceptExtendedWhitespace(target, ref index); if (index == TargetLength) { return true; } } ErrorIndex += TargetIndex; return false; } return false; }
public _OpTree(Expression Term) { this.Term = Term; }
/// <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; }
/// <summary> /// Parses the given text and either returns true with an expression and sound options, or false with an error index. /// </summary> public static bool Parse(string Text, out Expression Expression, out SoundOptions Options, out int ErrorIndex) { Expression = null; Options = new SoundOptions(); Dictionary<string, Expression> variables = null; int index = 0; AcceptProgram(Text, ref index, ref variables, ref Options, out ErrorIndex); if (index == Text.Length) { variables.TryGetValue(Result, out Expression); return true; } return false; }
/// <summary> /// Tries parsing an expression in the given text. /// </summary> public static bool AcceptExpression(Dictionary<string, Expression> Variables, string Text, ref int Index, ref Expression Expression, out int ErrorIndex) { ErrorIndex = Index; if (AcceptTerm(Variables, Text, ref Index, ref Expression, out ErrorIndex)) { _OpTree curtree = new _OpTree(Expression); int cur = Index; while (true) { AcceptExtendedWhitespace(Text, ref cur); Operator op = null; if (AcceptOperator(Text, ref cur, ref op)) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptTerm(Variables, Text, ref cur, ref Expression, out ErrorIndex)) { curtree = _OpTree.Combine(curtree, op, Expression); Index = cur; continue; } return false; } break; } Expression = curtree.Expression; return true; } return false; }
/// <summary> /// Tries parsing a literal term in the given text. /// </summary> public static bool AcceptLiteral(string Text, ref int Index, ref Expression Term, out int ErrorIndex) { int val = 0; if (AcceptInteger(Text, ref Index, ref val, out ErrorIndex)) { Term = new ConstantExpression(val); return true; } return false; }
/// <summary> /// Tries parsing an assignment. /// </summary> public static bool AcceptAssignment(Dictionary<string, Expression> Variables, string Text, ref int Index, ref string Name, ref Expression Value, out int ErrorIndex) { ErrorIndex = Index; int cur = Index; if (AcceptWord(Text, ref cur, ref Name)) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptString("=", Text, ref cur)) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptExpression(Variables, Text, ref cur, ref Value, out ErrorIndex)) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptString(";", Text, ref cur)) { Index = cur; return true; } } } } return false; }
public BinaryExpression(Expression Left, Expression Right, BinaryOperation Operation) { this.Left = Left; this.Right = Right; this.Operation = Operation; }
public UnaryExpression(Expression Source, UnaryOperation Operation) { this.Source = Source; this.Operation = Operation; }
public SequencerExpression(List<Expression> Items, Expression Parameter) { this.Items = Items; this.Parameter = Parameter; }
public EvaluatorStream(int BufferSize, Expression Expression, SoundOptions Options, bool Exporting) : this(BufferSize, Expression.GetEvaluator(new Dictionary<Expression,Evaluator>(), BufferSize, Options.Resolution), Options, Exporting) { }