public static object LookupHelperFuncs(CallExpr ce, Generator.Ctx ctx) { var dbConn = ce.args[0]; var dbConnName = OPs.TryAsName(dbConn, ctx); if (dbConnName == null) { throw new Generator.Exception($"Can't get DB connection name: {dbConn}"); } var pairs = ctx.GetConstant(ce.args[1]) as IList; if (pairs == null) { throw new Generator.Exception($"Can't interpet as array of constants: {ce.args[1]}"); } if (pairs.Count % 2 != 0) { throw new Generator.Exception($"Items count must be even (cause it must be array of pairs): {ce.args[1]}"); } var location = Convert.ToString(ctx.GetConstant(ce.args[2])); var defs = new List <FuncDef>(pairs.Count / 2); for (int i = 0; i < pairs.Count; i += 2) { var dbTableName = pairs[i].ToString(); var substance = pairs[i + 1].ToString(); // Dictionary loader func defs.Add(FuncDefs_Core.macroFuncImpl(ctx, // name new ConstExpr($"{dbConnName}:{dbTableName}_LoadDict"), // inputs new ArrayExpr(), // outputs new ArrayExpr(new ConstExpr($"{substance}_DICT_{location}")), // body new CallExpr(LoadCodeLookupDictRows, dbConn, new ConstExpr(dbTableName)), // null, false )); } return(defs); }
public static object SqlFuncsToText(CallExpr ce, Generator.Ctx ctx) { var set = new HashSet <string>(StringComparer.OrdinalIgnoreCase); { var arg0 = ctx.GetConstant(ce.args[0]); if (arg0 is IList lst) { foreach (var s in lst) { set.Add(Convert.ToString(s)); } } else { set.Add(Convert.ToString(arg0)); } } using (var sw = new StringWriter()) { SqlFuncsToTextImpl(ctx, sw, vi => set.Contains(vi.location)); return(sw.ToString()); } }
public static object SqlFuncsToDDL(CallExpr ce, Generator.Ctx ctx) { var locationCode = Convert.ToString(ctx.GetConstant(ce.args[0])); using (var sw = new StringWriter()) using (var drops = new StringWriter()) { SqlFuncsToDDL_Impl(ctx, sw, drops, locationCode); return(new object[] { sw.ToString(), drops.ToString() }); } }
static object solverAliasImpl(CallExpr ce, Generator.Ctx ctx) { var alias = OPs.TryAsName(ce.args[0], ctx); if (alias == null) { throw new SolverException(string.Format(ce.funcName + "(alias ...): alias must be constant or name instead of [{0}]", ce.args[0])); } var target = (ce.args.Count > 1) ? OPs.TryAsName(ce.args[1], ctx) : string.Empty; if (target == null) { throw new SolverException(string.Format(ce.funcName + "(..., target ...): target must be constant or name instead of [{0}]", ce.args[0])); } int priority = (ce.args.Count > 2) ? Convert.ToInt32(ctx.GetConstant(ce.args[2])) : 255; int i = ctx.GetOrCreateIndexOf(optionSolverAliases); var aliases = ctx[i] as SolverAliases; if (aliases == null) { aliases = new SolverAliases(); ctx[i] = aliases; } switch (ce.funcName) { case nameof(solverAlias): return(aliases.Set(alias, target, priority)); case nameof(solverAliasPush): return(aliases.Push(alias, target)); case nameof(solverAliasPop): return(aliases.Pop(alias)); default: throw new NotImplementedException(ce.funcName); } }
private Metadata(IEnumerable <NE> exprsHdr, IEnumerable <NE> exprsRow, IEnumerable <NE> exprsRes, Generator.Ctx parentCtx) { var sb = new StringBuilder(); FixSheetExpr = Expr.RecursiveModifier(FixSheetImpl); FixRowExpr = Expr.RecursiveModifier(FixRowImpl); #region generate header code ctxHdr = new Generator.Ctx(parentCtx); ctxHdr.CreateValue(HdrCellsRef.name, Generator.LazyDummy); ctxHdr.CreateValue(PrevValsRef.name, Generator.LazyDummy); foreach (var p in exprsHdr) { FCurrName = p.name; var expr = FixSheetExpr(p.expr); var val = Generator.Generate(expr, ctxHdr); ctxHdr.CreateValue(p.name, val); if (OPs.KindOf(val) == ValueKind.Const) { expr = new ConstExpr(val); } sb.AppendLine($"{p.name} = {expr}"); } sb.AppendLine(); RequiredInHdr(sSourceKey); RequiredInHdr(sSourceName); RequiredInHdr(sCondition); RequiredInHdr(sFirstDataRowNum); { if (ctxHdr.name2ndx.TryGetValue(sFileContentKind, out int i)) { FileContentKind = Convert.ToString(ctxHdr.GetConstant(sFileContentKind)); } else { FileContentKind = "XLSX"; } } { if (ctxHdr.name2ndx.TryGetValue(sFileNameExtension, out int i)) { var val = ctxHdr.GetConstant(sFileNameExtension); FileNameExtension = Utils.AsIList(val).Cast <object>().Select(Convert.ToString).ToArray(); } else { FileNameExtension = new string[] { "xls", "xlsx", "xlsm" } }; } #endregion #region generate row code ctxRow = new Generator.Ctx(ctxHdr); ctxRow.CreateValue(sDataKey, Generator.LazyDummy); // [0] - iKey ctxRow.CreateValue(sDataSubkey, Generator.LazyDummy); // [1] - iSubkey ctxRow.CreateValue(CellValsRef.name, Generator.LazyDummy); // [2] - iRowVals foreach (var p in exprsRow) { string name; if (p.name.StartsWith("~")) { // calculated name name = OPs.TryAsString(Parser.ParseToExpr(p.name.Substring(1)), ctxHdr) ?? p.name; } else { name = p.name; } FCurrName = name; int i = ctxRow.GetOrCreateIndexOf(name); var expr = FixRowExpr(p.expr); var val = Generator.Generate(expr, ctxRow); ctxRow.values[i] = val; if (OPs.KindOf(val) == ValueKind.Const) { expr = new ConstExpr(val); } sb.AppendLine($"{name} = {expr}"); } RequiredInRow(sDataKey); RequiredInRow(sDataSubkey); ctxRow.values[iRowVals] = null; ctxRow.CheckUndefinedValues(); #endregion #region generate res code if (exprsRes != null) { // copy all code from row to res ctxRes = new Generator.Ctx(ctxHdr); foreach (var p in ctxRow.name2ndx.OrderBy(p => p.Value)) { ctxRes.values[ctxRes.CreateValue(p.Key)] = ctxRow.values[p.Value]; } foreach (var p in exprsRes) { string name; if (p.name.StartsWith("~")) { // calculated name name = OPs.TryAsString(Parser.ParseToExpr(p.name.Substring(1)), ctxHdr) ?? p.name; } else { name = p.name; } FCurrName = name; int i = ctxRes.GetOrCreateIndexOf(name); var expr = FixRowExpr(p.expr); var val = Generator.Generate(expr, ctxRes); ctxRes.values[i] = val; if (OPs.KindOf(val) == ValueKind.Const) { expr = new ConstExpr(val); } } ctxRes.values[iRowVals] = null; ctxRes.CheckUndefinedValues(); } #endregion ctxHdr[HdrCellsRef.name] = null; ctxHdr[PrevValsRef.name] = null; ctxHdr.CheckUndefinedValues(); FCodeText = sb.ToString(); if (iHdrMinUsedRow > iHdrMaxUsedRow) { iHdrMinUsedRow = iHdrMaxUsedRow; } }
public static IEnumerable <Metadata> Create(TextReader txt, Generator.Ctx ctxParent) { var exprsHdr = new List <NE>(); var exprsRow = new List <NE>(); List <NE> exprsRes = null; string tmplParamName = null; bool withFileContentKind = false; bool withFileNameExtension = false; foreach (var(Section, Key, Value) in Ini.Read(txt, true)) { var expr = Parser.ParseToExpr(Value); List <NE> exprsLst = null; switch (Section) { case sHdr: if (Key.StartsWith(sTemplateParameterValuesPrefix)) { if (tmplParamName != null) { throw new Exception($"Template parameter already defined as '{tmplParamName}'"); } tmplParamName = Key; } else { switch (Key) { case sFileContentKind: withFileContentKind = true; break; case sFileNameExtension: withFileNameExtension = true; break; } } exprsLst = exprsHdr; break; case sRow: exprsLst = exprsRow; break; case sRes: if (exprsRes == null) { exprsRes = new List <NE>(); } exprsLst = exprsRes; break; default: throw new Exception($"Unknown section '{Section}' in source metadata. Expected '{sHdr}' or '{sRow}' sections."); } exprsLst.Add(new NE(Key ?? $".{exprsLst.Count}", expr)); } if (withFileContentKind && !withFileNameExtension) { throw new Exception($"{ctxParent.GetConstant(sMetadataFile)} : specified FileContentKind also requires FileNameExtension specification"); } if (tmplParamName != null) { var ctx = new Generator.Ctx(ctxParent); object tmplVals = null; Expr tmplExpr = null; for (int i = 0; i < exprsHdr.Count; i++) { var p = exprsHdr[i]; var val = Generator.Generate(p.expr, ctx); if (p.name == tmplParamName) { tmplVals = val; exprsHdr.RemoveAt(i); break; } ctx.CreateValue(p.name, val); } var tmplParamVals = tmplVals as IList; if (OPs.KindOf(tmplVals) != ValueKind.Const || tmplParamVals == null) { throw new Exception($"Can't interpret template parameter as list of constants //{tmplParamName}={tmplExpr}={tmplVals}"); } tmplParamName = tmplParamName.Substring(sTemplateParameterValuesPrefix.Length); foreach (var tmplVal in tmplParamVals) { exprsHdr.Insert(0, new NE(tmplParamName, new ConstExpr(tmplVal))); yield return(new Metadata(exprsHdr, exprsRow, exprsRes, ctxParent)); exprsHdr.RemoveAt(0); } } else { yield return(new Metadata(exprsHdr, exprsRow, exprsRes, ctxParent)); } }