private string dumpNfaCell(NfaCell <int, object> nfaCell, string symbolTypeName, Func <int, string> symbolNameConvert, string treeNodeName) { string code_str = null; if (nfaCell.ProductionUserAction != null) { // launch the fake action to retrieve the code of the action stored as string CodeLambda code = (CodeLambda)nfaCell.ProductionUserAction.Code(null); code_str = "ProductionAction<" + treeNodeName + ">.Convert(" + code.Make() + "," + code.RhsUnusedParamsCount + ")"; //code_str = (string)nfaCell.ProductionUserAction(null); } return("NfaCell<" + symbolTypeName + "," + treeNodeName + ">.Create(" + Environment.NewLine + symbolNameConvert(nfaCell.LhsSymbol) + "," + Environment.NewLine + nfaCell.RhsSeenCount + "," + Environment.NewLine + (!nfaCell.RecoveryTerminals.Any() ? CodeWords.Null : (CodeWords.New + "[]{" + nfaCell.RecoveryTerminals.Select(it => symbolNameConvert(it)).Join(",") + "}")) + "," + Environment.NewLine // markings are really raw ints (they are not symbols) + nfaCell.ProductionMark + "," + Environment.NewLine + "\"" + nfaCell.ProductionCoordinates + "\"," + Environment.NewLine + (nfaCell.ProductionTabooSymbols.Any(col => col.Count > 0) ? (CodeWords.New + " []{" + String.Join(",", nfaCell.ProductionTabooSymbols.Select(col => CodeWords.New + " HashSet<int>(" + CodeWords.New + " int[]{" + String.Join(",", col.Select(it => it)) + "})")) + "}") : CodeWords.Null) + "," + Environment.NewLine + (code_str == null ? CodeWords.Null : code_str) + ")" + Environment.NewLine); }
private string wrapStartProductions(string startSymbol, Dictionary <string, List <ProductionInfo> > productionsDict) { string new_start = grammar.RegisterNewSymbol("__start_" + startSymbol, grammar.GetTypeNameOfSymbol(startSymbol)); var prod = new ProductionInfo(SymbolPosition.None, new_start, RecursiveEnum.No, new[] { new RhsSymbol(SymbolPosition.None, null, startSymbol) }, null); var param = FuncParameter.Create(startSymbol, grammar.TreeNodeName, dummy: false); // this code is really an identity call prod.ActionCode = CodeLambda.CreateProxy(new_start, // parameters new FuncParameter[] { param }, grammar.TreeNodeName, functionsRegistry.Add(FunctionRegistry.IdentityFunction(new_start)), // its arguments new [] { param.NameAsCode() }); productionsDict.Add(new_start, new List <ProductionInfo> { prod }); return(new_start); }
protected override void Visit(CodeLambda method) { if (method.XmlDoc != null) { Visit(method.XmlDoc); } VisitList(method.CustomAttributes); VisitList(method.Parameters); if (method.Body != null) { VisitList(method.Body); } }
internal string Add(CodeLambda code) { if (code == null) { throw new ArgumentNullException(); } var key = code.Make(); Tuple <int, HashSet <string> > value; if (!revRegistry.TryGetValue(key, out value)) { value = Tuple.Create(revRegistry.Count, new HashSet <string>()); revRegistry.Add(key, value); } value.Item2.Add(code.LhsSymbol); return(functionEntryName(value.Item1)); }
private void feedProdBuilder(IEnumerable <ProductionInfo> prodInfos) { // we have to make sure, that the identical code (as string) is converted into identical code (as C#) // otherwise each production would get different C# code reference, which would make DFA builder think every action is unique var user_actions_pool = new Dictionary <CodeLambda, UserActionInfo <object> >(); foreach (ProductionInfo prod_info in prodInfos) { Production <int, object> production = productionBuilder.AddProduction(grammar.GetSymbolId(prod_info.LhsSymbol), prod_info.Recursive, prod_info.RhsSymbols.Select(it => grammar.GetSymbolId(it.SymbolName)).ToArray()); production.MarkWith = prod_info.EffectiveMarkedWith; production.TabooSymbols = prod_info.TabooSymbols; production.PositionDescription = "Action for \\\"" + prod_info.LhsSymbol + "\\\" " + (prod_info.Position.Equals(SymbolPosition.None) ? ("added by NLT generator for " + prod_info.CodeComment) : ("at " + prod_info.Position.XYString())); if (prod_info.ActionCode != null) { UserActionInfo <object> func; if (!user_actions_pool.TryGetValue(prod_info.ActionCode, out func)) { // this dummy variable serves as anti-closure, so DO NOT remove it CodeLambda anti_capture = prod_info.ActionCode; func = ProductionAction <object> .Convert(() => anti_capture, anti_capture.RhsUnusedParamsCount); user_actions_pool.Add(prod_info.ActionCode, func); } production.UserAction = func; if (prod_info.IdentityOuterFunctionParamIndex != ProductionInfo.NoIdentityFunction) { production.IdentityOuterFunctionParamIndex = prod_info.IdentityOuterFunctionParamIndex; } } } }
private string registerLambda(string lhsSymbol, IEnumerable <string> inputTypeNames, string outputTypeName, // each pair holds real name (like "expr") and (as backup) dummy name, like "_2" IEnumerable <Tuple <string, string> > arguments, CodeBody body) { if (inputTypeNames.Count() != arguments.Count()) { throw new ArgumentException("Creating a function -- types count vs. arguments count mismatch."); } CodeLambda lambda = null; // identity function, i.e. f(x) = x, we check only real name, if it was a dummy name, it would be if (arguments.Count() == 1) { if (arguments.Single().Item1 == body.Make().Trim()) { lambda = FunctionRegistry.IdentityFunction(lhsSymbol); } else if (arguments.Single().Item2 == body.Make().Trim()) { throw new InvalidOperationException("Somehow dummy name which should not exist was referenced."); } } if (lambda == null) { lambda = new CodeLambda(lhsSymbol, arguments.SyncZip(inputTypeNames) .Select(it => FuncParameter.Create(it.Item1.Item1, it.Item1.Item2, it.Item2)), outputTypeName, body); } return(functionsRegistry.Add(lambda)); }
private void substituteProductions(Dictionary <string, List <ProductionInfo> > productionsDict, ProductionInfo[] substitutes, // same LHS bool mixWithSource) { // this function is part of optimization of given production rules // we could have case, that sub production is marked and the one where there is replacement as well // in such case which marking to choose? so we don't allow substitutes to have markings if (substitutes.Any(it => it.IsMarked)) { throw new ArgumentException(); } string sub_lhs = substitutes.Select(it => it.LhsSymbol).Distinct().Single(); // making sure LHS symbol is the same Console.WriteLine("Substituting " + sub_lhs); foreach (string lhs in productionsDict.Keys.ToArray()) { var replacements = new List <ProductionInfo>(); foreach (ProductionInfo prod in productionsDict[lhs]) { // nothing to replace if (!prod.RhsSymbols.Any(it => it.SymbolName.Equals(sub_lhs))) { replacements.Add(prod); } else { // -1 -- use original symbol, >=0 -- substitute (the value is the index of substitution) IEnumerable <CycleCounter> counters = prod.RhsSymbols.ZipWithIndex() .Select(it => { bool hit = it.Item1.SymbolName.Equals(sub_lhs); return(new CycleCounter(((!hit || mixWithSource) ? -1 : 0), (hit ? substitutes.Length : 0), it.Item2)); }).ToArray(); // we have initial run only in case if we mix substitutions with original production, otherwise it pure substitution bool pass_first_as_source = mixWithSource; do { if (pass_first_as_source) { pass_first_as_source = false; if (!counters.All(it => it.Value == -1)) { throw new Exception("Oops, something wrong."); } // it is simply better to add original production instead of re-creating it from symbols // after all, for every rhs symbol we would have -1 value, meaning "use original" replacements.Add(prod); continue; } var p = new ProductionInfo(prod.Position, prod.LhsSymbol, prod.Recursive, counters.SyncZip(prod.RhsSymbols) .Select(it => it.Item1.Value == -1 ? new[] { it.Item2 } : substitutes[it.Item1.Value].RhsSymbols).Flatten(), prod.PassedMarkedWith); // if there was no action code, no point of building proxy for it if (prod.ActionCode != null) { FuncCallCode func_call = (FuncCallCode)(prod.ActionCode.Body); // do not rename those parameters which have counter == -1 IEnumerable <Tuple <FuncParameter, int>[]> parameters = null; parameters = counters.SyncZip(prod.ActionCode.Parameters) .Select(cit => cit.Item1.Value == -1 ? new[] { Tuple.Create(cit.Item2, cit.Item1.Index) } : substitutes[cit.Item1.Value].ActionCode.Parameters.Select(x => Tuple.Create(x, cit.Item1.Index)).ToArray()) .ToArray(); // only subsituted parameters are renamed Dictionary <Tuple <FuncParameter, int>, FuncParameter> param_map = FuncParameter.BuildParamMapping(parameters.Flatten()); p.ActionCode = CodeLambda.CreateProxy(lhs + "_sub__", // parameters parameters.Flatten().Select(it => param_map[it]), prod.ActionCode.ResultTypeName, functionsRegistry.Add(prod.ActionCode), // arguments counters.SyncZip(parameters) .Select(cit => cit.Item1.Value == -1 ? param_map[cit.Item2.Single()].NameAsCode() : new FuncCallCode(functionsRegistry.Add(substitutes[cit.Item1.Value].ActionCode), cit.Item2.Select(x => param_map[x].NameAsCode()))) ); } replacements.Add(p); }while (counters.Iterate()); } } productionsDict[lhs] = replacements; } }
private ProductionInfo makeBuilderCall(string lhsSymbol, RecursiveEnum recursive, AltRule alt, IEnumerable <SymbolMarked> symbolsMarked, string treeNodeName) { // add production with no code var prod_info = new ProductionInfo(alt.Position, lhsSymbol, recursive, symbolsMarked.Where(it => it.IsEnabled).Select(it => it.Symbol), alt.MarkWith); CodeBody code_body = null; if (alt.Code != null) { code_body = (alt.Code as CodeMix).BuildBody(symbolsMarked.Where(sym => sym.Symbol.ObjName != null) .Select(sym => sym.Symbol.GetCodeArgumentNames().Select(it => Tuple.Create(it, sym.IsEnabled))).Flatten()) .Trim(); string identity_function_on = null; // are we just passing one of the parameters? if (code_body.IsIdentity) { identity_function_on = code_body.IdentityIdentifier; } foreach (string var_name in code_body.GetVariables()) { SymbolMarked sym = symbolsMarked .Where(sm => sm.Symbol.GetCodeArgumentNames().Contains(var_name)) // there could be duplicates so we "prefer" enabled element .OrderBy(it => it.IsEnabled ? 0 : 1) .FirstOrDefault(); if (sym != null) { sym.IsParamUsed = true; } } var anon_args = new Dictionary <SymbolMarked, string>(); foreach (Tuple <SymbolMarked, int> sym_pair in symbolsMarked.ZipWithIndex()) { if (sym_pair.Item1.Symbol.ObjName == null) { anon_args.Add(sym_pair.Item1, code_body.RegisterNewIdentifier("_" + sym_pair.Item2)); } } IEnumerable <SymbolMarked> arg_symbols = symbolsMarked.Where(it => it.IsEnabled || it.IsParamUsed).ToList(); // build external function to run the user code string func_ref = registerLambda(lhsSymbol, arg_symbols.Select(sym => sym.Symbol.GetCodeArgumentTypes(grammar)).Flatten(), grammar.TreeNodeName, arg_symbols.Select(sym => sym.Symbol.GetCodeArgumentNames() .Select(it => Tuple.Create(it, anon_args.GetOrNull(sym)))).Flatten(), code_body); // build a lambda with call to a just built function // note that our lambda can have fewer arguments than the actual fuction // in such case we pass "nulls" for disabled arguments // we add nulls to params in order to keep track which arguments comes from which parameters IEnumerable <FuncParameter> lambda_params = arg_symbols.Where(it => it.IsEnabled) .Select(it => FuncParameter.Create(it.Symbol.ObjName, anon_args.GetOrNull(it), grammar.GetTypeNameOfSymbol(it.Symbol))).ToArray(); // if the code indicates that this is identity function, then just find out which parameter is passed along if (identity_function_on != null) { // we can fail for two reasons here: // (1) ok -- single variable we found in the code body is not a parameter, but global variable // (2) BAD -- we have case of unpacking the data, and that case so far we cannot handle // ad.2) consider such rule as // x -> (a b)+ { b }; // "a" and "b" will be handled as tuple of lists // so in entry function we will get a tuple, and then we will call actuall user action code // some "__function_13__(a,b)" which returns the "b" // so we could compute index for inner parameter (for "b" it is 1) // but we cannot compute index for outer function, because there is no index for "b" at all // there is only one parameter -- tuple -- holding "a" (in Item1) and "b" (in Item2) at the same time // so if anything we would have to introduce some combo index: // outer index --> optional unpacking index --> inner index // too much trouble for now Option <int> index = lambda_params.Select(it => it.Name) .ZipWithIndex().Where(it => it.Item1 == identity_function_on).Select(it => it.Item2).OptSingle(); if (index.HasValue) { prod_info.IdentityOuterFunctionParamIndex = index.Value; } } prod_info.ActionCode = CodeLambda.CreateProxy( lhsSymbol, // lambda arguments lambda_params, treeNodeName, func_ref, arg_symbols.Select(arg => arg.Symbol.CombinedSymbols == null // regular symbols ? new[] { new CodeBody().AddIdentifier(arg.IsEnabled ? (arg.Symbol.ObjName ?? anon_args[arg]) : CodeWords.Null) } // compound symbols, we have to use embedded atomic symbols instead now : arg.Symbol.UnpackTuple(arg.IsEnabled) ) .Flatten()); prod_info.CodeComment = alt.Code.Comment; } return(prod_info); }