Exemple #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> 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);
        }
Exemple #2
0
            public ModifyHelper()
            {
                modify = Expr.RecursiveModifier(e =>
                {
                    if (e.nodeType == ExprType.Reference || e.nodeType == ExprType.Constant)
                    {
                        return(e);
                    }
                    if (e.nodeType != ExprType.Call)
                    {
                        goto tryConst;
                    }
                    var ce     = (CallExpr)e;
                    bool isLet = ce.funcName == "let";
                    bool isDef = isLet || ce.funcName == "SV" || ce.funcName == "SSV";
                    if (!isDef && ce.funcName != "GV" && ce.funcName != "GSV")
                    {
                        goto tryConst;
                    }
                    var arg0 = ce.args[0];
                    string name;
                    if (arg0.nodeType == ExprType.Constant)
                    {
                        name = Convert.ToString((arg0 as ConstExpr).value);
                        if (!isLet && ce.args.Count == 1)
                        {
                            e = new ReferenceExpr(name);
                        }
                        goto constResolved;
                    }
                    if (arg0.nodeType == ExprType.Reference)
                    {
                        name = (arg0 as ReferenceExpr).name;
                        goto nameResolved;
                    }
                    // try to resolve name
                    var nameObj = TryGen(ctx, arg0);
                    if (nameObj == Fail || OPs.KindOf(nameObj) != ValueKind.Const)
                    {
                        thereIsNonresolved = true;
                        return(e);
                    }
                    name = Convert.ToString(nameObj);
                    thereIsNameResolve = true;
                    constResolved:
                    var args = new Expr[ce.args.Count];
                    if (isLet)
                    {
                        args[0] = new ReferenceExpr(name);
                    }
                    else
                    {
                        if (ce.args.Count == 1 && ce.funcName != "GSV")
                        {
                            return(new ReferenceExpr(name));
                        }
                        args[0] = new ConstExpr(name);
                    }
                    for (int i = 1; i < args.Length; i++)
                    {
                        args[i] = modify(ce.args[i]);
                    }
                    e = new CallExpr(ce.funcName, args);
                    nameResolved:
                    if (isDef && ce.args.Count == 2)
                    {
                        lets.Add(name);
                        //var value = modify(ce.args[1]);
                        //if (value.nodeType == ExprType.Constant)
                        //{ }
                    }
                    return(e);

                    tryConst:
                    var val = TryGen(ctx, e);
                    if (val == Fail || OPs.KindOf(val) != ValueKind.Const)
                    {
                        return(e);
                    }
                    return(new ConstExpr(val));
                });
            }
Exemple #3
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));
        }
Exemple #4
0
        static Dictionary <TAttr, object> ParseAttrs <TAttr>(Generator.Ctx ctx, IEnumerable <string> comments,
                                                             params TAttr[] firstLineDefaultKeys
                                                             )
            where TAttr : struct
        {
            bool firstLine = true;
            var  lstDescr  = new List <string>();
            Dictionary <TAttr, object> attrs = null;
            int iDefaultKey = (firstLineDefaultKeys.Length > 0) ? 0 : -1;

            foreach (var txt in comments)
            {
                bool isKeyValueComment;
                {
                    int iEqualSign = txt.IndexOf('=');
                    if (iEqualSign > 0)
                    {
                        isKeyValueComment = !Enumerable.Range(0, iEqualSign - 1).Any(i => char.IsWhiteSpace(txt[i]));
                    }
                    else
                    {
                        isKeyValueComment = false;
                    }
                }
                if ((firstLineDefaultKeys.Length == 0 || !firstLine) && !isKeyValueComment)
                {
                    if (txt.Trim().Length == 0)
                    {
                        lstDescr.Clear();
                    }
                    else
                    {
                        lstDescr.Add(txt);
                    }
                    continue; // do not parse simple comment lines
                }
                foreach (Expr attr in Parser.ParseSequence(txt, comparison: StringComparison.InvariantCulture))
                {
                    string attrName;
                    Expr   attrValue;
                    if (attr.nodeType == ExprType.Equal)
                    {   // named attribute
                        var be = (BinaryExpr)attr;
                        if (be.left.nodeType == ExprType.Reference)
                        {
                            attrName = Convert.ToString(be.left);
                        }
                        else
                        {
                            var name = Generator.Generate(be.left, ctx);
                            if (OPs.KindOf(name) != ValueKind.Const)
                            {
                                ctx.Error("ParseAttrs: attribute name must be constant\t" + be.left.ToString());
                            }
                            attrName = Convert.ToString(name);
                        }
                        attrValue = be.right;
                    }
                    else if (firstLine && iDefaultKey >= 0)
                    {   // unnamed attributes possible in first line
                        attrs = attrs ?? new Dictionary <TAttr, object>();
                        attrs.Add(firstLineDefaultKeys[iDefaultKey], attr);
                        if (++iDefaultKey >= firstLineDefaultKeys.Length)
                        {
                            iDefaultKey = -1;
                        }
                        continue;
                    }
                    else
                    {
                        break;  // it is simple comment to the end of line?
                    }
                    if (attrValue.nodeType != ExprType.Constant)
                    {
                        attrValue = new CallExpr(FuncDefs_Core._block, attrValue);
                    }
                    var value = Generator.Generate(attrValue, ctx);
                    if (OPs.KindOf(value) != ValueKind.Const)
                    {
                        ctx.Error(string.Format("ParseAttrs: attribute value must be constant\t{0}={1}", attrName, attrValue));
                    }
                    if (attrName != null)
                    {
                        if (!Enum.TryParse <TAttr>(attrName, out var attrKey))
                        {
                            throw new Generator.Exception($"Unrecognized attribute name, {attrName}={attrValue} // enum {typeof(TAttr)}");
                        }
                        attrs = attrs ?? new Dictionary <TAttr, object>();
                        Attr.Add(attrs, attrKey, value, false);
                    }
                }
                firstLine = false;
            }
            if (lstDescr.Count > 0)
            {
                attrs = attrs ?? new Dictionary <TAttr, object>();
                if (!Enum.TryParse <TAttr>(nameof(Attr.Tbl.Description), out var attrDescription))
                {
                    throw new Generator.Exception($"ParseAttrs: enum '{typeof(TAttr)}' does not contains value named '{nameof(Attr.Tbl.Description)}'");
                }
                attrs.Add(attrDescription, lstDescr);
            }
            return(attrs);
        }
Exemple #5
0
            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;
                }
            }
Exemple #6
0
            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));
                }
            }