Merge branch 'master' into 1.17

This commit is contained in:
gnembon committed 2021-05-05 01:31:00 -03:00
commit 820c9b6489
31 files changed
+1188 -211

No files matched your search

+13
View File
@@ -444,6 +444,19 @@ public class CarpetSettings
)
public static boolean scriptsAutoload = true;
@Rule(
desc = "Enables scripts debugging messages in system log",
category = SCARPET
)
public static boolean scriptsDebugging = false;
@Rule(
desc = "Enables scripts optimization",
category = SCARPET
)
public static boolean scriptsOptimization = true;
@Rule(desc = "Enables /player command to control/spawn players", category = COMMAND)
public static String commandPlayer = "ops";
@@ -1004,15 +1004,7 @@ public class CarpetScriptHost extends ScriptHost
return null;
}
String shebang = message;
if (expr.module != null)
{
shebang += " in " + expr.module.getName() + "";
}
else
{
shebang += " in system chat";
}
String shebang = message+" in "+expr.getModuleName();
if (token != null)
{
String[] lines = expr.getCodeString().split("\n");
+15 -11
View File
@@ -8,17 +8,21 @@ import java.util.Set;
public class Context
{
public static final int NONE = 0;
public static final int VOID = 1;
public static final int BOOLEAN = 2;
public static final int NUMBER = 3;
public static final int STRING = 4;
public static final int LIST = 5;
public static final int ITERATOR = 6;
public static final int SIGNATURE = 7;
public static final int LOCALIZATION = 8;
public static final int LVALUE = 9;
public static final int MAPDEF = 10;
public enum Type
{
NONE, VOID, BOOLEAN, NUMBER, STRING, LIST, ITERATOR, SIGNATURE, LOCALIZATION, LVALUE, MAPDEF
}
public static final Type NONE = Type.NONE;
public static final Type VOID = Type.VOID;
public static final Type BOOLEAN = Type.BOOLEAN;
public static final Type NUMBER = Type.NUMBER;
public static final Type STRING = Type.STRING;
public static final Type LIST = Type.LIST;
public static final Type ITERATOR = Type.ITERATOR;
public static final Type SIGNATURE = Type.SIGNATURE;
public static final Type LOCALIZATION = Type.LOCALIZATION;
public static final Type LVALUE = Type.LVALUE;
public static final Type MAPDEF = Type.MAPDEF;
public Map<String, LazyValue> variables = new HashMap<>();
+575 -77
View File
@@ -1,5 +1,6 @@
package carpet.script;
import carpet.CarpetSettings;
import carpet.script.Fluff.AbstractFunction;
import carpet.script.Fluff.AbstractLazyFunction;
import carpet.script.Fluff.AbstractLazyOperator;
@@ -32,6 +33,8 @@ import carpet.script.value.FunctionValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.math.BigInteger;
@@ -45,6 +48,7 @@ import java.util.Stack;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static java.lang.Math.max;
import static java.lang.Math.min;
@@ -62,7 +66,7 @@ public class Expression
public String getModuleName()
{
return module == null?null:module.getName();
return module == null?"system chat":module.getName();
}
public void asATextSource()
@@ -86,6 +90,27 @@ public class Expression
private final Map<String, ILazyFunction> functions = new Object2ObjectOpenHashMap<>();
public Set<String> getFunctionNames() {return functions.keySet();}
private final Map<String, String> functionalEquivalence = new Object2ObjectOpenHashMap<>();
public void addFunctionalEquivalence(String operator, String function)
{
assert operators.containsKey(operator);
assert functions.containsKey(function);
functionalEquivalence.put(operator, function);
}
private final Map<String, Value> constants = ImmutableMap.of(
"euler", Arithmetic.euler,
"pi", Arithmetic.PI,
"null", Value.NULL,
"true", Value.TRUE,
"false", Value.FALSE
);
protected Value getConstantFor(String surface)
{
return constants.get(surface);
}
public List<String> getExpressionSnippet(Tokenizer.Token token)
{
String code = this.getCodeString();
@@ -139,13 +164,28 @@ public class Expression
}
public void addLazyUnaryOperator(String surface, int precedence, boolean leftAssoc,
TriFunction<Context, Integer, LazyValue, LazyValue> lazyfun)
public void addLazyUnaryOperator(String surface, int precedence, boolean leftAssoc, boolean pure, Function<Context.Type,Context.Type> staticTyper,
TriFunction<Context, Context.Type, LazyValue, LazyValue> lazyfun)
{
operators.put(surface+"u", new AbstractLazyOperator(precedence, leftAssoc)
{
@Override
public LazyValue lazyEval(Context c, Integer t, Expression e, Tokenizer.Token token, LazyValue v, LazyValue v2)
public boolean pure() {
return pure;
}
@Override
public boolean transitive() {
return false;
}
@Override
public Context.Type staticType(Context.Type outerType) {
return staticTyper.apply(outerType);
}
@Override
public LazyValue lazyEval(Context c, Context.Type t, Expression e, Tokenizer.Token token, LazyValue v, LazyValue v2)
{
try
{
@@ -160,13 +200,23 @@ public class Expression
}
public void addLazyBinaryOperatorWithDelegation(String surface, int precedence, boolean leftAssoc,
SexFunction<Context, Integer, Expression, Tokenizer.Token, LazyValue, LazyValue, LazyValue> lazyfun)
public void addLazyBinaryOperatorWithDelegation(String surface, int precedence, boolean leftAssoc, boolean pure,
SexFunction<Context, Context.Type, Expression, Tokenizer.Token, LazyValue, LazyValue, LazyValue> lazyfun)
{
operators.put(surface, new AbstractLazyOperator(precedence, leftAssoc)
{
@Override
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, LazyValue v1, LazyValue v2)
public boolean pure() {
return pure;
}
@Override
public boolean transitive() {
return false;
}
@Override
public LazyValue lazyEval(Context c, Context.Type type, Expression e, Tokenizer.Token t, LazyValue v1, LazyValue v2)
{
try
{
@@ -180,13 +230,28 @@ public class Expression
});
}
public void addLazyFunctionWithDelegation(String name, int numpar,
QuinnFunction<Context, Integer, Expression, Tokenizer.Token, List<LazyValue>, LazyValue> lazyfun)
public void addCustomFunction(String name, ILazyFunction fun)
{
functions.put(name, fun);
}
public void addLazyFunctionWithDelegation(String name, int numpar, boolean pure, boolean transitive,
QuinnFunction<Context, Context.Type, Expression, Tokenizer.Token, List<LazyValue>, LazyValue> lazyfun)
{
functions.put(name, new AbstractLazyFunction(numpar, name)
{
@Override
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lv)
public boolean pure() {
return pure;
}
@Override
public boolean transitive() {
return transitive;
}
@Override
public LazyValue lazyEval(Context c, Context.Type type, Expression e, Tokenizer.Token t, List<LazyValue> lv)
{
ILazyFunction.checkInterrupts();
try
@@ -201,13 +266,23 @@ public class Expression
});
}
public void addFunctionWithDelegation(String name, int numpar,
QuinnFunction<Context, Integer, Expression, Tokenizer.Token, List<Value>, Value> fun)
public void addFunctionWithDelegation(String name, int numpar, boolean pure, boolean transitive,
QuinnFunction<Context, Context.Type, Expression, Tokenizer.Token, List<Value>, Value> fun)
{
functions.put(name, new AbstractLazyFunction(numpar, name)
{
@Override
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lv)
public boolean pure() {
return pure;
}
@Override
public boolean transitive() {
return transitive;
}
@Override
public LazyValue lazyEval(Context c, Context.Type type, Expression e, Tokenizer.Token t, List<LazyValue> lv)
{
try
{
@@ -222,13 +297,30 @@ public class Expression
});
}
public void addLazyBinaryOperator(String surface, int precedence, boolean leftAssoc,
QuadFunction<Context, Integer, LazyValue, LazyValue, LazyValue> lazyfun)
public void addLazyBinaryOperator(String surface, int precedence, boolean leftAssoc, boolean pure, Function<Context.Type, Context.Type> typer,
QuadFunction<Context, Context.Type, LazyValue, LazyValue, LazyValue> lazyfun)
{
operators.put(surface, new AbstractLazyOperator(precedence, leftAssoc)
{
@Override
public LazyValue lazyEval(Context c, Integer t, Expression e, Tokenizer.Token token, LazyValue v1, LazyValue v2)
public boolean pure() {
return pure;
}
@Override
public boolean transitive() {
return false;
}
@Override
public Context.Type staticType(Context.Type outerType)
{
return typer.apply(outerType);
}
@Override
public LazyValue lazyEval(Context c, Context.Type t, Expression e, Tokenizer.Token token, LazyValue v1, LazyValue v2)
{
ILazyFunction.checkInterrupts();
try
@@ -243,13 +335,23 @@ public class Expression
});
}
public void addBinaryContextOperator(String surface, int precedence, boolean leftAssoc,
QuadFunction<Context, Integer, Value, Value, Value> fun)
public void addBinaryContextOperator(String surface, int precedence, boolean leftAssoc, boolean pure, boolean transitive,
QuadFunction<Context, Context.Type, Value, Value, Value> fun)
{
operators.put(surface, new AbstractLazyOperator(precedence, leftAssoc)
{
@Override
public LazyValue lazyEval(Context c, Integer t, Expression e, Tokenizer.Token token, LazyValue v1, LazyValue v2)
public boolean pure() {
return pure;
}
@Override
public boolean transitive() {
return transitive;
}
@Override
public LazyValue lazyEval(Context c, Context.Type t, Expression e, Tokenizer.Token token, LazyValue v1, LazyValue v2)
{
try
{
@@ -312,6 +414,20 @@ public class Expression
});
}
public void addImpureUnaryFunction(String name, Function<Value, Value> fun)
{
functions.put(name, new AbstractFunction(1, name)
{
@Override
public boolean pure() {
return false;
}
@Override
public Value eval(List<Value> parameters) { return fun.apply(parameters.get(0)); }
});
}
public void addBinaryFunction(String name, BiFunction<Value, Value, Value> fun)
{
functions.put(name, new AbstractFunction(2, name)
@@ -330,6 +446,20 @@ public class Expression
});
}
public void addImpureFunction(String name, Function<List<Value>, Value> fun)
{
functions.put(name, new AbstractFunction(-1, name)
{
@Override
public boolean pure() {
return false;
}
@Override
public Value eval(List<Value> parameters) { return fun.apply(parameters); }
});
}
public void addMathematicalUnaryFunction(String name, Function<Double, Double> fun)
{
addUnaryFunction(name, (v) -> new NumericValue(fun.apply(NumericValue.asNumber(v).getDouble())));
@@ -347,12 +477,22 @@ public class Expression
}
public void addLazyFunction(String name, int num_params, TriFunction<Context, Integer, List<LazyValue>, LazyValue> fun)
public void addLazyFunction(String name, int num_params, TriFunction<Context, Context.Type, List<LazyValue>, LazyValue> fun)
{
functions.put(name, new AbstractLazyFunction(num_params, name)
{
@Override
public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
public boolean pure() {
return false;
}
@Override
public boolean transitive() {
return false;
}
@Override
public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
{
ILazyFunction.checkInterrupts();
try
@@ -367,12 +507,58 @@ public class Expression
});
}
public void addContextFunction(String name, int num_params, TriFunction<Context, Integer, List<Value>, Value> fun)
public void addPureLazyFunction(String name, int num_params, Function<Context.Type, Context.Type> typer, TriFunction<Context, Context.Type, List<LazyValue>, LazyValue> fun)
{
functions.put(name, new AbstractLazyFunction(num_params, name)
{
@Override
public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
public boolean pure() {
return true;
}
@Override
public boolean transitive() {
return false;
}
@Override
public Context.Type staticType(Context.Type outerType)
{
return typer.apply(outerType);
}
@Override
public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
{
ILazyFunction.checkInterrupts();
try
{
return fun.apply(c, i, lazyParams);
}
catch (RuntimeException exc)
{
throw handleCodeException(c, exc, e, t);
}
}
});
}
public void addContextFunction(String name, int num_params, TriFunction<Context, Context.Type, List<Value>, Value> fun)
{
functions.put(name, new AbstractLazyFunction(num_params, name)
{
@Override
public boolean pure() {
return false;
}
@Override
public boolean transitive() {
return false;
}
@Override
public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
{
ILazyFunction.checkInterrupts();
try
@@ -388,12 +574,27 @@ public class Expression
});
}
public void addTypedContextFunction(String name, int num_params, int reqType, TriFunction<Context, Integer, List<Value>, Value> fun)
public void addTypedContextFunction(String name, int num_params, Context.Type reqType, TriFunction<Context, Context.Type, List<Value>, Value> fun)
{
functions.put(name, new AbstractLazyFunction(num_params, name)
{
@Override
public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
public boolean pure() {
return true;
}
@Override
public boolean transitive() {
return false;
}
@Override
public Context.Type staticType(Context.Type outerType) {
return reqType;
}
@Override
public LazyValue lazyEval(Context c, Context.Type i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
{
try
{
@@ -451,7 +652,22 @@ public class Expression
}
@Override
public LazyValue lazyEval(Context c, Integer type, Expression expr, Tokenizer.Token token, List<LazyValue> lazyParams)
public boolean pure() {
return originalFunction.pure();
}
@Override
public boolean transitive() {
return originalFunction.transitive();
}
@Override
public Context.Type staticType(Context.Type outerType) {
return originalFunction.staticType(outerType);
}
@Override
public LazyValue lazyEval(Context c, Context.Type type, Expression expr, Tokenizer.Token token, List<LazyValue> lazyParams)
{
c.host.issueDeprecation(copy+"(...)");
return originalFunction.lazyEval(c, type, expr, token, lazyParams);
@@ -474,7 +690,7 @@ public class Expression
public LazyValue getOrSetAnyVariable(Context c, String name)
{
LazyValue var = null;
LazyValue var;
if (!name.startsWith("global_"))
{
var = c.getVariable(name);
@@ -599,16 +815,16 @@ public class Expression
case OPEN_PAREN:
if (previousToken != null)
{
if (previousToken.type == Tokenizer.Token.TokenType.LITERAL || previousToken.type == Tokenizer.Token.TokenType.CLOSE_PAREN
|| previousToken.type == Tokenizer.Token.TokenType.VARIABLE
|| previousToken.type == Tokenizer.Token.TokenType.HEX_LITERAL)
{
//if (previousToken.type == Tokenizer.Token.TokenType.LITERAL || previousToken.type == Tokenizer.Token.TokenType.CLOSE_PAREN
// || previousToken.type == Tokenizer.Token.TokenType.VARIABLE
// || previousToken.type == Tokenizer.Token.TokenType.HEX_LITERAL)
//{
// Implicit multiplication, e.g. 23(a+b) or (a+b)(a-b)
Tokenizer.Token multiplication = new Tokenizer.Token();
multiplication.append("*");
multiplication.type = Tokenizer.Token.TokenType.OPERATOR;
stack.push(multiplication);
}
// Tokenizer.Token multiplication = new Tokenizer.Token();
// multiplication.append("*");
// multiplication.type = Tokenizer.Token.TokenType.OPERATOR;
// stack.push(multiplication);
//}
// if the ( is preceded by a valid function, then it
// denotes the start of a parameter list
if (previousToken.type == Tokenizer.Token.TokenType.FUNCTION)
@@ -675,19 +891,15 @@ public class Expression
}
public Value eval(Context c)
{
return eval(c, Context.NONE);
}
public Value eval(Context c, Integer expectedType)
{
if (ast == null)
{
ast = getAST(c);
}
return evalValue(() -> ast, c, expectedType);
return evalValue(() -> ast, c, Context.Type.NONE);
}
public Value evalValue(Supplier<LazyValue> exprProvider, Context c, Integer expectedType)
public Value evalValue(Supplier<LazyValue> exprProvider, Context c, Context.Type expectedType)
{
try
{
@@ -715,35 +927,67 @@ public class Expression
}
}
private LazyValue getAST(Context context)
public static class ExpressionNode
{
Stack<LazyValue> stack = new Stack<>();
List<Tokenizer.Token> rpn = shuntingYard(context);
validate(context, rpn);
for (final Tokenizer.Token token : rpn)
public LazyValue op;
public List<ExpressionNode> args;
public Tokenizer.Token token;
public List<Tokenizer.Token> range;
/**
* The Value representation of the left parenthesis, used for parsing
* varying numbers of function parameters.
*/
public static final ExpressionNode PARAMS_START = new ExpressionNode(null, null, Tokenizer.Token.NONE);
public ExpressionNode(LazyValue op, List<ExpressionNode> args, Tokenizer.Token token)
{
this.op = op;
this.args = args;
this.token = token;
range = new ArrayList<>();
range.add(token);
}
public static ExpressionNode ofConstant(Value val, Tokenizer.Token token)
{
return new ExpressionNode(new LazyValue.Constant(val), Collections.emptyList(), token);
}
}
private ExpressionNode RPNToParseTree(List<Tokenizer.Token> tokens, Context context)
{
Stack<ExpressionNode> nodeStack = new Stack<>();
for (final Tokenizer.Token token : tokens)
{
switch (token.type)
{
case UNARY_OPERATOR:
{
final LazyValue value = stack.pop();
LazyValue result = (c, t) -> operators.get(token.surface).lazyEval(c, t, this, token, value, null).evalValue(c, t);
stack.push(result);
final ExpressionNode node = nodeStack.pop();
LazyValue result = (c, t) -> operators.get(token.surface).lazyEval(c, t, this, token, node.op, null).evalValue(c, t);
nodeStack.push(new ExpressionNode(result, Collections.singletonList(node), token));
break;
}
case OPERATOR:
final LazyValue v1 = stack.pop();
final LazyValue v2 = stack.pop();
LazyValue result = (c,t) -> operators.get(token.surface).lazyEval(c, t,this, token, v2, v1).evalValue(c, t);
stack.push(result);
final ExpressionNode v1 = nodeStack.pop();
final ExpressionNode v2 = nodeStack.pop();
LazyValue result = (c,t) -> operators.get(token.surface).lazyEval(c, t,this, token, v2.op, v1.op).evalValue(c, t);
nodeStack.push(new ExpressionNode(result, ImmutableList.of(v2, v1), token ));
break;
case VARIABLE:
stack.push((c, t) -> getOrSetAnyVariable(c, token.surface).evalValue(c, t));
Value constant = getConstantFor(token.surface);
if (constant != null)
{
token.morph(Tokenizer.Token.TokenType.CONSTANT, token.surface);
nodeStack.push(new ExpressionNode(LazyValue.ofConstant(constant), Collections.emptyList(), token));
}
else {
nodeStack.push(new ExpressionNode(((c, t) -> getOrSetAnyVariable(c, token.surface).evalValue(c, t)), Collections.emptyList(), token));
}
break;
case FUNCTION:
String name = token.surface;
ILazyFunction f;
ArrayList<LazyValue> p;
ArrayList<ExpressionNode> p;
boolean isKnown = functions.containsKey(name); // globals will be evaluated lazily, not at compile time via .
if (isKnown)
{
@@ -757,47 +1001,301 @@ public class Expression
}
// pop parameters off the stack until we hit the start of
// this function's parameter list
while (!stack.isEmpty() && stack.peek() != LazyValue.PARAMS_START)
while (!nodeStack.isEmpty() && nodeStack.peek() != ExpressionNode.PARAMS_START)
{
p.add(stack.pop());
p.add(nodeStack.pop());
}
if (!isKnown)
{
p.add(ExpressionNode.ofConstant(new StringValue(name), token.morphedInto(Tokenizer.Token.TokenType.STRINGPARAM, token.surface)));
token.morph(Tokenizer.Token.TokenType.FUNCTION, "call");
}
if (!isKnown) p.add( (c, t) -> new StringValue(name));
Collections.reverse(p);
if (stack.peek() == LazyValue.PARAMS_START)
if (nodeStack.peek() == ExpressionNode.PARAMS_START)
{
stack.pop();
}
nodeStack.pop();
};
List<LazyValue> params = p.stream().map(n -> n.op).collect(Collectors.toList());
stack.push((c, t) -> f.lazyEval(c, t, this, token, p).evalValue(c, t));
nodeStack.push(new ExpressionNode(
(c, t) -> f.lazyEval(c, t, this, token, params).evalValue(c, t),
p,token
));
break;
case OPEN_PAREN:
stack.push(LazyValue.PARAMS_START);
nodeStack.push(ExpressionNode.PARAMS_START);
break;
case LITERAL:
stack.push((c, t) ->
Value number;
try
{
try
{
return new NumericValue(token.surface);
}
catch (NumberFormatException exception)
{
throw new ExpressionException(c, this, token, "Not a number");
}
});
number = new NumericValue(token.surface);
}
catch (NumberFormatException exception)
{
throw new ExpressionException(context, this, token, "Not a number");
}
token.morph(Tokenizer.Token.TokenType.CONSTANT, token.surface);
nodeStack.push(ExpressionNode.ofConstant(number, token));
break;
case STRINGPARAM:
stack.push((c, t) -> new StringValue(token.surface) ); // was originally null
token.morph(Tokenizer.Token.TokenType.CONSTANT, token.surface);
nodeStack.push(ExpressionNode.ofConstant(new StringValue(token.surface), token));
break;
case HEX_LITERAL:
stack.push((c, t) -> new NumericValue(new BigInteger(token.surface.substring(2), 16).doubleValue()));
Value hexNumber;
try
{
hexNumber = new NumericValue(new BigInteger(token.surface.substring(2), 16).longValue());
}
catch (NumberFormatException exception)
{
throw new ExpressionException(context, this, token, "Not a number");
}
token.morph(Tokenizer.Token.TokenType.CONSTANT, token.surface);
nodeStack.push(ExpressionNode.ofConstant(hexNumber, token));
break;
default:
throw new ExpressionException(context, this, token, "Unexpected token '" + token.surface + "'");
}
}
return stack.pop();
return nodeStack.pop();
}
private LazyValue getAST(Context context)
{
//Stack<LazyValue> stack = new Stack<>();
List<Tokenizer.Token> rpn = shuntingYard(context);
validate(context, rpn);
ExpressionNode root = RPNToParseTree(rpn, context);
if (!CarpetSettings.scriptsOptimization)
return root.op;
CarpetScriptServer.LOG.info("Input code size for "+getModuleName()+": " + treeSize(root) + " nodes, " + treeDepth(root) + " deep");
boolean changed = true;
while(changed) {
changed = false;
while (true) {
int tree_size = treeSize(root);
int tree_depth = treeDepth(root);
boolean optimized = compactTree(root, Context.Type.NONE, 0);
if (!optimized) break;
changed = true;
CarpetScriptServer.LOG.info("Compacted from " + tree_size + " nodes, " + tree_depth + " code depth to " + treeSize(root) + " nodes, " + treeDepth(root) + " code depth");
}
while (true) {
int tree_size = treeSize(root);
int tree_depth = treeDepth(root);
boolean optimized = optimizeTree(context, root, Context.Type.NONE, 0);
if (!optimized) break;
changed = true;
CarpetScriptServer.LOG.info("Optimized from " + tree_size + " nodes, " + tree_depth + " code depth to " + treeSize(root) + " nodes, " + treeDepth(root) + " code depth");
}
}
return extractOp(context, root, Context.Type.NONE);
}
private int treeSize(ExpressionNode node)
{
if (node.op instanceof LazyValue.ContextFreeLazyValue) return 1;
return node.args.stream().mapToInt(this::treeSize).sum()+1;
}
private int treeDepth(ExpressionNode node)
{
if (node.op instanceof LazyValue.ContextFreeLazyValue) return 1;
return node.args.stream().mapToInt(this::treeDepth).max().orElse(0)+1;
}
private boolean compactTree(ExpressionNode node, Context.Type expectedType, int indent) {
// ctx is just to report errors, not values evaluation
boolean optimized = false;
Tokenizer.Token.TokenType token = node.token.type;
if (!token.isFunctional()) return false;
// input special cases here, like function signature
if (node.op instanceof LazyValue.Constant) return false; // optimized already
// function or operator
String symbol = node.token.surface;
Fluff.EvalNode operation = ((token == Tokenizer.Token.TokenType.FUNCTION) ? functions : operators).get(symbol);
Context.Type requestedType = operation.staticType(expectedType);
for (ExpressionNode arg : node.args) {
if (compactTree(arg, requestedType, indent+1)) optimized = true;
}
if (expectedType != Context.Type.MAPDEF && symbol.equals("->") && node.args.size() == 2) {
String rop = node.args.get(1).token.surface;
ExpressionNode returnNode = null;
if ((rop.equals(";") || rop.equals("then"))) {
List<ExpressionNode> thenArgs = node.args.get(1).args;
if (thenArgs.size() > 1 && thenArgs.get(thenArgs.size() - 1).token.surface.equals("return")) {
returnNode = thenArgs.get(thenArgs.size() - 1);
}
} else if (rop.equals("return")) {
returnNode = node.args.get(1);
}
if (returnNode != null) // tail return
{
if (returnNode.args.size() > 0) {
returnNode.op = returnNode.args.get(0).op;
returnNode.token = returnNode.args.get(0).token;
returnNode.range = returnNode.args.get(0).range;
returnNode.args = returnNode.args.get(0).args;
if (CarpetSettings.scriptsDebugging)
CarpetScriptServer.LOG.info(" - Removed unnecessary tail return of " + returnNode.token.surface + " from function body at line " + (returnNode.token.lineno + 1) + ", node depth " + indent);
} else {
returnNode.op = LazyValue.ofConstant(Value.NULL);
returnNode.token.morph(Tokenizer.Token.TokenType.CONSTANT, "");
returnNode.args = Collections.emptyList();
if (CarpetSettings.scriptsDebugging)
CarpetScriptServer.LOG.info(" - Removed unnecessary tail return from function body at line " + (returnNode.token.lineno + 1) + ", node depth " + indent);
}
}
}
for (Map.Entry<String, String> pair : functionalEquivalence.entrySet()) {
String operator = pair.getKey();
String function = pair.getValue();
if (symbol.equals(operator) || symbol.equals(function)) {
List<ExpressionNode> newargs = new ArrayList<>();
boolean contracted = false;
for (ExpressionNode arg : node.args) {
String argsymbol = arg.token.surface; // child is unoptimized and also same class
if ((argsymbol.equals(operator) || argsymbol.equals(function)) && (!(arg.op instanceof LazyValue.ContextFreeLazyValue))) {
newargs.addAll(arg.args);
contracted = true;
} else {
newargs.add(arg);
}
}
if (contracted) {
optimized = true;
if (CarpetSettings.scriptsDebugging)
CarpetScriptServer.LOG.info(" - " + symbol + "(" + node.args.size() + ") => " + function + "(" + newargs.size() + ") at line " + (node.token.lineno + 1) + ", node depth " + indent);
node.token.morph(Tokenizer.Token.TokenType.FUNCTION, function);
node.args = newargs;
}
}
}
return optimized;
}
private boolean optimizeTree(Context ctx, ExpressionNode node, Context.Type expectedType, int indent) {
// ctx is just to report errors, not values evaluation
boolean optimized = false;
Tokenizer.Token.TokenType token = node.token.type;
if (!token.isFunctional()) return false;
String symbol = node.token.surface;
// input special cases here, like function signature
if (node.op instanceof LazyValue.Constant) return false; // optimized already
// function or operator
Fluff.EvalNode operation = ((token == Tokenizer.Token.TokenType.FUNCTION) ? functions : operators).get(symbol);
Context.Type requestedType = operation.staticType(expectedType);
for (ExpressionNode arg : node.args) {
if (optimizeTree(ctx, arg, requestedType, indent+1)) optimized = true;
}
for (ExpressionNode arg : node.args) {
if (arg.token.type.isConstant()) continue;
if (arg.op instanceof LazyValue.ContextFreeLazyValue) continue;
return optimized;
}
if (!operation.pure())
{
if (symbol.equals("->") && expectedType == Context.Type.MAPDEF)
{
}
else {
return optimized;
}
}
List<LazyValue> args = new ArrayList<>(node.args.size());
for (ExpressionNode arg : node.args) {
try {
if (arg.op instanceof LazyValue.Constant) {
Value val = ((LazyValue.Constant) arg.op).get();
args.add((c, t) -> val);
}
else args.add((c, t) -> arg.op.evalValue(null, requestedType));
} catch (NullPointerException npe) {
throw new ExpressionException(ctx, this, node.token, "Attempted to evaluate context free expression");
}
}
// applying argument unpacking
args= AbstractLazyFunction.lazify(AbstractLazyFunction.unpackLazy(args, null, requestedType));
Value result;
if (operation instanceof ILazyFunction)
{
result = ((ILazyFunction) operation).lazyEval(null, expectedType, this, node.token, args).evalValue(null, expectedType);
}
else if (args.size() == 1)
{
result = ((ILazyOperator)operation).lazyEval(null, expectedType, this, node.token, args.get(0), null).evalValue(null, expectedType);
}
else // args == 2
{
result = ((ILazyOperator)operation).lazyEval(null, expectedType, this, node.token, args.get(0), args.get(1)).evalValue(null, expectedType);
}
node.op = LazyValue.ofConstant(result);
//node.token.morph(Tokenizer.Token.TokenType.CONSTANT, node.token.surface);
if (CarpetSettings.scriptsDebugging)
CarpetScriptServer.LOG.info(" - "+symbol+"("+args.stream().map(a -> a.evalValue(null, requestedType).getString()).collect(Collectors.joining(", "))+") => "+result.getString()+" at line "+(node.token.lineno+1) +", node depth "+indent);
return true;
}
private LazyValue extractOp(Context ctx, ExpressionNode node, Context.Type expectedType)
{
if (node.op instanceof LazyValue.Constant)
{
// constants are immutable
if (node.token.type.isConstant())
{
Value value = ((LazyValue.Constant) node.op).get();
return (c, t) -> value;
}
return node.op;
}
if (node.op instanceof LazyValue.ContextFreeLazyValue)
{
Value ret = ((LazyValue.ContextFreeLazyValue) node.op).evalType(expectedType);
return (c, t) -> ret;
}
Tokenizer.Token token = node.token;
switch (token.type)
{
case UNARY_OPERATOR: {
ILazyOperator op = operators.get(token.surface);
Context.Type requestedType = op.staticType(expectedType);
LazyValue arg = extractOp(ctx, node.args.get(0), requestedType);
return (c, t) -> op.lazyEval(c, t, this, token, arg, null).evalValue(c, t);
}
case OPERATOR: {
ILazyOperator op = operators.get(token.surface);
Context.Type requestedType = op.staticType(expectedType);
LazyValue arg = extractOp(ctx, node.args.get(0), requestedType);
LazyValue arh = extractOp(ctx, node.args.get(1), requestedType);
return (c, t) -> op.lazyEval(c, t, this, token, arg, arh).evalValue(c, t);
}
case VARIABLE:
return (c, t) -> getOrSetAnyVariable(c, token.surface).evalValue(c, t);
case FUNCTION: {
ILazyFunction f = functions.get(token.surface);
Context.Type requestedType = f.staticType(expectedType);
List<LazyValue> params = node.args.stream().map(n -> extractOp(ctx, n, requestedType)).collect(Collectors.toList());
return (c, t) -> f.lazyEval(c, t, this, token, params).evalValue(c, t);
}
case CONSTANT:
return node.op;
default:
throw new ExpressionException(ctx, this, node.token, "Unexpected token '" + node.token.type +" "+node.token.surface + "'");
}
}
private void validate(Context c, List<Tokenizer.Token> rpn)
+69 -14
View File
@@ -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)
{
+42 -7
View File
@@ -10,15 +10,50 @@ public interface LazyValue
LazyValue TRUE = (c, t) -> Value.TRUE;
LazyValue NULL = (c, t) -> Value.NULL;
LazyValue ZERO = (c, t) -> Value.ZERO;
/**
* The Value representation of the left parenthesis, used for parsing
* varying numbers of function parameters.
*/
LazyValue PARAMS_START = (c, t) -> null;
Value evalValue(Context c, Integer type);
public static LazyValue ofConstant(Value val) {
return new Constant(val);
}
Value evalValue(Context c, Context.Type type);
default Value evalValue(Context c){
return evalValue(c, Context.NONE);
return evalValue(c, Context.Type.NONE);
}
@FunctionalInterface
interface ContextFreeLazyValue extends LazyValue
{
Value evalType(Context.Type type);
@Override
default Value evalValue(Context c, Context.Type type) {
return evalType(type);
}
}
class Constant implements ContextFreeLazyValue
{
Value result;
public Constant(Value value)
{
result = value;
}
public Value get() {return result;}
@Override
public Value evalType(Context.Type type) {
return result.fromConstant();
}
@Override
public Value evalValue(Context c, Context.Type type) {
return result.fromConstant();
}
}
}
@@ -122,11 +122,6 @@ public abstract class ScriptHost
void initializeModuleGlobals(ModuleData md)
{
md.globalVariables.put("euler", (c, t) -> Arithmetic.euler);
md.globalVariables.put("pi", (c, t) -> Arithmetic.PI);
md.globalVariables.put("null", (c, t) -> Value.NULL);
md.globalVariables.put("true", (c, t) -> Value.TRUE);
md.globalVariables.put("false", (c, t) -> Value.FALSE);
}
public void importModule(Context c, String moduleName)
+22 -2
View File
@@ -398,8 +398,28 @@ public class Tokenizer implements Iterator<Tokenizer.Token>
{
enum TokenType
{
VARIABLE, FUNCTION, LITERAL, OPERATOR, UNARY_OPERATOR,
OPEN_PAREN, COMMA, CLOSE_PAREN, HEX_LITERAL, STRINGPARAM, MARKER
FUNCTION(true, false), OPERATOR(true, false), UNARY_OPERATOR(true, false),
VARIABLE(false, false), CONSTANT(false, true),
LITERAL(false, true), HEX_LITERAL(false, true), STRINGPARAM(false, true),
OPEN_PAREN(false, true), COMMA(false, true), CLOSE_PAREN(false, true), MARKER(false, true);
boolean functional;
boolean constant;
TokenType(boolean functional, boolean constant)
{
this.functional = functional;
this.constant = constant;
}
public boolean isFunctional()
{
return functional;
}
public boolean isConstant() {
return constant;
}
}
public String surface = "";
public TokenType type;
@@ -819,7 +819,7 @@ public class Auxiliary {
return Value.TRUE;
});
expression.addFunction("logger", lv ->
expression.addImpureFunction("logger", lv ->
{
Value res;
@@ -25,7 +25,7 @@ public class Threading
});
// has to be lazy due to deferred execution of the expression
expression.addLazyFunctionWithDelegation("task_dock", 1, (c, t, expr, tok, lv) -> {
expression.addLazyFunctionWithDelegation("task_dock", 1, false,true, (c, t, expr, tok, lv) -> {
CarpetContext cc = (CarpetContext)c;
MinecraftServer server = cc.s.getMinecraftServer();
if (server.isOnThread()) return lv.get(0); // pass through for on thread tasks
@@ -3,6 +3,7 @@ package carpet.script.language;
import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.exception.InternalExpressionException;
import carpet.script.value.BooleanValue;
import carpet.script.value.ListValue;
import carpet.script.value.NumericValue;
import carpet.script.value.Value;
@@ -15,7 +16,7 @@ public class Arithmetic {
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.addTypedContextFunction("not", 1, Context.Type.BOOLEAN, (c, t, lv) -> BooleanValue.of(lv.get(0).getBoolean()) );
expression.addUnaryFunction("fact", (v) ->
{
long number = NumericValue.asNumber(v).getLong();
@@ -21,13 +21,22 @@ public class ControlFlow {
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, (c, t, lv1, lv2) ->
expression.addLazyBinaryOperator(";", Operators.precedence.get("nextop;"), true, true, t -> Context.Type.VOID, (c, t, lv1, lv2) ->
{
lv1.evalValue(c, Context.VOID);
Value v2 = lv2.evalValue(c, t);
return (cc, tt) -> v2;
});
expression.addPureLazyFunction("then", -1, t -> Context.Type.VOID, (c, t, lv) -> {
int imax = lv.size()-1;
for (int i = 0; i < imax; i++) lv.get(i).evalValue(c, Context.VOID);
Value v = lv.get(imax).evalValue(c, t);
return (cc, tt) -> v;
});
expression.addFunctionalEquivalence(";", "then");
// obvious lazy due to conditional evaluation of arguments
expression.addLazyFunction("if", -1, (c, t, lv) ->
{
@@ -50,9 +59,9 @@ public class ControlFlow {
return (cc, tt) -> Value.NULL;
});
expression.addFunction("exit", (lv) -> { throw new ExitStatement(lv.size()==0?Value.NULL:lv.get(0)); });
expression.addImpureFunction("exit", (lv) -> { throw new ExitStatement(lv.size()==0?Value.NULL:lv.get(0)); });
expression.addFunction("throw", lv->
expression.addImpureFunction("throw", lv->
{
switch (lv.size())
{
@@ -189,7 +189,7 @@ public class DataStructures {
return Value.NULL;
});
expression.addBinaryContextOperator(":", Operators.precedence.get("attribute~:"),true, (c, t, container, address) ->
expression.addBinaryContextOperator(":", Operators.precedence.get("attribute~:"),true, true, false, (ctx, t, container, address) ->
{
if (container instanceof LContainerValue)
{
@@ -3,6 +3,8 @@ package carpet.script.language;
import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.Fluff;
import carpet.script.LazyValue;
import carpet.script.Tokenizer;
import carpet.script.argument.FunctionArgument;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ReturnStatement;
@@ -35,53 +37,75 @@ public class Functions {
return ListValue.wrap(c.host.availableImports(moduleName).map(StringValue::new).collect(Collectors.toList()));
});
// needs to be lazy because of custom context of execution of arguments as a signature
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
if (t != Context.SIGNATURE) // just call the function
expression.addCustomFunction("call", new Fluff.AbstractLazyFunction(-1, "call") {
@Override
public LazyValue lazyEval(Context c, Context.Type t, Expression expr, Tokenizer.Token tok, List<LazyValue> lv)
{
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
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++)
{
Value v = lv.get(i).evalValue(c, Context.LOCALIZATION);
if (!v.isBound())
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
if (t != Context.SIGNATURE) // just call the function
{
throw new InternalExpressionException("Only variables can be used in function signature, not " + v.getString());
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);
}
if (v instanceof FunctionAnnotationValue)
// gimme signature
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++)
{
if (((FunctionAnnotationValue) v).type == FunctionAnnotationValue.Type.GLOBAL)
Value v = lv.get(i).evalValue(c, Context.LOCALIZATION);
if (!v.isBound())
{
globals.add(v.boundVariable);
throw new InternalExpressionException("Only variables can be used in function signature, not " + v.getString());
}
if (v instanceof FunctionAnnotationValue)
{
if (((FunctionAnnotationValue) v).type == FunctionAnnotationValue.Type.GLOBAL)
{
globals.add(v.boundVariable);
}
else
{
if (varArgs != null)
throw new InternalExpressionException("Variable argument identifier is already defined as "+varArgs+", trying to overwrite with "+v.boundVariable);
varArgs = v.boundVariable;
}
}
else
{
if (varArgs != null)
throw new InternalExpressionException("Variable argument identifier is already defined as "+varArgs+", trying to overwrite with "+v.boundVariable);
varArgs = v.boundVariable;
args.add(v.boundVariable);
}
}
else
{
args.add(v.boundVariable);
}
Value retval = new FunctionSignatureValue(name, args, varArgs, globals);
return (cc, tt) -> retval;
}
@Override
public boolean pure()
{
return false; //true for sinature, but lets leave it for later
}
@Override
public boolean transitive() {
return false;
}
@Override
public Context.Type staticType(Context.Type outerType)
{
return outerType==Context.SIGNATURE?Context.LOCALIZATION:Context.NONE;
}
Value retval = new FunctionSignatureValue(name, args, varArgs, globals);
return (cc, tt) -> retval;
});
expression.addContextFunction("outer", 1, (c, t, lv) ->
{
if (t != Context.LOCALIZATION)
@@ -91,7 +115,7 @@ public class Functions {
//assigns const procedure to the lhs, returning its previous value
// must be lazy due to RHS being an expression to save to execute
expression.addLazyBinaryOperatorWithDelegation("->", Operators.precedence.get("def->"), false, (c, type, e, t, lv1, lv2) ->
expression.addLazyBinaryOperatorWithDelegation("->", Operators.precedence.get("def->"), false, false, (c, type, e, t, lv1, lv2) ->
{
if (type == Context.MAPDEF)
{
@@ -106,6 +130,6 @@ public class Functions {
return (cc, tt) -> result;
});
expression.addFunction("return", (lv) -> { throw new ReturnStatement(lv.size()==0?Value.NULL:lv.get(0));} );
expression.addImpureFunction("return", (lv) -> { throw new ReturnStatement(lv.size()==0?Value.NULL:lv.get(0));} );
}
}
@@ -21,14 +21,14 @@ 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.addImpureFunction("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.addImpureFunction("continue", lv ->
{
if (lv.size()==0) throw new ContinueStatement(null);
if (lv.size()==1) throw new ContinueStatement(lv.get(0));
@@ -5,6 +5,7 @@ import carpet.script.Expression;
import carpet.script.LazyValue;
import carpet.script.exception.InternalExpressionException;
import carpet.script.value.AbstractListValue;
import carpet.script.value.BooleanValue;
import carpet.script.value.ContainerValueInterface;
import carpet.script.value.FunctionAnnotationValue;
import carpet.script.value.FunctionUnpackedArgumentsValue;
@@ -14,6 +15,7 @@ import carpet.script.value.MapValue;
import carpet.script.value.NumericValue;
import carpet.script.value.Value;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -38,47 +40,203 @@ public class Operators {
public static void apply(Expression expression)
{
expression.addBinaryOperator("+", precedence.get("addition+-"), true, Value::add);
expression.addFunction("sum", lv -> {
int size = lv.size();
if (size == 0) return Value.NULL;
Value accumulator = lv.get(0);
for (Value v: lv.subList(1, size)) accumulator = accumulator.add(v);
return accumulator;
});
expression.addFunctionalEquivalence("+", "sum");
expression.addBinaryOperator("-", precedence.get("addition+-"), true, Value::subtract);
expression.addFunction("difference", lv -> {
int size = lv.size();
if (size == 0) return Value.NULL;
Value accumulator = lv.get(0);
for (Value v: lv.subList(1, size)) accumulator = accumulator.subtract(v);
return accumulator;
});
expression.addFunctionalEquivalence("-", "difference");
expression.addBinaryOperator("*", precedence.get("multiplication*/%"), true, Value::multiply);
expression.addFunction("product", lv -> {
int size = lv.size();
if (size == 0) return Value.NULL;
Value accumulator = lv.get(0);
for (Value v: lv.subList(1, size)) accumulator = accumulator.multiply(v);
return accumulator;
});
expression.addFunctionalEquivalence("*", "product");
expression.addBinaryOperator("/", precedence.get("multiplication*/%"), true, Value::divide);
expression.addFunction("quotient", lv -> {
int size = lv.size();
if (size == 0) return Value.NULL;
Value accumulator = lv.get(0);
for (Value v: lv.subList(1, size)) accumulator = accumulator.multiply(v);
return accumulator;
});
expression.addFunctionalEquivalence("/", "quotient");
expression.addBinaryOperator("%", precedence.get("multiplication*/%"), true, (v1, v2) ->
new NumericValue(NumericValue.asNumber(v1).getDouble() % NumericValue.asNumber(v2).getDouble()));
expression.addBinaryOperator("^", precedence.get("exponent^"), false, (v1, v2) ->
new NumericValue(java.lang.Math.pow(NumericValue.asNumber(v1).getDouble(), NumericValue.asNumber(v2).getDouble())));
// lazy cause RHS is only conditional
expression.addLazyBinaryOperator("&&", precedence.get("and&&"), false, (c, t, lv1, lv2) ->
{
expression.addLazyBinaryOperator("&&", precedence.get("and&&"), false, true, t -> Context.Type.BOOLEAN, (c, t, lv1, lv2) ->
{ // todo check how is optimizations going
Value v1 = lv1.evalValue(c, Context.BOOLEAN);
if (!v1.getBoolean()) return (cc, tt) -> v1;
return lv2;
});
expression.addPureLazyFunction("and", -1, t -> Context.Type.BOOLEAN, (c, t, lv) -> {
int last = lv.size()-1;
if (last == -1) return LazyValue.TRUE;
for (LazyValue l: lv.subList(0, last))
{
Value val = l.evalValue(c, Context.Type.BOOLEAN);
if (val instanceof FunctionUnpackedArgumentsValue)
{
for (Value it : (FunctionUnpackedArgumentsValue) val)
if (!it.getBoolean()) return (cc, tt) -> it;
}
else
{
if (!val.getBoolean()) return (cc, tt) -> val;
}
}
return lv.get(last);
});
expression.addFunctionalEquivalence("&&", "and");
// lazy cause RHS is only conditional
expression.addLazyBinaryOperator("||", precedence.get("or||"), false, (c, t, lv1, lv2) ->
expression.addLazyBinaryOperator("||", precedence.get("or||"), false, true, t -> Context.Type.BOOLEAN, (c, t, lv1, lv2) ->
{
Value v1 = lv1.evalValue(c, Context.BOOLEAN);
if (v1.getBoolean()) return (cc, tt) -> v1;
return lv2;
});
expression.addPureLazyFunction("or", -1, t -> Context.Type.BOOLEAN, (c, t, lv) -> {
int last = lv.size()-1;
if (last == -1) return LazyValue.FALSE;
for (LazyValue l: lv.subList(0, last))
{
Value val = l.evalValue(c, Context.Type.BOOLEAN);
if (val instanceof FunctionUnpackedArgumentsValue)
{
for (Value it : (FunctionUnpackedArgumentsValue) val)
if (it.getBoolean()) return (cc, tt) -> it;
}
else
{
if (val.getBoolean()) return (cc, tt) -> val;
}
}
return lv.get(last);
});
expression.addFunctionalEquivalence("||", "or");
expression.addBinaryOperator("~", precedence.get("attribute~:"), true, Value::in);
expression.addBinaryOperator(">", precedence.get("compare>=><=<"), false, (v1, v2) ->
v1.compareTo(v2) > 0 ? Value.TRUE : Value.FALSE);
BooleanValue.of(v1.compareTo(v2) > 0));
expression.addFunction("decreasing", lv -> {
int size = lv.size();
if (size < 2) return Value.TRUE;
Value prev = lv.get(0);
for (Value next: lv.subList(1, size))
{
if (prev.compareTo(next) <= 0) return Value.FALSE;
prev = next;
}
return Value.TRUE;
});
expression.addFunctionalEquivalence(">", "decreasing");
expression.addBinaryOperator(">=", precedence.get("compare>=><=<"), false, (v1, v2) ->
v1.compareTo(v2) >= 0 ? Value.TRUE : Value.FALSE);
BooleanValue.of(v1.compareTo(v2) >= 0));
expression.addFunction("nonincreasing", lv -> {
int size = lv.size();
if (size < 2) return Value.TRUE;
Value prev = lv.get(0);
for (Value next: lv.subList(1, size))
{
if (prev.compareTo(next) < 0) return Value.FALSE;
prev = next;
}
return Value.TRUE;
});
expression.addFunctionalEquivalence(">=", "nonincreasing");
expression.addBinaryOperator("<", precedence.get("compare>=><=<"), false, (v1, v2) ->
v1.compareTo(v2) < 0 ? Value.TRUE : Value.FALSE);
BooleanValue.of(v1.compareTo(v2) < 0));
expression.addFunction("increasing", lv -> {
int size = lv.size();
if (size < 2) return Value.TRUE;
Value prev = lv.get(0);
for (Value next: lv.subList(1, size))
{
if (prev.compareTo(next) >= 0) return Value.FALSE;
prev = next;
}
return Value.TRUE;
});
expression.addFunctionalEquivalence("<", "increasing");
expression.addBinaryOperator("<=", precedence.get("compare>=><=<"), false, (v1, v2) ->
v1.compareTo(v2) <= 0 ? Value.TRUE : Value.FALSE);
BooleanValue.of(v1.compareTo(v2) <= 0));
expression.addFunction("nondecreasing", lv -> {
int size = lv.size();
if (size < 2) return Value.TRUE;
Value prev = lv.get(0);
for (Value next: lv.subList(1, size))
{
if (prev.compareTo(next) > 0) return Value.FALSE;
prev = next;
}
return Value.TRUE;
});
expression.addFunctionalEquivalence("<=", "nondecreasing");
expression.addBinaryOperator("==", precedence.get("equal==!="), false, (v1, v2) ->
v1.equals(v2) ? Value.TRUE : Value.FALSE);
expression.addFunction("equal", lv -> {
int size = lv.size();
if (size < 2) return Value.TRUE;
Value prev = lv.get(0);
for (Value next: lv.subList(1, size))
{
if (!prev.equals(next)) return Value.FALSE;
prev = next;
}
return Value.TRUE;
});
expression.addFunctionalEquivalence("==", "equal");
expression.addBinaryOperator("!=", precedence.get("equal==!="), false, (v1, v2) ->
v1.equals(v2) ? Value.FALSE : Value.TRUE);
expression.addFunction("unique", lv -> {
int size = lv.size();
if (size < 2) return Value.TRUE;
// 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 (Value next: lv.subList(1, size))
{
if (prev.equals(next)) return Value.FALSE;
prev = next;
}
return Value.TRUE;
});
expression.addFunctionalEquivalence("!=", "unique");
// lazy cause of assignment which is non-trivial
expression.addLazyBinaryOperator("=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) ->
expression.addLazyBinaryOperator("=", precedence.get("assign=<>"), false, false, t -> Context.Type.LVALUE, (c, t, lv1, lv2) ->
{
Value v1 = lv1.evalValue(c, Context.LVALUE);
Value v2 = lv2.evalValue(c);
@@ -117,7 +275,7 @@ public class Operators {
});
// lazy due to assignment
expression.addLazyBinaryOperator("+=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) ->
expression.addLazyBinaryOperator("+=", precedence.get("assign=<>"), false, false, t -> Context.Type.LVALUE, (c, t, lv1, lv2) ->
{
Value v1 = lv1.evalValue(c, Context.LVALUE);
Value v2 = lv2.evalValue(c);
@@ -177,7 +335,7 @@ public class Operators {
return boundedLHS;
});
expression.addBinaryContextOperator("<>", precedence.get("assign=<>"), false, (c, t, v1, v2) ->
expression.addBinaryContextOperator("<>", precedence.get("assign=<>"), false, false, false, (c, t, v1, v2) ->
{
if (v1 instanceof ListValue.ListConstructorValue && v2 instanceof ListValue.ListConstructorValue)
{
@@ -218,12 +376,12 @@ public class Operators {
expression.addUnaryOperator("+", false, NumericValue::asNumber);
// could be non-lazy, but who cares - its a small one.
expression.addLazyUnaryOperator("!", precedence.get("unary+-!..."), false, (c, t, lv) ->
expression.addLazyUnaryOperator("!", precedence.get("unary+-!..."), false, true, x -> Context.Type.BOOLEAN, (c, t, lv) ->
lv.evalValue(c, Context.BOOLEAN).getBoolean() ? (cc, tt)-> Value.FALSE : (cc, tt) -> Value.TRUE
); // might need context boolean
// lazy because of typed evaluation of the argument
expression.addLazyUnaryOperator("...", Operators.precedence.get("unary+-!..."), false, (c, t, lv) ->
expression.addLazyUnaryOperator("...", Operators.precedence.get("unary+-!..."), false, true, t -> t== Context.Type.LOCALIZATION?Context.NONE:t, (c, t, lv) ->
{
if (t == Context.LOCALIZATION)
return (cc, tt) -> new FunctionAnnotationValue(lv.evalValue(c), FunctionAnnotationValue.Type.VARARG);
@@ -36,7 +36,7 @@ public class Sys {
{
expression.addUnaryFunction("hash_code", v -> new NumericValue(v.hashCode()));
expression.addUnaryFunction("copy", Value::deepcopy);
expression.addImpureUnaryFunction("copy", Value::deepcopy);
expression.addTypedContextFunction("bool", 1, Context.BOOLEAN, (c, t, lv) ->
{
@@ -12,7 +12,7 @@ public class Threading
{
public static void apply(Expression expression)
{
expression.addFunctionWithDelegation("task", -1, (c, t, expr, tok, lv) ->
expression.addFunctionWithDelegation("task", -1, false, false, (c, t, expr, tok, lv) ->
{
if (lv.size() == 0)
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
@@ -22,7 +22,7 @@ public class Threading
return thread;
});
expression.addFunctionWithDelegation("task_thread", -1, (c, t, expr, tok, lv) ->
expression.addFunctionWithDelegation("task_thread", -1, false, false, (c, t, expr, tok, lv) ->
{
if (lv.size() < 2)
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
@@ -15,4 +15,9 @@ public abstract class AbstractListValue extends Value implements Iterable<Value>
{
throw new InternalExpressionException("Cannot append a value to an abstract list");
}
@Override
public Value fromConstant() {
return this.deepcopy();
}
}
@@ -54,7 +54,7 @@ public class FormattedTextValue extends StringValue
@Override
public boolean getBoolean() {
return !text.asString().isEmpty();
return !text.getString().isEmpty();
}
@Override
@@ -150,7 +150,7 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
return varArgs != null;
}
public LazyValue callInContext(Context c, Integer type, List<Value> params)
public LazyValue callInContext(Context c, Context.Type type, List<Value> params)
{
try
{
@@ -204,12 +204,12 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
}
@Override
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) {
public LazyValue lazyEval(Context c, Context.Type type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) {
List<Value> resolvedParams = unpackArgs(lazyParams, c);
return execute(c, type, e, t, resolvedParams);
}
public LazyValue execute(Context c, Integer type, Expression e, Tokenizer.Token t, List<Value> params)
public LazyValue execute(Context c, Context.Type type, Expression e, Tokenizer.Token t, List<Value> params)
{
assertArgsOk(params, (fixedArgs) ->{
if (fixedArgs) // wrong number of args for fixed args
@@ -301,10 +301,13 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
}
}
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;
@Override
public boolean pure() {
return false;
}
@Override
public boolean transitive() {
return false;
}
}
@@ -192,6 +192,11 @@ public abstract class LazyListValue extends AbstractListValue implements Iterato
return copy;
}
@Override
public Value fromConstant() {
return (Value)clone();
}
@Override
public int hashCode()
{
@@ -152,6 +152,11 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
return copy;
}
@Override
public Value fromConstant() {
return deepcopy();
}
// stolen from HopperBlockEntity, adjusted for threaded operation
public static Inventory getInventoryAt(ServerWorld world, BlockPos blockPos)
{
@@ -244,4 +244,11 @@ public abstract class Value implements Comparable<Value>, Cloneable
}
public boolean isNull() { return false; }
/**
* @return retrieves useful in-run value of an optimized code-base value.
* For immutable values (most of them) it can return itself,
* but for mutables, it needs to be its copy or deep copy.
*/
public Value fromConstant() { return this; }
}