Exemplo n.º 1
0
        private bool TryGetNaturalSetting(string setting, Configuration config, List <Flag> flags, int defValue, ref int value, out Cnst cnstVal)
        {
            cnstVal = null;
            if (Module == null || CollectionName == null || InstanceName == null)
            {
                value = defValue;
                return(true);
            }

            if (!config.TryGetSetting(CollectionName, InstanceName, setting, out cnstVal))
            {
                value = defValue;
                return(true);
            }
            else if (cnstVal.CnstKind != CnstKind.Numeric)
            {
                flags.Add(new Flag(
                              SeverityKind.Error,
                              cnstVal,
                              Constants.BadSetting.ToString(setting, cnstVal.Raw, "Expected a small non-negative integer"),
                              Constants.BadSetting.Code));
                return(false);
            }

            var r = cnstVal.GetNumericValue();

            if (r.Sign < 0 || !r.IsInteger || r.Numerator >= int.MaxValue)
            {
                flags.Add(new Flag(
                              SeverityKind.Error,
                              cnstVal,
                              Constants.BadSetting.ToString(setting, cnstVal.Raw, "Expected a small non-negative integer"),
                              Constants.BadSetting.Code));
                return(false);
            }

            defValue = (int)r.Numerator;
            return(true);
        }
Exemplo n.º 2
0
        string RenderExpr(Node n)
        {
            if (n.NodeKind == NodeKind.Cnst)
            {
                Cnst cnst = (Cnst)n;
                if (cnst.CnstKind == CnstKind.Numeric)
                {
                    return(cnst.GetNumericValue().ToString());
                }
                else
                {
                    return(cnst.GetStringValue());
                }
            }
            else if (n.NodeKind == NodeKind.Id)
            {
                Id id = (Id)n;
                if (id.Name == "TRUE")
                {
                    return("true");
                }
                else if (id.Name == "FALSE")
                {
                    return("false");
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else
            {
                FuncTerm ft           = (FuncTerm)n;
                string   functionName = ((Id)ft.Function).Name;
                if (functionName == "Identifier")
                {
                    using (var enumerator = ft.Args.GetEnumerator())
                    {
                        enumerator.MoveNext();
                        return(((Cnst)enumerator.Current).GetStringValue());
                    }
                }
                else if (functionName == "Call")
                {
                    List <string> args = new List <string>();
                    using (var enumerator = ft.Args.GetEnumerator())
                    {
                        enumerator.MoveNext();
                        RenderArgs((FuncTerm)enumerator.Current, args);
                    }
                    string ret   = args[0] + "(";
                    bool   first = true;
                    for (int i = 1; i < args.Count; i++)
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            ret = ret + ", ";
                        }
                        ret = ret + args[i];
                    }
                    return(ret + ")");
                }
                else if (functionName == "New")
                {
                    using (var enumerator = ft.Args.GetEnumerator())
                    {
                        enumerator.MoveNext();
                        Cnst type = (Cnst)enumerator.Current;
                        enumerator.MoveNext();
                        Node sizeExpr = enumerator.Current;
                        if (sizeExpr.NodeKind == NodeKind.Id && ((Id)sizeExpr).Name == "NIL")
                        {
                            return(string.Format("new {0}", type.GetStringValue()));
                        }
                        else
                        {
                            return(string.Format("new {0}[{1}]", type.GetStringValue(), RenderExpr(sizeExpr)));
                        }
                    }
                }
                else
                {
                    using (var enumerator = ft.Args.GetEnumerator())
                    {
                        enumerator.MoveNext();
                        string        opName = ((Id)enumerator.Current).Name;
                        List <string> args   = new List <string>();
                        enumerator.MoveNext();
                        RenderArgs((FuncTerm)enumerator.Current, args);
                        switch (opName)
                        {
                        case "NOT": return(string.Format("!{0}", args[0]));

                        case "NEG": return(string.Format("-{0}", args[0]));

                        case "ADD": return(string.Format("({0} + {1})", args[0], args[1]));

                        case "SUB": return(string.Format("({0} - {1})", args[0], args[1]));

                        case "MUL": return(string.Format("({0} * {1})", args[0], args[1]));

                        case "INTDIV": return(string.Format("({0} / {1})", args[0], args[1]));

                        case "AND": return(string.Format("({0} && {1})", args[0], args[1]));

                        case "OR": return(string.Format("({0} || {1})", args[0], args[1]));

                        case "EQ": return(string.Format("({0} == {1})", args[0], args[1]));

                        case "NEQ": return(string.Format("({0} != {1})", args[0], args[1]));

                        case "LT": return(string.Format("({0} < {1})", args[0], args[1]));

                        case "LE": return(string.Format("({0} <= {1})", args[0], args[1]));

                        case "GT": return(string.Format("({0} > {1})", args[0], args[1]));

                        case "GE": return(string.Format("({0} >= {1})", args[0], args[1]));

                        case "DOT": return(string.Format("{0}.{1}", args[0], args[1]));

                        case "IN": return(string.Format("({0} in {1})", args[0], args[1]));

                        case "INDEX": return(string.Format("{0}[{1}]", args[0], args[1]));

                        default: throw new NotImplementedException();
                        }
                    }
                }
            }
        }