risky optimizations.

This commit is contained in:
gnembon committed 2020-11-27 02:30:10 -04:00
1 parent 989bc1ec2b
commit 07dce7bc2e
5 files changed
+19 -20

No files matched your search

+4 -4
View File
@@ -658,18 +658,18 @@ public class Expression
case UNARY_OPERATOR: case UNARY_OPERATOR:
{ {
final LazyValue value = stack.pop(); final LazyValue value = stack.pop();
LazyValue result = (c, t) -> operators.get(token.surface).lazyEval(c, t, this, token, value, null).evalValue(c); LazyValue result = (c, t) -> operators.get(token.surface).lazyEval(c, t, this, token, value, null).evalValue(c, t);
stack.push(result); stack.push(result);
break; break;
} }
case OPERATOR: case OPERATOR:
final LazyValue v1 = stack.pop(); final LazyValue v1 = stack.pop();
final LazyValue v2 = stack.pop(); final LazyValue v2 = stack.pop();
LazyValue result = (c,t) -> operators.get(token.surface).lazyEval(c, t,this, token, v2, v1).evalValue(c); LazyValue result = (c,t) -> operators.get(token.surface).lazyEval(c, t,this, token, v2, v1).evalValue(c, t);
stack.push(result); stack.push(result);
break; break;
case VARIABLE: case VARIABLE:
stack.push((c, t) -> getOrSetAnyVariable(c, token.surface).evalValue(c)); stack.push((c, t) -> getOrSetAnyVariable(c, token.surface).evalValue(c, t));
break; break;
case FUNCTION: case FUNCTION:
String name = token.surface; String name = token.surface;
@@ -700,7 +700,7 @@ public class Expression
stack.pop(); stack.pop();
} }
stack.push((c, t) -> f.lazyEval(c, t, this, token, p).evalValue(c)); stack.push((c, t) -> f.lazyEval(c, t, this, token, p).evalValue(c, t));
break; break;
case OPEN_PAREN: case OPEN_PAREN:
stack.push(LazyValue.PARAMS_START); stack.push(LazyValue.PARAMS_START);
+1 -1
View File
@@ -149,7 +149,7 @@ public abstract class Fluff
{ {
try try
{ {
return (c, type_ignored) -> AbstractOperator.this.eval(v1.evalValue(c), v2.evalValue(c)); return (c, type_ignored) -> AbstractOperator.this.eval(v1.evalValue(c, type), v2.evalValue(c, type));
} }
catch (RuntimeException exc) catch (RuntimeException exc)
{ {
@@ -45,11 +45,10 @@ public class Functions {
{ {
FunctionArgument<LazyValue> functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true); FunctionArgument<LazyValue> functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true);
FunctionValue fun = functionArgument.function; FunctionValue fun = functionArgument.function;
Value retval = fun.callInContext(expr, c, t, fun.getExpression(), fun.getToken(), functionArgument.args).evalValue(c); return fun.callInContext(c, t, functionArgument.args);
return (cc, tt) -> retval; ///!!!! dono might need to store expr and token in statics? (e? t?)
} }
// gimme signature // gimme signature
String name = lv.get(0).evalValue(c).getString(); String name = lv.get(0).evalValue(c, Context.NONE).getString();
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
List<String> globals = new ArrayList<>(); List<String> globals = new ArrayList<>();
String varArgs = null; String varArgs = null;
@@ -70,7 +70,7 @@ public class Loops {
// expr receives bounded variable '_' indicating iteration // expr receives bounded variable '_' indicating iteration
expression.addLazyFunction("loop", 2, (c, t, lv) -> expression.addLazyFunction("loop", 2, (c, t, lv) ->
{ {
long limit = NumericValue.asNumber(lv.get(0).evalValue(c)).getLong(); long limit = NumericValue.asNumber(lv.get(0).evalValue(c, Context.NONE)).getLong();
Value lastOne = Value.NULL; Value lastOne = Value.NULL;
LazyValue expr = lv.get(1); LazyValue expr = lv.get(1);
//scoping //scoping
@@ -99,7 +99,7 @@ public class Loops {
// receives bounded variable '_' with the expression // receives bounded variable '_' with the expression
expression.addLazyFunction("map", 2, (c, t, lv) -> expression.addLazyFunction("map", 2, (c, t, lv) ->
{ {
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c, Context.NONE);
if (!(rval instanceof AbstractListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'map' function should be a list or iterator"); throw new InternalExpressionException("First argument of 'map' function should be a list or iterator");
Iterator<Value> iterator = ((AbstractListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
@@ -144,7 +144,7 @@ public class Loops {
// produces list of values for which the expression is true // produces list of values for which the expression is true
expression.addLazyFunction("filter", 2, (c, t, lv) -> expression.addLazyFunction("filter", 2, (c, t, lv) ->
{ {
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c, Context.NONE);
if (!(rval instanceof AbstractListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'filter' function should be a list or iterator"); throw new InternalExpressionException("First argument of 'filter' function should be a list or iterator");
Iterator<Value> iterator = ((AbstractListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
@@ -191,7 +191,7 @@ public class Loops {
expression.addLazyFunction("first", 2, (c, t, lv) -> expression.addLazyFunction("first", 2, (c, t, lv) ->
{ {
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c, Context.NONE);
if (!(rval instanceof AbstractListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'first' function should be a list or iterator"); throw new InternalExpressionException("First argument of 'first' function should be a list or iterator");
Iterator<Value> iterator = ((AbstractListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
@@ -242,7 +242,7 @@ public class Loops {
// returns true if expr is true for all items // returns true if expr is true for all items
expression.addLazyFunction("all", 2, (c, t, lv) -> expression.addLazyFunction("all", 2, (c, t, lv) ->
{ {
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c, Context.NONE);
if (!(rval instanceof AbstractListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'all' function should be a list or iterator"); throw new InternalExpressionException("First argument of 'all' function should be a list or iterator");
Iterator<Value> iterator = ((AbstractListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
@@ -306,7 +306,7 @@ public class Loops {
// can be substituted for first and all, but first is more efficient and all doesn't require knowing list size // 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) -> expression.addLazyFunction("for", 2, (c, t, lv) ->
{ {
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c, Context.NONE);
if (!(rval instanceof AbstractListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'for' function should be a list or iterator"); throw new InternalExpressionException("First argument of 'for' function should be a list or iterator");
Iterator<Value> iterator = ((AbstractListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
@@ -358,8 +358,8 @@ public class Loops {
{ {
LazyValue expr = lv.get(1); LazyValue expr = lv.get(1);
Value acc = lv.get(2).evalValue(c); Value acc = lv.get(2).evalValue(c, Context.NONE);
Value rval= lv.get(0).evalValue(c); Value rval= lv.get(0).evalValue(c, Context.NONE);
if (!(rval instanceof AbstractListValue)) if (!(rval instanceof AbstractListValue))
throw new InternalExpressionException("First argument of 'reduce' should be a list or iterator"); throw new InternalExpressionException("First argument of 'reduce' should be a list or iterator");
Iterator<Value> iterator = ((AbstractListValue) rval).iterator(); Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
@@ -150,11 +150,11 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
return varArgs != null; return varArgs != null;
} }
public LazyValue callInContext(Expression callingExpression, Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) public LazyValue callInContext(Context c, Integer type, List<LazyValue> lazyParams)
{ {
try try
{ {
return lazyEval(c, type, e, t, lazyParams); return lazyEval(c, type, expression, token, lazyParams);
} }
catch (ExpressionException exc) catch (ExpressionException exc)
{ {
@@ -164,12 +164,12 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
catch (InternalExpressionException exc) catch (InternalExpressionException exc)
{ {
exc.stack.add(this); exc.stack.add(this);
throw new ExpressionException(c, e, t, exc.getMessage(), exc.stack); throw new ExpressionException(c, expression, token, exc.getMessage(), exc.stack);
} }
catch (ArithmeticException exc) catch (ArithmeticException exc)
{ {
throw new ExpressionException(c, e, t, "Your math is wrong, "+exc.getMessage(), Collections.singletonList(this)); throw new ExpressionException(c, expression, token, "Your math is wrong, "+exc.getMessage(), Collections.singletonList(this));
} }
} }