This commit is contained in:
gnembon committed 2020-09-09 16:19:12 -03:00
1 parent 59575ec607
commit 33f400e904
17 files changed
+369 -303

No files matched your search

@@ -14,103 +14,102 @@ public class Arithmetic {
"2.71828182845904523536028747135266249775724709369995957496696762772407663");
public static void apply(Expression expression)
{
expression.addLazyFunction("not", 1, (c, t, lv) -> lv.get(0).evalValue(c, Context.BOOLEAN).getBoolean() ? ((cc, tt) -> Value.FALSE) : ((cc, tt) -> Value.TRUE));
expression.addUnaryFunction("fact", (v) ->
{
long number = NumericValue.asNumber(v).getLong();
long factorial = 1;
for (int i = 1; i <= number; i++)
{
expression.addLazyFunction("not", 1, (c, t, lv) -> lv.get(0).evalValue(c, Context.BOOLEAN).getBoolean() ? ((cc, tt) -> Value.FALSE) : ((cc, tt) -> Value.TRUE));
expression.addUnaryFunction("fact", (v) ->
{
long number = NumericValue.asNumber(v).getLong();
long factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial = factorial * i;
}
return new NumericValue(factorial);
});
expression.addMathematicalUnaryFunction("sin", (d) -> Math.sin(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("tan", (d) -> Math.tan(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("asin", (d) -> Math.toDegrees(Math.asin(d)));
expression.addMathematicalUnaryFunction("acos", (d) -> Math.toDegrees(Math.acos(d)));
expression.addMathematicalUnaryFunction("atan", (d) -> Math.toDegrees(Math.atan(d)));
expression.addMathematicalBinaryFunction("atan2", (d, d2) -> Math.toDegrees(Math.atan2(d, d2)) );
expression.addMathematicalUnaryFunction("sinh", Math::sinh );
expression.addMathematicalUnaryFunction("cosh", Math::cosh );
expression.addMathematicalUnaryFunction("tanh", Math::tanh );
expression.addMathematicalUnaryFunction("sec", (d) -> 1.0 / Math.cos(Math.toRadians(d)) ); // Formula: sec(x) = 1 / cos(x)
expression.addMathematicalUnaryFunction("csc", (d) -> 1.0 / Math.sin(Math.toRadians(d)) ); // Formula: csc(x) = 1 / sin(x)
expression.addMathematicalUnaryFunction("sech", (d) -> 1.0 / Math.cosh(d) ); // Formula: sech(x) = 1 / cosh(x)
expression.addMathematicalUnaryFunction("csch", (d) -> 1.0 / Math.sinh(d) ); // Formula: csch(x) = 1 / sinh(x)
expression.addMathematicalUnaryFunction("cot", (d) -> 1.0 / Math.tan(Math.toRadians(d)) ); // Formula: cot(x) = cos(x) / sin(x) = 1 / tan(x)
expression.addMathematicalUnaryFunction("acot", (d) -> Math.toDegrees(Math.atan(1.0 / d)) );// Formula: acot(x) = atan(1/x)
expression.addMathematicalUnaryFunction("coth", (d) -> 1.0 / Math.tanh(d) ); // Formula: coth(x) = 1 / tanh(x)
expression.addMathematicalUnaryFunction("asinh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) + 1)))); // Formula: asinh(x) = ln(x + sqrt(x^2 + 1))
expression.addMathematicalUnaryFunction("acosh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) - 1)))); // Formula: acosh(x) = ln(x + sqrt(x^2 - 1))
expression.addMathematicalUnaryFunction("atanh", (d) -> // Formula: atanh(x) = 0.5*ln((1 + x)/(1 - x))
{
if (Math.abs(d) > 1 || Math.abs(d) == 1)
throw new InternalExpressionException("Number must be |x| < 1");
return 0.5 * Math.log((1 + d) / (1 - d));
});
expression.addMathematicalUnaryFunction("rad", Math::toRadians);
expression.addMathematicalUnaryFunction("deg", Math::toDegrees);
expression.addMathematicalUnaryFunction("ln", Math::log);
expression.addMathematicalUnaryFunction("ln1p", Math::log1p);
expression.addMathematicalUnaryFunction("log10", Math::log10);
expression.addMathematicalUnaryFunction("log", a -> Math.log(a)/Math.log(2));
expression.addMathematicalUnaryFunction("log1p", x -> Math.log1p(x)/Math.log(2));
expression.addMathematicalUnaryFunction("sqrt", Math::sqrt);
expression.addMathematicalUnaryFunction("abs", Math::abs);
expression.addMathematicalUnaryFunction("round", (d) -> (double)Math.round(d));
expression.addMathematicalUnaryFunction("floor", Math::floor);
expression.addMathematicalUnaryFunction("ceil", Math::ceil);
expression.addLazyFunction("mandelbrot", 3, (c, t, lv) -> {
double a0 = NumericValue.asNumber(lv.get(0).evalValue(c)).getDouble();
double b0 = NumericValue.asNumber(lv.get(1).evalValue(c)).getDouble();
long maxiter = NumericValue.asNumber(lv.get(2).evalValue(c)).getLong();
double a = 0.0D;
double b = 0.0D;
long iter = 0;
while(a*a+b*b<4 && iter < maxiter)
{
double temp = a*a-b*b+a0;
b = 2*a*b+b0;
a = temp;
iter++;
}
long iFinal = iter;
return (cc, tt) -> new NumericValue(iFinal);
});
expression.addFunction("max", (lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'max' requires at least one parameter");
Value max = null;
if (lv.size()==1 && lv.get(0) instanceof ListValue)
lv = ((ListValue) lv.get(0)).getItems();
for (Value parameter : lv)
{
if (max == null || parameter.compareTo(max) > 0) max = parameter;
}
return max;
});
expression.addFunction("min", (lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'min' requires at least one parameter");
Value min = null;
if (lv.size()==1 && lv.get(0) instanceof ListValue)
lv = ((ListValue) lv.get(0)).getItems();
for (Value parameter : lv)
{
if (min == null || parameter.compareTo(min) < 0) min = parameter;
}
return min;
});
expression.addUnaryFunction("relu", (v) -> v.compareTo(Value.ZERO) < 0 ? Value.ZERO : v);
factorial = factorial * i;
}
return new NumericValue(factorial);
});
expression.addMathematicalUnaryFunction("sin", (d) -> Math.sin(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("tan", (d) -> Math.tan(Math.toRadians(d)));
expression.addMathematicalUnaryFunction("asin", (d) -> Math.toDegrees(Math.asin(d)));
expression.addMathematicalUnaryFunction("acos", (d) -> Math.toDegrees(Math.acos(d)));
expression.addMathematicalUnaryFunction("atan", (d) -> Math.toDegrees(Math.atan(d)));
expression.addMathematicalBinaryFunction("atan2", (d, d2) -> Math.toDegrees(Math.atan2(d, d2)) );
expression.addMathematicalUnaryFunction("sinh", Math::sinh );
expression.addMathematicalUnaryFunction("cosh", Math::cosh );
expression.addMathematicalUnaryFunction("tanh", Math::tanh );
expression.addMathematicalUnaryFunction("sec", (d) -> 1.0 / Math.cos(Math.toRadians(d)) ); // Formula: sec(x) = 1 / cos(x)
expression.addMathematicalUnaryFunction("csc", (d) -> 1.0 / Math.sin(Math.toRadians(d)) ); // Formula: csc(x) = 1 / sin(x)
expression.addMathematicalUnaryFunction("sech", (d) -> 1.0 / Math.cosh(d) ); // Formula: sech(x) = 1 / cosh(x)
expression.addMathematicalUnaryFunction("csch", (d) -> 1.0 / Math.sinh(d) ); // Formula: csch(x) = 1 / sinh(x)
expression.addMathematicalUnaryFunction("cot", (d) -> 1.0 / Math.tan(Math.toRadians(d)) ); // Formula: cot(x) = cos(x) / sin(x) = 1 / tan(x)
expression.addMathematicalUnaryFunction("acot", (d) -> Math.toDegrees(Math.atan(1.0 / d)) );// Formula: acot(x) = atan(1/x)
expression.addMathematicalUnaryFunction("coth", (d) -> 1.0 / Math.tanh(d) ); // Formula: coth(x) = 1 / tanh(x)
expression.addMathematicalUnaryFunction("asinh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) + 1)))); // Formula: asinh(x) = ln(x + sqrt(x^2 + 1))
expression.addMathematicalUnaryFunction("acosh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) - 1)))); // Formula: acosh(x) = ln(x + sqrt(x^2 - 1))
expression.addMathematicalUnaryFunction("atanh", (d) -> // Formula: atanh(x) = 0.5*ln((1 + x)/(1 - x))
{
if (Math.abs(d) > 1 || Math.abs(d) == 1)
throw new InternalExpressionException("Number must be |x| < 1");
return 0.5 * Math.log((1 + d) / (1 - d));
});
expression.addMathematicalUnaryFunction("rad", Math::toRadians);
expression.addMathematicalUnaryFunction("deg", Math::toDegrees);
expression.addMathematicalUnaryFunction("ln", Math::log);
expression.addMathematicalUnaryFunction("ln1p", Math::log1p);
expression.addMathematicalUnaryFunction("log10", Math::log10);
expression.addMathematicalUnaryFunction("log", a -> Math.log(a)/Math.log(2));
expression.addMathematicalUnaryFunction("log1p", x -> Math.log1p(x)/Math.log(2));
expression.addMathematicalUnaryFunction("sqrt", Math::sqrt);
expression.addMathematicalUnaryFunction("abs", Math::abs);
expression.addMathematicalUnaryFunction("round", (d) -> (double)Math.round(d));
expression.addMathematicalUnaryFunction("floor", Math::floor);
expression.addMathematicalUnaryFunction("ceil", Math::ceil);
expression.addLazyFunction("mandelbrot", 3, (c, t, lv) -> {
double a0 = NumericValue.asNumber(lv.get(0).evalValue(c)).getDouble();
double b0 = NumericValue.asNumber(lv.get(1).evalValue(c)).getDouble();
long maxiter = NumericValue.asNumber(lv.get(2).evalValue(c)).getLong();
double a = 0.0D;
double b = 0.0D;
long iter = 0;
while(a*a+b*b<4 && iter < maxiter)
{
double temp = a*a-b*b+a0;
b = 2*a*b+b0;
a = temp;
iter++;
}
long iFinal = iter;
return (cc, tt) -> new NumericValue(iFinal);
});
expression.addFunction("max", (lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'max' requires at least one parameter");
Value max = null;
if (lv.size()==1 && lv.get(0) instanceof ListValue)
lv = ((ListValue) lv.get(0)).getItems();
for (Value parameter : lv)
{
if (max == null || parameter.compareTo(max) > 0) max = parameter;
}
return max;
});
expression.addFunction("min", (lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'min' requires at least one parameter");
Value min = null;
if (lv.size()==1 && lv.get(0) instanceof ListValue)
lv = ((ListValue) lv.get(0)).getItems();
for (Value parameter : lv)
{
if (min == null || parameter.compareTo(min) < 0) min = parameter;
}
return min;
});
expression.addUnaryFunction("relu", (v) -> v.compareTo(Value.ZERO) < 0 ? Value.ZERO : v);
}
}
@@ -64,7 +64,5 @@ public class ControlFlow {
return (c_, t_) -> val;
}
});
}
}
@@ -51,13 +51,15 @@ public class DataStructures {
return new StringValue(toJoin.stream().map(Value::getString).collect(Collectors.joining(delimiter)));
});
expression.addBinaryFunction("split", (d, v) -> {
expression.addBinaryFunction("split", (d, v) ->
{
String delimiter = d.getString();
String hwat = v.getString();
return ListValue.wrap(Arrays.stream(hwat.split(delimiter)).map(StringValue::new).collect(Collectors.toList()));
});
expression.addFunction("slice", (lv) -> {
expression.addFunction("slice", (lv) ->
{
if (lv.size() != 2 && lv.size() != 3)
throw new InternalExpressionException("'slice' takes 2 or 3 arguments");
@@ -144,19 +146,22 @@ public class DataStructures {
return (cc, tt) -> ret;
});
expression.addUnaryFunction("keys", v -> {
expression.addUnaryFunction("keys", v ->
{
if (v instanceof MapValue)
return new ListValue(((MapValue) v).getMap().keySet());
return Value.NULL;
});
expression.addUnaryFunction("values", v -> {
expression.addUnaryFunction("values", v ->
{
if (v instanceof MapValue)
return new ListValue(((MapValue) v).getMap().values());
return Value.NULL;
});
expression.addUnaryFunction("pairs", v -> {
expression.addUnaryFunction("pairs", v ->
{
if (v instanceof MapValue)
return ListValue.wrap(((MapValue) v).getMap().entrySet().stream().map(
(p) -> ListValue.of(p.getKey(), p.getValue())
@@ -36,7 +36,8 @@ public class Functions {
return (cc, tt) -> list;
});
expression.addLazyFunctionWithDelegation("call",-1, (c, t, expr, tok, lv) -> { // adjust based on c
expression.addLazyFunctionWithDelegation("call",-1, (c, t, expr, tok, lv) ->
{ // adjust based on c
if (lv.size() == 0)
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
@@ -70,6 +71,7 @@ public class Functions {
Value retval = new FunctionSignatureValue(name, args, globals);
return (cc, tt) -> retval;
});
expression.addLazyFunction("outer", 1, (c, t, lv) -> {
if (t != Context.LOCALIZATION)
throw new InternalExpressionException("Outer scoping of variables is only possible in function signatures");
@@ -93,6 +95,5 @@ public class Functions {
});
expression.addFunction("return", (lv) -> { throw new ReturnStatement(lv.size()==0?Value.NULL:lv.get(0));} );
}
}
@@ -21,13 +21,15 @@ public class Loops {
// condition and expression will get a bound '_i'
// returns last successful expression or false
// while(cond, limit, expr) => ??
expression.addFunction("break", lv -> {
expression.addFunction("break", lv ->
{
if (lv.size()==0) throw new BreakStatement(null);
if (lv.size()==1) throw new BreakStatement(lv.get(0));
throw new InternalExpressionException("'break' can only be called with zero or one argument");
});
expression.addFunction("continue", lv -> {
expression.addFunction("continue", lv ->
{
if (lv.size()==0) throw new ContinueStatement(null);
if (lv.size()==1) throw new ContinueStatement(lv.get(0));
throw new InternalExpressionException("'continue' can only be called with zero or one argument");
@@ -273,7 +275,8 @@ public class Loops {
});
// runs traditional for(init, condition, increment, body) tri-argument for loop with body in between
expression.addLazyFunction("c_for", 4, (c, t, lv) -> {
expression.addLazyFunction("c_for", 4, (c, t, lv) ->
{
LazyValue initial = lv.get(0);
LazyValue condition = lv.get(1);
LazyValue increment = lv.get(2);
+16 -73
View File
@@ -4,7 +4,6 @@ import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.LazyValue;
import carpet.script.ScriptHost;
import carpet.script.argument.FunctionArgument;
import carpet.script.exception.InternalExpressionException;
import carpet.script.utils.PerlinNoiseSampler;
import carpet.script.utils.SimplexNoiseSampler;
@@ -13,7 +12,6 @@ import carpet.script.value.ListValue;
import carpet.script.value.NullValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.ThreadValue;
import carpet.script.value.Value;
import org.apache.commons.lang3.text.WordUtils;
@@ -38,7 +36,8 @@ public class Sys {
expression.addUnaryFunction("copy", Value::deepcopy);
expression.addLazyFunction("bool", 1, (c, t, lv) -> {
expression.addLazyFunction("bool", 1, (c, t, lv) ->
{
Value v = lv.get(0).evalValue(c, Context.BOOLEAN);
if (v instanceof StringValue)
{
@@ -51,7 +50,9 @@ public class Sys {
Value retval = new NumericValue(v.getBoolean());
return (cc, tt) -> retval;
});
expression.addUnaryFunction("number", v -> {
expression.addUnaryFunction("number", v ->
{
if (v instanceof NumericValue)
{
return v;
@@ -65,6 +66,7 @@ public class Sys {
return Value.NULL;
}
});
expression.addFunction("str", lv ->
{
if (lv.size() == 0)
@@ -174,9 +176,9 @@ public class Sys {
});
expression.addUnaryFunction("type", v -> new StringValue(v.getTypeString()));
expression.addUnaryFunction("length", v -> new NumericValue(v.length()));
expression.addLazyFunction("rand", -1, (c, t, lv) -> {
expression.addLazyFunction("rand", -1, (c, t, lv) ->
{
int argsize = lv.size();
Random randomizer = Sys.randomizer;
if (argsize != 1 && argsize != 2)
@@ -199,7 +201,8 @@ public class Sys {
return (cc, tt) -> retval;
});
expression.addLazyFunction("perlin", -1, (c, t, lv) -> {
expression.addLazyFunction("perlin", -1, (c, t, lv) ->
{
PerlinNoiseSampler sampler;
Value x, y, z;
@@ -242,7 +245,8 @@ public class Sys {
return (cc, tt) -> ret;
});
expression.addLazyFunction("simplex", -1, (c, t, lv) -> {
expression.addLazyFunction("simplex", -1, (c, t, lv) ->
{
SimplexNoiseSampler sampler;
Value x, y, z;
@@ -373,7 +377,8 @@ public class Sys {
return (cc, tt) -> res;
});
expression.addLazyFunction("var", 1, (c, t, lv) -> {
expression.addLazyFunction("var", 1, (c, t, lv) ->
{
String varname = lv.get(0).evalValue(c).getString();
return expression.getOrSetAnyVariable(c, varname);
});
@@ -418,7 +423,8 @@ public class Sys {
});
//deprecate
expression.addLazyFunction("vars", 1, (c, t, lv) -> {
expression.addLazyFunction("vars", 1, (c, t, lv) ->
{
String prefix = lv.get(0).evalValue(c).getString();
List<Value> values = new ArrayList<>();
if (prefix.startsWith("global"))
@@ -433,67 +439,6 @@ public class Sys {
return (cc, tt) -> retval;
});
expression.addLazyFunctionWithDelegation("task", -1, (c, t, expr, tok, lv) -> {
if (lv.size() == 0)
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, true, false);
Value queue = Value.NULL;
if (lv.size() > functionArgument.offset) queue = lv.get(functionArgument.offset).evalValue(c);
ThreadValue thread = new ThreadValue(queue, functionArgument.function, expr, tok, c, functionArgument.args);
Thread.yield();
return (cc, tt) -> thread;
});
expression.addFunction("task_count", (lv) ->
{
if (lv.size() > 0)
{
return new NumericValue(ThreadValue.taskCount(lv.get(0)));
}
return new NumericValue(ThreadValue.taskCount());
});
expression.addUnaryFunction("task_value", (v) -> {
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'task_value' could only be used with a task value");
return ((ThreadValue) v).getValue();
});
expression.addUnaryFunction("task_join", (v) -> {
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'task_join' could only be used with a task value");
return ((ThreadValue) v).join();
});
expression.addLazyFunction("task_dock", 1, (c, t, lv) -> {
// pass through placeholder
// implmenetation should dock the task on the main thread.
return lv.get(0);
});
expression.addUnaryFunction("task_completed", (v) -> {
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'task_completed' could only be used with a task value");
return new NumericValue(((ThreadValue) v).isFinished());
});
expression.addLazyFunction("synchronize", -1, (c, t, lv) ->
{
if (lv.size() == 0) throw new InternalExpressionException("'synchronize' require at least an expression to synchronize");
Value lockValue = Value.NULL;
int ind = 0;
if (lv.size() == 2)
{
lockValue = lv.get(0).evalValue(c);
ind = 1;
}
synchronized (ThreadValue.getLock(lockValue))
{
Value ret = lv.get(ind).evalValue(c, t);
return (_c, _t) -> ret;
}
});
expression.addLazyFunction("system_variable_get", -1, (c, t, lv) ->
{
if (lv.size() == 0) throw new InternalExpressionException("'system_variable_get' expects at least a key to be fetched");
@@ -515,7 +460,5 @@ public class Sys {
if (res!=null) return (cc, tt) -> res;
return (cc, tt) -> Value.NULL;
});
}
}
@@ -0,0 +1,80 @@
package carpet.script.language;
import carpet.script.Expression;
import carpet.script.argument.FunctionArgument;
import carpet.script.exception.InternalExpressionException;
import carpet.script.value.NumericValue;
import carpet.script.value.ThreadValue;
import carpet.script.value.Value;
public class Threading
{
public static void apply(Expression expression)
{
expression.addLazyFunctionWithDelegation("task", -1, (c, t, expr, tok, lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, true, false);
Value queue = Value.NULL;
if (lv.size() > functionArgument.offset) queue = lv.get(functionArgument.offset).evalValue(c);
ThreadValue thread = new ThreadValue(queue, functionArgument.function, expr, tok, c, functionArgument.args);
Thread.yield();
return (cc, tt) -> thread;
});
expression.addFunction("task_count", (lv) ->
{
if (lv.size() > 0)
{
return new NumericValue(ThreadValue.taskCount(lv.get(0)));
}
return new NumericValue(ThreadValue.taskCount());
});
expression.addUnaryFunction("task_value", (v) ->
{
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'task_value' could only be used with a task value");
return ((ThreadValue) v).getValue();
});
expression.addUnaryFunction("task_join", (v) ->
{
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'task_join' could only be used with a task value");
return ((ThreadValue) v).join();
});
expression.addLazyFunction("task_dock", 1, (c, t, lv) ->
{
// pass through placeholder
// implmenetation should dock the task on the main thread.
return lv.get(0);
});
expression.addUnaryFunction("task_completed", (v) ->
{
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'task_completed' could only be used with a task value");
return new NumericValue(((ThreadValue) v).isFinished());
});
expression.addLazyFunction("synchronize", -1, (c, t, lv) ->
{
if (lv.size() == 0) throw new InternalExpressionException("'synchronize' require at least an expression to synchronize");
Value lockValue = Value.NULL;
int ind = 0;
if (lv.size() == 2)
{
lockValue = lv.get(0).evalValue(c);
ind = 1;
}
synchronized (ThreadValue.getLock(lockValue))
{
Value ret = lv.get(ind).evalValue(c, t);
return (_c, _t) -> ret;
}
});
}
}