lazy functions with set parameter sizes will report an error if no properly specced. Fixes #1064
This commit is contained in:
1 parent
229d0b037b
commit
36df542072
6 files changed
+45
-12
No files matched your search
@@ -489,6 +489,39 @@ public class Expression
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
|
||||||
|
{
|
||||||
|
ILazyFunction.checkInterrupts();
|
||||||
|
if (num_params >= 0 && lazyParams.size() != num_params)
|
||||||
|
throw new InternalExpressionException("'"+name+"' requires "+num_params+" parameters, "+lazyParams.size()+" provided.");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return fun.apply(c, i, lazyParams);
|
||||||
|
}
|
||||||
|
catch (RuntimeException exc)
|
||||||
|
{
|
||||||
|
throw handleCodeException(c, exc, e, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLazyFunction(String name, TriFunction<Context, Context.Type, List<LazyValue>, LazyValue> fun)
|
||||||
|
{
|
||||||
|
functions.put(name, new AbstractLazyFunction(-1, name)
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean pure() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean transitive() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
|
public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class BlockIterators {
|
|||||||
public static void apply(Expression expression)
|
public static void apply(Expression expression)
|
||||||
{
|
{
|
||||||
// lazy cause of lazy expression
|
// lazy cause of lazy expression
|
||||||
expression.addLazyFunction("scan", -1, (c, t, llv) ->
|
expression.addLazyFunction("scan", (c, t, llv) ->
|
||||||
{
|
{
|
||||||
if (llv.size() < 3) throw new InternalExpressionException("'scan' needs many more arguments");
|
if (llv.size() < 3) throw new InternalExpressionException("'scan' needs many more arguments");
|
||||||
List<Value> lv = Fluff.AbstractFunction.unpackLazy(llv.subList(0, llv.size()-1), c, Context.NONE);
|
List<Value> lv = Fluff.AbstractFunction.unpackLazy(llv.subList(0, llv.size()-1), c, Context.NONE);
|
||||||
@@ -140,7 +140,7 @@ public class BlockIterators {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// must be lazy
|
// must be lazy
|
||||||
expression.addLazyFunction("volume", -1, (c, t, llv) ->
|
expression.addLazyFunction("volume", (c, t, llv) ->
|
||||||
{
|
{
|
||||||
CarpetContext cc = (CarpetContext)c;
|
CarpetContext cc = (CarpetContext)c;
|
||||||
if (llv.size() < 3) throw new InternalExpressionException("'volume' needs many more arguments");
|
if (llv.size() < 3) throw new InternalExpressionException("'volume' needs many more arguments");
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public class ControlFlow {
|
|||||||
|
|
||||||
|
|
||||||
// obvious lazy due to conditional evaluation of arguments
|
// obvious lazy due to conditional evaluation of arguments
|
||||||
expression.addLazyFunction("if", -1, (c, t, lv) ->
|
expression.addLazyFunction("if", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if ( lv.size() < 2 )
|
if ( lv.size() < 2 )
|
||||||
throw new InternalExpressionException("'if' statement needs to have at least one condition and one case");
|
throw new InternalExpressionException("'if' statement needs to have at least one condition and one case");
|
||||||
@@ -79,7 +79,7 @@ public class ControlFlow {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// needs to be lazy since execution of parameters but first one are conditional
|
// needs to be lazy since execution of parameters but first one are conditional
|
||||||
expression.addLazyFunction("try", -1, (c, t, lv) ->
|
expression.addLazyFunction("try", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if (lv.size()==0)
|
if (lv.size()==0)
|
||||||
throw new InternalExpressionException("'try' needs at least an expression block, and either a catch_epr, or a number of pairs of filters and catch_expr");
|
throw new InternalExpressionException("'try' needs at least an expression block, and either a catch_epr, or a number of pairs of filters and catch_expr");
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ public class DataStructures {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// needs lazy cause sort function is reused
|
// needs lazy cause sort function is reused
|
||||||
expression.addLazyFunction("sort_key", -1, (c, t, lv) -> //get working with iterators
|
expression.addLazyFunction("sort_key", (c, t, lv) -> //get working with iterators
|
||||||
{
|
{
|
||||||
if (lv.size() == 0)
|
if (lv.size() == 0)
|
||||||
throw new InternalExpressionException("First argument for 'sort_key' should be a List");
|
throw new InternalExpressionException("First argument for 'sort_key' should be a List");
|
||||||
@@ -207,7 +207,7 @@ public class DataStructures {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// lazy cause conditional typing - questionable
|
// lazy cause conditional typing - questionable
|
||||||
expression.addLazyFunction("get", -1, (c, t, lv) ->
|
expression.addLazyFunction("get", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if (lv.size() == 0)
|
if (lv.size() == 0)
|
||||||
throw new InternalExpressionException("'get' requires parameters");
|
throw new InternalExpressionException("'get' requires parameters");
|
||||||
@@ -235,7 +235,7 @@ public class DataStructures {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// same as `get`
|
// same as `get`
|
||||||
expression.addLazyFunction("has", -1, (c, t, lv) ->
|
expression.addLazyFunction("has", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if (lv.size() == 0)
|
if (lv.size() == 0)
|
||||||
throw new InternalExpressionException("'has' requires parameters");
|
throw new InternalExpressionException("'has' requires parameters");
|
||||||
@@ -263,7 +263,7 @@ public class DataStructures {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// same as `get`
|
// same as `get`
|
||||||
expression.addLazyFunction("put", -1, (c, t, lv) ->
|
expression.addLazyFunction("put", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if(lv.size()<2)
|
if(lv.size()<2)
|
||||||
{
|
{
|
||||||
@@ -302,7 +302,7 @@ public class DataStructures {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// same as `get`
|
// same as `get`
|
||||||
expression.addLazyFunction("delete", -1, (c, t, lv) ->
|
expression.addLazyFunction("delete", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if (lv.size() == 0)
|
if (lv.size() == 0)
|
||||||
throw new InternalExpressionException("'delete' requires parameters");
|
throw new InternalExpressionException("'delete' requires parameters");
|
||||||
|
|||||||
@@ -423,7 +423,7 @@ public class Sys {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// lazy cause default expression may not be executed if not needed
|
// lazy cause default expression may not be executed if not needed
|
||||||
expression.addLazyFunction("system_variable_get", -1, (c, t, lv) ->
|
expression.addLazyFunction("system_variable_get", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if (lv.size() == 0) throw new InternalExpressionException("'system_variable_get' expects at least a key to be fetched");
|
if (lv.size() == 0) throw new InternalExpressionException("'system_variable_get' expects at least a key to be fetched");
|
||||||
Value key = lv.get(0).evalValue(c);
|
Value key = lv.get(0).evalValue(c);
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public class Threading
|
|||||||
});
|
});
|
||||||
|
|
||||||
// lazy cause expr is evaluated in the same type
|
// lazy cause expr is evaluated in the same type
|
||||||
expression.addLazyFunction("synchronize", -1, (c, t, lv) ->
|
expression.addLazyFunction("synchronize", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
if (lv.size() == 0) throw new InternalExpressionException("'synchronize' require at least an expression to synchronize");
|
if (lv.size() == 0) throw new InternalExpressionException("'synchronize' require at least an expression to synchronize");
|
||||||
Value lockValue = Value.NULL;
|
Value lockValue = Value.NULL;
|
||||||
@@ -85,7 +85,7 @@ public class Threading
|
|||||||
});
|
});
|
||||||
|
|
||||||
// lazy since exception expression is very conditional
|
// lazy since exception expression is very conditional
|
||||||
expression.addLazyFunction("sleep", -1, (c, t, lv) ->
|
expression.addLazyFunction("sleep", (c, t, lv) ->
|
||||||
{
|
{
|
||||||
long time = lv.isEmpty()?0L:NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
long time = lv.isEmpty()?0L:NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
||||||
boolean interrupted = false;
|
boolean interrupted = false;
|
||||||
|
|||||||
Reference in new issue
Block a user