Skip to content

Commit

Permalink
make conditional execution an argfunc property
Browse files Browse the repository at this point in the history
prepare to add and(), or() argfunctions
    that also do conditional execution
  • Loading branch information
molsonkiko committed Jan 6, 2024
1 parent 0651a28 commit 66c9da8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 46 deletions.
11 changes: 10 additions & 1 deletion JsonToolsNppPlugin/JSONTools/RemesPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,13 @@ JNode idxr_list_func(JNode obj, List<IndexerFunc> idxrs, int ii)
#region APPLY_ARG_FUNCTION
private JNode ApplyArgFunction(ArgFunctionWithArgs func)
{
if (func.function.conditionalExecution)
{
// need to add a CurJson after all the normal args
// so that the function will have a pointer to the current json,
// and all instances of the @ symbol can be resolved
func.args.Add(new CurJson());
}
if (func.function.maxArgs == 0)
{
// paramterless function like rand()
Expand All @@ -929,7 +936,9 @@ private JNode ApplyArgFunction(ArgFunctionWithArgs func)
{
JNode arg = func.args[ii + 1];
if (arg is CurJson) { other_callables = true; }
argsCanBeFunctions[ii] = (func.function.TypeOptions(ii + 1) & Dtype.FUNCTION) != 0;
argsCanBeFunctions[ii] = ((func.function.TypeOptions(ii + 1) & Dtype.FUNCTION) != 0)
&& !(func.function.conditionalExecution && ii == func.args.Count - 2); // if conditional execution, last arg is CurJson
// that must be called before argfunction is evaluated
other_args.Add(arg);
}
Dtype out_type = func.function.OutputType(x);
Expand Down
13 changes: 11 additions & 2 deletions JsonToolsNppPlugin/JSONTools/RemesPathFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,12 @@ public class ArgFunction
/// transforms arguments at compile time
/// </summary>
public ArgsTransform argsTransform;
/// <summary>
/// iff true, one or more of this function's arguments is evaluated after the function is called, not before<br></br>
/// This allows for things like the ifelse function imitating the ternary operator<br></br>
/// and the and() and or() functions short-circuiting the same way as the corresponding binops in Python.
/// </summary>
public bool conditionalExecution { get; private set; }

/// <summary>
/// A function whose arguments must be given in parentheses (e.g., len(x), concat(x, y), s_mul(abc, 3).<br></br>
Expand All @@ -989,6 +995,7 @@ public class ArgFunction
/// <param name="inputTypes">must have maxArgs values (the i^th value indicates the acceptable types for the i^th arg), or minArgs + 1 values if maxArgs is int.MaxValue</param>
/// <param name="isDeterministic">if false, the function outputs a random value</param>
/// <param name="argsTransform">transformations that are applied to any number of arguments at compile time</param>
/// <param name="conditionalExecution">whether the function's excution of one or more arguments is conditional on something</param>
public ArgFunction(Func<List<JNode>, JNode> function,
string name,
Dtype type,
Expand All @@ -997,7 +1004,8 @@ public ArgFunction(Func<List<JNode>, JNode> function,
bool isVectorized,
Dtype[] inputTypes,
bool isDeterministic = true,
ArgsTransform argsTransform = null)
ArgsTransform argsTransform = null,
bool conditionalExecution = false)
{
Function = function;
this.name = name;
Expand All @@ -1008,6 +1016,7 @@ public ArgFunction(Func<List<JNode>, JNode> function,
this.inputTypes = inputTypes;
this.isDeterministic = isDeterministic;
this.argsTransform = argsTransform;
this.conditionalExecution = conditionalExecution;
}

/// <summary>
Expand Down Expand Up @@ -3540,7 +3549,7 @@ public static JNode ObjectsToJNode(object obj)
["abs"] = new ArgFunction(Abs, "abs", Dtype.FLOAT_OR_INT, 1, 1, true, new Dtype[] {Dtype.FLOAT_OR_INT | Dtype.ITERABLE}),
["bool"] = new ArgFunction(ToBool, "bool", Dtype.BOOL, 1, 1, true, new Dtype[] { Dtype.ANYTHING }),
["float"] = new ArgFunction(ToFloat, "float", Dtype.FLOAT, 1, 1, true, new Dtype[] { Dtype.ANYTHING}),
["ifelse"] = new ArgFunction(IfElse, "ifelse", Dtype.UNKNOWN, 3, 4, true, new Dtype[] {Dtype.ANYTHING, Dtype.ANYTHING | Dtype.FUNCTION, Dtype.ANYTHING | Dtype.FUNCTION, Dtype.ANYTHING}, argsTransform: new ArgsTransform((3, Dtype.ANYTHING, _ => new CurJson()))),
["ifelse"] = new ArgFunction(IfElse, "ifelse", Dtype.UNKNOWN, 3, 3, true, new Dtype[] {Dtype.ANYTHING, Dtype.ANYTHING | Dtype.FUNCTION, Dtype.ANYTHING | Dtype.FUNCTION}, conditionalExecution: true),
["int"] = new ArgFunction(ToInt, "int", Dtype.INT, 1, 1, true, new Dtype[] {Dtype.ANYTHING}),
["is_expr"] = new ArgFunction(IsExpr, "is_expr", Dtype.BOOL, 1, 1, true, new Dtype[] {Dtype.ANYTHING}),
["is_num"] = new ArgFunction(IsNum, "is_num", Dtype.BOOL, 1, 1, true, new Dtype[] {Dtype.ANYTHING}),
Expand Down
4 changes: 2 additions & 2 deletions JsonToolsNppPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("6.1.1.4")]
[assembly: AssemblyFileVersion("6.1.1.4")]
[assembly: AssemblyVersion("6.1.1.5")]
[assembly: AssemblyFileVersion("6.1.1.5")]
Loading

0 comments on commit 66c9da8

Please sign in to comment.