// funcRef == null, means it is (not created because it is pointless) identity function private CodeBody createFunctionCall(CodeBody funcRef, IEnumerable <CodeBody> arguments) { if (funcRef == null && arguments.Count() != 1) { throw new ArgumentException("Identity function works only for single argument"); } var code = new CodeBody(); if (funcRef != null) { code.Append(funcRef).AddSnippet("("); } code.AddCommaSeparatedElements(arguments); if (funcRef != null) { code.AddSnippet(")"); } return(code); }
private AltRule createSeed(SymbolPosition position, List <Production> productions, bool doubled, // should we double the seed right at the start string lhsSymbolName, IEnumerable <string> elementTypeNames, params RhsSymbol[] symbols) { RhsSymbol[] init_symbols = symbols.Where(it => !it.SkipInitially).ToArray(); var main_code = new CodeBody().AddWithIdentifier(CodeWords.New, " "); main_code.Append(makeTupleListCode(position, elementTypeNames)); main_code.AddSnippet("("); IEnumerable <CodeBody> code_lists = elementTypeNames.Select(it => new CodeBody().AddWithIdentifier(CodeWords.New, " ", CodeWords.List, "<", it, ">{")).ToList(); // purpose: to handle names in doubled seed mode Dictionary <RhsSymbol, string[]> obj_name_substs = new Dictionary <RhsSymbol, string[]>(); { IEnumerable <RhsSymbol> named_symbols = symbols.Where(it => it.ObjName != null); if (named_symbols.Any()) { foreach (Tuple <RhsSymbol, CodeBody> tuple in named_symbols.SyncZip(code_lists)) { RhsSymbol nsymbol = tuple.Item1; CodeBody lcode = tuple.Item2; bool double_sym = doubled && !nsymbol.SkipInitially; string[] name_subst = new string[double_sym ? 2 : 1]; obj_name_substs.Add(nsymbol, name_subst); // if we double the element, we have to come up with new names if (double_sym) { name_subst[0] = AutoNames.Double1 + nsymbol.ObjName + "__"; name_subst[1] = AutoNames.Double2 + nsymbol.ObjName + "__"; } else { name_subst[0] = nsymbol.ObjName; } for (int i = 0; i < (double_sym ? 2 : 1); ++i) { if (i == 1) { lcode.AddSnippet(","); } if (double_sym) { lcode.AddIdentifier(name_subst[i]); } else { lcode.AddIdentifier(nsymbol.ObjName); } } } } } foreach (Tuple <CodeBody, int> code_pair in code_lists.ZipWithIndex()) { code_pair.Item1.AddSnippet("}"); if (code_pair.Item2 > 0) { main_code.AddSnippet(","); } main_code.Append(code_pair.Item1); } main_code.AddSnippet(")"); // in case of doubled seed we have to rename the symbols // otherwise just make shallow copies without renaming // but since we already set proxy name correctly, we can use shared code for both cases var seed_symbols = new LinkedList <RhsSymbol>(); for (int i = 0; i < (doubled ? 2 : 1); ++i) { foreach (RhsSymbol sym in (i == 0 ? init_symbols : symbols)) { if (sym.ObjName == null) { seed_symbols.AddLast(sym.ShallowClone()); } else { int s_idx = (i == 1 && sym.SkipInitially) ? 0 : i; seed_symbols.AddLast(sym.Renamed(obj_name_substs[sym][s_idx])); } } } return(AltRule.CreateInternally(position, // are there any symbols for production seed_symbols.Any() ? new RhsGroup[] { RhsGroup.CreateSequence(position, RepetitionEnum.Once, seed_symbols.Select(it => it.SetSkip(false)).ToArray()) } : new RhsGroup[] { }, new CodeMix(CodeMix.SeedComment).AddBody(main_code))); }