mirror of
https://github.com/AppleCommander/bastools.git
synced 2025-04-10 09:37:03 +00:00
Adding renumber (which takes advantage of reassignment). Closes #12.
This commit is contained in:
parent
bf5c9f8e10
commit
3f4c47551a
@ -58,7 +58,8 @@ public class Main implements Callable<Void> {
|
||||
@Option(names = "-f", converter = Optimization.TypeConverter.class, split = ",", description = {
|
||||
"Enable specific optimizations.",
|
||||
"* @|green remove-empty-statements|@ - Strip out all '::'-like statements.",
|
||||
"* @|yellow remove-rem-statements|@ - Remove all REM statements."
|
||||
"* @|green remove-rem-statements|@ - Remove all REM statements.",
|
||||
"* @|green renumber|@ - Renumber program."
|
||||
})
|
||||
private List<Optimization> optimizations = new ArrayList<>();
|
||||
|
||||
|
@ -25,6 +25,17 @@ public enum Optimization {
|
||||
public Statement visit(Statement statement) {
|
||||
return statement.tokens.get(0).type == Type.COMMENT ? null : statement;
|
||||
}
|
||||
}),
|
||||
RENUMBER(new BaseVisitor() {
|
||||
protected int lineNumber = 0;
|
||||
@Override
|
||||
public Line visit(Line line) {
|
||||
Line newLine = new Line(lineNumber++);
|
||||
newLine.statements.addAll(line.statements);
|
||||
// Track what went where so lines can get renumbered automatically
|
||||
reassignments.put(line.lineNumber, newLine.lineNumber);
|
||||
return newLine;
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
@ -53,10 +64,10 @@ public enum Optimization {
|
||||
}
|
||||
/** Common base class for optimization visitors that allow the program tree to be rewritten. */
|
||||
private static class BaseVisitor implements Visitor {
|
||||
protected Map<Integer,Integer> reassignments = new HashMap<>();
|
||||
@Override
|
||||
public Program visit(Program program) {
|
||||
final Program newProgram = new Program();
|
||||
Map<Integer,Integer> reassignments = new HashMap<>();
|
||||
program.lines.forEach(l -> {
|
||||
Line line = l.accept(this);
|
||||
boolean lineKept = line != null && !line.statements.isEmpty();
|
||||
|
Loading…
x
Reference in New Issue
Block a user