mirror of
https://github.com/AppleCommander/bastools.git
synced 2025-01-18 08:30:48 +00:00
Added --addresses to display addresses of each line number. Closes #13.
This commit is contained in:
parent
a830af29ea
commit
b715dbc536
@ -11,6 +11,7 @@ import java.util.Optional;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
import java.util.TreeMap;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@ -180,11 +181,13 @@ public class Visitors {
|
|||||||
|
|
||||||
public static class ByteVisitor implements Visitor {
|
public static class ByteVisitor implements Visitor {
|
||||||
private Stack<ByteArrayOutputStream> stack;
|
private Stack<ByteArrayOutputStream> stack;
|
||||||
|
private Map<Integer,Integer> lineAddresses;
|
||||||
private int address;
|
private int address;
|
||||||
|
|
||||||
private ByteVisitor(int address) {
|
private ByteVisitor(int address) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.stack = new Stack<>();
|
this.stack = new Stack<>();
|
||||||
|
this.lineAddresses = new TreeMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A convenience method to invoke {@link Program#accept(Visitor)} and {@link #getBytes()}. */
|
/** A convenience method to invoke {@link Program#accept(Visitor)} and {@link #getBytes()}. */
|
||||||
@ -198,7 +201,10 @@ public class Visitors {
|
|||||||
stack.push(new ByteArrayOutputStream());
|
stack.push(new ByteArrayOutputStream());
|
||||||
line.accept(this);
|
line.accept(this);
|
||||||
return stack.pop().size();
|
return stack.pop().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Integer, Integer> getLineAddresses() {
|
||||||
|
return lineAddresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getBytes() {
|
public byte[] getBytes() {
|
||||||
@ -210,9 +216,7 @@ public class Visitors {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Program visit(Program program) {
|
public Program visit(Program program) {
|
||||||
if (stack.size() != 0) {
|
stack.clear();
|
||||||
throw new RuntimeException("Please do not reuse this ByteVisitor as that is an unsafe operation.");
|
|
||||||
}
|
|
||||||
stack.push(new ByteArrayOutputStream());
|
stack.push(new ByteArrayOutputStream());
|
||||||
program.lines.forEach(line -> line.accept(this));
|
program.lines.forEach(line -> line.accept(this));
|
||||||
ByteArrayOutputStream os = stack.peek();
|
ByteArrayOutputStream os = stack.peek();
|
||||||
@ -235,6 +239,7 @@ public class Visitors {
|
|||||||
statement.accept(this);
|
statement.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.lineAddresses.put(line.lineNumber, this.address);
|
||||||
byte[] content = stack.pop().toByteArray();
|
byte[] content = stack.pop().toByteArray();
|
||||||
int nextAddress = address + content.length + 5;
|
int nextAddress = address + content.length + 5;
|
||||||
ByteArrayOutputStream os = stack.peek();
|
ByteArrayOutputStream os = stack.peek();
|
||||||
|
@ -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.Token.Type;
|
||||||
import com.webcodepro.applecommander.util.applesoft.TokenReader;
|
import com.webcodepro.applecommander.util.applesoft.TokenReader;
|
||||||
import com.webcodepro.applecommander.util.applesoft.Visitors;
|
import com.webcodepro.applecommander.util.applesoft.Visitors;
|
||||||
|
import com.webcodepro.applecommander.util.applesoft.Visitors.ByteVisitor;
|
||||||
|
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
import picocli.CommandLine.Command;
|
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.")
|
@Option(names = "--tokens", description = "Dump token list to stdout for debugging.")
|
||||||
boolean showTokens;
|
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)
|
@Option(names = "--max-line-length", description = "Maximum line length for generated lines.", showDefaultValue = Visibility.ALWAYS)
|
||||||
int maxLineLength = 255;
|
int maxLineLength = 255;
|
||||||
|
|
||||||
@ -107,11 +111,12 @@ public class Main implements Callable<Void> {
|
|||||||
optimizations.clear();
|
optimizations.clear();
|
||||||
optimizations.addAll(Arrays.asList(Optimization.values()));
|
optimizations.addAll(Arrays.asList(Optimization.values()));
|
||||||
}
|
}
|
||||||
boolean hasOutput = hexFormat || copyFormat || prettyPrint || listPrint || showTokens || showVariableReport || debugFlag;
|
boolean hasTextOutput = hexFormat || copyFormat || prettyPrint || listPrint || showTokens || showVariableReport
|
||||||
if (pipeOutput && hasOutput) {
|
|| debugFlag || showLineAddresses;
|
||||||
|
if (pipeOutput && hasTextOutput) {
|
||||||
System.err.println("The pipe option blocks any other stdout options.");
|
System.err.println("The pipe option blocks any other stdout options.");
|
||||||
return false;
|
return false;
|
||||||
} else if (!(pipeOutput || hasOutput || outputFile != null)) {
|
} else if (!(pipeOutput || hasTextOutput || outputFile != null)) {
|
||||||
System.err.println("What do you want to do?");
|
System.err.println("What do you want to do?");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -139,7 +144,8 @@ public class Main implements Callable<Void> {
|
|||||||
program.accept(Visitors.variableReportVisitor());
|
program.accept(Visitors.variableReportVisitor());
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] data = Visitors.byteVisitor(address).dump(program);
|
ByteVisitor byteVisitor = Visitors.byteVisitor(address);
|
||||||
|
byte[] data = byteVisitor.dump(program);
|
||||||
if (hexFormat) {
|
if (hexFormat) {
|
||||||
HexDumper.standard().dump(address, data);
|
HexDumper.standard().dump(address, data);
|
||||||
}
|
}
|
||||||
@ -149,6 +155,9 @@ public class Main implements Callable<Void> {
|
|||||||
if (outputFile != null) {
|
if (outputFile != null) {
|
||||||
Files.write(outputFile.toPath(), data);
|
Files.write(outputFile.toPath(), data);
|
||||||
}
|
}
|
||||||
|
if (showLineAddresses) {
|
||||||
|
byteVisitor.getLineAddresses().forEach((l,a) -> System.out.printf("%5d ... $%04x\n", l, a));
|
||||||
|
}
|
||||||
if (pipeOutput) {
|
if (pipeOutput) {
|
||||||
System.out.write(data);
|
System.out.write(data);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user