예제 #1
0
        /// <summary>
        /// Iterative topological sorting of expressions (ability of calculated values names is taked into account)
        /// </summary>
        /// <param name="srcItems"></param>
        /// <param name="parentCtx"></param>
        /// <returns></returns>
        public static IEnumerable <Expr> DoSort(IEnumerable <Expr> srcItems, Generator.Ctx parentCtx)
        {
            List <ItemInfo> infos = srcItems.Select(item => new ItemInfo(item)).ToList();

            while (true)
            {
                //****** sort items with defined names
                var allLets = new Dictionary <string, ItemInfo>();

                foreach (var nfo in infos)
                {
                    foreach (var name in nfo.lets.Keys)
                    {
                        allLets[name] = nfo;
                    }
                }

                infos = TopoSort.DoSort(infos, ii => ii.refs.Keys.Where(s => allLets.ContainsKey(s)).Select(s => allLets[s]));

                var thereIsNameResolve = false;
                var allNamesRosolved   = true;
                var ctx = new Generator.Ctx(parentCtx);

                //****** calculate unresolved names
                for (int i = 0; i < infos.Count; i++)
                {
                    var nfo = infos[i];
                    // side effect?: new values can be declared in context
                    var value = TryGen(ctx, nfo.expr);
                    var lets2 = nfo.lets2;
                    for (int j = 0; j < lets2.Count;)
                    {
                        var let2    = lets2[j];
                        var letName = TryGen(ctx, let2);
                        if (letName != Fail && OPs.KindOf(letName) == ValueKind.Const)
                        {
                            thereIsNameResolve = true;
                            lets2.RemoveAt(j);
                            nfo.lets[Convert.ToString(letName)] = true;
                        }
                        else
                        {
                            j++; allNamesRosolved = false;
                        }
                    }
                    var refs2 = nfo.refs2;
                    for (int j = 0; j < refs2.Count;)
                    {
                        var ref2    = refs2[j];
                        var refName = TryGen(ctx, ref2);
                        if (refName != Fail && OPs.KindOf(refName) == ValueKind.Const)
                        {
                            thereIsNameResolve = true;
                            refs2.RemoveAt(j);
                            nfo.refs[Convert.ToString(refName)] = true;
                        }
                        else
                        {
                            j++; allNamesRosolved = false;
                        }
                    }
                    if (nfo.refs.Keys.Any(s => ctx.IndexOf(s) < 0))
                    {
                        allNamesRosolved = false;
                    }
                }

                if (allNamesRosolved || !thereIsNameResolve)
                {
                    break;
                }
            }
            return(infos.Select(ii => ii.expr));
        }
예제 #2
0
        /// <summary>
        /// Iterative topological sorting of expressions (ability of calculated values names is taked into account)
        /// </summary>
        /// <param name="srcItems"></param>
        /// <param name="parentCtx"></param>
        /// <returns></returns>
        public static IEnumerable <Expr> DoSort2(IEnumerable <Expr> srcItems, Generator.Ctx parentCtx)
        {
            var items = new List <Expr>(srcItems);

            var mh      = new ModifyHelper();
            var allLets = new Dictionary <string, Expr>();

            while (true)
            {
                mh.ctx = new Generator.Ctx(parentCtx);
                mh.ctx.CreateValue(FuncDefs_Core.stateTryGetConst, string.Empty);

                for (int i = 0; i < items.Count; i++)
                {
                    var item = items[i];
                    item     = mh.modify(item);
                    items[i] = item;
                    foreach (var letName in mh.lets)
                    {
                        allLets[letName] = item;
                        //mh.ctx.CreateValue(letName);
                    }
                    mh.lets.Clear();
                }

                {
                    var values = mh.ctx.values;
                    var lets   = new List <Expr>();
                    foreach (var p in mh.ctx.name2ndx)
                    {
                        if (p.Value == 0)
                        {
                            continue;
                        }
                        var v = values[p.Value];
                        if (v == null || allLets.ContainsKey(p.Key) || OPs.KindOf(v) != ValueKind.Const)
                        {
                            continue;
                        }
                        var le = CallExpr.let(new ReferenceExpr(p.Key), new ConstExpr(v));
                        allLets.Add(p.Key, le);
                        lets.Add(le);
                    }
                    if (lets.Count > 0)
                    {
                        items.InsertRange(0, lets);
                    }
                }

                if (!mh.thereIsNameResolve && !mh.thereIsNonresolved)
                {
                    break;
                }

                mh.thereIsNonresolved = mh.thereIsNameResolve = false;

                items = TopoSort.DoSort(items,
                                        e => EnumRefs(e)
                                        .Where(s => s != null && allLets.ContainsKey(s))
                                        .Select(s => allLets[s])
                                        );

                allLets.Clear();
            }

            return(items);
        }
예제 #3
0
            public static string DumpDeps(Generator.Ctx parentCtx,
                                          Dictionary <string, XlsxR.Sheet> sheets, string[] stringTable, string[] forCells)
            {
                var sb = new StringBuilder();

                foreach (var sheetCells in forCells.Select(SheetAndCell).GroupBy(sc => sc.Sheet))
                {
                    var sheetKey = (sheetCells.Key == null) ? sheets.Keys.First() : sheetCells.Key;
                    var hlp      = new DepsHelper()
                    {
                        already     = new Dictionary <string, bool>(),
                        sheet       = sheets[sheetKey],
                        stringTable = stringTable,
                        parentCtx   = parentCtx,
                    };
                    foreach (var r in hlp.sheet.vals.Keys)
                    {
                        if (hlp.maxNdxColVal < r.col)
                        {
                            hlp.maxNdxColVal = r.col;
                        }
                        if (hlp.maxNdxRowVal < r.row)
                        {
                            hlp.maxNdxRowVal = r.row;
                        }
                    }

                    var deps = new List <(CellRange cell, Expr expr)>();
                    foreach (var sc in sheetCells)
                    {
                        deps.AddRange(hlp.DumpDepsImpl(sc.Cell));
                    }

                    deps.Sort((a, b) => CellRange.Compare(a.cell, b.cell));

                    var dict = deps.ToDictionary(d => d.cell.name, d => d.expr);

                    var sorted = TopoSort.DoSort(
                        deps.Select(d => d.cell.name),
                        cell => dict[cell].EnumerateReferences()
                        );

                    if (sb.Length > 0)
                    {
                        sb.AppendLine();
                    }
                    sb.AppendLine($"//********** sheet: {sheetKey}");

                    foreach (var cell in sorted)
                    {
                        var info = hlp.GetCode(CellRange.FromName(cell), dict[cell]);
                        if (!string.IsNullOrEmpty(info.rowComm))
                        {
                            sb.AppendLine($"//row: {info.rowComm}");
                        }
                        if (!string.IsNullOrEmpty(info.colComm))
                        {
                            sb.AppendLine($"//col: {info.colComm}");
                        }
                        sb.AppendLine(info.rngExpr);
                    }
                }
                return(sb.ToString());
            }