示例#1
0
        public static string EnsureNewArray(this PfaContext ctx)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            const string name    = "hasChars";
            const string refname = "u." + name;

            if (ctx.ContainsFunc(name))
            {
                return(refname);
            }
            var arrType = Type.Array(Type.Double);

            JObject jobj = null;

            JArray elseBlock = new JArray();

            elseBlock.Add(jobj.AddReturn("let", jobj.AddReturn("halfsize",
                                                               Call(refname, Call("//", "size", 2)))));
            elseBlock.Add(jobj.AddReturn("let", jobj.AddReturn("fullsize",
                                                               Call("a.concat", "halfsize", "halfsize"))));
            elseBlock.Add(If(
                              Call("==", Call("&", "size", 1), 1),
                              Call("a.append", "fullsize", 0.0), "fullsize"));

            ctx.AddFunc(name, new JArray(Param("size", Type.Int)), arrType,
                        If(Call("==", "size", 0), jobj.AddReturn("type", arrType).AddReturn("value", new JArray()),
                           elseBlock));
            return(refname);
        }
示例#2
0
        /// <summary>
        /// A string -> bool function for determining whether a string has content.
        /// </summary>
        public static string EnsureHasChars(this PfaContext ctx)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            const string name = "hasChars";

            if (ctx.ContainsFunc(name))
            {
                return("u." + name);
            }
            ctx.AddFunc(name, new JArray(Param("str", Type.String)), Type.Bool,
                        Call(">", Call("s.len", "str"), 0));
            return("u." + name);
        }
        public BoundPfaContext(IHostEnvironment env, Schema inputSchema, HashSet <string> toDrop, bool allowSet)
        {
            Contracts.CheckValue(env, nameof(env));
            _host = env.Register(nameof(BoundPfaContext));
            _host.CheckValue(inputSchema, nameof(inputSchema));
            _host.CheckValue(toDrop, nameof(toDrop));

            Pfa            = new PfaContext(_host);
            _nameToVarName = new Dictionary <string, string>();
            _unavailable   = new HashSet <string>();
            _allowSet      = allowSet;
            SetInput(inputSchema, toDrop);
        }
示例#4
0
        /// <summary>
        /// This ensures that there is a function formatted as "count_type" (for example, "count_double"),
        /// that takes either a map or array and returns the number of items in that map or array.
        /// </summary>
        /// <param name="ctx">The context to check for the existence of this</param>
        /// <param name="itemType">The item type this will operate on</param>
        public static string EnsureCount(this PfaContext ctx, JToken itemType)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            Contracts.CheckValue(itemType, nameof(itemType));
            var name = "count_" + itemType.ToString();

            if (ctx.ContainsFunc(name))
            {
                return("u." + name);
            }
            ctx.AddFunc(name, new JArray(Param("a", Type.Vector(itemType))), Type.Int,
                        VectorCase(itemType, "a", "ma", Call("map.len", "ma"), "aa", Call("a.len", "aa")));
            return("u." + name);
        }
示例#5
0
        private static string EnsureOpCore(PfaContext ctx, string funcPrefix, string binOp, JToken itemType, JToken returnType = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            Contracts.AssertNonEmpty(funcPrefix);
            Contracts.AssertNonEmpty(binOp);
            Contracts.CheckValue(itemType, nameof(itemType));
            Contracts.CheckValueOrNull(returnType);
            returnType = returnType ?? itemType;

            var name = funcPrefix + "_" + itemType.ToString();

            if (ctx.ContainsFunc(name))
            {
                return("u." + name);
            }
            ctx.AddFunc(name, new JArray(Param("a", itemType), Param("b", itemType)), returnType, Call(binOp, "a", "b"));
            return("u." + name);
        }
示例#6
0
 public static string EnsureDiv(this PfaContext ctx, JToken itemType)
 => EnsureOpCore(ctx, "div", "/", itemType);
示例#7
0
 public static string EnsureMul(this PfaContext ctx, JToken itemType)
 => EnsureOpCore(ctx, "mul", "*", itemType);
示例#8
0
 public static string EnsureSub(this PfaContext ctx, JToken itemType)
 => EnsureOpCore(ctx, "sub", "-", itemType);
示例#9
0
 public static string EnsureAdd(this PfaContext ctx, JToken itemType)
 => EnsureOpCore(ctx, "add", "+", itemType);