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

@@ -1208,7 +1208,7 @@ public class EntityValue extends Value
else if (v instanceof ListValue)
{
List<Value> lv = ((ListValue) v).getItems();
Vector3Argument locator = Vector3Argument.findIn(lv, 0, false);
Vector3Argument locator = Vector3Argument.findIn(lv, 0, false, false);
pos = new BlockPos(locator.vec.x, locator.vec.y, locator.vec.z);
if (lv.size() > locator.offset)
{
@@ -1237,7 +1237,7 @@ public class EntityValue extends Value
else if (a instanceof ListValue)
{
List<Value> params= ((ListValue) a).getItems();
Vector3Argument blockLocator = Vector3Argument.findIn(params, 0, false);
Vector3Argument blockLocator = Vector3Argument.findIn(params, 0, false, false);
BlockPos pos = new BlockPos(blockLocator.vec);
RegistryKey<World> world = spe.getEntityWorld().getRegistryKey();
float angle = spe.getHeadYaw();
@@ -5,10 +5,12 @@ import net.minecraft.nbt.Tag;
public class FunctionAnnotationValue extends Value
{
// 0 global
// 1 vararg
public int type;
public FunctionAnnotationValue(Value variable, int type)
public enum Type
{
GLOBAL, VARARG
}
public Type type;
public FunctionAnnotationValue(Value variable, Type type)
{
if (variable.boundVariable == null)
throw new InternalExpressionException("You can only borrow variables from the outer scope");
@@ -1,5 +1,6 @@
package carpet.script.value;
import carpet.CarpetSettings;
import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.Fluff;
@@ -149,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> args)
{
try
{
return lazyEval(c, type, expression, token, lazyParams);
return execute(c, type, expression, token, args);
}
catch (ExpressionException exc)
{
@@ -182,10 +183,34 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
throw new InternalExpressionException("Function " + getPrettyString() + " requires " + actual + " arguments");
}
public static List<Value> unpackArgs(List<LazyValue> lzargs, Context c)
{
// TODO we shoudn't need that if all fuctions are not lazy really
List<Value> args = new ArrayList<>();
for (LazyValue lv : lzargs)
{
Value arg = lv.evalValue(c, Context.NONE);
if (arg instanceof FunctionUnpackedArgumentsValue)
{
CarpetSettings.LOG.error("How did we get here?");
args.addAll(((ListValue) arg).getItems());
}
else
{
args.add(arg);
}
}
return args;
}
@Override
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyPrams)
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) {
List<Value> resolvedArgs = unpackArgs(lazyParams, c);
return execute(c, type, e, t, resolvedArgs);
}
public LazyValue execute(Context c, Integer type, Expression e, Tokenizer.Token t, List<Value> resolvedArgs)
{
List<Value> resolvedArgs = Fluff.AbstractFunction.unpackArgs(lazyPrams, c);
assertArgsOk(resolvedArgs, (fixedArgs) ->{
if (fixedArgs) // wrong number of args for fixed args
{
@@ -275,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)
{
@@ -4,6 +4,7 @@ public class LContainerValue extends FrameworkValue
{
private ContainerValueInterface container;
private Value address;
public static final LContainerValue NULL_CONTAINER = new LContainerValue(null, null);
public LContainerValue(ContainerValueInterface c, Value v)
{
@@ -81,6 +81,12 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
owned = true;
}
public static Value of(Tag tag)
{
if (tag == null) return Value.NULL;
return new NBTSerializableValue(tag);
}
public NBTSerializableValue(Supplier<Tag> tagSupplier)
{
nbtSupplier = tagSupplier;
@@ -178,29 +184,29 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
return inventory;
}
public static InventoryLocator locateInventory(CarpetContext c, List<LazyValue> params, int offset)
public static InventoryLocator locateInventory(CarpetContext c, List<Value> params, int offset)
{
try
{
Value v1 = params.get(offset).evalValue(c);
Value v1 = params.get(offset);
if (v1.isNull())
{
offset ++;
v1 = params.get(offset).evalValue(c);
v1 = params.get(offset);
}
else if (v1 instanceof StringValue)
{
String strVal = v1.getString().toLowerCase(Locale.ROOT);
if (strVal.equals("enderchest"))
{
Value v2 = params.get(1 + offset).evalValue(c);
Value v2 = params.get(1 + offset);
ServerPlayerEntity player = EntityValue.getPlayerByValue(c.s.getMinecraftServer(), v2);
if (player == null) throw new InternalExpressionException("enderchest inventory requires player argument");
return new InventoryLocator(player, player.getBlockPos(), player.getEnderChestInventory(), offset + 2, true);
}
if (strVal.equals("equipment"))
{
Value v2 = params.get(1 + offset).evalValue(c);
Value v2 = params.get(1 + offset);
if (!(v2 instanceof EntityValue)) throw new InternalExpressionException("Equipment inventory requires a living entity argument");
Entity e = ((EntityValue) v2).getEntity();
if (!(e instanceof LivingEntity)) throw new InternalExpressionException("Equipment inventory requires a living entity argument");
@@ -256,8 +262,8 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
}
BlockPos pos = new BlockPos(
NumericValue.asNumber(v1).getDouble(),
NumericValue.asNumber(params.get(1 + offset).evalValue(c)).getDouble(),
NumericValue.asNumber(params.get(2 + offset).evalValue(c)).getDouble());
NumericValue.asNumber(params.get(1 + offset)).getDouble(),
NumericValue.asNumber(params.get(2 + offset)).getDouble());
Inventory inv = getInventoryAt(c.s.getWorld(), pos);
if (inv == null)
return null;
@@ -35,7 +35,7 @@ public class ThreadValue extends Value
{
try
{
return function.lazyEval(ctx, Context.NONE, expr, token, FunctionValue.lazify(args)).evalValue(ctx);
return function.execute(ctx, Context.NONE, expr, token, args).evalValue(ctx);
}
catch (ExitStatement exit)
{