/// <summary>Searches a list of expressions/statements for one or more /// patterns, and performs replacements.</summary> /// <param name="stmts">A list of expressions/statements in which to search.</param> /// <param name="patterns">Each pair consists of (A) something to search /// for and (B) a replacement expression. Part A can use the substitution /// operator with an identifier inside (e.g. $Foo) to "capture" any /// subexpression, and part B can use the same substitution (e.g. $Foo) /// to insert the captured subexpression(s) into the output.</param> /// <param name="replacementCount">Number of replacements that occurred.</param> /// <returns>The result of applying the replacements.</returns> /// <remarks><see cref="LNodeExt.MatchesPattern"/> is used for matching.</remarks> public static RVList <LNode> Replace(RVList <LNode> stmts, Pair <LNode, LNode>[] patterns, out int replacementCount) { // This list is used to support simple token replacement in TokenTrees _tokenTreeRepls = InternalList <Triplet <Symbol, LNode, int> > .Empty; foreach (var pair in patterns) // Look for Id => Id or Id => Literal { if (pair.A.IsId && (pair.B.IsId || pair.B.IsLiteral)) { _tokenTreeRepls.Add(new Triplet <Symbol, LNode, int>(pair.A.Name, pair.B, 0)); } } // Scan the syntax tree for things to replace... int count = 0; var temp = new MMap <Symbol, LNode>(); var output = stmts.SmartSelect(stmt => stmt.ReplaceRecursive(n => { LNode r = TryReplaceHere(n, patterns, temp); if (r != null) { count++; } return(r); })); replacementCount = count; return(output); }
static RVList <LNode> Replace(RVList <LNode> stmts, Pair <LNode, LNode>[] patterns) { var temp = new MMap <Symbol, LNode>(); var output = stmts.SmartSelect(stmt => stmt.ReplaceRecursive(n => TryReplaceHere(n, patterns, temp))); return(output); }
public override LNode WithArgs(Func <LNode, LNode> selector) { RVList <LNode> args = Args, newArgs = args.SmartSelect(selector); if (args == newArgs) { return(this); } return(WithArgs(newArgs)); }
/// <summary>Searches a list of expressions/statements for one or more /// patterns, and performs replacements.</summary> /// <param name="stmts">A list of expressions/statements in which to search.</param> /// <param name="patterns">Each pair consists of (A) something to search /// for and (B) a replacement expression. Part A can use the substitution /// operator with an identifier inside (e.g. $Foo) to "capture" any /// subexpression, and part B can use the same substitution (e.g. $Foo) /// to insert the captured subexpression(s) into the output.</param> /// <param name="replacementCount">Number of replacements that occurred.</param> /// <returns>The result of applying the replacements.</returns> /// <remarks><see cref="LNodeExt.MatchesPattern"/> is used for matching.</remarks> public static RVList<LNode> Replace(RVList<LNode> stmts, Pair<LNode, LNode>[] patterns, out int replacementCount) { // This list is used to support simple token replacement in TokenTrees _tokenTreeRepls = InternalList<Triplet<Symbol, LNode, int>>.Empty; foreach (var pair in patterns) // Look for Id => Id or Id => Literal if (pair.A.IsId && (pair.B.IsId || pair.B.IsLiteral)) _tokenTreeRepls.Add(new Triplet<Symbol,LNode,int>(pair.A.Name, pair.B, 0)); // Scan the syntax tree for things to replace... int count = 0; var temp = new MMap<Symbol, LNode>(); var output = stmts.SmartSelect(stmt => stmt.ReplaceRecursive(n => { LNode r = TryReplaceHere(n, patterns, temp); if (r != null) count++; return r; })); replacementCount = count; return output; }