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 COLUMN(CallExpr ce, Generator.Ctx ctx) { var cellName = OPs.TryAsName(ce.args[0], ctx); if (cellName == null) { throw new Generator.Exception($"COLUMN(cellName): can't get cell name from expession // {ce.args[0]}"); } int i = 0; if (CellRange.ParseCellName(cellName, ref i, cellName.Length, out int row, out int col)) { return(col); } throw new Generator.Exception($"COLUMN(cellName): can't parse given cell name // {cellName}"); }
Expr SpecFuncs(CallExpr ce) { Expr src, ndx; bool forPrev = false, forCell = false; int nMinArgs = 1; switch (ce.funcName) { case "PREVCELL": forCell = true; forPrev = true; break; case "PREV": nMinArgs = 0; forPrev = true; break; case "INDIRECT": forCell = true; break; case nameof(FuncDefs_Xlsx.COLUMN): // avoid COLUMN(args) fixing return(new CallExpr(nameof(FuncDefs_Xlsx.COLUMN), ce.args)); default: return(ce); } if (ce.args.Count < nMinArgs) { return(ce); } Expr arg; string name; if (ce.args.Count == 0) { arg = null; name = FCurrName; } else { arg = ce.args[0]; if (arg.nodeType == ExprType.Reference) { name = ((ReferenceExpr)arg).name; if (IsColumnName(name) || !forPrev && IsFullRelativeCell(CellRange.FromName(name))) { forCell = true; } else { name = OPs.TryAsString(arg, ctxRow) ?? name; } } else { arg = FixRowExpr(arg); name = OPs.TryAsName(arg, ctxRow); } } if (name == null) { var undefs = string.Join(", ", ctxHdr.NamesOfUndefinedValues().Concat(ctxRow.NamesOfUndefinedValues())); throw new Source.Exception($"Constant expected as value of {ce.args[0]}={arg} // ??? {undefs}"); } if (forCell || IsColumnName(name) || !forPrev && IsFullRelativeCell(CellRange.FromName(name))) { if (forPrev) { src = new IndexExpr(PrevValsRef, new ConstExpr(ctxRow.IndexOf(CellValsRef.name))); } else { var re1 = new ReferenceExpr(name); var re2 = FixHdrCellRef(re1); if (re2 != re1) { // ref to header cell return(re2); } else { // ref to row cell src = CellValsRef; } } ndx = new ConstExpr(name); } else { int i = ctxRow.IndexOf(name); if (i < 0 || Generator.Ctx.ctxDepthStep <= i) { throw new Source.Exception($"Value named '{name}' not found in row context"); } src = PrevValsRef; ndx = new ConstExpr(i); } return(new IndexExpr(src, ndx)); }