warning cleanup
This commit is contained in:
1 parent
5ab4a9e442
commit
277872b33c
106 files changed
+4495
-4345
No files matched your search
@@ -13,12 +13,12 @@ public class Arithmetic
|
||||
public static final Value PI = new NumericValue(Math.PI);
|
||||
public static final Value euler = new NumericValue(Math.E);
|
||||
|
||||
public static void apply(final Expression expression)
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
expression.addTypedContextFunction("not", 1, Context.Type.BOOLEAN, (c, t, lv) -> BooleanValue.of(lv.get(0).getBoolean()));
|
||||
expression.addUnaryFunction("fact", (v) ->
|
||||
expression.addUnaryFunction("fact", v ->
|
||||
{
|
||||
final long number = NumericValue.asNumber(v).getLong();
|
||||
long number = NumericValue.asNumber(v).getLong();
|
||||
if (number < 21)
|
||||
{
|
||||
long factorial = 1;
|
||||
@@ -82,26 +82,26 @@ public class Arithmetic
|
||||
expression.addMathematicalUnaryIntFunction("ceil", n -> (long) Math.ceil(n));
|
||||
|
||||
expression.addContextFunction("mandelbrot", 3, (c, t, lv) -> {
|
||||
final double a0 = NumericValue.asNumber(lv.get(0)).getDouble();
|
||||
final double b0 = NumericValue.asNumber(lv.get(1)).getDouble();
|
||||
final long maxiter = NumericValue.asNumber(lv.get(2)).getLong();
|
||||
double a0 = NumericValue.asNumber(lv.get(0)).getDouble();
|
||||
double b0 = NumericValue.asNumber(lv.get(1)).getDouble();
|
||||
long maxiter = NumericValue.asNumber(lv.get(2)).getLong();
|
||||
double a = 0.0D;
|
||||
double b = 0.0D;
|
||||
long iter = 0;
|
||||
while (a * a + b * b < 4 && iter < maxiter)
|
||||
{
|
||||
final double temp = a * a - b * b + a0;
|
||||
double temp = a * a - b * b + a0;
|
||||
b = 2 * a * b + b0;
|
||||
a = temp;
|
||||
iter++;
|
||||
}
|
||||
final long iFinal = iter;
|
||||
long iFinal = iter;
|
||||
return new NumericValue(iFinal);
|
||||
});
|
||||
|
||||
expression.addFunction("max", (lv) ->
|
||||
expression.addFunction("max", lv ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'max' requires at least one parameter");
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class Arithmetic
|
||||
{
|
||||
lv = ((ListValue) lv.get(0)).getItems();
|
||||
}
|
||||
for (final Value parameter : lv)
|
||||
for (Value parameter : lv)
|
||||
{
|
||||
if (max == null || parameter.compareTo(max) > 0)
|
||||
{
|
||||
@@ -120,9 +120,9 @@ public class Arithmetic
|
||||
return max;
|
||||
});
|
||||
|
||||
expression.addFunction("min", (lv) ->
|
||||
expression.addFunction("min", lv ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'min' requires at least one parameter");
|
||||
}
|
||||
@@ -131,7 +131,7 @@ public class Arithmetic
|
||||
{
|
||||
lv = ((ListValue) lv.get(0)).getItems();
|
||||
}
|
||||
for (final Value parameter : lv)
|
||||
for (Value parameter : lv)
|
||||
{
|
||||
if (min == null || parameter.compareTo(min) < 0)
|
||||
{
|
||||
|
||||
@@ -19,23 +19,23 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class ControlFlow
|
||||
{
|
||||
public static void apply(final Expression expression) // public just to get the javadoc right
|
||||
public static void apply(Expression expression) // public just to get the javadoc right
|
||||
{
|
||||
// needs to be lazy cause of custom contextualization
|
||||
expression.addLazyBinaryOperator(";", Operators.precedence.get("nextop;"), true, true, t -> Context.Type.VOID, (c, t, lv1, lv2) ->
|
||||
{
|
||||
lv1.evalValue(c, Context.VOID);
|
||||
final Value v2 = lv2.evalValue(c, t);
|
||||
Value v2 = lv2.evalValue(c, t);
|
||||
return (cc, tt) -> v2;
|
||||
});
|
||||
|
||||
expression.addPureLazyFunction("then", -1, t -> Context.Type.VOID, (c, t, lv) -> {
|
||||
final int imax = lv.size() - 1;
|
||||
int imax = lv.size() - 1;
|
||||
for (int i = 0; i < imax; i++)
|
||||
{
|
||||
lv.get(i).evalValue(c, Context.VOID);
|
||||
}
|
||||
final Value v = lv.get(imax).evalValue(c, t);
|
||||
Value v = lv.get(imax).evalValue(c, t);
|
||||
return (cc, tt) -> v;
|
||||
});
|
||||
expression.addFunctionalEquivalence(";", "then");
|
||||
@@ -52,20 +52,20 @@ public class ControlFlow
|
||||
{
|
||||
if (lv.get(i).evalValue(c, Context.BOOLEAN).getBoolean())
|
||||
{
|
||||
final Value ret = lv.get(i + 1).evalValue(c, t);
|
||||
Value ret = lv.get(i + 1).evalValue(c, t);
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
}
|
||||
if (lv.size() % 2 == 1)
|
||||
{
|
||||
final Value ret = lv.get(lv.size() - 1).evalValue(c, t);
|
||||
Value ret = lv.get(lv.size() - 1).evalValue(c, t);
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
return (cc, tt) -> Value.NULL;
|
||||
});
|
||||
|
||||
expression.addImpureFunction("exit", (lv) -> {
|
||||
throw new ExitStatement(lv.size() == 0 ? Value.NULL : lv.get(0));
|
||||
expression.addImpureFunction("exit", lv -> {
|
||||
throw new ExitStatement(lv.isEmpty() ? Value.NULL : lv.get(0));
|
||||
});
|
||||
|
||||
expression.addImpureFunction("throw", lv ->
|
||||
@@ -83,16 +83,16 @@ public class ControlFlow
|
||||
// needs to be lazy since execution of parameters but first one are conditional
|
||||
expression.addLazyFunction("try", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'try' needs at least an expression block, and either a catch_epr, or a number of pairs of filters and catch_expr");
|
||||
}
|
||||
try
|
||||
{
|
||||
final Value retval = lv.get(0).evalValue(c, t);
|
||||
return (c_, t_) -> retval;
|
||||
Value retval = lv.get(0).evalValue(c, t);
|
||||
return (ct, tt) -> retval;
|
||||
}
|
||||
catch (final ProcessedThrowStatement ret)
|
||||
catch (ProcessedThrowStatement ret)
|
||||
{
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
@@ -100,7 +100,7 @@ public class ControlFlow
|
||||
{
|
||||
throw ret;
|
||||
}
|
||||
return (c_, t_) -> Value.NULL;
|
||||
return (ct, tt) -> Value.NULL;
|
||||
}
|
||||
if (lv.size() > 3 && lv.size() % 2 == 0)
|
||||
{
|
||||
@@ -109,10 +109,10 @@ public class ControlFlow
|
||||
|
||||
Value val = null; // This is always assigned at some point, just the compiler doesn't know
|
||||
|
||||
final LazyValue __ = c.getVariable("_");
|
||||
c.setVariable("_", (__c, __t) -> ret.data.reboundedTo("_"));
|
||||
final LazyValue _trace = c.getVariable("_trace");
|
||||
c.setVariable("_trace", (__c, __t) -> MapValue.wrap(Map.of(
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
c.setVariable("_", (ct, tt) -> ret.data.reboundedTo("_"));
|
||||
LazyValue trace = c.getVariable("_trace");
|
||||
c.setVariable("_trace", (ct, tt) -> MapValue.wrap(Map.of(
|
||||
StringValue.of("stack"), ListValue.wrap(ret.stack.stream().map(f -> ListValue.of(
|
||||
StringValue.of(f.getModule().name()),
|
||||
StringValue.of(f.getString()),
|
||||
@@ -151,10 +151,10 @@ public class ControlFlow
|
||||
pointer += 2;
|
||||
}
|
||||
}
|
||||
c.setVariable("_", __);
|
||||
if (_trace != null)
|
||||
c.setVariable("_", defaultVal);
|
||||
if (trace != null)
|
||||
{
|
||||
c.setVariable("_trace", _trace);
|
||||
c.setVariable("_trace", trace);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -164,8 +164,8 @@ public class ControlFlow
|
||||
{
|
||||
throw ret;
|
||||
}
|
||||
final Value retval = val;
|
||||
return (c_, t_) -> retval;
|
||||
Value retval = val;
|
||||
return (ct, tt) -> retval;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -27,21 +27,21 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class DataStructures
|
||||
{
|
||||
public static void apply(final Expression expression)
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
expression.addFunction("l", lv ->
|
||||
lv.size() == 1 && lv.get(0) instanceof final LazyListValue llv
|
||||
? ListValue.wrap(llv.unroll())
|
||||
: new ListValue.ListConstructorValue(lv));
|
||||
|
||||
expression.addFunction("join", (lv) ->
|
||||
expression.addFunction("join", lv ->
|
||||
{
|
||||
if (lv.size() < 2)
|
||||
{
|
||||
throw new InternalExpressionException("'join' takes at least 2 arguments");
|
||||
}
|
||||
final String delimiter = lv.get(0).getString();
|
||||
final List<Value> toJoin;
|
||||
String delimiter = lv.get(0).getString();
|
||||
List<Value> toJoin;
|
||||
if (lv.size() == 2 && lv.get(1) instanceof final LazyListValue llv)
|
||||
{
|
||||
toJoin = llv.unroll();
|
||||
@@ -57,10 +57,10 @@ public class DataStructures
|
||||
return new StringValue(toJoin.stream().map(Value::getString).collect(Collectors.joining(delimiter)));
|
||||
});
|
||||
|
||||
expression.addFunction("split", (lv) ->
|
||||
expression.addFunction("split", lv ->
|
||||
{
|
||||
final Value delimiter;
|
||||
final Value hwat;
|
||||
Value delimiter;
|
||||
Value hwat;
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
hwat = lv.get(0);
|
||||
@@ -78,14 +78,14 @@ public class DataStructures
|
||||
return hwat.split(delimiter);
|
||||
});
|
||||
|
||||
expression.addFunction("slice", (lv) ->
|
||||
expression.addFunction("slice", lv ->
|
||||
{
|
||||
if (lv.size() != 2 && lv.size() != 3)
|
||||
{
|
||||
throw new InternalExpressionException("'slice' takes 2 or 3 arguments");
|
||||
}
|
||||
final Value hwat = lv.get(0);
|
||||
final long from = NumericValue.asNumber(lv.get(1)).getLong();
|
||||
Value hwat = lv.get(0);
|
||||
long from = NumericValue.asNumber(lv.get(1)).getLong();
|
||||
Long to = null;
|
||||
if (lv.size() == 3)
|
||||
{
|
||||
@@ -94,7 +94,7 @@ public class DataStructures
|
||||
return hwat.slice(from, to);
|
||||
});
|
||||
|
||||
expression.addFunction("sort", (lv) ->
|
||||
expression.addFunction("sort", lv ->
|
||||
{
|
||||
List<Value> toSort = lv;
|
||||
if (lv.size() == 1 && lv.get(0) instanceof final ListValue llv)
|
||||
@@ -108,44 +108,44 @@ public class DataStructures
|
||||
// needs lazy cause sort function is reused
|
||||
expression.addLazyFunction("sort_key", (c, t, lv) -> //get working with iterators
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("First argument for 'sort_key' should be a List");
|
||||
}
|
||||
final Value v = lv.get(0).evalValue(c);
|
||||
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");
|
||||
}
|
||||
final List<Value> toSort = new ArrayList<>(list.getItems());
|
||||
List<Value> toSort = new ArrayList<>(list.getItems());
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
Collections.shuffle(toSort);
|
||||
final Value ret = ListValue.wrap(toSort);
|
||||
return (_c, _t) -> ret;
|
||||
Value ret = ListValue.wrap(toSort);
|
||||
return (ct, tt) -> ret;
|
||||
}
|
||||
final LazyValue sortKey = lv.get(1);
|
||||
LazyValue sortKey = lv.get(1);
|
||||
//scoping
|
||||
final LazyValue __ = c.getVariable("_");
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
toSort.sort((v1, v2) -> {
|
||||
c.setVariable("_", (cc, tt) -> v1);
|
||||
final Value ev1 = sortKey.evalValue(c);
|
||||
Value ev1 = sortKey.evalValue(c);
|
||||
c.setVariable("_", (cc, tt) -> v2);
|
||||
final Value ev2 = sortKey.evalValue(c);
|
||||
Value ev2 = sortKey.evalValue(c);
|
||||
return ev1.compareTo(ev2);
|
||||
});
|
||||
//revering scope
|
||||
c.setVariable("_", __);
|
||||
final Value ret = ListValue.wrap(toSort);
|
||||
c.setVariable("_", defaultVal);
|
||||
Value ret = ListValue.wrap(toSort);
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
expression.addFunction("range", (lv) ->
|
||||
expression.addFunction("range", lv ->
|
||||
{
|
||||
NumericValue from = Value.ZERO;
|
||||
NumericValue to;
|
||||
NumericValue step = Value.ONE;
|
||||
final int argsize = lv.size();
|
||||
int argsize = lv.size();
|
||||
if (argsize == 0 || argsize > 3)
|
||||
{
|
||||
throw new InternalExpressionException("'range' accepts from 1 to 3 arguments, not " + argsize);
|
||||
@@ -192,7 +192,7 @@ public class DataStructures
|
||||
{
|
||||
if (container instanceof final LContainerValue lcv)
|
||||
{
|
||||
final ContainerValueInterface outerContainer = lcv.container();
|
||||
ContainerValueInterface outerContainer = lcv.container();
|
||||
if (outerContainer == null)
|
||||
{
|
||||
return LContainerValue.NULL_CONTAINER;
|
||||
@@ -211,23 +211,23 @@ public class DataStructures
|
||||
// lazy cause conditional typing - questionable
|
||||
expression.addLazyFunction("get", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'get' requires parameters");
|
||||
}
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
final Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof final LContainerValue lcv))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final ContainerValueInterface container = lcv.container();
|
||||
ContainerValueInterface container = lcv.container();
|
||||
if (container == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final Value ret = container.get(lcv.address());
|
||||
Value ret = container.get(lcv.address());
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
Value container = lv.get(0).evalValue(c);
|
||||
@@ -243,30 +243,30 @@ public class DataStructures
|
||||
{
|
||||
return (cc, tt) -> Value.NULL;
|
||||
}
|
||||
final Value finalContainer = container;
|
||||
Value finalContainer = container;
|
||||
return (cc, tt) -> finalContainer;
|
||||
});
|
||||
|
||||
// same as `get`
|
||||
expression.addLazyFunction("has", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'has' requires parameters");
|
||||
}
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
final Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof final LContainerValue lcv))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final ContainerValueInterface container = lcv.container();
|
||||
ContainerValueInterface container = lcv.container();
|
||||
if (container == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final Value ret = BooleanValue.of(container.has(lcv.address()));
|
||||
Value ret = BooleanValue.of(container.has(lcv.address()));
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
Value container = lv.get(0).evalValue(c);
|
||||
@@ -282,7 +282,7 @@ public class DataStructures
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final Value ret = BooleanValue.of(cvi.has(lv.get(lv.size() - 1).evalValue(c)));
|
||||
Value ret = BooleanValue.of(cvi.has(lv.get(lv.size() - 1).evalValue(c)));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
@@ -293,17 +293,17 @@ public class DataStructures
|
||||
{
|
||||
throw new InternalExpressionException("'put' takes at least three arguments, a container, address, and values to insert at that index");
|
||||
}
|
||||
final Value container = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
Value container = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (container instanceof final LContainerValue lcv)
|
||||
{
|
||||
final ContainerValueInterface internalContainer = lcv.container();
|
||||
ContainerValueInterface internalContainer = lcv.container();
|
||||
if (internalContainer == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final Value address = lcv.address();
|
||||
final Value what = lv.get(1).evalValue(c);
|
||||
final Value retVal = BooleanValue.of((lv.size() > 2)
|
||||
Value address = lcv.address();
|
||||
Value what = lv.get(1).evalValue(c);
|
||||
Value retVal = BooleanValue.of((lv.size() > 2)
|
||||
? internalContainer.put(address, what, lv.get(2).evalValue(c))
|
||||
: internalContainer.put(address, what));
|
||||
return (cc, tt) -> retVal;
|
||||
@@ -317,9 +317,9 @@ public class DataStructures
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final Value where = lv.get(1).evalValue(c);
|
||||
final Value what = lv.get(2).evalValue(c);
|
||||
final Value retVal = BooleanValue.of((lv.size() > 3)
|
||||
Value where = lv.get(1).evalValue(c);
|
||||
Value what = lv.get(2).evalValue(c);
|
||||
Value retVal = BooleanValue.of((lv.size() > 3)
|
||||
? cvi.put(where, what, lv.get(3).evalValue(c))
|
||||
: cvi.put(where, what));
|
||||
return (cc, tt) -> retVal;
|
||||
@@ -328,23 +328,23 @@ public class DataStructures
|
||||
// same as `get`
|
||||
expression.addLazyFunction("delete", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'delete' requires parameters");
|
||||
}
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
final Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
Value v = lv.get(0).evalValue(c, Context.LVALUE);
|
||||
if (!(v instanceof final LContainerValue lcv))
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final ContainerValueInterface container = lcv.container();
|
||||
ContainerValueInterface container = lcv.container();
|
||||
if (container == null)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final Value ret = BooleanValue.of(container.delete(lcv.address()));
|
||||
Value ret = BooleanValue.of(container.delete(lcv.address()));
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
Value container = lv.get(0).evalValue(c);
|
||||
@@ -360,7 +360,7 @@ public class DataStructures
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
}
|
||||
final Value ret = BooleanValue.of(cvi.delete(lv.get(lv.size() - 1).evalValue(c)));
|
||||
Value ret = BooleanValue.of(cvi.delete(lv.get(lv.size() - 1).evalValue(c)));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
@@ -370,7 +370,7 @@ public class DataStructures
|
||||
{
|
||||
return StringValue.of(new String(Base64.getDecoder().decode(v.getString()), StandardCharsets.UTF_8));
|
||||
}
|
||||
catch (final IllegalArgumentException iae)
|
||||
catch (IllegalArgumentException iae)
|
||||
{
|
||||
throw new ThrowStatement("Invalid b64 string: " + v.getString(), Throwables.B64_ERROR);
|
||||
}
|
||||
@@ -382,7 +382,7 @@ public class DataStructures
|
||||
{
|
||||
return Auxiliary.GSON.fromJson(v.getString(), Value.class);
|
||||
}
|
||||
catch (final JsonParseException jpe)
|
||||
catch (JsonParseException jpe)
|
||||
{
|
||||
throw new ThrowStatement("Invalid json string: " + v.getString(), Throwables.JSON_ERROR);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.Locale;
|
||||
|
||||
public class Functions
|
||||
{
|
||||
public static void apply(final Expression expression) // public just to get the javadoc right
|
||||
public static void apply(Expression expression) // public just to get the javadoc right
|
||||
{
|
||||
// artificial construct to handle user defined functions and function definitions
|
||||
expression.addContextFunction("import", -1, (c, t, lv) ->
|
||||
@@ -45,28 +45,28 @@ public class Functions
|
||||
expression.addCustomFunction("call", new Fluff.AbstractLazyFunction(-1, "call")
|
||||
{
|
||||
@Override
|
||||
public LazyValue lazyEval(final Context c, final Context.Type t, final Expression expr, final Tokenizer.Token tok, final List<LazyValue> lv)
|
||||
public LazyValue lazyEval(Context c, Context.Type t, Expression expr, Tokenizer.Token tok, List<LazyValue> lv)
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'call' expects at least function name to call");
|
||||
}
|
||||
//lv.remove(lv.size()-1); // aint gonna cut it // maybe it will because of the eager eval changes
|
||||
if (t != Context.SIGNATURE) // just call the function
|
||||
{
|
||||
final List<Value> args = Fluff.AbstractFunction.unpackLazy(lv, c, Context.NONE);
|
||||
final FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, args, 0, false, true);
|
||||
final FunctionValue fun = functionArgument.function;
|
||||
List<Value> args = Fluff.AbstractFunction.unpackLazy(lv, c, Context.NONE);
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, args, 0, false, true);
|
||||
FunctionValue fun = functionArgument.function;
|
||||
return fun.callInContext(c, t, functionArgument.args);
|
||||
}
|
||||
// gimme signature
|
||||
final String name = lv.get(0).evalValue(c, Context.NONE).getString();
|
||||
final List<String> args = new ArrayList<>();
|
||||
final List<String> globals = new ArrayList<>();
|
||||
String name = lv.get(0).evalValue(c, Context.NONE).getString();
|
||||
List<String> args = new ArrayList<>();
|
||||
List<String> globals = new ArrayList<>();
|
||||
String varArgs = null;
|
||||
for (int i = 1; i < lv.size(); i++)
|
||||
{
|
||||
final Value v = lv.get(i).evalValue(c, Context.LOCALIZATION);
|
||||
Value v = lv.get(i).evalValue(c, Context.LOCALIZATION);
|
||||
if (!v.isBound())
|
||||
{
|
||||
throw new InternalExpressionException("Only variables can be used in function signature, not " + v.getString());
|
||||
@@ -91,7 +91,7 @@ public class Functions
|
||||
args.add(v.boundVariable);
|
||||
}
|
||||
}
|
||||
final Value retval = new FunctionSignatureValue(name, args, varArgs, globals);
|
||||
Value retval = new FunctionSignatureValue(name, args, varArgs, globals);
|
||||
return (cc, tt) -> retval;
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ public class Functions
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context.Type staticType(final Context.Type outerType)
|
||||
public Context.Type staticType(Context.Type outerType)
|
||||
{
|
||||
return outerType == Context.SIGNATURE ? Context.LOCALIZATION : Context.NONE;
|
||||
}
|
||||
@@ -130,19 +130,19 @@ public class Functions
|
||||
{
|
||||
if (type == Context.MAPDEF)
|
||||
{
|
||||
final Value result = ListValue.of(lv1.evalValue(c), lv2.evalValue(c));
|
||||
Value result = ListValue.of(lv1.evalValue(c), lv2.evalValue(c));
|
||||
return (cc, tt) -> result;
|
||||
}
|
||||
final Value v1 = lv1.evalValue(c, Context.SIGNATURE);
|
||||
Value v1 = lv1.evalValue(c, Context.SIGNATURE);
|
||||
if (!(v1 instanceof final FunctionSignatureValue sign))
|
||||
{
|
||||
throw new InternalExpressionException("'->' operator requires a function signature on the LHS");
|
||||
}
|
||||
final Value result = expression.createUserDefinedFunction(c, sign.identifier(), e, t, sign.arguments(), sign.varArgs(), sign.globals(), lv2);
|
||||
Value result = expression.createUserDefinedFunction(c, sign.identifier(), e, t, sign.arguments(), sign.varArgs(), sign.globals(), lv2);
|
||||
return (cc, tt) -> result;
|
||||
});
|
||||
|
||||
expression.addImpureFunction("return", (lv) -> {
|
||||
expression.addImpureFunction("return", lv -> {
|
||||
throw new ReturnStatement(lv.size() == 0 ? Value.NULL : lv.get(0));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -17,14 +17,14 @@ import java.util.List;
|
||||
|
||||
public class Loops
|
||||
{
|
||||
public static void apply(final Expression expression)
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
// condition and expression will get a bound '_i'
|
||||
// returns last successful expression or false
|
||||
// while(cond, limit, expr) => ??
|
||||
expression.addImpureFunction("break", lv ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new BreakStatement(null);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class Loops
|
||||
|
||||
expression.addImpureFunction("continue", lv ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new ContinueStatement(null);
|
||||
}
|
||||
@@ -51,13 +51,13 @@ public class Loops
|
||||
// lazy
|
||||
expression.addLazyFunction("while", 3, (c, t, lv) ->
|
||||
{
|
||||
final long limit = NumericValue.asNumber(lv.get(1).evalValue(c)).getLong();
|
||||
final LazyValue condition = lv.get(0);
|
||||
final LazyValue expr = lv.get(2);
|
||||
long limit = NumericValue.asNumber(lv.get(1).evalValue(c)).getLong();
|
||||
LazyValue condition = lv.get(0);
|
||||
LazyValue expr = lv.get(2);
|
||||
long i = 0;
|
||||
Value lastOne = Value.NULL;
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
c.setVariable("_", (cc, tt) -> new NumericValue(0).bindTo("_"));
|
||||
while (i < limit && condition.evalValue(c, Context.BOOLEAN).getBoolean())
|
||||
{
|
||||
@@ -65,7 +65,7 @@ public class Loops
|
||||
{
|
||||
lastOne = expr.evalValue(c, t);
|
||||
}
|
||||
catch (final BreakStatement | ContinueStatement stmt)
|
||||
catch (BreakStatement | ContinueStatement stmt)
|
||||
{
|
||||
if (stmt.retval != null)
|
||||
{
|
||||
@@ -77,33 +77,33 @@ public class Loops
|
||||
}
|
||||
}
|
||||
i++;
|
||||
final long seriously = i;
|
||||
long seriously = i;
|
||||
c.setVariable("_", (cc, tt) -> new NumericValue(seriously).bindTo("_"));
|
||||
}
|
||||
//revering scope
|
||||
c.setVariable("_", _val);
|
||||
final Value lastValueNoKidding = lastOne;
|
||||
c.setVariable("_", defaultVal);
|
||||
Value lastValueNoKidding = lastOne;
|
||||
return (cc, tt) -> lastValueNoKidding;
|
||||
});
|
||||
|
||||
// loop(Num, expr) => last_value
|
||||
// loop(Num, expr) => lastdefaultValue
|
||||
// expr receives bounded variable '_' indicating iteration
|
||||
expression.addLazyFunction("loop", 2, (c, t, lv) ->
|
||||
{
|
||||
final long limit = NumericValue.asNumber(lv.get(0).evalValue(c, Context.NONE)).getLong();
|
||||
long limit = NumericValue.asNumber(lv.get(0).evalValue(c, Context.NONE)).getLong();
|
||||
Value lastOne = Value.NULL;
|
||||
final LazyValue expr = lv.get(1);
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
for (long i = 0; i < limit; i++)
|
||||
{
|
||||
final long whyYouAsk = i;
|
||||
long whyYouAsk = i;
|
||||
c.setVariable("_", (cc, tt) -> new NumericValue(whyYouAsk).bindTo("_"));
|
||||
try
|
||||
{
|
||||
lastOne = expr.evalValue(c, t);
|
||||
}
|
||||
catch (final BreakStatement | ContinueStatement stmt)
|
||||
catch (BreakStatement | ContinueStatement stmt)
|
||||
{
|
||||
if (stmt.retval != null)
|
||||
{
|
||||
@@ -116,8 +116,8 @@ public class Loops
|
||||
}
|
||||
}
|
||||
//revering scope
|
||||
c.setVariable("_", _val);
|
||||
final Value trulyLastOne = lastOne;
|
||||
c.setVariable("_", defaultVal);
|
||||
Value trulyLastOne = lastOne;
|
||||
return (cc, tt) -> trulyLastOne;
|
||||
});
|
||||
|
||||
@@ -125,7 +125,7 @@ public class Loops
|
||||
// receives bounded variable '_' with the expression
|
||||
expression.addLazyFunction("map", 2, (c, t, lv) ->
|
||||
{
|
||||
final Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull())
|
||||
{
|
||||
return ListValue.lazyEmpty();
|
||||
@@ -134,25 +134,25 @@ public class Loops
|
||||
{
|
||||
throw new InternalExpressionException("First argument of 'map' function should be a list or iterator");
|
||||
}
|
||||
final Iterator<Value> iterator = alv.iterator();
|
||||
final LazyValue expr = lv.get(1);
|
||||
Iterator<Value> iterator = alv.iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
final LazyValue _iter = c.getVariable("_i");
|
||||
final List<Value> result = new ArrayList<>();
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
LazyValue iterVal = c.getVariable("_i");
|
||||
List<Value> result = new ArrayList<>();
|
||||
for (int i = 0; iterator.hasNext(); i++)
|
||||
{
|
||||
final Value next = iterator.next();
|
||||
final String var = next.boundVariable;
|
||||
Value next = iterator.next();
|
||||
String variable = next.boundVariable;
|
||||
next.bindTo("_");
|
||||
final int doYouReally = i;
|
||||
int doYouReally = i;
|
||||
c.setVariable("_", (cc, tt) -> next);
|
||||
c.setVariable("_i", (cc, tt) -> new NumericValue(doYouReally).bindTo("_i"));
|
||||
try
|
||||
{
|
||||
result.add(expr.evalValue(c, t));
|
||||
}
|
||||
catch (final BreakStatement | ContinueStatement stmt)
|
||||
catch (BreakStatement | ContinueStatement stmt)
|
||||
{
|
||||
if (stmt.retval != null)
|
||||
{
|
||||
@@ -160,17 +160,17 @@ public class Loops
|
||||
}
|
||||
if (stmt instanceof BreakStatement)
|
||||
{
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
break;
|
||||
}
|
||||
}
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
}
|
||||
((AbstractListValue) rval).fatality();
|
||||
final Value ret = ListValue.wrap(result);
|
||||
Value ret = ListValue.wrap(result);
|
||||
//revering scope
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
c.setVariable("_", defaultVal);
|
||||
c.setVariable("_i", iterVal);
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
@@ -179,7 +179,7 @@ public class Loops
|
||||
// produces list of values for which the expression is true
|
||||
expression.addLazyFunction("filter", 2, (c, t, lv) ->
|
||||
{
|
||||
final Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull())
|
||||
{
|
||||
return ListValue.lazyEmpty();
|
||||
@@ -188,18 +188,18 @@ public class Loops
|
||||
{
|
||||
throw new InternalExpressionException("First argument of 'filter' function should be a list or iterator");
|
||||
}
|
||||
final Iterator<Value> iterator = alv.iterator();
|
||||
final LazyValue expr = lv.get(1);
|
||||
Iterator<Value> iterator = alv.iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
final LazyValue _iter = c.getVariable("_i");
|
||||
final List<Value> result = new ArrayList<>();
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
LazyValue iterVal = c.getVariable("_i");
|
||||
List<Value> result = new ArrayList<>();
|
||||
for (int i = 0; iterator.hasNext(); i++)
|
||||
{
|
||||
final Value next = iterator.next();
|
||||
final String var = next.boundVariable;
|
||||
Value next = iterator.next();
|
||||
String veriable = next.boundVariable;
|
||||
next.bindTo("_");
|
||||
final int seriously = i;
|
||||
int seriously = i;
|
||||
c.setVariable("_", (cc, tt) -> next);
|
||||
c.setVariable("_i", (cc, tt) -> new NumericValue(seriously).bindTo("_i"));
|
||||
try
|
||||
@@ -209,7 +209,7 @@ public class Loops
|
||||
result.add(next);
|
||||
}
|
||||
}
|
||||
catch (final BreakStatement | ContinueStatement stmt)
|
||||
catch (BreakStatement | ContinueStatement stmt)
|
||||
{
|
||||
if (stmt.retval != null && stmt.retval.getBoolean())
|
||||
{
|
||||
@@ -217,17 +217,17 @@ public class Loops
|
||||
}
|
||||
if (stmt instanceof BreakStatement)
|
||||
{
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = veriable;
|
||||
break;
|
||||
}
|
||||
}
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = veriable;
|
||||
}
|
||||
((AbstractListValue) rval).fatality();
|
||||
final Value ret = ListValue.wrap(result);
|
||||
Value ret = ListValue.wrap(result);
|
||||
//revering scope
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
c.setVariable("_", defaultVal);
|
||||
c.setVariable("_i", iterVal);
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
@@ -236,7 +236,7 @@ public class Loops
|
||||
// returns first element on the list for which the expr is true
|
||||
expression.addLazyFunction("first", 2, (c, t, lv) ->
|
||||
{
|
||||
final Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull())
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
@@ -245,18 +245,18 @@ public class Loops
|
||||
{
|
||||
throw new InternalExpressionException("First argument of 'first' function should be a list or iterator");
|
||||
}
|
||||
final Iterator<Value> iterator = alv.iterator();
|
||||
final LazyValue expr = lv.get(1);
|
||||
Iterator<Value> iterator = alv.iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
final LazyValue _iter = c.getVariable("_i");
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
LazyValue iterVal = c.getVariable("_i");
|
||||
Value result = Value.NULL;
|
||||
for (int i = 0; iterator.hasNext(); i++)
|
||||
{
|
||||
final Value next = iterator.next();
|
||||
final String var = next.boundVariable;
|
||||
Value next = iterator.next();
|
||||
String variable = next.boundVariable;
|
||||
next.bindTo("_");
|
||||
final int seriously = i;
|
||||
int seriously = i;
|
||||
c.setVariable("_", (cc, tt) -> next);
|
||||
c.setVariable("_i", (cc, tt) -> new NumericValue(seriously).bindTo("_i"));
|
||||
try
|
||||
@@ -264,27 +264,27 @@ public class Loops
|
||||
if (expr.evalValue(c, Context.BOOLEAN).getBoolean())
|
||||
{
|
||||
result = next;
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (final BreakStatement stmt)
|
||||
catch (BreakStatement stmt)
|
||||
{
|
||||
result = stmt.retval == null ? next : stmt.retval;
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
break;
|
||||
}
|
||||
catch (final ContinueStatement ignored)
|
||||
catch (ContinueStatement ignored)
|
||||
{
|
||||
throw new InternalExpressionException("'continue' inside 'first' function has no sense");
|
||||
}
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
}
|
||||
//revering scope
|
||||
((AbstractListValue) rval).fatality();
|
||||
final Value whyWontYouTrustMeJava = result;
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
Value whyWontYouTrustMeJava = result;
|
||||
c.setVariable("_", defaultVal);
|
||||
c.setVariable("_i", iterVal);
|
||||
return (cc, tt) -> whyWontYouTrustMeJava;
|
||||
});
|
||||
|
||||
@@ -293,7 +293,7 @@ public class Loops
|
||||
// returns true if expr is true for all items
|
||||
expression.addLazyFunction("all", 2, (c, t, lv) ->
|
||||
{
|
||||
final Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull())
|
||||
{
|
||||
return LazyValue.TRUE;
|
||||
@@ -302,42 +302,42 @@ public class Loops
|
||||
{
|
||||
throw new InternalExpressionException("First argument of 'all' function should be a list or iterator");
|
||||
}
|
||||
final Iterator<Value> iterator = alv.iterator();
|
||||
final LazyValue expr = lv.get(1);
|
||||
Iterator<Value> iterator = alv.iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
final LazyValue _iter = c.getVariable("_i");
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
LazyValue iterVal = c.getVariable("_i");
|
||||
LazyValue result = LazyValue.TRUE;
|
||||
for (int i = 0; iterator.hasNext(); i++)
|
||||
{
|
||||
final Value next = iterator.next();
|
||||
final String var = next.boundVariable;
|
||||
Value next = iterator.next();
|
||||
String variable = next.boundVariable;
|
||||
next.bindTo("_");
|
||||
final int seriously = i;
|
||||
int seriously = i;
|
||||
c.setVariable("_", (cc, tt) -> next);
|
||||
c.setVariable("_i", (cc, tt) -> new NumericValue(seriously).bindTo("_i"));
|
||||
if (!expr.evalValue(c, Context.BOOLEAN).getBoolean())
|
||||
{
|
||||
result = LazyValue.FALSE;
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
break;
|
||||
}
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
}
|
||||
//revering scope
|
||||
((AbstractListValue) rval).fatality();
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _iter);
|
||||
c.setVariable("_", defaultVal);
|
||||
c.setVariable("_i", iterVal);
|
||||
return result;
|
||||
});
|
||||
|
||||
// runs traditional for(init, condition, increment, body) tri-argument for loop with body in between
|
||||
expression.addLazyFunction("c_for", 4, (c, t, lv) ->
|
||||
{
|
||||
final LazyValue initial = lv.get(0);
|
||||
final LazyValue condition = lv.get(1);
|
||||
final LazyValue increment = lv.get(2);
|
||||
final LazyValue body = lv.get(3);
|
||||
LazyValue initial = lv.get(0);
|
||||
LazyValue condition = lv.get(1);
|
||||
LazyValue increment = lv.get(2);
|
||||
LazyValue body = lv.get(3);
|
||||
int iterations = 0;
|
||||
for (initial.evalValue(c, Context.VOID); condition.evalValue(c, Context.BOOLEAN).getBoolean(); increment.evalValue(c, Context.VOID))
|
||||
{
|
||||
@@ -345,16 +345,16 @@ public class Loops
|
||||
{
|
||||
body.evalValue(c, Context.VOID);
|
||||
}
|
||||
catch (final BreakStatement stmt)
|
||||
catch (BreakStatement stmt)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (final ContinueStatement ignored)
|
||||
catch (ContinueStatement ignored)
|
||||
{
|
||||
}
|
||||
iterations++;
|
||||
}
|
||||
final int finalIterations = iterations;
|
||||
int finalIterations = iterations;
|
||||
return (cc, tt) -> new NumericValue(finalIterations);
|
||||
});
|
||||
|
||||
@@ -363,7 +363,7 @@ public class Loops
|
||||
// can be substituted for first and all, but first is more efficient and all doesn't require knowing list size
|
||||
expression.addLazyFunction("for", 2, (c, t, lv) ->
|
||||
{
|
||||
final Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull())
|
||||
{
|
||||
return LazyValue.ZERO;
|
||||
@@ -372,18 +372,18 @@ public class Loops
|
||||
{
|
||||
throw new InternalExpressionException("First argument of 'for' function should be a list or iterator");
|
||||
}
|
||||
final Iterator<Value> iterator = alv.iterator();
|
||||
final LazyValue expr = lv.get(1);
|
||||
Iterator<Value> iterator = alv.iterator();
|
||||
LazyValue expr = lv.get(1);
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
final LazyValue _ite = c.getVariable("_i");
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
LazyValue iterVal = c.getVariable("_i");
|
||||
int successCount = 0;
|
||||
for (int i = 0; iterator.hasNext(); i++)
|
||||
{
|
||||
final Value next = iterator.next();
|
||||
final String var = next.boundVariable;
|
||||
Value next = iterator.next();
|
||||
String variable = next.boundVariable;
|
||||
next.bindTo("_");
|
||||
final int seriously = i;
|
||||
int seriously = i;
|
||||
c.setVariable("_", (cc, tt) -> next);
|
||||
c.setVariable("_i", (cc, tt) -> new NumericValue(seriously).bindTo("_i"));
|
||||
Value result = Value.FALSE;
|
||||
@@ -391,7 +391,7 @@ public class Loops
|
||||
{
|
||||
result = expr.evalValue(c, t);
|
||||
}
|
||||
catch (final BreakStatement | ContinueStatement stmt)
|
||||
catch (BreakStatement | ContinueStatement stmt)
|
||||
{
|
||||
if (stmt.retval != null)
|
||||
{
|
||||
@@ -399,7 +399,7 @@ public class Loops
|
||||
}
|
||||
if (stmt instanceof BreakStatement)
|
||||
{
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -407,13 +407,13 @@ public class Loops
|
||||
{
|
||||
successCount++;
|
||||
}
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
}
|
||||
//revering scope
|
||||
((AbstractListValue) rval).fatality();
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _ite);
|
||||
final long promiseWontChange = successCount;
|
||||
c.setVariable("_", defaultVal);
|
||||
c.setVariable("_i", iterVal);
|
||||
long promiseWontChange = successCount;
|
||||
return (cc, tt) -> new NumericValue(promiseWontChange);
|
||||
});
|
||||
|
||||
@@ -425,7 +425,7 @@ public class Loops
|
||||
expression.addLazyFunction("reduce", 3, (c, t, lv) ->
|
||||
{
|
||||
|
||||
final Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
Value rval = lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull())
|
||||
{
|
||||
return ListValue.lazyEmpty();
|
||||
@@ -434,28 +434,28 @@ public class Loops
|
||||
{
|
||||
throw new InternalExpressionException("First argument of 'reduce' should be a list or iterator");
|
||||
}
|
||||
final LazyValue expr = lv.get(1);
|
||||
LazyValue expr = lv.get(1);
|
||||
Value acc = lv.get(2).evalValue(c, Context.NONE);
|
||||
final Iterator<Value> iterator = alv.iterator();
|
||||
Iterator<Value> iterator = alv.iterator();
|
||||
|
||||
if (!iterator.hasNext())
|
||||
{
|
||||
final Value seriouslyWontChange = acc;
|
||||
Value seriouslyWontChange = acc;
|
||||
return (cc, tt) -> seriouslyWontChange;
|
||||
}
|
||||
|
||||
//scoping
|
||||
final LazyValue _val = c.getVariable("_");
|
||||
final LazyValue _acc = c.getVariable("_a");
|
||||
final LazyValue _ite = c.getVariable("_i");
|
||||
LazyValue defaultVal = c.getVariable("_");
|
||||
LazyValue accumulatorVal = c.getVariable("_a");
|
||||
LazyValue iterVal = c.getVariable("_i");
|
||||
|
||||
for (int i = 0; iterator.hasNext(); i++)
|
||||
{
|
||||
final Value next = iterator.next();
|
||||
final String var = next.boundVariable;
|
||||
Value next = iterator.next();
|
||||
String variable = next.boundVariable;
|
||||
next.bindTo("_");
|
||||
final Value promiseWontChangeYou = acc;
|
||||
final int seriously = i;
|
||||
Value promiseWontChangeYou = acc;
|
||||
int seriously = i;
|
||||
c.setVariable("_a", (cc, tt) -> promiseWontChangeYou.bindTo("_a"));
|
||||
c.setVariable("_", (cc, tt) -> next);
|
||||
c.setVariable("_i", (cc, tt) -> new NumericValue(seriously).bindTo("_i"));
|
||||
@@ -463,7 +463,7 @@ public class Loops
|
||||
{
|
||||
acc = expr.evalValue(c, t);
|
||||
}
|
||||
catch (final BreakStatement | ContinueStatement stmt)
|
||||
catch (BreakStatement | ContinueStatement stmt)
|
||||
{
|
||||
if (stmt.retval != null)
|
||||
{
|
||||
@@ -471,19 +471,19 @@ public class Loops
|
||||
}
|
||||
if (stmt instanceof BreakStatement)
|
||||
{
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
break;
|
||||
}
|
||||
}
|
||||
next.boundVariable = var;
|
||||
next.boundVariable = variable;
|
||||
}
|
||||
//reverting scope
|
||||
((AbstractListValue) rval).fatality();
|
||||
c.setVariable("_a", _acc);
|
||||
c.setVariable("_", _val);
|
||||
c.setVariable("_i", _ite);
|
||||
c.setVariable("_a", accumulatorVal);
|
||||
c.setVariable("_", defaultVal);
|
||||
c.setVariable("_i", iterVal);
|
||||
|
||||
final Value hopeItsEnoughPromise = acc;
|
||||
Value hopeItsEnoughPromise = acc;
|
||||
return (cc, tt) -> hopeItsEnoughPromise;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -39,17 +39,17 @@ public class Operators
|
||||
put("nextop;", 1);
|
||||
}};
|
||||
|
||||
public static void apply(final Expression expression)
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
expression.addBinaryOperator("+", precedence.get("addition+-"), true, Value::add);
|
||||
expression.addFunction("sum", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size == 0)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
Value accumulator = lv.get(0);
|
||||
for (final Value v : lv.subList(1, size))
|
||||
for (Value v : lv.subList(1, size))
|
||||
{
|
||||
accumulator = accumulator.add(v);
|
||||
}
|
||||
@@ -59,13 +59,13 @@ public class Operators
|
||||
|
||||
expression.addBinaryOperator("-", precedence.get("addition+-"), true, Value::subtract);
|
||||
expression.addFunction("difference", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size == 0)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
Value accumulator = lv.get(0);
|
||||
for (final Value v : lv.subList(1, size))
|
||||
for (Value v : lv.subList(1, size))
|
||||
{
|
||||
accumulator = accumulator.subtract(v);
|
||||
}
|
||||
@@ -75,13 +75,13 @@ public class Operators
|
||||
|
||||
expression.addBinaryOperator("*", precedence.get("multiplication*/%"), true, Value::multiply);
|
||||
expression.addFunction("product", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size == 0)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
Value accumulator = lv.get(0);
|
||||
for (final Value v : lv.subList(1, size))
|
||||
for (Value v : lv.subList(1, size))
|
||||
{
|
||||
accumulator = accumulator.multiply(v);
|
||||
}
|
||||
@@ -91,13 +91,13 @@ public class Operators
|
||||
|
||||
expression.addBinaryOperator("/", precedence.get("multiplication*/%"), true, Value::divide);
|
||||
expression.addFunction("quotient", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size == 0)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
Value accumulator = lv.get(0);
|
||||
for (final Value v : lv.subList(1, size))
|
||||
for (Value v : lv.subList(1, size))
|
||||
{
|
||||
accumulator = accumulator.divide(v);
|
||||
}
|
||||
@@ -111,13 +111,13 @@ public class Operators
|
||||
new NumericValue(java.lang.Math.pow(NumericValue.asNumber(v1).getDouble(), NumericValue.asNumber(v2).getDouble())));
|
||||
|
||||
expression.addFunction("bitwise_and", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size == 0)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
long accumulator = NumericValue.asNumber(lv.get(0)).getLong();
|
||||
for (final Value v : lv.subList(1, size))
|
||||
for (Value v : lv.subList(1, size))
|
||||
{
|
||||
accumulator = accumulator & NumericValue.asNumber(v).getLong();
|
||||
}
|
||||
@@ -125,13 +125,13 @@ public class Operators
|
||||
});
|
||||
|
||||
expression.addFunction("bitwise_xor", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size == 0)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
long accumulator = NumericValue.asNumber(lv.get(0)).getLong();
|
||||
for (final Value v : lv.subList(1, size))
|
||||
for (Value v : lv.subList(1, size))
|
||||
{
|
||||
accumulator = accumulator ^ NumericValue.asNumber(v).getLong();
|
||||
}
|
||||
@@ -139,13 +139,13 @@ public class Operators
|
||||
});
|
||||
|
||||
expression.addFunction("bitwise_or", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size == 0)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
long accumulator = NumericValue.asNumber(lv.get(0)).getLong();
|
||||
for (final Value v : lv.subList(1, size))
|
||||
for (Value v : lv.subList(1, size))
|
||||
{
|
||||
accumulator = accumulator | NumericValue.asNumber(v).getLong();
|
||||
}
|
||||
@@ -155,22 +155,22 @@ public class Operators
|
||||
// lazy cause RHS is only conditional
|
||||
expression.addLazyBinaryOperator("&&", precedence.get("and&&"), false, true, t -> Context.Type.BOOLEAN, (c, t, lv1, lv2) ->
|
||||
{ // todo check how is optimizations going
|
||||
final Value v1 = lv1.evalValue(c, Context.BOOLEAN);
|
||||
Value v1 = lv1.evalValue(c, Context.BOOLEAN);
|
||||
return v1.getBoolean() ? lv2 : ((cc, tt) -> v1);
|
||||
});
|
||||
|
||||
expression.addPureLazyFunction("and", -1, t -> Context.Type.BOOLEAN, (c, t, lv) -> {
|
||||
final int last = lv.size() - 1;
|
||||
int last = lv.size() - 1;
|
||||
if (last == -1)
|
||||
{
|
||||
return LazyValue.TRUE;
|
||||
}
|
||||
for (final LazyValue l : lv.subList(0, last))
|
||||
for (LazyValue l : lv.subList(0, last))
|
||||
{
|
||||
final Value val = l.evalValue(c, Context.Type.BOOLEAN);
|
||||
Value val = l.evalValue(c, Context.Type.BOOLEAN);
|
||||
if (val instanceof final FunctionUnpackedArgumentsValue fuav)
|
||||
{
|
||||
for (final Value it : fuav)
|
||||
for (Value it : fuav)
|
||||
{
|
||||
if (!it.getBoolean())
|
||||
{
|
||||
@@ -193,22 +193,22 @@ public class Operators
|
||||
// lazy cause RHS is only conditional
|
||||
expression.addLazyBinaryOperator("||", precedence.get("or||"), false, true, t -> Context.Type.BOOLEAN, (c, t, lv1, lv2) ->
|
||||
{
|
||||
final Value v1 = lv1.evalValue(c, Context.BOOLEAN);
|
||||
Value v1 = lv1.evalValue(c, Context.BOOLEAN);
|
||||
return v1.getBoolean() ? ((cc, tt) -> v1) : lv2;
|
||||
});
|
||||
|
||||
expression.addPureLazyFunction("or", -1, t -> Context.Type.BOOLEAN, (c, t, lv) -> {
|
||||
final int last = lv.size() - 1;
|
||||
int last = lv.size() - 1;
|
||||
if (last == -1)
|
||||
{
|
||||
return LazyValue.FALSE;
|
||||
}
|
||||
for (final LazyValue l : lv.subList(0, last))
|
||||
for (LazyValue l : lv.subList(0, last))
|
||||
{
|
||||
final Value val = l.evalValue(c, Context.Type.BOOLEAN);
|
||||
Value val = l.evalValue(c, Context.Type.BOOLEAN);
|
||||
if (val instanceof final FunctionUnpackedArgumentsValue fuav)
|
||||
{
|
||||
for (final Value it : fuav)
|
||||
for (Value it : fuav)
|
||||
{
|
||||
if (it.getBoolean())
|
||||
{
|
||||
@@ -233,13 +233,13 @@ public class Operators
|
||||
expression.addBinaryOperator(">", precedence.get("compare>=><=<"), false, (v1, v2) ->
|
||||
BooleanValue.of(v1.compareTo(v2) > 0));
|
||||
expression.addFunction("decreasing", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size < 2)
|
||||
{
|
||||
return Value.TRUE;
|
||||
}
|
||||
Value prev = lv.get(0);
|
||||
for (final Value next : lv.subList(1, size))
|
||||
for (Value next : lv.subList(1, size))
|
||||
{
|
||||
if (prev.compareTo(next) <= 0)
|
||||
{
|
||||
@@ -254,13 +254,13 @@ public class Operators
|
||||
expression.addBinaryOperator(">=", precedence.get("compare>=><=<"), false, (v1, v2) ->
|
||||
BooleanValue.of(v1.compareTo(v2) >= 0));
|
||||
expression.addFunction("nonincreasing", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size < 2)
|
||||
{
|
||||
return Value.TRUE;
|
||||
}
|
||||
Value prev = lv.get(0);
|
||||
for (final Value next : lv.subList(1, size))
|
||||
for (Value next : lv.subList(1, size))
|
||||
{
|
||||
if (prev.compareTo(next) < 0)
|
||||
{
|
||||
@@ -275,13 +275,13 @@ public class Operators
|
||||
expression.addBinaryOperator("<", precedence.get("compare>=><=<"), false, (v1, v2) ->
|
||||
BooleanValue.of(v1.compareTo(v2) < 0));
|
||||
expression.addFunction("increasing", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size < 2)
|
||||
{
|
||||
return Value.TRUE;
|
||||
}
|
||||
Value prev = lv.get(0);
|
||||
for (final Value next : lv.subList(1, size))
|
||||
for (Value next : lv.subList(1, size))
|
||||
{
|
||||
if (prev.compareTo(next) >= 0)
|
||||
{
|
||||
@@ -296,13 +296,13 @@ public class Operators
|
||||
expression.addBinaryOperator("<=", precedence.get("compare>=><=<"), false, (v1, v2) ->
|
||||
BooleanValue.of(v1.compareTo(v2) <= 0));
|
||||
expression.addFunction("nondecreasing", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size < 2)
|
||||
{
|
||||
return Value.TRUE;
|
||||
}
|
||||
Value prev = lv.get(0);
|
||||
for (final Value next : lv.subList(1, size))
|
||||
for (Value next : lv.subList(1, size))
|
||||
{
|
||||
if (prev.compareTo(next) > 0)
|
||||
{
|
||||
@@ -316,21 +316,21 @@ public class Operators
|
||||
expression.addMathematicalBinaryIntFunction("bitwise_shift_left", (num, amount) -> num << amount);
|
||||
expression.addMathematicalBinaryIntFunction("bitwise_shift_right", (num, amount) -> num >> amount);
|
||||
expression.addMathematicalBinaryIntFunction("bitwise_roll_left", (num, num2) -> {
|
||||
final long amount = num2 % 64;
|
||||
final long amountToRoll = 64 - amount;
|
||||
final long rolledBits = ((-1L) >> amountToRoll) << amountToRoll;
|
||||
final long rolledAmount = (num & rolledBits) >> amountToRoll;
|
||||
long amount = num2 % 64;
|
||||
long amountToRoll = 64 - amount;
|
||||
long rolledBits = ((-1L) >> amountToRoll) << amountToRoll;
|
||||
long rolledAmount = (num & rolledBits) >> amountToRoll;
|
||||
return num << amount | rolledAmount;
|
||||
});
|
||||
expression.addMathematicalBinaryIntFunction("bitwise_roll_right", (num, num2) -> {
|
||||
final long amount = num2 % 64;
|
||||
final long amountToRoll = 64 - amount;
|
||||
final long rolledBits = ((-1L) << amountToRoll) >> amountToRoll;
|
||||
final long rolledAmount = (num & rolledBits) << amountToRoll;
|
||||
long amount = num2 % 64;
|
||||
long amountToRoll = 64 - amount;
|
||||
long rolledBits = ((-1L) << amountToRoll) >> amountToRoll;
|
||||
long rolledAmount = (num & rolledBits) << amountToRoll;
|
||||
return num >> amount | rolledAmount;
|
||||
});
|
||||
expression.addMathematicalUnaryIntFunction("bitwise_not", d -> {
|
||||
final long num = (long) d;
|
||||
long num = (long) d;
|
||||
return ~num;
|
||||
});
|
||||
expression.addMathematicalUnaryIntFunction("bitwise_popcount", d -> (long) Long.bitCount((long)d));
|
||||
@@ -340,13 +340,13 @@ public class Operators
|
||||
expression.addBinaryOperator("==", precedence.get("equal==!="), false, (v1, v2) ->
|
||||
v1.equals(v2) ? Value.TRUE : Value.FALSE);
|
||||
expression.addFunction("equal", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size < 2)
|
||||
{
|
||||
return Value.TRUE;
|
||||
}
|
||||
Value prev = lv.get(0);
|
||||
for (final Value next : lv.subList(1, size))
|
||||
for (Value next : lv.subList(1, size))
|
||||
{
|
||||
if (!prev.equals(next))
|
||||
{
|
||||
@@ -360,7 +360,7 @@ public class Operators
|
||||
expression.addBinaryOperator("!=", precedence.get("equal==!="), false, (v1, v2) ->
|
||||
v1.equals(v2) ? Value.FALSE : Value.TRUE);
|
||||
expression.addFunction("unique", lv -> {
|
||||
final int size = lv.size();
|
||||
int size = lv.size();
|
||||
if (size < 2)
|
||||
{
|
||||
return Value.TRUE;
|
||||
@@ -368,7 +368,7 @@ public class Operators
|
||||
// need to order them so same obejects will be next to each other.
|
||||
lv.sort(Comparator.comparingInt(Value::hashCode));
|
||||
Value prev = lv.get(0);
|
||||
for (final Value next : lv.subList(1, size))
|
||||
for (Value next : lv.subList(1, size))
|
||||
{
|
||||
if (prev.equals(next))
|
||||
{
|
||||
@@ -383,12 +383,12 @@ public class Operators
|
||||
// lazy cause of assignment which is non-trivial
|
||||
expression.addLazyBinaryOperator("=", precedence.get("assign=<>"), false, false, t -> Context.Type.LVALUE, (c, t, lv1, lv2) ->
|
||||
{
|
||||
final Value v1 = lv1.evalValue(c, Context.LVALUE);
|
||||
final Value v2 = lv2.evalValue(c);
|
||||
Value v1 = lv1.evalValue(c, Context.LVALUE);
|
||||
Value v2 = lv2.evalValue(c);
|
||||
if (v1 instanceof final ListValue.ListConstructorValue lcv && v2 instanceof final ListValue list)
|
||||
{
|
||||
final List<Value> ll = lcv.getItems();
|
||||
final List<Value> rl = list.getItems();
|
||||
List<Value> ll = lcv.getItems();
|
||||
List<Value> rl = list.getItems();
|
||||
if (ll.size() < rl.size())
|
||||
{
|
||||
throw new InternalExpressionException("Too many values to unpack");
|
||||
@@ -397,28 +397,28 @@ public class Operators
|
||||
{
|
||||
throw new InternalExpressionException("Too few values to unpack");
|
||||
}
|
||||
for (final Value v : ll)
|
||||
for (Value v : ll)
|
||||
{
|
||||
v.assertAssignable();
|
||||
}
|
||||
final Iterator<Value> li = ll.iterator();
|
||||
final Iterator<Value> ri = rl.iterator();
|
||||
Iterator<Value> li = ll.iterator();
|
||||
Iterator<Value> ri = rl.iterator();
|
||||
while (li.hasNext())
|
||||
{
|
||||
final String lname = li.next().getVariable();
|
||||
final Value vval = ri.next().reboundedTo(lname);
|
||||
String lname = li.next().getVariable();
|
||||
Value vval = ri.next().reboundedTo(lname);
|
||||
expression.setAnyVariable(c, lname, (cc, tt) -> vval);
|
||||
}
|
||||
return (cc, tt) -> Value.TRUE;
|
||||
}
|
||||
if (v1 instanceof final LContainerValue lcv)
|
||||
{
|
||||
final ContainerValueInterface container = lcv.container();
|
||||
ContainerValueInterface container = lcv.container();
|
||||
if (container == null)
|
||||
{
|
||||
return (cc, tt) -> Value.NULL;
|
||||
}
|
||||
final Value address = lcv.address();
|
||||
Value address = lcv.address();
|
||||
if (!(container.put(address, v2)))
|
||||
{
|
||||
return (cc, tt) -> Value.NULL;
|
||||
@@ -426,9 +426,9 @@ public class Operators
|
||||
return (cc, tt) -> v2;
|
||||
}
|
||||
v1.assertAssignable();
|
||||
final String varname = v1.getVariable();
|
||||
final Value copy = v2.reboundedTo(varname);
|
||||
final LazyValue boundedLHS = (cc, tt) -> copy;
|
||||
String varname = v1.getVariable();
|
||||
Value copy = v2.reboundedTo(varname);
|
||||
LazyValue boundedLHS = (cc, tt) -> copy;
|
||||
expression.setAnyVariable(c, varname, boundedLHS);
|
||||
return boundedLHS;
|
||||
});
|
||||
@@ -436,12 +436,12 @@ public class Operators
|
||||
// lazy due to assignment
|
||||
expression.addLazyBinaryOperator("+=", precedence.get("assign=<>"), false, false, t -> Context.Type.LVALUE, (c, t, lv1, lv2) ->
|
||||
{
|
||||
final Value v1 = lv1.evalValue(c, Context.LVALUE);
|
||||
final Value v2 = lv2.evalValue(c);
|
||||
Value v1 = lv1.evalValue(c, Context.LVALUE);
|
||||
Value v2 = lv2.evalValue(c);
|
||||
if (v1 instanceof final ListValue.ListConstructorValue lcv && v2 instanceof final ListValue list)
|
||||
{
|
||||
final List<Value> ll = lcv.getItems();
|
||||
final List<Value> rl = list.getItems();
|
||||
List<Value> ll = lcv.getItems();
|
||||
List<Value> rl = list.getItems();
|
||||
if (ll.size() < rl.size())
|
||||
{
|
||||
throw new InternalExpressionException("Too many values to unpack");
|
||||
@@ -450,30 +450,30 @@ public class Operators
|
||||
{
|
||||
throw new InternalExpressionException("Too few values to unpack");
|
||||
}
|
||||
for (final Value v : ll)
|
||||
for (Value v : ll)
|
||||
{
|
||||
v.assertAssignable();
|
||||
}
|
||||
final Iterator<Value> li = ll.iterator();
|
||||
final Iterator<Value> ri = rl.iterator();
|
||||
Iterator<Value> li = ll.iterator();
|
||||
Iterator<Value> ri = rl.iterator();
|
||||
while (li.hasNext())
|
||||
{
|
||||
final Value lval = li.next();
|
||||
final String lname = lval.getVariable();
|
||||
final Value result = lval.add(ri.next()).bindTo(lname);
|
||||
Value lval = li.next();
|
||||
String lname = lval.getVariable();
|
||||
Value result = lval.add(ri.next()).bindTo(lname);
|
||||
expression.setAnyVariable(c, lname, (cc, tt) -> result);
|
||||
}
|
||||
return (cc, tt) -> Value.TRUE;
|
||||
}
|
||||
if (v1 instanceof final LContainerValue lcv)
|
||||
{
|
||||
final ContainerValueInterface cvi = lcv.container();
|
||||
ContainerValueInterface cvi = lcv.container();
|
||||
if (cvi == null)
|
||||
{
|
||||
throw new InternalExpressionException("Failed to resolve left hand side of the += operation");
|
||||
}
|
||||
final Value key = lcv.address();
|
||||
final Value value = cvi.get(key);
|
||||
Value key = lcv.address();
|
||||
Value value = cvi.get(key);
|
||||
if (value instanceof ListValue || value instanceof MapValue)
|
||||
{
|
||||
((AbstractListValue) value).append(v2);
|
||||
@@ -481,14 +481,14 @@ public class Operators
|
||||
}
|
||||
else
|
||||
{
|
||||
final Value res = value.add(v2);
|
||||
Value res = value.add(v2);
|
||||
cvi.put(key, res);
|
||||
return (cc, tt) -> res;
|
||||
}
|
||||
}
|
||||
v1.assertAssignable();
|
||||
final String varname = v1.getVariable();
|
||||
final LazyValue boundedLHS;
|
||||
String varname = v1.getVariable();
|
||||
LazyValue boundedLHS;
|
||||
if (v1 instanceof ListValue || v1 instanceof MapValue)
|
||||
{
|
||||
((AbstractListValue) v1).append(v2);
|
||||
@@ -496,7 +496,7 @@ public class Operators
|
||||
}
|
||||
else
|
||||
{
|
||||
final Value result = v1.add(v2).bindTo(varname);
|
||||
Value result = v1.add(v2).bindTo(varname);
|
||||
boundedLHS = (cc, tt) -> result;
|
||||
}
|
||||
expression.setAnyVariable(c, varname, boundedLHS);
|
||||
@@ -507,8 +507,8 @@ public class Operators
|
||||
{
|
||||
if (v1 instanceof final ListValue.ListConstructorValue lcv1 && v2 instanceof final ListValue.ListConstructorValue lcv2)
|
||||
{
|
||||
final List<Value> ll = lcv1.getItems();
|
||||
final List<Value> rl = lcv2.getItems();
|
||||
List<Value> ll = lcv1.getItems();
|
||||
List<Value> rl = lcv2.getItems();
|
||||
if (ll.size() < rl.size())
|
||||
{
|
||||
throw new InternalExpressionException("Too many values to unpack");
|
||||
@@ -517,22 +517,22 @@ public class Operators
|
||||
{
|
||||
throw new InternalExpressionException("Too few values to unpack");
|
||||
}
|
||||
for (final Value v : ll)
|
||||
for (Value v : ll)
|
||||
{
|
||||
v.assertAssignable();
|
||||
}
|
||||
for (final Value v : rl)
|
||||
for (Value v : rl)
|
||||
{
|
||||
v.assertAssignable();
|
||||
}
|
||||
final Iterator<Value> li = ll.iterator();
|
||||
final Iterator<Value> ri = rl.iterator();
|
||||
Iterator<Value> li = ll.iterator();
|
||||
Iterator<Value> ri = rl.iterator();
|
||||
while (li.hasNext())
|
||||
{
|
||||
final Value lval = li.next();
|
||||
final Value rval = ri.next();
|
||||
final String lname = lval.getVariable();
|
||||
final String rname = rval.getVariable();
|
||||
Value lval = li.next();
|
||||
Value rval = ri.next();
|
||||
String lname = lval.getVariable();
|
||||
String rname = rval.getVariable();
|
||||
lval.reboundedTo(rname);
|
||||
rval.reboundedTo(lname);
|
||||
expression.setAnyVariable(c, lname, (cc, tt) -> rval);
|
||||
@@ -542,10 +542,10 @@ public class Operators
|
||||
}
|
||||
v1.assertAssignable();
|
||||
v2.assertAssignable();
|
||||
final String lvalvar = v1.getVariable();
|
||||
final String rvalvar = v2.getVariable();
|
||||
final Value lval = v2.reboundedTo(lvalvar);
|
||||
final Value rval = v1.reboundedTo(rvalvar);
|
||||
String lvalvar = v1.getVariable();
|
||||
String rvalvar = v2.getVariable();
|
||||
Value lval = v2.reboundedTo(lvalvar);
|
||||
Value rval = v1.reboundedTo(rvalvar);
|
||||
expression.setAnyVariable(c, lvalvar, (cc, tt) -> lval);
|
||||
expression.setAnyVariable(c, rvalvar, (cc, tt) -> rval);
|
||||
return lval;
|
||||
@@ -571,7 +571,7 @@ public class Operators
|
||||
{
|
||||
throw new InternalExpressionException("Unable to unpack a non-list");
|
||||
}
|
||||
final FunctionUnpackedArgumentsValue fuaval = new FunctionUnpackedArgumentsValue(alv.unpack());
|
||||
FunctionUnpackedArgumentsValue fuaval = new FunctionUnpackedArgumentsValue(alv.unpack());
|
||||
return (cc, tt) -> fuaval;
|
||||
});
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ import carpet.script.value.MapValue;
|
||||
import carpet.script.value.NumericValue;
|
||||
import carpet.script.value.StringValue;
|
||||
import carpet.script.value.Value;
|
||||
import org.apache.commons.lang3.text.WordUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
@@ -32,7 +31,7 @@ public class Sys
|
||||
// %[argument_index$][flags][width][.precision][t]conversion
|
||||
private static final Pattern formatPattern = Pattern.compile("%(\\d+\\$)?([-#+ 0,(<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])");
|
||||
|
||||
public static void apply(final Expression expression)
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
expression.addUnaryFunction("hash_code", v -> new NumericValue(v.hashCode()));
|
||||
|
||||
@@ -40,10 +39,10 @@ public class Sys
|
||||
|
||||
expression.addTypedContextFunction("bool", 1, Context.BOOLEAN, (c, t, lv) ->
|
||||
{
|
||||
final Value v = lv.get(0);
|
||||
Value v = lv.get(0);
|
||||
if (v instanceof StringValue)
|
||||
{
|
||||
final String str = v.getString().toLowerCase(Locale.ROOT);
|
||||
String str = v.getString().toLowerCase(Locale.ROOT);
|
||||
if ("false".equals(str) || "null".equals(str))
|
||||
{
|
||||
return Value.FALSE;
|
||||
@@ -66,7 +65,7 @@ public class Sys
|
||||
{
|
||||
return new NumericValue(v.getString());
|
||||
}
|
||||
catch (final NumberFormatException format)
|
||||
catch (NumberFormatException format)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
@@ -74,11 +73,11 @@ public class Sys
|
||||
|
||||
expression.addFunction("str", lv ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'str' requires at least one argument");
|
||||
}
|
||||
final String format = lv.get(0).getString();
|
||||
String format = lv.get(0).getString();
|
||||
if (lv.size() == 1)
|
||||
{
|
||||
return new StringValue(format);
|
||||
@@ -89,8 +88,8 @@ public class Sys
|
||||
lv = list.getItems();
|
||||
argIndex = 0;
|
||||
}
|
||||
final List<Object> args = new ArrayList<>();
|
||||
final Matcher m = formatPattern.matcher(format);
|
||||
List<Object> args = new ArrayList<>();
|
||||
Matcher m = formatPattern.matcher(format);
|
||||
|
||||
for (int i = 0, len = format.length(); i < len; )
|
||||
{
|
||||
@@ -100,7 +99,7 @@ public class Sys
|
||||
// of the format specifier is either fixed text or contains
|
||||
// an invalid format string.
|
||||
// [[scarpet]] but we skip it and let the String.format fail
|
||||
final char fmt = m.group(6).toLowerCase().charAt(0);
|
||||
char fmt = m.group(6).toLowerCase().charAt(0);
|
||||
if (fmt == 's')
|
||||
{
|
||||
if (argIndex >= lv.size())
|
||||
@@ -137,11 +136,7 @@ public class Sys
|
||||
args.add(lv.get(argIndex).getBoolean());
|
||||
argIndex++;
|
||||
}
|
||||
else if (fmt == '%')
|
||||
{
|
||||
//skip /%%
|
||||
}
|
||||
else
|
||||
else if (fmt != '%')
|
||||
{
|
||||
throw new InternalExpressionException("Format not supported: " + m.group(6));
|
||||
}
|
||||
@@ -160,7 +155,7 @@ public class Sys
|
||||
{
|
||||
return new StringValue(String.format(Locale.ROOT, format, args.toArray()));
|
||||
}
|
||||
catch (final IllegalFormatException ife)
|
||||
catch (IllegalFormatException ife)
|
||||
{
|
||||
throw new InternalExpressionException("Illegal string format: " + ife.getMessage());
|
||||
}
|
||||
@@ -170,16 +165,16 @@ public class Sys
|
||||
|
||||
expression.addUnaryFunction("upper", v -> new StringValue(v.getString().toUpperCase(Locale.ROOT)));
|
||||
|
||||
expression.addUnaryFunction("title", v -> new StringValue(WordUtils.capitalizeFully(v.getString())));
|
||||
expression.addUnaryFunction("title", v -> new StringValue(titleCase(v.getString())));
|
||||
|
||||
expression.addFunction("replace", (lv) ->
|
||||
expression.addFunction("replace", lv ->
|
||||
{
|
||||
if (lv.size() != 3 && lv.size() != 2)
|
||||
{
|
||||
throw new InternalExpressionException("'replace' expects string to read, pattern regex, and optional replacement string");
|
||||
}
|
||||
final String data = lv.get(0).getString();
|
||||
final String regex = lv.get(1).getString();
|
||||
String data = lv.get(0).getString();
|
||||
String regex = lv.get(1).getString();
|
||||
String replacement = "";
|
||||
if (lv.size() == 3)
|
||||
{
|
||||
@@ -189,20 +184,20 @@ public class Sys
|
||||
{
|
||||
return new StringValue(data.replaceAll(regex, replacement));
|
||||
}
|
||||
catch (final PatternSyntaxException pse)
|
||||
catch (PatternSyntaxException pse)
|
||||
{
|
||||
throw new InternalExpressionException("Incorrect pattern for 'replace': " + pse.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
expression.addFunction("replace_first", (lv) ->
|
||||
expression.addFunction("replace_first", lv ->
|
||||
{
|
||||
if (lv.size() != 3 && lv.size() != 2)
|
||||
{
|
||||
throw new InternalExpressionException("'replace_first' expects string to read, pattern regex, and optional replacement string");
|
||||
}
|
||||
final String data = lv.get(0).getString();
|
||||
final String regex = lv.get(1).getString();
|
||||
String data = lv.get(0).getString();
|
||||
String regex = lv.get(1).getString();
|
||||
String replacement = "";
|
||||
if (lv.size() == 3)
|
||||
{
|
||||
@@ -215,7 +210,7 @@ public class Sys
|
||||
expression.addUnaryFunction("length", v -> new NumericValue(v.length()));
|
||||
expression.addContextFunction("rand", -1, (c, t, lv) ->
|
||||
{
|
||||
final int argsize = lv.size();
|
||||
int argsize = lv.size();
|
||||
Random randomizer = Sys.randomizer;
|
||||
if (argsize != 1 && argsize != 2)
|
||||
{
|
||||
@@ -225,24 +220,24 @@ public class Sys
|
||||
{
|
||||
randomizer = c.host.getRandom(NumericValue.asNumber(lv.get(1)).getLong());
|
||||
}
|
||||
final Value argument = lv.get(0);
|
||||
Value argument = lv.get(0);
|
||||
if (argument instanceof final ListValue listValue)
|
||||
{
|
||||
final List<Value> list = listValue.getItems();
|
||||
List<Value> list = listValue.getItems();
|
||||
return list.get(randomizer.nextInt(list.size()));
|
||||
}
|
||||
final double value = NumericValue.asNumber(argument).getDouble() * randomizer.nextDouble();
|
||||
double value = NumericValue.asNumber(argument).getDouble() * randomizer.nextDouble();
|
||||
return t == Context.BOOLEAN ? BooleanValue.of(value >= 1.0D) : new NumericValue(value);
|
||||
});
|
||||
expression.addContextFunction("reset_seed", 1, (c, t, lv) -> {
|
||||
final boolean gotIt = c.host.resetRandom(NumericValue.asNumber(lv.get(0)).getLong());
|
||||
boolean gotIt = c.host.resetRandom(NumericValue.asNumber(lv.get(0)).getLong());
|
||||
return BooleanValue.of(gotIt);
|
||||
});
|
||||
|
||||
expression.addFunction("perlin", lv ->
|
||||
{
|
||||
final PerlinNoiseSampler sampler;
|
||||
final Value x;
|
||||
PerlinNoiseSampler sampler;
|
||||
Value x;
|
||||
Value y;
|
||||
Value z;
|
||||
|
||||
@@ -258,7 +253,7 @@ public class Sys
|
||||
sampler = PerlinNoiseSampler.instance;
|
||||
y = Value.NULL;
|
||||
z = Value.NULL;
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'perlin' requires at least one dimension to sample from");
|
||||
}
|
||||
@@ -273,7 +268,7 @@ public class Sys
|
||||
}
|
||||
}
|
||||
|
||||
final double result;
|
||||
double result;
|
||||
|
||||
if (z.isNull())
|
||||
{
|
||||
@@ -293,9 +288,9 @@ public class Sys
|
||||
|
||||
expression.addFunction("simplex", lv ->
|
||||
{
|
||||
final SimplexNoiseSampler sampler;
|
||||
final Value x;
|
||||
final Value y;
|
||||
SimplexNoiseSampler sampler;
|
||||
Value x;
|
||||
Value y;
|
||||
Value z;
|
||||
|
||||
if (lv.size() >= 4)
|
||||
@@ -320,7 +315,7 @@ public class Sys
|
||||
z = NumericValue.asNumber(lv.get(2));
|
||||
}
|
||||
}
|
||||
final double result;
|
||||
double result;
|
||||
|
||||
if (z.isNull())
|
||||
{
|
||||
@@ -336,7 +331,7 @@ public class Sys
|
||||
return new NumericValue(result);
|
||||
});
|
||||
|
||||
expression.addUnaryFunction("print", (v) ->
|
||||
expression.addUnaryFunction("print", v ->
|
||||
{
|
||||
System.out.println(v.getString());
|
||||
return v; // pass through for variables
|
||||
@@ -351,14 +346,14 @@ public class Sys
|
||||
expression.addFunction("convert_date", lv ->
|
||||
{
|
||||
int argsize = lv.size();
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'convert_date' requires at least one parameter");
|
||||
}
|
||||
final Value value = lv.get(0);
|
||||
Value value = lv.get(0);
|
||||
if (argsize == 1 && !(value instanceof ListValue))
|
||||
{
|
||||
final Calendar cal = new GregorianCalendar(Locale.ROOT);
|
||||
Calendar cal = new GregorianCalendar(Locale.ROOT);
|
||||
cal.setTimeInMillis(NumericValue.asNumber(value, "timestamp").getLong());
|
||||
int weekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
||||
if (weekday == 0)
|
||||
@@ -382,7 +377,7 @@ public class Sys
|
||||
lv = list.getItems();
|
||||
argsize = lv.size();
|
||||
}
|
||||
final Calendar cal = new GregorianCalendar(0, 0, 0, 0, 0, 0);
|
||||
Calendar cal = new GregorianCalendar(0, Calendar.JANUARY, 1, 0, 0, 0);
|
||||
|
||||
if (argsize == 3)
|
||||
{
|
||||
@@ -413,15 +408,15 @@ public class Sys
|
||||
// lazy cause evaluates expression multiple times
|
||||
expression.addLazyFunction("profile_expr", 1, (c, t, lv) ->
|
||||
{
|
||||
final LazyValue lazy = lv.get(0);
|
||||
final long end = System.nanoTime() + 50000000L;
|
||||
LazyValue lazy = lv.get(0);
|
||||
long end = System.nanoTime() + 50000000L;
|
||||
long it = 0;
|
||||
while (System.nanoTime() < end)
|
||||
{
|
||||
lazy.evalValue(c);
|
||||
it++;
|
||||
}
|
||||
final Value res = new NumericValue(it);
|
||||
Value res = new NumericValue(it);
|
||||
return (cc, tt) -> res;
|
||||
});
|
||||
|
||||
@@ -430,14 +425,14 @@ public class Sys
|
||||
|
||||
expression.addContextFunction("undef", 1, (c, t, lv) ->
|
||||
{
|
||||
final Value remove = lv.get(0);
|
||||
Value remove = lv.get(0);
|
||||
if (remove instanceof FunctionValue)
|
||||
{
|
||||
c.host.delFunction(expression.module, remove.getString());
|
||||
return Value.NULL;
|
||||
}
|
||||
String varname = remove.getString();
|
||||
final boolean isPrefix = varname.endsWith("*");
|
||||
boolean isPrefix = varname.endsWith("*");
|
||||
if (isPrefix)
|
||||
{
|
||||
varname = varname.replaceAll("\\*+$", "");
|
||||
@@ -472,11 +467,11 @@ public class Sys
|
||||
//deprecate
|
||||
expression.addContextFunction("vars", 1, (c, t, lv) ->
|
||||
{
|
||||
final String prefix = lv.get(0).getString();
|
||||
final List<Value> values = new ArrayList<>();
|
||||
String prefix = lv.get(0).getString();
|
||||
List<Value> values = new ArrayList<>();
|
||||
if (prefix.startsWith("global"))
|
||||
{
|
||||
c.host.globalVariableNames(expression.module, (s) -> s.startsWith(prefix)).forEach(s -> values.add(new StringValue(s)));
|
||||
c.host.globalVariableNames(expression.module, s -> s.startsWith(prefix)).forEach(s -> values.add(new StringValue(s)));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -488,23 +483,43 @@ public class Sys
|
||||
// lazy cause default expression may not be executed if not needed
|
||||
expression.addLazyFunction("system_variable_get", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'system_variable_get' expects at least a key to be fetched");
|
||||
}
|
||||
final Value key = lv.get(0).evalValue(c);
|
||||
Value key = lv.get(0).evalValue(c);
|
||||
if (lv.size() > 1)
|
||||
{
|
||||
c.host.scriptServer().systemGlobals.computeIfAbsent(key, k -> lv.get(1).evalValue(c));
|
||||
}
|
||||
final Value res = c.host.scriptServer().systemGlobals.get(key);
|
||||
Value res = c.host.scriptServer().systemGlobals.get(key);
|
||||
return res == null ? LazyValue.NULL : ((cc, tt) -> res);
|
||||
});
|
||||
|
||||
expression.addContextFunction("system_variable_set", 2, (c, t, lv) ->
|
||||
{
|
||||
final Value res = c.host.scriptServer().systemGlobals.put(lv.get(0), lv.get(1));
|
||||
Value res = c.host.scriptServer().systemGlobals.put(lv.get(0), lv.get(1));
|
||||
return res == null ? Value.NULL : res;
|
||||
});
|
||||
}
|
||||
|
||||
public static String titleCase(String str) {
|
||||
if (str.isEmpty()) {
|
||||
return str;
|
||||
}
|
||||
str = str.toLowerCase();
|
||||
char[] buffer = str.toCharArray();
|
||||
boolean capitalizeNext = true;
|
||||
for (int i = 0; i < buffer.length; i++) {
|
||||
char ch = buffer[i];
|
||||
if (Character.isWhitespace(ch)) {
|
||||
capitalizeNext = true;
|
||||
} else if (capitalizeNext) {
|
||||
buffer[i] = Character.toTitleCase(ch);
|
||||
capitalizeNext = false;
|
||||
}
|
||||
}
|
||||
return new String(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,16 +11,16 @@ import carpet.script.value.Value;
|
||||
|
||||
public class Threading
|
||||
{
|
||||
public static void apply(final Expression expression)
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
expression.addFunctionWithDelegation("task", -1, false, false, (c, t, expr, tok, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
|
||||
}
|
||||
final FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true);
|
||||
final ThreadValue thread = new ThreadValue(Value.NULL, functionArgument.function, expr, tok, c, functionArgument.checkedArgs());
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true);
|
||||
ThreadValue thread = new ThreadValue(Value.NULL, functionArgument.function, expr, tok, c, functionArgument.checkedArgs());
|
||||
Thread.yield();
|
||||
return thread;
|
||||
});
|
||||
@@ -31,18 +31,18 @@ public class Threading
|
||||
{
|
||||
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
|
||||
}
|
||||
final Value queue = lv.get(0);
|
||||
final FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, true);
|
||||
final ThreadValue thread = new ThreadValue(queue, functionArgument.function, expr, tok, c, functionArgument.checkedArgs());
|
||||
Value queue = lv.get(0);
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, true);
|
||||
ThreadValue thread = new ThreadValue(queue, functionArgument.function, expr, tok, c, functionArgument.checkedArgs());
|
||||
Thread.yield();
|
||||
return thread;
|
||||
});
|
||||
|
||||
|
||||
expression.addContextFunction("task_count", -1, (c, t, lv) ->
|
||||
(lv.size() > 0) ? new NumericValue(c.host.taskCount(lv.get(0))) : new NumericValue(c.host.taskCount()));
|
||||
(!lv.isEmpty()) ? new NumericValue(c.host.taskCount(lv.get(0))) : new NumericValue(c.host.taskCount()));
|
||||
|
||||
expression.addUnaryFunction("task_value", (v) ->
|
||||
expression.addUnaryFunction("task_value", v ->
|
||||
{
|
||||
if (!(v instanceof final ThreadValue tv))
|
||||
{
|
||||
@@ -51,7 +51,7 @@ public class Threading
|
||||
return tv.getValue();
|
||||
});
|
||||
|
||||
expression.addUnaryFunction("task_join", (v) ->
|
||||
expression.addUnaryFunction("task_join", v ->
|
||||
{
|
||||
if (!(v instanceof final ThreadValue tv))
|
||||
{
|
||||
@@ -61,13 +61,12 @@ public class Threading
|
||||
});
|
||||
|
||||
expression.addLazyFunction("task_dock", 1, (c, t, lv) ->
|
||||
{
|
||||
// pass through placeholder
|
||||
// implmenetation should dock the task on the main thread.
|
||||
return lv.get(0);
|
||||
});
|
||||
lv.get(0)
|
||||
);
|
||||
|
||||
expression.addUnaryFunction("task_completed", (v) ->
|
||||
expression.addUnaryFunction("task_completed", v ->
|
||||
{
|
||||
if (!(v instanceof final ThreadValue tv))
|
||||
{
|
||||
@@ -79,7 +78,7 @@ public class Threading
|
||||
// lazy cause expr is evaluated in the same type
|
||||
expression.addLazyFunction("synchronize", (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
if (lv.isEmpty())
|
||||
{
|
||||
throw new InternalExpressionException("'synchronize' require at least an expression to synchronize");
|
||||
}
|
||||
@@ -92,15 +91,15 @@ public class Threading
|
||||
}
|
||||
synchronized (c.host.getLock(lockValue))
|
||||
{
|
||||
final Value ret = lv.get(ind).evalValue(c, t);
|
||||
return (_c, _t) -> ret;
|
||||
Value ret = lv.get(ind).evalValue(c, t);
|
||||
return (ct, tt) -> ret;
|
||||
}
|
||||
});
|
||||
|
||||
// lazy since exception expression is very conditional
|
||||
expression.addLazyFunction("sleep", (c, t, lv) ->
|
||||
{
|
||||
final long time = lv.isEmpty() ? 0L : NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
||||
long time = lv.isEmpty() ? 0L : NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
||||
boolean interrupted = false;
|
||||
try
|
||||
{
|
||||
@@ -114,7 +113,7 @@ public class Threading
|
||||
}
|
||||
Thread.yield();
|
||||
}
|
||||
catch (final InterruptedException ignored)
|
||||
catch (InterruptedException ignored)
|
||||
{
|
||||
interrupted = true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user