From b715dbc53639f37ec580bef63eacba3752846097 Mon Sep 17 00:00:00 2001 From: Rob Greene Date: Sat, 19 May 2018 20:02:54 -0500 Subject: [PATCH] Added --addresses to display addresses of each line number. Closes #13. --- .../applecommander/util/applesoft/Visitors.java | 15 ++++++++++----- .../applecommander/bastokenizer/Main.java | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/webcodepro/applecommander/util/applesoft/Visitors.java b/src/main/java/com/webcodepro/applecommander/util/applesoft/Visitors.java index d8305ca..1712e1a 100644 --- a/src/main/java/com/webcodepro/applecommander/util/applesoft/Visitors.java +++ b/src/main/java/com/webcodepro/applecommander/util/applesoft/Visitors.java @@ -11,6 +11,7 @@ import java.util.Optional; import java.util.Set; import java.util.SortedSet; import java.util.Stack; +import java.util.TreeMap; import java.util.TreeSet; import java.util.function.Function; @@ -180,11 +181,13 @@ public class Visitors { public static class ByteVisitor implements Visitor { private Stack stack; + private Map lineAddresses; private int address; private ByteVisitor(int address) { this.address = address; this.stack = new Stack<>(); + this.lineAddresses = new TreeMap<>(); } /** A convenience method to invoke {@link Program#accept(Visitor)} and {@link #getBytes()}. */ @@ -198,7 +201,10 @@ public class Visitors { stack.push(new ByteArrayOutputStream()); line.accept(this); return stack.pop().size(); - + } + + public Map getLineAddresses() { + return lineAddresses; } public byte[] getBytes() { @@ -210,9 +216,7 @@ public class Visitors { @Override public Program visit(Program program) { - if (stack.size() != 0) { - throw new RuntimeException("Please do not reuse this ByteVisitor as that is an unsafe operation."); - } + stack.clear(); stack.push(new ByteArrayOutputStream()); program.lines.forEach(line -> line.accept(this)); ByteArrayOutputStream os = stack.peek(); @@ -234,7 +238,8 @@ public class Visitors { } statement.accept(this); } - + + this.lineAddresses.put(line.lineNumber, this.address); byte[] content = stack.pop().toByteArray(); int nextAddress = address + content.length + 5; ByteArrayOutputStream os = stack.peek(); diff --git a/src/main/java/io/github/applecommander/bastokenizer/Main.java b/src/main/java/io/github/applecommander/bastokenizer/Main.java index 01b7285..3a0c95f 100644 --- a/src/main/java/io/github/applecommander/bastokenizer/Main.java +++ b/src/main/java/io/github/applecommander/bastokenizer/Main.java @@ -19,6 +19,7 @@ import com.webcodepro.applecommander.util.applesoft.Token; import com.webcodepro.applecommander.util.applesoft.Token.Type; import com.webcodepro.applecommander.util.applesoft.TokenReader; import com.webcodepro.applecommander.util.applesoft.Visitors; +import com.webcodepro.applecommander.util.applesoft.Visitors.ByteVisitor; import picocli.CommandLine; import picocli.CommandLine.Command; @@ -60,6 +61,9 @@ public class Main implements Callable { @Option(names = "--tokens", description = "Dump token list to stdout for debugging.") boolean showTokens; + @Option(names = "--addresses", description = "Dump line number addresses out.") + boolean showLineAddresses; + @Option(names = "--max-line-length", description = "Maximum line length for generated lines.", showDefaultValue = Visibility.ALWAYS) int maxLineLength = 255; @@ -107,11 +111,12 @@ public class Main implements Callable { optimizations.clear(); optimizations.addAll(Arrays.asList(Optimization.values())); } - boolean hasOutput = hexFormat || copyFormat || prettyPrint || listPrint || showTokens || showVariableReport || debugFlag; - if (pipeOutput && hasOutput) { + boolean hasTextOutput = hexFormat || copyFormat || prettyPrint || listPrint || showTokens || showVariableReport + || debugFlag || showLineAddresses; + if (pipeOutput && hasTextOutput) { System.err.println("The pipe option blocks any other stdout options."); return false; - } else if (!(pipeOutput || hasOutput || outputFile != null)) { + } else if (!(pipeOutput || hasTextOutput || outputFile != null)) { System.err.println("What do you want to do?"); return false; } @@ -139,7 +144,8 @@ public class Main implements Callable { program.accept(Visitors.variableReportVisitor()); } - byte[] data = Visitors.byteVisitor(address).dump(program); + ByteVisitor byteVisitor = Visitors.byteVisitor(address); + byte[] data = byteVisitor.dump(program); if (hexFormat) { HexDumper.standard().dump(address, data); } @@ -149,6 +155,9 @@ public class Main implements Callable { if (outputFile != null) { Files.write(outputFile.toPath(), data); } + if (showLineAddresses) { + byteVisitor.getLineAddresses().forEach((l,a) -> System.out.printf("%5d ... $%04x\n", l, a)); + } if (pipeOutput) { System.out.write(data); }