Merge branch 'master' into 1.17
# Conflicts: # src/main/java/carpet/script/api/Auxiliary.java # src/main/java/carpet/script/api/Entities.java # src/main/java/carpet/script/api/Inventories.java # src/main/java/carpet/script/api/Scoreboards.java # src/main/java/carpet/script/api/WorldAccess.java # src/main/java/carpet/script/value/FunctionValue.java # src/main/java/carpet/script/value/NBTSerializableValue.java
This commit is contained in:
commit
5e3812e1f0
35 files changed
+1250
-1416
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import carpet.CarpetSettings;
|
||||
import carpet.script.Context;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.Fluff;
|
||||
@@ -11,7 +12,6 @@ import carpet.script.exception.ContinueStatement;
|
||||
import carpet.script.exception.ExpressionException;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.exception.ReturnStatement;
|
||||
import carpet.script.exception.ThrowStatement;
|
||||
import net.minecraft.nbt.NbtString;
|
||||
import net.minecraft.nbt.NbtElement;
|
||||
|
||||
@@ -150,11 +150,11 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
return varArgs != null;
|
||||
}
|
||||
|
||||
public LazyValue callInContext(Context c, Integer type, List<LazyValue> lazyParams)
|
||||
public LazyValue callInContext(Context c, Integer type, List<Value> params)
|
||||
{
|
||||
try
|
||||
{
|
||||
return lazyEval(c, type, expression, token, lazyParams);
|
||||
return execute(c, type, expression, token, params);
|
||||
}
|
||||
catch (ExpressionException exc)
|
||||
{
|
||||
@@ -173,15 +173,50 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
|
||||
public void checkArgs(int candidates)
|
||||
{
|
||||
assertArgsOk(lazyParams, (fixedArgs) ->{
|
||||
int actual = getArguments().size();
|
||||
|
||||
if (candidates < actual)
|
||||
throw new InternalExpressionException("Function " + getPrettyString() + " requires at least " + actual + " arguments");
|
||||
if (candidates > actual && getVarArgs() == null)
|
||||
throw new InternalExpressionException("Function " + getPrettyString() + " requires " + actual + " arguments");
|
||||
}
|
||||
|
||||
public static List<Value> unpackArgs(List<LazyValue> lazyParams, Context c)
|
||||
{
|
||||
// TODO we shoudn't need that if all fuctions are not lazy really
|
||||
List<Value> params = new ArrayList<>();
|
||||
for (LazyValue lv : lazyParams)
|
||||
{
|
||||
Value param = lv.evalValue(c, Context.NONE);
|
||||
if (param instanceof FunctionUnpackedArgumentsValue)
|
||||
{
|
||||
CarpetSettings.LOG.error("How did we get here?");
|
||||
params.addAll(((ListValue) param).getItems());
|
||||
}
|
||||
else
|
||||
{
|
||||
params.add(param);
|
||||
}
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) {
|
||||
List<Value> resolvedParams = unpackArgs(lazyParams, c);
|
||||
return execute(c, type, e, t, resolvedParams);
|
||||
}
|
||||
|
||||
public LazyValue execute(Context c, Integer type, Expression e, Tokenizer.Token t, List<Value> params)
|
||||
{
|
||||
assertArgsOk(params, (fixedArgs) ->{
|
||||
if (fixedArgs) // wrong number of args for fixed args
|
||||
{
|
||||
throw new ExpressionException(c, e, t,
|
||||
"Incorrect number of arguments for function "+name+
|
||||
". Should be "+args.size()+", not "+lazyParams.size()+" like "+args
|
||||
". Should be "+args.size()+", not "+params.size()+" like "+args
|
||||
);
|
||||
}
|
||||
else // too few args for varargs
|
||||
@@ -190,7 +225,7 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
argList.add("... "+varArgs);
|
||||
throw new ExpressionException(c, e, t,
|
||||
"Incorrect number of arguments for function "+name+
|
||||
". Should be at least "+args.size()+", not "+lazyParams.size()+" like "+argList
|
||||
". Should be at least "+args.size()+", not "+params.size()+" like "+argList
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -200,15 +235,15 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
for (int i=0; i<args.size(); i++)
|
||||
{
|
||||
String arg = args.get(i);
|
||||
Value val = lazyParams.get(i).evalValue(c).reboundedTo(arg); // todo check if we need to copy that
|
||||
Value val = params.get(i).reboundedTo(arg); // todo check if we need to copy that
|
||||
newFrame.setVariable(arg, (cc, tt) -> val);
|
||||
}
|
||||
if (varArgs != null)
|
||||
{
|
||||
List<Value> extraParams = new ArrayList<>();
|
||||
for (int i = args.size(), mx = lazyParams.size(); i < mx; i++)
|
||||
for (int i = args.size(), mx = params.size(); i < mx; i++)
|
||||
{
|
||||
extraParams.add(lazyParams.get(i).evalValue(c).reboundedTo(null)); // copy by value I guess
|
||||
extraParams.add(params.get(i).reboundedTo(null)); // copy by value I guess
|
||||
}
|
||||
Value rest = ListValue.wrap(extraParams).bindTo(varArgs); // didn't we just copied that?
|
||||
newFrame.setVariable(varArgs, (cc, tt) -> rest);
|
||||
@@ -265,12 +300,6 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
feedback.accept(false);
|
||||
}
|
||||
}
|
||||
public static List<Value> resolveArgs(List<LazyValue> lzargs, Context c, Integer t)
|
||||
{
|
||||
List<Value> args = new ArrayList<>(lzargs.size());
|
||||
lzargs.forEach( v -> args.add( v.evalValue(c, t)));
|
||||
return args;
|
||||
}
|
||||
|
||||
public static List<LazyValue> lazify(List<Value> args)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user