maps unpack
This commit is contained in:
1 parent
5ec664240f
commit
e22474156b
6 files changed
+35
-32
No files matched your search
@@ -6,15 +6,12 @@ import carpet.script.Fluff;
|
||||
import carpet.script.argument.FunctionArgument;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.exception.ReturnStatement;
|
||||
import carpet.script.value.AbstractListValue;
|
||||
import carpet.script.value.FunctionSignatureValue;
|
||||
import carpet.script.value.FunctionUnpackedArgumentsValue;
|
||||
import carpet.script.value.FunctionValue;
|
||||
import carpet.script.value.FunctionAnnotationValue;
|
||||
import carpet.script.value.ListValue;
|
||||
import carpet.script.value.StringValue;
|
||||
import carpet.script.value.Value;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -92,28 +89,6 @@ public class Functions {
|
||||
return new FunctionAnnotationValue(lv.get(0), FunctionAnnotationValue.Type.GLOBAL);
|
||||
});
|
||||
|
||||
// lazy because of typed evaluation of the argument
|
||||
expression.addLazyUnaryOperator("...", Operators.precedence.get("unary+-!..."), false, (c, t, lv) ->
|
||||
{
|
||||
if (t == Context.LOCALIZATION)
|
||||
return (cc, tt) -> new FunctionAnnotationValue(lv.evalValue(c), FunctionAnnotationValue.Type.VARARG);
|
||||
|
||||
Value params = lv.evalValue(c, t);
|
||||
FunctionUnpackedArgumentsValue fuaval;
|
||||
if (params instanceof ListValue)
|
||||
{
|
||||
fuaval = new FunctionUnpackedArgumentsValue(((ListValue) params).getItems());
|
||||
return (cc, tt) -> fuaval;
|
||||
}
|
||||
if (!(params instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("Unable to unpack a non-list");
|
||||
fuaval = new FunctionUnpackedArgumentsValue(
|
||||
ImmutableList.copyOf(((AbstractListValue) params).iterator())
|
||||
);
|
||||
return (cc, tt) -> fuaval;
|
||||
//throw new InternalExpressionException("That functionality has not been implemented yet.");
|
||||
});
|
||||
|
||||
//assigns const procedure to the lhs, returning its previous value
|
||||
// must be lazy due to RHS being an expression to save to execute
|
||||
expression.addLazyBinaryOperatorWithDelegation("->", Operators.precedence.get("def->"), false, (c, type, e, t, lv1, lv2) ->
|
||||
|
||||
@@ -6,6 +6,8 @@ import carpet.script.LazyValue;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.value.AbstractListValue;
|
||||
import carpet.script.value.ContainerValueInterface;
|
||||
import carpet.script.value.FunctionAnnotationValue;
|
||||
import carpet.script.value.FunctionUnpackedArgumentsValue;
|
||||
import carpet.script.value.LContainerValue;
|
||||
import carpet.script.value.ListValue;
|
||||
import carpet.script.value.MapValue;
|
||||
@@ -220,5 +222,19 @@ public class Operators {
|
||||
lv.evalValue(c, Context.BOOLEAN).getBoolean() ? (cc, tt)-> Value.FALSE : (cc, tt) -> Value.TRUE
|
||||
); // might need context boolean
|
||||
|
||||
// lazy because of typed evaluation of the argument
|
||||
expression.addLazyUnaryOperator("...", Operators.precedence.get("unary+-!..."), false, (c, t, lv) ->
|
||||
{
|
||||
if (t == Context.LOCALIZATION)
|
||||
return (cc, tt) -> new FunctionAnnotationValue(lv.evalValue(c), FunctionAnnotationValue.Type.VARARG);
|
||||
|
||||
Value params = lv.evalValue(c, t);
|
||||
if (!(params instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("Unable to unpack a non-list");
|
||||
FunctionUnpackedArgumentsValue fuaval = new FunctionUnpackedArgumentsValue( ((AbstractListValue) params).unpack());
|
||||
return (cc, tt) -> fuaval;
|
||||
//throw new InternalExpressionException("That functionality has not been implemented yet.");
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractListValue extends Value implements Iterable<Value>
|
||||
{
|
||||
public abstract Iterator<Value> iterator();
|
||||
public List<Value> unpack() { return Lists.newArrayList(iterator()); }
|
||||
public void fatality() { }
|
||||
public void append(Value v)
|
||||
{
|
||||
|
||||
@@ -29,7 +29,7 @@ import static java.lang.Math.abs;
|
||||
|
||||
public class ListValue extends AbstractListValue implements ContainerValueInterface
|
||||
{
|
||||
protected List<Value> items;
|
||||
protected final List<Value> items;
|
||||
@Override
|
||||
public String getString()
|
||||
{
|
||||
@@ -99,9 +99,7 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
|
||||
|
||||
public static ListValue wrap(List<Value> list)
|
||||
{
|
||||
ListValue created = new ListValue();
|
||||
created.items = list;
|
||||
return created;
|
||||
return new ListValue(list);
|
||||
}
|
||||
public static ListValue of(Value ... list)
|
||||
{
|
||||
@@ -283,10 +281,14 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
|
||||
|
||||
public Iterator<Value> iterator() { return new ArrayList<>(items).iterator(); } // should be thread safe
|
||||
|
||||
public List<Value> unpack()
|
||||
{
|
||||
return new ArrayList<>(items);
|
||||
}
|
||||
|
||||
public void extend(List<Value> subList)
|
||||
{
|
||||
for (Value v: subList)
|
||||
items.add(v);
|
||||
items.addAll(subList);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -48,6 +48,12 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
return new ArrayList<>(map.keySet()).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Value> unpack()
|
||||
{
|
||||
return map.entrySet().stream().map(e -> ListValue.of(e.getKey(), e.getValue())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user