mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2024-12-23 09:47:38 +00:00
Improved the expression parser's optimizer.
Added optimizers for Conditional, For, SimpleFor and While. Improved the Sequence optimizer. Removed the optimizer TODOs from Break and Return.
This commit is contained in:
parent
05b427316d
commit
c1e151ac19
@ -47,6 +47,4 @@ public class Break extends Node {
|
||||
public String toString() {
|
||||
return doContinue ? "continue" : "break";
|
||||
}
|
||||
|
||||
//TODO: optimizer
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ public class Conditional extends Node {
|
||||
if (condition.getValue() > 0.0) {
|
||||
return truePart.getValue();
|
||||
} else {
|
||||
return falsePart == null ? 0 : falsePart.getValue();
|
||||
return falsePart == null ? 0.0 : falsePart.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,5 +62,18 @@ public class Conditional extends Node {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: optimizer
|
||||
@Override
|
||||
public RValue optimize() throws EvaluationException {
|
||||
final RValue newCondition = condition.optimize();
|
||||
|
||||
if (newCondition instanceof Constant) {
|
||||
if (newCondition.getValue() > 0) {
|
||||
return truePart.optimize();
|
||||
} else {
|
||||
return falsePart == null ? new Constant(getPosition(), 0.0) : falsePart.optimize();
|
||||
}
|
||||
}
|
||||
|
||||
return new Conditional(getPosition(), newCondition, truePart.optimize(), falsePart.optimize());
|
||||
}
|
||||
}
|
||||
|
@ -74,5 +74,17 @@ public class For extends Node {
|
||||
return "for (" + init + "; " + condition + "; " + increment + ") { " + body + " }";
|
||||
}
|
||||
|
||||
//TODO: optimizer
|
||||
@Override
|
||||
public RValue optimize() throws EvaluationException {
|
||||
final RValue newCondition = condition.optimize();
|
||||
|
||||
if (newCondition instanceof Constant && newCondition.getValue() <= 0) {
|
||||
// If the condition is always false, the loop can be flattened.
|
||||
// So we run the init part and then return 0.0.
|
||||
return new Sequence(getPosition(), init, new Constant(getPosition(), 0.0)).optimize();
|
||||
}
|
||||
|
||||
//return new Sequence(getPosition(), init.optimize(), new While(getPosition(), condition, new Sequence(getPosition(), body, increment), false)).optimize();
|
||||
return new For(getPosition(), init.optimize(), newCondition, increment.optimize(), body.optimize());
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,4 @@ public class Return extends Node {
|
||||
public String toString() {
|
||||
return "return " + value;
|
||||
}
|
||||
|
||||
//TODO: optimizer
|
||||
}
|
||||
|
@ -88,6 +88,10 @@ public class Sequence extends Node {
|
||||
newSequence.add(droppedLast);
|
||||
}
|
||||
|
||||
if (newSequence.size() == 1) {
|
||||
return newSequence.get(0);
|
||||
}
|
||||
|
||||
return new Sequence(getPosition(), newSequence.toArray(new RValue[newSequence.size()]));
|
||||
}
|
||||
}
|
||||
|
@ -78,5 +78,10 @@ public class SimpleFor extends Node {
|
||||
return "for (" + counter + " = " + first + ", " + last + ") { " + body + " }";
|
||||
}
|
||||
|
||||
//TODO: optimizer
|
||||
@Override
|
||||
public RValue optimize() throws EvaluationException {
|
||||
// TODO: unroll small loops into Sequences
|
||||
|
||||
return new SimpleFor(getPosition(), (LValue) counter.optimize(), first.optimize(), last.optimize(), body.optimize());
|
||||
}
|
||||
}
|
||||
|
@ -95,5 +95,21 @@ public class While extends Node {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: optimizer
|
||||
@Override
|
||||
public RValue optimize() throws EvaluationException {
|
||||
final RValue newCondition = condition.optimize();
|
||||
|
||||
if (newCondition instanceof Constant && newCondition.getValue() <= 0) {
|
||||
// If the condition is always false, the loop can be flattened.
|
||||
if (footChecked) {
|
||||
// Foot-checked loops run at least once.
|
||||
return body.optimize();
|
||||
} else {
|
||||
// Loops that never run always return 0.0.
|
||||
return new Constant(getPosition(), 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
return new While(getPosition(), newCondition, body.optimize(), footChecked);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user