maps unpack

This commit is contained in:
gnembon committed 2021-04-23 10:58:33 -03:00
1 parent 5ec664240f
commit e22474156b
6 files changed
+35 -32

No files matched your search

+2 -1
View File
@@ -239,7 +239,7 @@ flips boolean condition of the expression. Equivalent of `bool(expr)==false`
### `Unpacking Operator ...`
Unpacks elements of a list of an iterator into a sequence of arguments in a function making so that `fun(...[1, 2, 3])` is
identical with `fun(1, 2, 3)`.
identical with `fun(1, 2, 3)`. For maps, it unpacks them to a list of key-value pairs.
In function signatures it identifies a vararg parameter.
@@ -255,3 +255,4 @@ Unpacking mechanics can be used for list and map constriction, not just for func
`[...range(5), pi, ...range(5,-1,-1)]` => `[0, 1, 2, 3, 4, 3.14159265359, 5, 4, 3, 2, 1, 0]`
`{ ... map(range(5), _ -> _*_ )}` => `{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}`
`{...{1 -> 2, 3 -> 4}, ...{5 -> 6, 7 -> 8}}` => `{1: 2, 3: 4, 5: 6, 7: 8}`
@@ -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()
{