/// <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); }
/// <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); }
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); }