mirror of
https://github.com/AppleCommander/bastools.git
synced 2025-08-09 16:25:03 +00:00
A bit of documentation and making constructors uniform.
This commit is contained in:
@@ -8,10 +8,10 @@ import io.github.applecommander.bastokenizer.api.optimizations.RemoveRemStatemen
|
|||||||
import io.github.applecommander.bastokenizer.api.optimizations.Renumber;
|
import io.github.applecommander.bastokenizer.api.optimizations.Renumber;
|
||||||
|
|
||||||
public enum Optimization {
|
public enum Optimization {
|
||||||
REMOVE_EMPTY_STATEMENTS(config -> new RemoveEmptyStatements()),
|
REMOVE_EMPTY_STATEMENTS(RemoveEmptyStatements::new),
|
||||||
REMOVE_REM_STATEMENTS(config -> new RemoveRemStatements()),
|
REMOVE_REM_STATEMENTS(RemoveRemStatements::new),
|
||||||
MERGE_LINES(config -> new MergeLines(config)),
|
MERGE_LINES(MergeLines::new),
|
||||||
RENUMBER(config -> new Renumber());
|
RENUMBER(Renumber::new);
|
||||||
|
|
||||||
private Function<Configuration,Visitor> factory;
|
private Function<Configuration,Visitor> factory;
|
||||||
|
|
||||||
|
@@ -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.Statement;
|
||||||
import io.github.applecommander.bastokenizer.api.model.Token;
|
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 {
|
public class BaseVisitor implements Visitor {
|
||||||
protected Map<Integer,Integer> reassignments = new HashMap<>();
|
protected Map<Integer,Integer> reassignments = new HashMap<>();
|
||||||
protected Program newProgram;
|
protected Program newProgram;
|
||||||
|
@@ -1,8 +1,14 @@
|
|||||||
package io.github.applecommander.bastokenizer.api.optimizations;
|
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.Statement;
|
||||||
|
|
||||||
|
/** Remove any empty statements during the tree walk. Effective removes double "::"'s. */
|
||||||
public class RemoveEmptyStatements extends BaseVisitor {
|
public class RemoveEmptyStatements extends BaseVisitor {
|
||||||
|
public RemoveEmptyStatements(Configuration config) {
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visit(Statement statement) {
|
public Statement visit(Statement statement) {
|
||||||
return statement.tokens.isEmpty() ? null : statement;
|
return statement.tokens.isEmpty() ? null : statement;
|
||||||
|
@@ -1,9 +1,15 @@
|
|||||||
package io.github.applecommander.bastokenizer.api.optimizations;
|
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.Statement;
|
||||||
import io.github.applecommander.bastokenizer.api.model.Token.Type;
|
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 class RemoveRemStatements extends BaseVisitor {
|
||||||
|
public RemoveRemStatements(Configuration config) {
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visit(Statement statement) {
|
public Statement visit(Statement statement) {
|
||||||
return statement.tokens.get(0).type == Type.COMMENT ? null : statement;
|
return statement.tokens.get(0).type == Type.COMMENT ? null : statement;
|
||||||
|
@@ -1,9 +1,19 @@
|
|||||||
package io.github.applecommander.bastokenizer.api.optimizations;
|
package io.github.applecommander.bastokenizer.api.optimizations;
|
||||||
|
|
||||||
|
import io.github.applecommander.bastokenizer.api.Configuration;
|
||||||
import io.github.applecommander.bastokenizer.api.model.Line;
|
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 {
|
public class Renumber extends BaseVisitor {
|
||||||
protected int lineNumber = 0;
|
protected int lineNumber = 0;
|
||||||
|
|
||||||
|
public Renumber(Configuration config) {
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Line visit(Line line) {
|
public Line visit(Line line) {
|
||||||
Line newLine = new Line(lineNumber++, this.newProgram);
|
Line newLine = new Line(lineNumber++, this.newProgram);
|
||||||
|
Reference in New Issue
Block a user