From 36df542072e2560fecafd04ac5d106e055212159 Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 6 Sep 2021 23:05:13 -0300 Subject: [PATCH] lazy functions with set parameter sizes will report an error if no properly specced. Fixes #1064 --- src/main/java/carpet/script/Expression.java | 33 +++++++++++++++++++ .../carpet/script/api/BlockIterators.java | 4 +-- .../carpet/script/language/ControlFlow.java | 4 +-- .../script/language/DataStructures.java | 10 +++--- src/main/java/carpet/script/language/Sys.java | 2 +- .../carpet/script/language/Threading.java | 4 +-- 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 3b35939c..1e09dc02 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -489,6 +489,39 @@ public class Expression return false; } + @Override + public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List 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, 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 lazyParams) { diff --git a/src/main/java/carpet/script/api/BlockIterators.java b/src/main/java/carpet/script/api/BlockIterators.java index 0c4ce028..27c15452 100644 --- a/src/main/java/carpet/script/api/BlockIterators.java +++ b/src/main/java/carpet/script/api/BlockIterators.java @@ -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 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"); diff --git a/src/main/java/carpet/script/language/ControlFlow.java b/src/main/java/carpet/script/language/ControlFlow.java index bbb0610e..f7f32a44 100644 --- a/src/main/java/carpet/script/language/ControlFlow.java +++ b/src/main/java/carpet/script/language/ControlFlow.java @@ -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"); diff --git a/src/main/java/carpet/script/language/DataStructures.java b/src/main/java/carpet/script/language/DataStructures.java index c472278d..13bb4516 100644 --- a/src/main/java/carpet/script/language/DataStructures.java +++ b/src/main/java/carpet/script/language/DataStructures.java @@ -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"); diff --git a/src/main/java/carpet/script/language/Sys.java b/src/main/java/carpet/script/language/Sys.java index 8ecea532..c5fbb401 100644 --- a/src/main/java/carpet/script/language/Sys.java +++ b/src/main/java/carpet/script/language/Sys.java @@ -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); diff --git a/src/main/java/carpet/script/language/Threading.java b/src/main/java/carpet/script/language/Threading.java index bc2c0d09..52f3c1a5 100644 --- a/src/main/java/carpet/script/language/Threading.java +++ b/src/main/java/carpet/script/language/Threading.java @@ -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;