lazy functions with set parameter sizes will report an error if no properly specced. Fixes #1064

This commit is contained in:
gnembon committed 2021-09-06 23:05:13 -03:00
1 parent 229d0b037b
commit 36df542072
6 files changed
+45 -12

No files matched your search

@@ -489,6 +489,39 @@ public class Expression
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
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)
{
// 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");
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
expression.addLazyFunction("volume", -1, (c, t, llv) ->
expression.addLazyFunction("volume", (c, t, llv) ->
{
CarpetContext cc = (CarpetContext)c;
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
expression.addLazyFunction("if", -1, (c, t, lv) ->
expression.addLazyFunction("if", (c, t, lv) ->
{
if ( lv.size() < 2 )
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
expression.addLazyFunction("try", -1, (c, t, lv) ->
expression.addLazyFunction("try", (c, t, lv) ->
{
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");
@@ -104,7 +104,7 @@ public class DataStructures {
});
// 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)
throw new InternalExpressionException("First argument for 'sort_key' should be a List");
@@ -207,7 +207,7 @@ public class DataStructures {
});
// lazy cause conditional typing - questionable
expression.addLazyFunction("get", -1, (c, t, lv) ->
expression.addLazyFunction("get", (c, t, lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'get' requires parameters");
@@ -235,7 +235,7 @@ public class DataStructures {
});
// same as `get`
expression.addLazyFunction("has", -1, (c, t, lv) ->
expression.addLazyFunction("has", (c, t, lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'has' requires parameters");
@@ -263,7 +263,7 @@ public class DataStructures {
});
// same as `get`
expression.addLazyFunction("put", -1, (c, t, lv) ->
expression.addLazyFunction("put", (c, t, lv) ->
{
if(lv.size()<2)
{
@@ -302,7 +302,7 @@ public class DataStructures {
});
// same as `get`
expression.addLazyFunction("delete", -1, (c, t, lv) ->
expression.addLazyFunction("delete", (c, t, lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'delete' requires parameters");
@@ -423,7 +423,7 @@ public class Sys {
});
// 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");
Value key = lv.get(0).evalValue(c);
@@ -67,7 +67,7 @@ public class Threading
});
// 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");
Value lockValue = Value.NULL;
@@ -85,7 +85,7 @@ public class Threading
});
// 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();
boolean interrupted = false;