A bit of documentation and making constructors uniform.

This commit is contained in:
Rob Greene 2018-06-09 11:24:29 -05:00
parent 4fb2cfe558
commit d45c17dcaf
5 changed files with 31 additions and 5 deletions

View File

@ -8,10 +8,10 @@ import io.github.applecommander.bastokenizer.api.optimizations.RemoveRemStatemen
import io.github.applecommander.bastokenizer.api.optimizations.Renumber;
public enum Optimization {
REMOVE_EMPTY_STATEMENTS(config -> new RemoveEmptyStatements()),
REMOVE_REM_STATEMENTS(config -> new RemoveRemStatements()),
MERGE_LINES(config -> new MergeLines(config)),
RENUMBER(config -> new Renumber());
REMOVE_EMPTY_STATEMENTS(RemoveEmptyStatements::new),
REMOVE_REM_STATEMENTS(RemoveRemStatements::new),
MERGE_LINES(MergeLines::new),
RENUMBER(Renumber::new);
private Function<Configuration,Visitor> factory;

View File

@ -10,7 +10,11 @@ import io.github.applecommander.bastokenizer.api.model.Program;
import io.github.applecommander.bastokenizer.api.model.Statement;
import io.github.applecommander.bastokenizer.api.model.Token;
/** Common base class for optimization visitors that allow the program tree to be rewritten. */
/**
* Common base class for optimization visitors that allow the program tree to be rewritten.
* Note that {@code #reassignments} is used to track line number movement and is <em>automatically</em>
* applied at the end of the program visit.
*/
public class BaseVisitor implements Visitor {
protected Map<Integer,Integer> reassignments = new HashMap<>();
protected Program newProgram;

View File

@ -1,8 +1,14 @@
package io.github.applecommander.bastokenizer.api.optimizations;
import io.github.applecommander.bastokenizer.api.Configuration;
import io.github.applecommander.bastokenizer.api.model.Statement;
/** Remove any empty statements during the tree walk. Effective removes double "::"'s. */
public class RemoveEmptyStatements extends BaseVisitor {
public RemoveEmptyStatements(Configuration config) {
// ignored
}
@Override
public Statement visit(Statement statement) {
return statement.tokens.isEmpty() ? null : statement;

View File

@ -1,9 +1,15 @@
package io.github.applecommander.bastokenizer.api.optimizations;
import io.github.applecommander.bastokenizer.api.Configuration;
import io.github.applecommander.bastokenizer.api.model.Statement;
import io.github.applecommander.bastokenizer.api.model.Token.Type;
/** Drop all REM statements as they are encountered in the tree walk. */
public class RemoveRemStatements extends BaseVisitor {
public RemoveRemStatements(Configuration config) {
// ignored
}
@Override
public Statement visit(Statement statement) {
return statement.tokens.get(0).type == Type.COMMENT ? null : statement;

View File

@ -1,9 +1,19 @@
package io.github.applecommander.bastokenizer.api.optimizations;
import io.github.applecommander.bastokenizer.api.Configuration;
import io.github.applecommander.bastokenizer.api.model.Line;
/**
* A simple renumbering algorithm that maps the reassignments and lets {@code BaseVisitor}
* perform the actual renumbering!
*/
public class Renumber extends BaseVisitor {
protected int lineNumber = 0;
public Renumber(Configuration config) {
// ignored
}
@Override
public Line visit(Line line) {
Line newLine = new Line(lineNumber++, this.newProgram);