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

@@ -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";
}
}
+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");
}
public abstract String getTypeString();
public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an intenal type.");
}
}