script compile time optimizations.
This commit is contained in:
1 parent
06c9aea326
commit
8d97ab26a9
27 files changed
+1071
-193
No files matched your search
@@ -30,13 +30,33 @@ public abstract class Fluff
|
||||
@FunctionalInterface
|
||||
public interface SexFunction<A, B, C, D, E, F, R> { R apply(A a, B b, C c, D d, E e, F f);}
|
||||
|
||||
public interface ILazyFunction
|
||||
public interface EvalNode
|
||||
{
|
||||
/**
|
||||
* @return true if function has constant output if arguments are constant and can be evaluated
|
||||
* statically (without context)
|
||||
*/
|
||||
boolean pure();
|
||||
|
||||
/**
|
||||
* @return true if function has constant output if arguments are constant and can be evaluated
|
||||
* statically (without context)
|
||||
*/
|
||||
boolean transitive();
|
||||
|
||||
/**
|
||||
* @return required argument eval type in case its evaluated statically without context but with a given context type
|
||||
*/
|
||||
default Context.Type staticType(Context.Type outerType) {return transitive()?outerType:Context.NONE;};
|
||||
}
|
||||
|
||||
public interface ILazyFunction extends EvalNode
|
||||
{
|
||||
int getNumParams();
|
||||
|
||||
boolean numParamsVaries();
|
||||
|
||||
LazyValue lazyEval(Context c, Integer type, Expression expr, Tokenizer.Token token, List<LazyValue> lazyParams);
|
||||
LazyValue lazyEval(Context c, Context.Type type, Expression expr, Tokenizer.Token token, List<LazyValue> lazyParams);
|
||||
|
||||
static void checkInterrupts()
|
||||
{
|
||||
@@ -51,13 +71,13 @@ public abstract class Fluff
|
||||
Value eval(List<Value> parameters);
|
||||
}
|
||||
|
||||
public interface ILazyOperator
|
||||
public interface ILazyOperator extends EvalNode
|
||||
{
|
||||
int getPrecedence();
|
||||
|
||||
boolean isLeftAssoc();
|
||||
|
||||
LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, LazyValue v1, LazyValue v2);
|
||||
LazyValue lazyEval(Context c, Context.Type type, Expression e, Tokenizer.Token t, LazyValue v1, LazyValue v2);
|
||||
}
|
||||
|
||||
public interface IOperator extends ILazyOperator
|
||||
@@ -70,7 +90,7 @@ public abstract class Fluff
|
||||
protected String name;
|
||||
int numParams;
|
||||
|
||||
AbstractLazyFunction(int numParams, String name)
|
||||
public AbstractLazyFunction(int numParams, String name)
|
||||
{
|
||||
this.numParams = numParams;
|
||||
this.name = name;
|
||||
@@ -89,7 +109,7 @@ public abstract class Fluff
|
||||
return numParams < 0;
|
||||
}
|
||||
|
||||
public static List<Value> unpackLazy(List<LazyValue> lzargs, Context c, int contextType)
|
||||
public static List<Value> unpackLazy(List<LazyValue> lzargs, Context c, Context.Type contextType)
|
||||
{
|
||||
List<Value> args = new ArrayList<>();
|
||||
for (LazyValue lv : lzargs)
|
||||
@@ -103,7 +123,7 @@ public abstract class Fluff
|
||||
return args;
|
||||
}
|
||||
|
||||
public List<Value> unpackArgs(List<LazyValue> lzargs, Context c, int contextType)
|
||||
public List<Value> unpackArgs(List<LazyValue> lzargs, Context c, Context.Type contextType)
|
||||
{
|
||||
List<Value> args = new ArrayList<>();
|
||||
for (LazyValue lv : lzargs)
|
||||
@@ -118,6 +138,13 @@ public abstract class Fluff
|
||||
throw new InternalExpressionException("Function " + getName() + " expected " + getNumParams() + " parameters, got " + args.size());
|
||||
return args;
|
||||
}
|
||||
|
||||
public static List<LazyValue> lazify(List<Value> args)
|
||||
{
|
||||
List<LazyValue> lzargs = new ArrayList<>(args.size());
|
||||
args.forEach( v -> lzargs.add( (c, t) -> v));
|
||||
return lzargs;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class AbstractFunction extends AbstractLazyFunction implements IFunction
|
||||
@@ -127,7 +154,17 @@ public abstract class Fluff
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context cc, Integer type, Expression e, Tokenizer.Token t, final List<LazyValue> lazyParams)
|
||||
public boolean pure() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean transitive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context cc, Context.Type type, Expression e, Tokenizer.Token t, final List<LazyValue> lazyParams)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -135,7 +172,7 @@ public abstract class Fluff
|
||||
{ // eager evaluation always ignores the required type and evals params by none default
|
||||
private List<Value> params;
|
||||
|
||||
public Value evalValue(Context c, Integer type) {
|
||||
public Value evalValue(Context c, Context.Type type) {
|
||||
ILazyFunction.checkInterrupts();
|
||||
return AbstractFunction.this.eval(getParams(c));
|
||||
}
|
||||
@@ -143,7 +180,7 @@ public abstract class Fluff
|
||||
private List<Value> getParams(Context c) {
|
||||
if (params == null) {
|
||||
// very likely needs to be dynamic, so not static like here, or remember if it was.
|
||||
params = unpackArgs(lazyParams, c, Context.NONE);
|
||||
params = unpackArgs(lazyParams, c, Context.Type.NONE);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -187,13 +224,22 @@ public abstract class Fluff
|
||||
AbstractOperator(int precedence, boolean leftAssoc) {
|
||||
super(precedence, leftAssoc);
|
||||
}
|
||||
@Override
|
||||
public boolean pure() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context cc, Integer type, Expression e, Tokenizer.Token t, final LazyValue v1, final LazyValue v2)
|
||||
public boolean transitive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context cc, Context.Type type, Expression e, Tokenizer.Token t, final LazyValue v1, final LazyValue v2)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (c, type_ignored) -> AbstractOperator.this.eval(v1.evalValue(c, Context.NONE), v2.evalValue(c, Context.NONE));
|
||||
return (c, type_ignored) -> AbstractOperator.this.eval(v1.evalValue(c, Context.Type.NONE), v2.evalValue(c, Context.Type.NONE));
|
||||
}
|
||||
catch (RuntimeException exc)
|
||||
{
|
||||
@@ -207,9 +253,18 @@ public abstract class Fluff
|
||||
AbstractUnaryOperator(int precedence, boolean leftAssoc) {
|
||||
super(precedence, leftAssoc);
|
||||
}
|
||||
@Override
|
||||
public boolean pure() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context cc, Integer type, Expression e, Tokenizer.Token t, final LazyValue v1, final LazyValue v2)
|
||||
public boolean transitive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context cc, Context.Type type, Expression e, Tokenizer.Token t, final LazyValue v1, final LazyValue v2)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -217,7 +272,7 @@ public abstract class Fluff
|
||||
{
|
||||
throw new ExpressionException(cc, e, t, "Did not expect a second parameter for unary operator");
|
||||
}
|
||||
return (c, ignored_type) -> AbstractUnaryOperator.this.evalUnary(v1.evalValue(c, Context.NONE));
|
||||
return (c, ignored_type) -> AbstractUnaryOperator.this.evalUnary(v1.evalValue(c, Context.Type.NONE));
|
||||
}
|
||||
catch (RuntimeException exc)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user