semi-auto cleanup
This commit is contained in:
1 parent
cad80f82c6
commit
8f61271606
99 files changed
+11463
-8007
No files matched your search
@@ -25,42 +25,42 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DataStructures {
|
||||
public static void apply(Expression expression)
|
||||
public class DataStructures
|
||||
{
|
||||
public static void apply(final Expression expression)
|
||||
{
|
||||
expression.addFunction("l", lv ->
|
||||
{
|
||||
if (lv.size() == 1 && lv.get(0) instanceof LazyListValue)
|
||||
return ListValue.wrap(((LazyListValue) lv.get(0)).unroll());
|
||||
return new ListValue.ListConstructorValue(lv);
|
||||
});
|
||||
lv.size() == 1 && lv.get(0) instanceof final LazyListValue llv
|
||||
? ListValue.wrap(llv.unroll())
|
||||
: new ListValue.ListConstructorValue(lv));
|
||||
|
||||
expression.addFunction("join", (lv) ->
|
||||
{
|
||||
if (lv.size() < 2)
|
||||
{
|
||||
throw new InternalExpressionException("'join' takes at least 2 arguments");
|
||||
String delimiter = lv.get(0).getString();
|
||||
List<Value> toJoin;
|
||||
if (lv.size()==2 && lv.get(1) instanceof LazyListValue)
|
||||
{
|
||||
toJoin = ((LazyListValue) lv.get(1)).unroll();
|
||||
|
||||
}
|
||||
else if (lv.size() == 2 && lv.get(1) instanceof ListValue)
|
||||
final String delimiter = lv.get(0).getString();
|
||||
final List<Value> toJoin;
|
||||
if (lv.size() == 2 && lv.get(1) instanceof final LazyListValue llv)
|
||||
{
|
||||
toJoin = new ArrayList<>(((ListValue)lv.get(1)).getItems());
|
||||
toJoin = llv.unroll();
|
||||
}
|
||||
else if (lv.size() == 2 && lv.get(1) instanceof final ListValue llv)
|
||||
{
|
||||
toJoin = new ArrayList<>(llv.getItems());
|
||||
}
|
||||
else
|
||||
{
|
||||
toJoin = lv.subList(1,lv.size());
|
||||
toJoin = lv.subList(1, lv.size());
|
||||
}
|
||||
return new StringValue(toJoin.stream().map(Value::getString).collect(Collectors.joining(delimiter)));
|
||||
});
|
||||
|
||||
expression.addFunction("split", (lv) ->
|
||||
{
|
||||
Value delimiter;
|
||||
Value hwat;
|
||||
final Value delimiter;
|
||||
final Value hwat;
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
hwat = lv.get(0);
|
||||
@@ -80,23 +80,26 @@ public class DataStructures {
|
||||
|
||||
expression.addFunction("slice", (lv) ->
|
||||
{
|
||||
|
||||
if (lv.size() != 2 && lv.size() != 3)
|
||||
{
|
||||
throw new InternalExpressionException("'slice' takes 2 or 3 arguments");
|
||||
Value hwat = lv.get(0);
|
||||
long from = NumericValue.asNumber(lv.get(1)).getLong();
|
||||
}
|
||||
final Value hwat = lv.get(0);
|
||||
final long from = NumericValue.asNumber(lv.get(1)).getLong();
|
||||
Long to = null;
|
||||
if (lv.size()== 3)
|
||||
if (lv.size() == 3)
|
||||
{
|
||||
to = NumericValue.asNumber(lv.get(2)).getLong();
|
||||
}
|
||||
return hwat.slice(from, to);
|
||||
});
|
||||
|
||||
expression.addFunction("sort", (lv) ->
|
||||
{
|
||||
List<Value> toSort = lv;
|
||||
if (lv.size()==1 && lv.get(0) instanceof ListValue)
|
||||
if (lv.size() == 1 && lv.get(0) instanceof final ListValue llv)
|
||||
{
|
||||
toSort = new ArrayList<>(((ListValue)lv.get(0)).getItems());
|
||||
toSort = new ArrayList<>(llv.getItems());
|
||||
}
|
||||
Collections.sort(toSort);
|
||||
return ListValue.wrap(toSort);
|
||||
@@ -106,30 +109,34 @@ public class DataStructures {
|
||||
expression.addLazyFunction("sort_key", (c, t, lv) -> //get working with iterators
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
throw new InternalExpressionException("First argument for 'sort_key' should be a List");
|
||||
Value v = lv.get(0).evalValue(c);
|
||||
if (!(v instanceof ListValue))
|
||||
}
|
||||
final Value v = lv.get(0).evalValue(c);
|
||||
if (!(v instanceof final ListValue list))
|
||||
{
|
||||
throw new InternalExpressionException("First argument for 'sort_key' should be a List");
|
||||
List<Value> toSort = new ArrayList<>(((ListValue) v).getItems());
|
||||
if (lv.size()==1)
|
||||
}
|
||||
final List<Value> toSort = new ArrayList<>(list.getItems());
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
Collections.shuffle(toSort);
|
||||
Value ret = ListValue.wrap(toSort);
|
||||
final Value ret = ListValue.wrap(toSort);
|
||||
return (_c, _t) -> ret;
|
||||
}
|
||||
LazyValue sortKey = lv.get(1);
|
||||
final LazyValue sortKey = lv.get(1);
|
||||
//scoping
|
||||
LazyValue __ = c.getVariable("_");
|
||||
Collections.sort(toSort,(v1, v2) -> {
|
||||
c.setVariable("_",(cc, tt) -> v1);
|
||||
Value ev1 = sortKey.evalValue(c);
|
||||
c.setVariable("_",(cc, tt) -> v2);
|
||||
Value ev2 = sortKey.evalValue(c);
|
||||
final LazyValue __ = c.getVariable("_");
|
||||
toSort.sort((v1, v2) -> {
|
||||
c.setVariable("_", (cc, tt) -> v1);
|
||||
final Value ev1 = sortKey.evalValue(c);
|
||||
c.setVariable("_", (cc, tt) -> v2);
|
||||
final Value ev2 = sortKey.evalValue(c);
|
||||
return ev1.compareTo(ev2);
|
||||
});
|
||||
//revering scope
|
||||
c.setVariable("_", __);
|
||||
Value ret = ListValue.wrap(toSort);
|
||||
final Value ret = ListValue.wrap(toSort);
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
@@ -138,9 +145,11 @@ public class DataStructures {
|
||||
NumericValue from = Value.ZERO;
|
||||
NumericValue to;
|
||||
NumericValue step = Value.ONE;
|
||||
int argsize = lv.size();
|
||||
final int argsize = lv.size();
|
||||
if (argsize == 0 || argsize > 3)
|
||||
throw new InternalExpressionException("'range' accepts from 1 to 3 arguments, not "+argsize);
|
||||
{
|
||||
throw new InternalExpressionException("'range' accepts from 1 to 3 arguments, not " + argsize);
|
||||
}
|
||||
to = NumericValue.asNumber(lv.get(0));
|
||||
if (lv.size() > 1)
|
||||
{
|
||||
@@ -157,79 +166,84 @@ public class DataStructures {
|
||||
});
|
||||
|
||||
expression.addTypedContextFunction("m", -1, Context.MAPDEF, (c, t, lv) ->
|
||||
{
|
||||
Value ret;
|
||||
if (lv.size() == 1 && lv.get(0) instanceof LazyListValue)
|
||||
ret = new MapValue(((LazyListValue) lv.get(0)).unroll());
|
||||
else
|
||||
ret = new MapValue(lv);
|
||||
return ret;
|
||||
});
|
||||
lv.size() == 1 && lv.get(0) instanceof final LazyListValue llv
|
||||
? new MapValue(llv.unroll())
|
||||
: new MapValue(lv)
|
||||
);
|
||||
|
||||
expression.addUnaryFunction("keys", v ->
|
||||
{
|
||||
if (v instanceof MapValue)
|
||||
return new ListValue(((MapValue) v).getMap().keySet());
|
||||
return Value.NULL;
|
||||
});
|
||||
v instanceof final MapValue map
|
||||
? new ListValue(map.getMap().keySet())
|
||||
: Value.NULL
|
||||
);
|
||||
|
||||
expression.addUnaryFunction("values", v ->
|
||||
{
|
||||
if (v instanceof MapValue)
|
||||
return new ListValue(((MapValue) v).getMap().values());
|
||||
return Value.NULL;
|
||||
});
|
||||
v instanceof final MapValue map
|
||||
? new ListValue(map.getMap().values())
|
||||
: Value.NULL
|
||||
);
|
||||
|
||||
expression.addUnaryFunction("pairs", v ->
|
||||
{
|
||||
if (v instanceof MapValue)
|
||||
return ListValue.wrap(((MapValue) v).getMap().entrySet().stream().map(
|
||||
(p) -> ListValue.of(p.getKey(), p.getValue())
|
||||
));
|
||||
return Value.NULL;
|
||||
});
|
||||
v instanceof final MapValue map
|
||||
? ListValue.wrap(map.getMap().entrySet().stream().map(p -> ListValue.of(p.getKey(), p.getValue())))
|
||||
: Value.NULL);
|
||||
|
||||
expression.addBinaryContextOperator(":", Operators.precedence.get("attribute~:"),true, true, false, (ctx, t, container, address) ->
|
||||
expression.addBinaryContextOperator(":", Operators.precedence.get("attribute~:"), true, true, false, (ctx, t, container, address) ->
|
||||
{
|
||||
if (container instanceof LContainerValue)
|
||||
if (container instanceof final LContainerValue lcv)
|
||||
{
|
||||
ContainerValueInterface outerContainer = ((LContainerValue) container).getContainer();
|
||||
if (outerContainer == null) return LContainerValue.NULL_CONTAINER;
|
||||
Value innerContainer = outerContainer.get(((LContainerValue) container).getAddress());
|
||||
if (!(innerContainer instanceof ContainerValueInterface)) return LContainerValue.NULL_CONTAINER;
|
||||
return new LContainerValue((ContainerValueInterface) innerContainer, address);
|
||||
final ContainerValueInterface outerContainer = lcv.container();
|
||||
if (outerContainer == null)
|
||||
{
|
||||
return LContainerValue.NULL_CONTAINER;
|
||||
}
|
||||
return outerContainer.get(lcv.address()) instanceof final ContainerValueInterface cvi
|
||||
? new LContainerValue(cvi, address)
|
||||
: LContainerValue.NULL_CONTAINER;
|
||||
}
|
||||
if (!(container instanceof ContainerValueInterface))
|
||||
if (!(container instanceof final ContainerValueInterface cvi))
|
||||
{
|
||||
return t == Context.LVALUE ? LContainerValue.NULL_CONTAINER : Value.NULL;
|
||||
if (t != Context.LVALUE) return ((ContainerValueInterface) container).get(address);
|
||||
return new LContainerValue((ContainerValueInterface) container, address);
|
||||
}
|
||||
return t != Context.LVALUE ? cvi.get(address) : new LContainerValue(cvi, address);
|
||||
});
|
||||
|
||||
// lazy cause conditional typing - questionable
|
||||
expression.addLazyFunction("get", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
throw new InternalExpressionException("'get' requires parameters");
|
||||
}
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof LContainerValue))
|
||||
final Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof final LContainerValue lcv))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
ContainerValueInterface container = ((LContainerValue) v).getContainer();
|
||||
}
|
||||
final ContainerValueInterface container = lcv.container();
|
||||
if (container == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
Value ret = container.get(((LContainerValue) v).getAddress());
|
||||
}
|
||||
final Value ret = container.get(lcv.address());
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
Value container = lv.get(0).evalValue(c);
|
||||
for (int i = 1; i < lv.size(); i++)
|
||||
{
|
||||
if (!(container instanceof ContainerValueInterface)) return (cc, tt) -> Value.NULL;
|
||||
container = ((ContainerValueInterface) container).get(lv.get(i).evalValue(c));
|
||||
if (!(container instanceof final ContainerValueInterface cvi))
|
||||
{
|
||||
return (cc, tt) -> Value.NULL;
|
||||
}
|
||||
container = cvi.get(lv.get(i).evalValue(c));
|
||||
}
|
||||
if (container == null)
|
||||
{
|
||||
return (cc, tt) -> Value.NULL;
|
||||
Value finalContainer = container;
|
||||
}
|
||||
final Value finalContainer = container;
|
||||
return (cc, tt) -> finalContainer;
|
||||
});
|
||||
|
||||
@@ -237,66 +251,77 @@ public class DataStructures {
|
||||
expression.addLazyFunction("has", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
throw new InternalExpressionException("'has' requires parameters");
|
||||
}
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof LContainerValue))
|
||||
final Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof final LContainerValue lcv))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
ContainerValueInterface container = ((LContainerValue) v).getContainer();
|
||||
}
|
||||
final ContainerValueInterface container = lcv.container();
|
||||
if (container == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
Value ret = BooleanValue.of(container.has(((LContainerValue) v).getAddress()));
|
||||
}
|
||||
final Value ret = BooleanValue.of(container.has(lcv.address()));
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
Value container = lv.get(0).evalValue(c);
|
||||
for (int i = 1; i < lv.size()-1; i++)
|
||||
for (int i = 1; i < lv.size() - 1; i++)
|
||||
{
|
||||
if (!(container instanceof ContainerValueInterface)) return LazyValue.NULL;
|
||||
container = ((ContainerValueInterface) container).get(lv.get(i).evalValue(c));
|
||||
if (!(container instanceof final ContainerValueInterface cvi))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
container = cvi.get(lv.get(i).evalValue(c));
|
||||
}
|
||||
if (!(container instanceof ContainerValueInterface))
|
||||
if (!(container instanceof final ContainerValueInterface cvi))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
Value ret = BooleanValue.of(((ContainerValueInterface) container).has(lv.get(lv.size()-1).evalValue(c)));
|
||||
}
|
||||
final Value ret = BooleanValue.of(cvi.has(lv.get(lv.size() - 1).evalValue(c)));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
// same as `get`
|
||||
expression.addLazyFunction("put", (c, t, lv) ->
|
||||
{
|
||||
if(lv.size()<2)
|
||||
if (lv.size() < 2)
|
||||
{
|
||||
throw new InternalExpressionException("'put' takes at least three arguments, a container, address, and values to insert at that index");
|
||||
}
|
||||
Value container = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (container instanceof LContainerValue)
|
||||
final Value container = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (container instanceof final LContainerValue lcv)
|
||||
{
|
||||
ContainerValueInterface internalContainer = ((LContainerValue) container).getContainer();
|
||||
final ContainerValueInterface internalContainer = lcv.container();
|
||||
if (internalContainer == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
Value address = ((LContainerValue) container).getAddress();
|
||||
Value what = lv.get(1).evalValue(c);
|
||||
Value retVal = BooleanValue.of((lv.size() > 2)
|
||||
final Value address = lcv.address();
|
||||
final Value what = lv.get(1).evalValue(c);
|
||||
final Value retVal = BooleanValue.of((lv.size() > 2)
|
||||
? internalContainer.put(address, what, lv.get(2).evalValue(c))
|
||||
: internalContainer.put(address, what));
|
||||
return (cc, tt) -> retVal;
|
||||
|
||||
}
|
||||
if(lv.size()<3)
|
||||
if (lv.size() < 3)
|
||||
{
|
||||
throw new InternalExpressionException("'put' takes at least three arguments, a container, address, and values to insert at that index");
|
||||
}
|
||||
if (!(container instanceof ContainerValueInterface))
|
||||
if (!(container instanceof final ContainerValueInterface cvi))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
Value where = lv.get(1).evalValue(c);
|
||||
Value what = lv.get(2).evalValue(c);
|
||||
Value retVal = BooleanValue.of((lv.size()>3)
|
||||
? ((ContainerValueInterface) container).put(where, what, lv.get(3).evalValue(c))
|
||||
: ((ContainerValueInterface) container).put(where, what));
|
||||
final Value where = lv.get(1).evalValue(c);
|
||||
final Value what = lv.get(2).evalValue(c);
|
||||
final Value retVal = BooleanValue.of((lv.size() > 3)
|
||||
? cvi.put(where, what, lv.get(3).evalValue(c))
|
||||
: cvi.put(where, what));
|
||||
return (cc, tt) -> retVal;
|
||||
});
|
||||
|
||||
@@ -304,44 +329,61 @@ public class DataStructures {
|
||||
expression.addLazyFunction("delete", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
throw new InternalExpressionException("'delete' requires parameters");
|
||||
}
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof LContainerValue))
|
||||
final Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof final LContainerValue lcv))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
ContainerValueInterface container = ((LContainerValue) v).getContainer();
|
||||
}
|
||||
final ContainerValueInterface container = lcv.container();
|
||||
if (container == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
Value ret = BooleanValue.of(container.delete(((LContainerValue) v).getAddress()));
|
||||
}
|
||||
final Value ret = BooleanValue.of(container.delete(lcv.address()));
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
Value container = lv.get(0).evalValue(c);
|
||||
for (int i = 1; i < lv.size()-1; i++)
|
||||
for (int i = 1; i < lv.size() - 1; i++)
|
||||
{
|
||||
if (!(container instanceof ContainerValueInterface)) return LazyValue.NULL;
|
||||
container = ((ContainerValueInterface) container).get(lv.get(i).evalValue(c));
|
||||
if (!(container instanceof final ContainerValueInterface cvi))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
container = cvi.get(lv.get(i).evalValue(c));
|
||||
}
|
||||
if (!(container instanceof ContainerValueInterface))
|
||||
if (!(container instanceof final ContainerValueInterface cvi))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
Value ret = BooleanValue.of(((ContainerValueInterface) container).delete(lv.get(lv.size()-1).evalValue(c)));
|
||||
}
|
||||
final Value ret = BooleanValue.of(cvi.delete(lv.get(lv.size() - 1).evalValue(c)));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
expression.addUnaryFunction("encode_b64", v -> StringValue.of(Base64.getEncoder().encodeToString(v.getString().getBytes(StandardCharsets.UTF_8))));
|
||||
expression.addUnaryFunction("decode_b64", v -> {
|
||||
try {
|
||||
try
|
||||
{
|
||||
return StringValue.of(new String(Base64.getDecoder().decode(v.getString()), StandardCharsets.UTF_8));
|
||||
} catch (IllegalArgumentException iae){
|
||||
}
|
||||
catch (final IllegalArgumentException iae)
|
||||
{
|
||||
throw new ThrowStatement("Invalid b64 string: " + v.getString(), Throwables.B64_ERROR);
|
||||
}
|
||||
});
|
||||
|
||||
expression.addUnaryFunction("encode_json", v -> StringValue.of(v.toJson().toString()));
|
||||
expression.addUnaryFunction("decode_json", v -> {
|
||||
try {
|
||||
try
|
||||
{
|
||||
return Auxiliary.GSON.fromJson(v.getString(), Value.class);
|
||||
} catch (JsonParseException jpe){
|
||||
}
|
||||
catch (final JsonParseException jpe)
|
||||
{
|
||||
throw new ThrowStatement("Invalid json string: " + v.getString(), Throwables.JSON_ERROR);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user