list tweaks

This commit is contained in:
gnembon committed 2019-07-20 22:52:32 -03:00
1 parent 6b24d7e93f
commit 6e4de969c8
9 files changed
+124 -67

No files matched your search

@@ -47,7 +47,8 @@ public class ServerPlayNetworkHandler_scarpetEventsMixin
)) ))
private void onClicked(PlayerActionC2SPacket packet, CallbackInfo ci) private void onClicked(PlayerActionC2SPacket packet, CallbackInfo ci)
{ {
CarpetServer.scriptServer.events.onBlockClicked(player, packet.getPos(), packet.getDirection()); if (packet.getAction() == PlayerActionC2SPacket.Action.START_DESTROY_BLOCK)
CarpetServer.scriptServer.events.onBlockClicked(player, packet.getPos(), packet.getDirection());
} }
@Inject(method = "onPlayerInteractBlock", at = @At( @Inject(method = "onPlayerInteractBlock", at = @At(
@@ -1977,9 +1977,7 @@ public class CarpetExpression
int y; int y;
int z; int z;
{ {
x = minx; reset();
y = miny;
z = minz;
} }
@Override @Override
public boolean hasNext() public boolean hasNext()
@@ -2012,6 +2010,15 @@ public class CarpetExpression
public void fatality() public void fatality()
{ {
// possibly return original x, y, z // possibly return original x, y, z
super.fatality();
}
@Override
public void reset()
{
x = minx;
y = miny;
z = minz;
} }
}; };
}); });
@@ -2066,10 +2073,10 @@ public class CarpetExpression
{ {
return (c_, t_) -> new LazyListValue() return (c_, t_) -> new LazyListValue()
{ {
int curradius = 0; int curradius;
int curpos = 0; int curpos;
{ {
reset();
} }
@Override @Override
public boolean hasNext() public boolean hasNext()
@@ -2097,15 +2104,25 @@ public class CarpetExpression
return block; return block;
} }
@Override
public void reset()
{
curradius = 0;
curpos = 0;
}
}; };
} }
else else
{ {
return (c_, t_) -> new LazyListValue() return (c_, t_) -> new LazyListValue()
{ {
int curradius = 0; int curradius;
int curpos = 0; int curpos;
int curheight = -height; int curheight;
{
reset();
}
@Override @Override
public boolean hasNext() public boolean hasNext()
{ {
@@ -2142,6 +2159,14 @@ public class CarpetExpression
} }
return block; return block;
} }
@Override
public void reset()
{
curradius = 0;
curpos = 0;
curheight = -height;
}
}; };
} }
}); });
+26 -25
View File
@@ -12,6 +12,7 @@ import carpet.script.Fluff.SexFunction;
import carpet.script.Fluff.TriFunction; import carpet.script.Fluff.TriFunction;
import carpet.script.exception.ExpressionException; import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.value.AbstractListValue;
import carpet.script.value.FunctionSignatureValue; import carpet.script.value.FunctionSignatureValue;
import carpet.script.value.GlobalValue; import carpet.script.value.GlobalValue;
import carpet.script.value.LazyListValue; import carpet.script.value.LazyListValue;
@@ -1857,12 +1858,12 @@ public class Expression implements Cloneable
return LazyListValue.range(from, to, step); return LazyListValue.range(from, to, step);
}); });
addBinaryFunction("get", (v1, v2) -> v1.getElementAt(v2)); addBinaryFunction("get", Value::getElementAt);
//Deprecated, use "get" instead //Deprecated, use "get" instead
addBinaryFunction("element", (v1, v2) -> addBinaryFunction("element", (v1, v2) ->
{ {
if (v1 instanceof LazyListValue || !(v1 instanceof ListValue)) if (!(v1 instanceof ListValue))
throw new InternalExpressionException("First argument of get should be a list"); throw new InternalExpressionException("First argument of get should be a list");
List<Value> items = ((ListValue)v1).getItems(); List<Value> items = ((ListValue)v1).getItems();
long index = NumericValue.asNumber(v2).getLong(); long index = NumericValue.asNumber(v2).getLong();
@@ -1880,7 +1881,7 @@ public class Expression implements Cloneable
throw new InternalExpressionException("put takes at least three arguments, a list, index, and values to insert at that index"); throw new InternalExpressionException("put takes at least three arguments, a list, index, and values to insert at that index");
} }
Value list = lv.get(0); Value list = lv.get(0);
if (list instanceof LazyListValue || !(list instanceof ListValue)) if (!(list instanceof ListValue))
{ {
throw new InternalExpressionException("First argument of element should be a list"); throw new InternalExpressionException("First argument of element should be a list");
} }
@@ -1965,9 +1966,9 @@ public class Expression implements Cloneable
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c);
if (!(rval instanceof ListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of map function should be a list or iterator"); throw new InternalExpressionException("First argument of map function should be a list or iterator");
Iterator<Value> iterator = ((ListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
LazyValue expr = lv.get(1); LazyValue expr = lv.get(1);
LazyValue cond = null; LazyValue cond = null;
if(lv.size() > 2) cond = lv.get(2); if(lv.size() > 2) cond = lv.get(2);
@@ -1991,12 +1992,12 @@ public class Expression implements Cloneable
} }
next.boundVariable = var; next.boundVariable = var;
} }
((ListValue) rval).fatality(); ((AbstractListValue) rval).fatality();
LazyValue ret = (cc, tt) -> ListValue.wrap(result); Value ret = ListValue.wrap(result);
//revering scope //revering scope
c.setVariable("_", _val); c.setVariable("_", _val);
c.setVariable("_i", _iter); c.setVariable("_i", _iter);
return ret; return (cc, tt) -> ret;
}); });
// grep(list or num, expr, exit_expr) => list // grep(list or num, expr, exit_expr) => list
@@ -2010,9 +2011,9 @@ public class Expression implements Cloneable
} }
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c);
if (!(rval instanceof ListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of filter function should be a list or iterator"); throw new InternalExpressionException("First argument of filter function should be a list or iterator");
Iterator<Value> iterator = ((ListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
LazyValue expr = lv.get(1); LazyValue expr = lv.get(1);
LazyValue cond = null; LazyValue cond = null;
if(lv.size() > 2) cond = lv.get(2); if(lv.size() > 2) cond = lv.get(2);
@@ -2037,12 +2038,12 @@ public class Expression implements Cloneable
} }
next.boundVariable = var; next.boundVariable = var;
} }
((ListValue) rval).fatality(); ((AbstractListValue) rval).fatality();
LazyValue ret = (cc, tt) -> ListValue.wrap(result); // might be a trap - lazy evaluation Value ret = ListValue.wrap(result);
//revering scope //revering scope
c.setVariable("_", _val); c.setVariable("_", _val);
c.setVariable("_i", _iter); c.setVariable("_i", _iter);
return ret; return (cc, tt) -> ret;
}); });
// first(list, expr) => elem or null // first(list, expr) => elem or null
@@ -2052,9 +2053,9 @@ public class Expression implements Cloneable
{ {
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c);
if (!(rval instanceof ListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'first' function should be a list or iterator"); throw new InternalExpressionException("First argument of 'first' function should be a list or iterator");
Iterator<Value> iterator = ((ListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
LazyValue expr = lv.get(1); LazyValue expr = lv.get(1);
//scoping //scoping
LazyValue _val = c.getVariable("_"); LazyValue _val = c.getVariable("_");
@@ -2077,7 +2078,7 @@ public class Expression implements Cloneable
next.boundVariable = var; next.boundVariable = var;
} }
//revering scope //revering scope
((ListValue) rval).fatality(); ((AbstractListValue) rval).fatality();
Value whyWontYouTrustMeJava = result; Value whyWontYouTrustMeJava = result;
c.setVariable("_", _val); c.setVariable("_", _val);
c.setVariable("_i", _iter); c.setVariable("_i", _iter);
@@ -2090,9 +2091,9 @@ public class Expression implements Cloneable
addLazyFunction("all", 2, (c, t, lv) -> addLazyFunction("all", 2, (c, t, lv) ->
{ {
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c);
if (!(rval instanceof ListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'all' function should be a list or iterator"); throw new InternalExpressionException("First argument of 'all' function should be a list or iterator");
Iterator<Value> iterator = ((ListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
LazyValue expr = lv.get(1); LazyValue expr = lv.get(1);
//scoping //scoping
LazyValue _val = c.getVariable("_"); LazyValue _val = c.getVariable("_");
@@ -2115,7 +2116,7 @@ public class Expression implements Cloneable
next.boundVariable = var; next.boundVariable = var;
} }
//revering scope //revering scope
((ListValue) rval).fatality(); ((AbstractListValue) rval).fatality();
c.setVariable("_", _val); c.setVariable("_", _val);
c.setVariable("_i", _iter); c.setVariable("_i", _iter);
return result; return result;
@@ -2131,9 +2132,9 @@ public class Expression implements Cloneable
throw new InternalExpressionException("Incorrect number of attributes for 'for', should be 2 or 3, not "+lv.size()); throw new InternalExpressionException("Incorrect number of attributes for 'for', should be 2 or 3, not "+lv.size());
} }
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c);
if (!(rval instanceof ListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("Second argument of 'for' function should be a list or iterator"); throw new InternalExpressionException("Second argument of 'for' function should be a list or iterator");
Iterator<Value> iterator = ((ListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
LazyValue expr = lv.get(1); LazyValue expr = lv.get(1);
LazyValue cond = null; LazyValue cond = null;
if(lv.size() > 2) cond = lv.get(2); if(lv.size() > 2) cond = lv.get(2);
@@ -2160,7 +2161,7 @@ public class Expression implements Cloneable
next.boundVariable = var; next.boundVariable = var;
} }
//revering scope //revering scope
((ListValue) rval).fatality(); ((AbstractListValue) rval).fatality();
c.setVariable("_", _val); c.setVariable("_", _val);
c.setVariable("_i", _iter); c.setVariable("_i", _iter);
long promiseWontChange = successCount; long promiseWontChange = successCount;
@@ -2178,9 +2179,9 @@ public class Expression implements Cloneable
Value acc = lv.get(2).evalValue(c); Value acc = lv.get(2).evalValue(c);
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c);
if (!(rval instanceof ListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'reduce' should be a list or iterator"); throw new InternalExpressionException("First argument of 'reduce' should be a list or iterator");
Iterator<Value> iterator = ((ListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
if (!iterator.hasNext()) if (!iterator.hasNext())
{ {
@@ -2204,7 +2205,7 @@ public class Expression implements Cloneable
next.boundVariable = var; next.boundVariable = var;
} }
//reverting scope //reverting scope
((ListValue) rval).fatality(); ((AbstractListValue) rval).fatality();
c.setVariable("_a", _acc); c.setVariable("_a", _acc);
c.setVariable("_", _val); c.setVariable("_", _val);
@@ -0,0 +1,21 @@
package carpet.script.value;
import carpet.script.exception.InternalExpressionException;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.registry.Registry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import static java.lang.Math.abs;
public abstract class AbstractListValue extends Value
{
public abstract Iterator<Value> iterator();
public void fatality() { }
}
@@ -1,7 +1,6 @@
package carpet.script.value; package carpet.script.value;
import carpet.script.exception.ExpressionException; import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException;
import java.util.List; import java.util.List;
@@ -23,12 +22,6 @@ public class FunctionSignatureValue extends Value
throw new ExpressionException("Function "+identifier+" is not defined yet"); throw new ExpressionException("Function "+identifier+" is not defined yet");
} }
@Override
public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct.");
}
@Override @Override
public Value clone() public Value clone()
{ {
@@ -1,7 +1,5 @@
package carpet.script.value; package carpet.script.value;
import carpet.script.exception.InternalExpressionException;
public class GlobalValue extends Value public class GlobalValue extends Value
{ {
public GlobalValue(Value variable) public GlobalValue(Value variable)
@@ -21,10 +19,4 @@ public class GlobalValue extends Value
{ {
return false; return false;
} }
@Override
public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct.");
}
} }
@@ -7,7 +7,7 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
public abstract class LazyListValue extends ListValue implements Iterator<Value> public abstract class LazyListValue extends AbstractListValue implements Iterator<Value>
{ {
public static LazyListValue range(long from, long to, long step) public static LazyListValue range(long from, long to, long step)
{ {
@@ -16,10 +16,12 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
{ {
if (step == 0) if (step == 0)
throw new InternalExpressionException("range will never end with zero step"); throw new InternalExpressionException("range will never end with zero step");
this.current = from; this.start = from;
this.current = this.start;
this.limit = to; this.limit = to;
this.stepp = step; this.stepp = step;
} }
private long start;
private long current; private long current;
private long limit; private long limit;
private long stepp; private long stepp;
@@ -31,6 +33,12 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
return val; return val;
} }
@Override
public void reset()
{
current = start;
}
@Override @Override
public boolean hasNext() public boolean hasNext()
{ {
@@ -39,11 +47,6 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
}; };
} }
public LazyListValue()
{
super(Collections.emptyList());
}
@Override @Override
public String getString() public String getString()
{ {
@@ -59,6 +62,10 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
public abstract Value next(); public abstract Value next();
@Override
public void fatality() {reset();}
public abstract void reset();
@Override @Override
public Iterator<Value> iterator() {return this;} public Iterator<Value> iterator() {return this;}
@@ -66,6 +73,7 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
{ {
List<Value> result = new ArrayList<>(); List<Value> result = new ArrayList<>();
this.forEachRemaining(result::add); this.forEachRemaining(result::add);
fatality();
return result; return result;
} }
@@ -83,14 +91,20 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
if (hasNext()) if (hasNext())
next(); next();
else else
{
fatality();
return ListValue.wrap(result); return ListValue.wrap(result);
}
} }
for (i = (int)from; i < to; i++) for (i = (int)from; i < to; i++)
{ {
if (hasNext()) if (hasNext())
result.add(next()); result.add(next());
else else
{
fatality();
return ListValue.wrap(result); return ListValue.wrap(result);
}
} }
return ListValue.wrap(result); return ListValue.wrap(result);
} }
@@ -116,5 +130,19 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
{ {
return "iterator"; return "iterator";
} }
@Override
public Object clone()
{
Object copy;
try
{
copy = super.clone();
}
catch (CloneNotSupportedException e)
{
throw new InternalExpressionException("cannot copy iterator");
}
((LazyListValue)copy).reset();
return copy;
}
} }
@@ -2,7 +2,6 @@ package carpet.script.value;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import java.util.ArrayList; import java.util.ArrayList;
@@ -14,7 +13,7 @@ import java.util.stream.Collectors;
import static java.lang.Math.abs; import static java.lang.Math.abs;
public class ListValue extends Value public class ListValue extends AbstractListValue
{ {
protected List<Value> items; protected List<Value> items;
@Override @Override
@@ -241,9 +240,6 @@ public class ListValue extends Value
{ {
return items.iterator(); return items.iterator();
} }
public void fatality()
{
}
public void extend(List<Value> subList) public void extend(List<Value> subList)
{ {
@@ -331,7 +327,4 @@ public class ListValue extends Value
{ {
return "list"; return "list";
} }
} }
+4 -1
View File
@@ -178,5 +178,8 @@ public abstract class Value implements Comparable<Value>, Cloneable
throw new InternalExpressionException("get element can only be obtained for lists and nbt values"); throw new InternalExpressionException("get element can only be obtained for lists and nbt values");
} }
public abstract String getTypeString(); public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an intenal type.");
}
} }