node optimizations should optimize only one argument depending on associativity. Fixes #1046
This commit is contained in:
1 parent
61052ea06d
commit
6f80c7959e
1 file changed
+21
-13
@@ -1162,20 +1162,28 @@ public class Expression
|
|||||||
for (Map.Entry<String, String> pair : functionalEquivalence.entrySet()) {
|
for (Map.Entry<String, String> pair : functionalEquivalence.entrySet()) {
|
||||||
String operator = pair.getKey();
|
String operator = pair.getKey();
|
||||||
String function = pair.getValue();
|
String function = pair.getValue();
|
||||||
if (symbol.equals(operator) || symbol.equals(function)) {
|
if ((symbol.equals(operator) || symbol.equals(function)) && node.args.size() > 0)
|
||||||
List<ExpressionNode> newargs = new ArrayList<>();
|
{
|
||||||
boolean contracted = false;
|
boolean leftOptimizable = operators.get(operator).isLeftAssoc();
|
||||||
for (ExpressionNode arg : node.args) {
|
ExpressionNode optimizedChild = node.args.get(leftOptimizable?0:(node.args.size()-1));
|
||||||
String argsymbol = arg.token.surface; // child is unoptimized and also same class
|
String type = optimizedChild.token.surface;
|
||||||
if ((argsymbol.equals(operator) || argsymbol.equals(function)) && (!(arg.op instanceof LazyValue.ContextFreeLazyValue))) {
|
if ((type.equals(operator) || type.equals(function)) && (!(optimizedChild.op instanceof LazyValue.ContextFreeLazyValue)))
|
||||||
newargs.addAll(arg.args);
|
{
|
||||||
contracted = true;
|
|
||||||
} else {
|
|
||||||
newargs.add(arg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (contracted) {
|
|
||||||
optimized = true;
|
optimized = true;
|
||||||
|
List<ExpressionNode> newargs = new ArrayList<>();
|
||||||
|
if (leftOptimizable)
|
||||||
|
{
|
||||||
|
newargs.addAll(optimizedChild.args);
|
||||||
|
for (int i = 1; i < node.args.size(); i++)
|
||||||
|
newargs.add(node.args.get(i));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 0; i < node.args.size()-1; i++)
|
||||||
|
newargs.add(node.args.get(i));
|
||||||
|
newargs.addAll(optimizedChild.args);
|
||||||
|
}
|
||||||
|
|
||||||
if (CarpetSettings.scriptsDebugging)
|
if (CarpetSettings.scriptsDebugging)
|
||||||
CarpetScriptServer.LOG.info(" - " + symbol + "(" + node.args.size() + ") => " + function + "(" + newargs.size() + ") at line " + (node.token.lineno + 1) + ", node depth " + indent);
|
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.token.morph(Tokenizer.Token.TokenType.FUNCTION, function);
|
||||||
|
|||||||
Reference in new issue
Block a user