list tweaks
This commit is contained in:
1 parent
6b24d7e93f
commit
6e4de969c8
9 files changed
+124
-67
No files matched your search
@@ -1977,9 +1977,7 @@ public class CarpetExpression
|
||||
int y;
|
||||
int z;
|
||||
{
|
||||
x = minx;
|
||||
y = miny;
|
||||
z = minz;
|
||||
reset();
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
@@ -2012,6 +2010,15 @@ public class CarpetExpression
|
||||
public void fatality()
|
||||
{
|
||||
// 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()
|
||||
{
|
||||
int curradius = 0;
|
||||
int curpos = 0;
|
||||
int curradius;
|
||||
int curpos;
|
||||
{
|
||||
|
||||
reset();
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
@@ -2097,15 +2104,25 @@ public class CarpetExpression
|
||||
return block;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset()
|
||||
{
|
||||
curradius = 0;
|
||||
curpos = 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return (c_, t_) -> new LazyListValue()
|
||||
{
|
||||
int curradius = 0;
|
||||
int curpos = 0;
|
||||
int curheight = -height;
|
||||
int curradius;
|
||||
int curpos;
|
||||
int curheight;
|
||||
{
|
||||
reset();
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
@@ -2142,6 +2159,14 @@ public class CarpetExpression
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset()
|
||||
{
|
||||
curradius = 0;
|
||||
curpos = 0;
|
||||
curheight = -height;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ import carpet.script.Fluff.SexFunction;
|
||||
import carpet.script.Fluff.TriFunction;
|
||||
import carpet.script.exception.ExpressionException;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.value.AbstractListValue;
|
||||
import carpet.script.value.FunctionSignatureValue;
|
||||
import carpet.script.value.GlobalValue;
|
||||
import carpet.script.value.LazyListValue;
|
||||
@@ -1857,12 +1858,12 @@ public class Expression implements Cloneable
|
||||
return LazyListValue.range(from, to, step);
|
||||
});
|
||||
|
||||
addBinaryFunction("get", (v1, v2) -> v1.getElementAt(v2));
|
||||
addBinaryFunction("get", Value::getElementAt);
|
||||
|
||||
//Deprecated, use "get" instead
|
||||
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");
|
||||
List<Value> items = ((ListValue)v1).getItems();
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
@@ -1965,9 +1966,9 @@ public class Expression implements Cloneable
|
||||
|
||||
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");
|
||||
Iterator<Value> iterator = ((ListValue) rval).iterator();
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
LazyValue cond = null;
|
||||
if(lv.size() > 2) cond = lv.get(2);
|
||||
@@ -1991,12 +1992,12 @@ public class Expression implements Cloneable
|
||||
}
|
||||
next.boundVariable = var;
|
||||
}
|
||||
((ListValue) rval).fatality();
|
||||
LazyValue ret = (cc, tt) -> ListValue.wrap(result);
|
||||
((AbstractListValue) rval).fatality();
|
||||
Value ret = ListValue.wrap(result);
|
||||
//revering scope
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
return ret;
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
// grep(list or num, expr, exit_expr) => list
|
||||
@@ -2010,9 +2011,9 @@ public class Expression implements Cloneable
|
||||
}
|
||||
|
||||
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");
|
||||
Iterator<Value> iterator = ((ListValue) rval).iterator();
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
LazyValue cond = null;
|
||||
if(lv.size() > 2) cond = lv.get(2);
|
||||
@@ -2037,12 +2038,12 @@ public class Expression implements Cloneable
|
||||
}
|
||||
next.boundVariable = var;
|
||||
}
|
||||
((ListValue) rval).fatality();
|
||||
LazyValue ret = (cc, tt) -> ListValue.wrap(result); // might be a trap - lazy evaluation
|
||||
((AbstractListValue) rval).fatality();
|
||||
Value ret = ListValue.wrap(result);
|
||||
//revering scope
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
return ret;
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
// first(list, expr) => elem or null
|
||||
@@ -2052,9 +2053,9 @@ public class Expression implements Cloneable
|
||||
{
|
||||
|
||||
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");
|
||||
Iterator<Value> iterator = ((ListValue) rval).iterator();
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
LazyValue _val = c.getVariable("_");
|
||||
@@ -2077,7 +2078,7 @@ public class Expression implements Cloneable
|
||||
next.boundVariable = var;
|
||||
}
|
||||
//revering scope
|
||||
((ListValue) rval).fatality();
|
||||
((AbstractListValue) rval).fatality();
|
||||
Value whyWontYouTrustMeJava = result;
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
@@ -2090,9 +2091,9 @@ public class Expression implements Cloneable
|
||||
addLazyFunction("all", 2, (c, t, lv) ->
|
||||
{
|
||||
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");
|
||||
Iterator<Value> iterator = ((ListValue) rval).iterator();
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
LazyValue _val = c.getVariable("_");
|
||||
@@ -2115,7 +2116,7 @@ public class Expression implements Cloneable
|
||||
next.boundVariable = var;
|
||||
}
|
||||
//revering scope
|
||||
((ListValue) rval).fatality();
|
||||
((AbstractListValue) rval).fatality();
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
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());
|
||||
}
|
||||
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");
|
||||
Iterator<Value> iterator = ((ListValue) rval).iterator();
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
LazyValue cond = null;
|
||||
if(lv.size() > 2) cond = lv.get(2);
|
||||
@@ -2160,7 +2161,7 @@ public class Expression implements Cloneable
|
||||
next.boundVariable = var;
|
||||
}
|
||||
//revering scope
|
||||
((ListValue) rval).fatality();
|
||||
((AbstractListValue) rval).fatality();
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
long promiseWontChange = successCount;
|
||||
@@ -2178,9 +2179,9 @@ public class Expression implements Cloneable
|
||||
|
||||
Value acc = lv.get(2).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");
|
||||
Iterator<Value> iterator = ((ListValue) rval).iterator();
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
|
||||
if (!iterator.hasNext())
|
||||
{
|
||||
@@ -2204,7 +2205,7 @@ public class Expression implements Cloneable
|
||||
next.boundVariable = var;
|
||||
}
|
||||
//reverting scope
|
||||
((ListValue) rval).fatality();
|
||||
((AbstractListValue) rval).fatality();
|
||||
c.setVariable("_a", _acc);
|
||||
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;
|
||||
|
||||
import carpet.script.exception.ExpressionException;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -23,12 +22,6 @@ public class FunctionSignatureValue extends Value
|
||||
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
|
||||
public Value clone()
|
||||
{
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
|
||||
public class GlobalValue extends Value
|
||||
{
|
||||
public GlobalValue(Value variable)
|
||||
@@ -21,10 +19,4 @@ public class GlobalValue extends Value
|
||||
{
|
||||
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.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)
|
||||
{
|
||||
@@ -16,10 +16,12 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
|
||||
{
|
||||
if (step == 0)
|
||||
throw new InternalExpressionException("range will never end with zero step");
|
||||
this.current = from;
|
||||
this.start = from;
|
||||
this.current = this.start;
|
||||
this.limit = to;
|
||||
this.stepp = step;
|
||||
}
|
||||
private long start;
|
||||
private long current;
|
||||
private long limit;
|
||||
private long stepp;
|
||||
@@ -31,6 +33,12 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset()
|
||||
{
|
||||
current = start;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
@@ -39,11 +47,6 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
|
||||
};
|
||||
}
|
||||
|
||||
public LazyListValue()
|
||||
{
|
||||
super(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString()
|
||||
{
|
||||
@@ -59,6 +62,10 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
|
||||
|
||||
public abstract Value next();
|
||||
|
||||
@Override
|
||||
public void fatality() {reset();}
|
||||
public abstract void reset();
|
||||
|
||||
@Override
|
||||
public Iterator<Value> iterator() {return this;}
|
||||
|
||||
@@ -66,6 +73,7 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
|
||||
{
|
||||
List<Value> result = new ArrayList<>();
|
||||
this.forEachRemaining(result::add);
|
||||
fatality();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -83,14 +91,20 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
|
||||
if (hasNext())
|
||||
next();
|
||||
else
|
||||
{
|
||||
fatality();
|
||||
return ListValue.wrap(result);
|
||||
}
|
||||
}
|
||||
for (i = (int)from; i < to; i++)
|
||||
{
|
||||
if (hasNext())
|
||||
result.add(next());
|
||||
else
|
||||
{
|
||||
fatality();
|
||||
return ListValue.wrap(result);
|
||||
}
|
||||
}
|
||||
return ListValue.wrap(result);
|
||||
}
|
||||
@@ -116,5 +130,19 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
|
||||
{
|
||||
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 net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -14,7 +13,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
public class ListValue extends Value
|
||||
public class ListValue extends AbstractListValue
|
||||
{
|
||||
protected List<Value> items;
|
||||
@Override
|
||||
@@ -241,9 +240,6 @@ public class ListValue extends Value
|
||||
{
|
||||
return items.iterator();
|
||||
}
|
||||
public void fatality()
|
||||
{
|
||||
}
|
||||
|
||||
public void extend(List<Value> subList)
|
||||
{
|
||||
@@ -331,7 +327,4 @@ public class ListValue extends Value
|
||||
{
|
||||
return "list";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
public abstract String getTypeString();
|
||||
public String getTypeString()
|
||||
{
|
||||
throw new InternalExpressionException("How did you get here? Cannot get type of an intenal type.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user