Added --addresses to display addresses of each line number. Closes #13.

This commit is contained in:
Rob Greene 2018-05-19 20:02:54 -05:00
parent a830af29ea
commit b715dbc536
2 changed files with 23 additions and 9 deletions

View File

@ -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<ByteArrayOutputStream> stack;
private Map<Integer,Integer> 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<Integer, Integer> 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();

View File

@ -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<Void> {
@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<Void> {
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<Void> {
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<Void> {
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);
}