public Maybe <LNode> Replace(LNode node) { TokenTree tt; if (node.IsId) { Symbol name = node.Name; for (int i = 0; i < Replacements.Count; i++) { if (Replacements[i].A == name) { Replacements.InternalArray[i].C++; var repl = Replacements[i].B; return(repl.WithAttrs(node.Attrs.WhereSelect(_replace).AddRange(repl.Attrs))); } } return(node); } else if ((tt = node.Value as TokenTree) != null) { if (ReplaceInTokenTree(ref tt, Replacements)) { return(node.WithValue(tt)); } } return(node.Select(_replace)); }
/// <summary>Recursively runs a replacement function on a Loyc tree.</summary> /// <param name="root">A node to scan recursively (depth-first, starting /// at the top level).</param> /// <param name="replaceFunc">A user-defined method that returns either /// (1) null, to do no replacement and request that child nodes be processed, /// or (2) non-null, to replace the input node with an output node. When /// replaceFunc returns a node, <c>FindAndReplace</c> does not recursively /// call replaceFunc again on that node or its children.</param> /// <param name="replaceRoot">If true, <c>replaceFunc</c> is called on the /// root node. If false, <c>replaceFunc</c> is not called on the root node.</param> /// <returns>The new node produced after all replacements have occurred.</returns> /// <remarks>If <c>replaceFunc</c> always returns null (or if <c>replaceRoot</c> /// is false and the root has no children), <c>FindAndReplace</c> returns /// <c>root</c>.</remarks> public static LNode FindAndReplace(this LNode root, Func <LNode, LNode> replaceFunc, bool replaceRoot = true) { Func <LNode, LNode> replaceRecursive = null; replaceRecursive = node => { LNode @new = replaceFunc(node); return(@new ?? node.Select(replaceRecursive)); }; LNode newRoot = replaceRoot ? replaceFunc(root) : null; return(newRoot ?? root.Select(replaceRecursive)); }
public AltType(VList <LNode> classAttrs, LNode typeName, VList <LNode> baseTypes, AltType parentType) { _classAttrs = classAttrs; TypeName = typeName; BaseTypes = baseTypes; ParentType = parentType; { LNode stem; VList <LNode> a = default(VList <LNode>); if (TypeName.CallsMin(CodeSymbols.Of, 1) && (stem = TypeName.Args[0]) != null && (a = new VList <LNode>(TypeName.Args.Slice(1))).IsEmpty | true || (stem = TypeName) != null) { _typeNameStem = stem; _genericArgs = a.ToWList(); } else { _genericArgs = new WList <LNode>(); } } if (ParentType != null) { BaseTypes.Insert(0, ParentType.TypeNameWithoutAttrs); bool changed = false; for (int i = 0; i < _genericArgs.Count; i++) { var arg = _genericArgs[i]; var parentArg = ParentType._genericArgs.FirstOrDefault(a => a.IsIdNamed(arg.Name)); if (parentArg != null) { var wheres = new HashSet <LNode>(WhereTypes(arg)); int oldCount = wheres.Count; var parentWheres = WhereTypes(parentArg); foreach (var where in parentWheres) { wheres.Add(where); } if (wheres.Count > oldCount) { arg = arg.WithAttrs(arg.Attrs.Where(a => !a.Calls(S.Where)).Add(LNode.Call(S.Where, LNode.List(wheres)))); _genericArgs[i] = arg; changed = true; } } } if (changed) { TypeName = LNode.Call(CodeSymbols.Of, LNode.List().Add(_typeNameStem).AddRange(_genericArgs)); } } TypeNameWithoutAttrs = TypeName.Select(n => n.WithoutAttrs()); }
public LNode Replace(LNode node) { if (node.IsId) { Symbol name = node.Name; for (int i = 0; i < Replacements.Length; i++) { if (Replacements[i].A == name) { Replacements[i].C++; var repl = Replacements[i].B; return(repl.WithAttrs(node.Attrs.SmartSelect(_replace).AddRange(repl.Attrs))); } } return(node); } else { return(node.Select(_replace)); } }
public AltType(VList <LNode> classAttrs, LNode typeName, VList <LNode> baseTypes, AltType parentType) { _classAttrs = classAttrs; TypeName = typeName; BaseTypes = baseTypes; ParentType = parentType; //matchCode (TypeName) { // case $stem<$(..a)>, $stem: // _typeNameStem = stem; // _genericArgs = a; // default: // _genericArgs = new WList<LNode>(); //} { // Above matchCode expanded: LNode stem; VList <LNode> a = default(VList <LNode>); if (TypeName.CallsMin(CodeSymbols.Of, 1) && (stem = TypeName.Args[0]) != null && (a = new VList <LNode>(TypeName.Args.Slice(1))).IsEmpty | true || (stem = TypeName) != null) { _typeNameStem = stem; _genericArgs = a.ToWList(); } else { _genericArgs = new WList <LNode>(); } } if (ParentType != null) { BaseTypes.Insert(0, ParentType.TypeNameWithoutAttrs); // Search for all 'where' clauses on the ParentType and make sure OUR generic args have them too. bool changed = false; for (int i = 0; i < _genericArgs.Count; i++) { var arg = _genericArgs[i]; var parentArg = ParentType._genericArgs.FirstOrDefault(a => a.IsIdNamed(arg.Name)); if (parentArg != null) { var wheres = new HashSet <LNode>(WhereTypes(arg)); int oldCount = wheres.Count; var parentWheres = WhereTypes(parentArg); foreach (var where in parentWheres) { wheres.Add(where); } if (wheres.Count > oldCount) { arg = arg.WithAttrs(arg.Attrs.SmartWhere(a => !a.Calls(S.Where)) .Add(LNode.Call(S.Where, LNode.List(wheres)))); _genericArgs[i] = arg; changed = true; } } } if (changed) { TypeName = LNode.Call(CodeSymbols.Of, LNode.List().Add(_typeNameStem).AddRange(_genericArgs)).SetStyle(NodeStyle.Operator); } } TypeNameWithoutAttrs = TypeName.Select(n => n.WithoutAttrs()); }
private RoundTripPerformance MakeLesRoundTrip(LNode[] Nodes) { var timer = new Stopwatch(); timer.Start(); string[] data = null; for (int i = 0; i < TimedRoundTripCount; i++) { data = Nodes.Select(LesLanguageService.Value.Print).ToArray(); } var writePerf = timer.Elapsed; timer.Stop(); int size = data.Aggregate(0, (acc, item) => acc + item.Length); timer.Restart(); for (int i = 0; i < TimedRoundTripCount; i++) { data.Select(item => LesLanguageService.Value.Parse(item)).ToArray(); } timer.Stop(); var readPerf = timer.Elapsed; return new RoundTripPerformance(readPerf, writePerf, size); }
public Maybe<LNode> Replace(LNode node) { TokenTree tt; if (node.IsId) { Symbol name = node.Name; for (int i = 0; i < Replacements.Count; i++) if (Replacements[i].A == name) { Replacements.InternalArray[i].C++; var repl = Replacements[i].B; return repl.WithAttrs(node.Attrs.WhereSelect(_replace).AddRange(repl.Attrs)); } return node; } else if ((tt = node.Value as TokenTree) != null) { if (ReplaceInTokenTree(ref tt, Replacements)) return node.WithValue(tt); } return node.Select(_replace); }