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); }
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)); }
public static CodeLambda IdentityFunction(string lhs) { // we are creating effectivelly singleton identity function, not 100% though, because // in case of struct instances it would lead to boxing/unboxing, BUT... TREE_NODE in NLT has to be "class" return(new CodeLambda(lhs, new[] { FuncParameter.Create("x", "object", dummy: false) }, "object", new CodeBody().AddIdentifier("x"))); }
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); }