dont be lazy refactor

This commit is contained in:
gnembon committed 2021-04-22 20:37:48 -03:00
1 parent a65d16c9ba
commit 74bd0bb81e
31 files changed
+1078 -1383

No files matched your search

+36 -24
View File
@@ -1,5 +1,6 @@
package carpet.script;
import carpet.CarpetSettings;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException;
import carpet.script.value.FunctionUnpackedArgumentsValue;
@@ -80,6 +81,36 @@ public abstract class Fluff
public boolean numParamsVaries() {
return numParams < 0;
}
public static List<Value> unpackLazy(List<LazyValue> lzargs, Context c, int contextType)
{
List<Value> args = new ArrayList<>();
for (LazyValue lv : lzargs)
{
Value arg = lv.evalValue(c, contextType);
if (arg instanceof FunctionUnpackedArgumentsValue)
args.addAll(((ListValue) arg).getItems());
else
args.add(arg);
}
return args;
}
public List<Value> unpackArgs(List<LazyValue> lzargs, Context c, int contextType)
{
List<Value> args = new ArrayList<>();
for (LazyValue lv : lzargs)
{
Value arg = lv.evalValue(c, contextType);
if (arg instanceof FunctionUnpackedArgumentsValue)
args.addAll(((ListValue) arg).getItems());
else
args.add(arg);
}
if (!numParamsVaries() && getNumParams() != args.size())
throw new InternalExpressionException("Function " + getName() + " expected " + getNumParams() + " parameters, got " + args.size());
return args;
}
}
public abstract static class AbstractFunction extends AbstractLazyFunction implements IFunction
@@ -88,29 +119,7 @@ public abstract class Fluff
super(numParams);
}
public static List<Value> unpackArgs(List<LazyValue> lzargs, Context c)
{
List<Value> args = new ArrayList<>();
for (LazyValue lv : lzargs)
{
Value arg = lv.evalValue(c, Context.CALLARGS);
if (arg instanceof FunctionUnpackedArgumentsValue)
{
args.addAll(((ListValue) arg).getItems());
}
else
{
args.add(arg);
}
}
return args;
}
public void checkArgs(int candidates)
{
if (!numParamsVaries() && getNumParams() !=candidates)
throw new InternalExpressionException("Function " + getName() + " expected " + getNumParams() + " parameters, got " + candidates);
}
@Override
public LazyValue lazyEval(Context cc, Integer type, Expression e, Tokenizer.Token t, final List<LazyValue> lazyParams)
@@ -128,8 +137,11 @@ public abstract class Fluff
private List<Value> getParams(Context c) {
if (params == null) {
// very likely needs to be dynamic, so not static like here, or remember if it was.
params = unpackArgs(lazyParams, c);
checkArgs(params.size());
params = unpackArgs(lazyParams, c, Context.NONE);
}
else
{
CarpetSettings.LOG.error("How did we get here 1");
}
return params;
}