Now supporting 3 "tokenizers" (modern, classic, preserve). Updated output visitors (byte, print, pretty print) to support number preservation.

This commit is contained in:
Rob Greene
2025-11-06 15:06:27 -06:00
parent 3cf47af205
commit 4a3c117d5e
6 changed files with 95 additions and 41 deletions
@@ -17,6 +17,8 @@
*/
package org.applecommander.bastools.api;
import org.applecommander.bastools.api.model.Token;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;
@@ -30,13 +32,29 @@ public class Configuration {
public final int maxLineLength;
public final PrintStream debugStream;
public final Map<String,String >variableReplacements = new HashMap<>();
public final boolean preserveNumbers;
private Configuration(Builder b) {
this.sourceFile = b.sourceFile;
this.startAddress = b.startAddress;
this.maxLineLength = b.maxLineLength;
this.debugStream = b.debugStream;
this.preserveNumbers = b.preserveNumbers;
}
public String numberToString(Token token) {
if (preserveNumbers && token.text() != null) {
return token.text();
}
else {
if (Math.rint(token.number()) == token.number()) {
return Integer.toString(token.number().intValue());
} else {
return Double.toString(token.number());
}
}
}
public static Builder builder() {
return new Builder();
@@ -53,6 +71,7 @@ public class Configuration {
// Do nothing
}
});
private boolean preserveNumbers;
public Builder sourceFile(File sourceFile) {
this.sourceFile = sourceFile;
@@ -70,6 +89,10 @@ public class Configuration {
this.debugStream = debugStream;
return this;
}
public Builder preserveNumbers(boolean preserveNumbers) {
this.preserveNumbers = preserveNumbers;
return this;
}
public Configuration build() {
Objects.requireNonNull(sourceFile, "Please configure a sourceFile");
@@ -38,12 +38,17 @@ import org.applecommander.bastools.api.visitors.VariableReportVisitor;
* @author rob
*/
public class Visitors {
public static PrintBuilder printBuilder() {
return new PrintBuilder();
public static PrintBuilder printBuilder(Configuration config) {
return new PrintBuilder(config);
}
public static class PrintBuilder {
private final Configuration config;
private PrintStream printStream = System.out;
private Function<PrintBuilder,Visitor> creator = PrintVisitor::new;
private PrintBuilder(Configuration config) {
this.config = config;
}
public PrintBuilder printStream(PrintStream printStream) {
Objects.requireNonNull(printStream);
@@ -69,7 +74,10 @@ public class Visitors {
public PrintStream getPrintStream() {
return printStream;
}
}
public Configuration getConfig() {
return config;
}
}
public static ByteVisitor byteVisitor(Configuration config) {
return new ByteVisitor(config);
@@ -150,11 +150,7 @@ public class ByteVisitor implements Visitor {
currentDirective = Directives.find(token.text(), config, os);
break;
case NUMBER:
if (Math.rint(token.number()) == token.number()) {
os.write(Integer.toString(token.number().intValue()).getBytes());
} else {
os.write(Double.toString(token.number()).getBytes());
}
os.write(config.numberToString(token).getBytes());
break;
case STRING:
os.write('"');
@@ -19,6 +19,7 @@ package org.applecommander.bastools.api.visitors;
import java.io.PrintStream;
import org.applecommander.bastools.api.Configuration;
import org.applecommander.bastools.api.Visitor;
import org.applecommander.bastools.api.Visitors.PrintBuilder;
import org.applecommander.bastools.api.model.Line;
@@ -26,9 +27,11 @@ import org.applecommander.bastools.api.model.Statement;
import org.applecommander.bastools.api.model.Token;
public class PrettyPrintVisitor implements Visitor {
private final Configuration config;
private final PrintStream printStream;
public PrettyPrintVisitor(PrintBuilder builder) {
this.config = builder.getConfig();
this.printStream = builder.getPrintStream();
}
@@ -56,7 +59,7 @@ public class PrettyPrintVisitor implements Visitor {
case COMMENT:
printStream.printf(" REM %s", token.text());
break;
case DATA:
case DATA, IDENT, SYNTAX:
printStream.print(token.text());
break;
case STRING:
@@ -65,19 +68,11 @@ public class PrettyPrintVisitor implements Visitor {
case KEYWORD:
printStream.printf(" %s ", token.keyword().text);
break;
case IDENT:
case SYNTAX:
printStream.print(token.text());
break;
case DIRECTIVE:
case DIRECTIVE:
printStream.printf("%s ", token.text());
break;
case NUMBER:
if (Math.rint(token.number()) == token.number()) {
printStream.print(token.number().intValue());
} else {
printStream.print(token.number());
}
printStream.print(config.numberToString(token));
break;
}
return token;
@@ -19,6 +19,7 @@ package org.applecommander.bastools.api.visitors;
import java.io.PrintStream;
import org.applecommander.bastools.api.Configuration;
import org.applecommander.bastools.api.Visitor;
import org.applecommander.bastools.api.Visitors.PrintBuilder;
import org.applecommander.bastools.api.model.Line;
@@ -26,9 +27,11 @@ import org.applecommander.bastools.api.model.Statement;
import org.applecommander.bastools.api.model.Token;
public class PrintVisitor implements Visitor {
private final Configuration config;
private final PrintStream printStream;
public PrintVisitor(PrintBuilder builder) {
this.config = builder.getConfig();
this.printStream = builder.getPrintStream();
}
@@ -40,7 +43,7 @@ public class PrintVisitor implements Visitor {
if (first) {
first = false;
} else {
printStream.printf(":");
printStream.print(":");
}
statement.accept(this);
}
@@ -56,7 +59,7 @@ public class PrintVisitor implements Visitor {
case COMMENT:
printStream.printf("REM %s", token.text());
break;
case DATA:
case DATA, IDENT, SYNTAX:
printStream.print(token.text());
break;
case STRING:
@@ -65,19 +68,11 @@ public class PrintVisitor implements Visitor {
case KEYWORD:
printStream.printf(" %s ", token.keyword().text);
break;
case IDENT:
case SYNTAX:
printStream.print(token.text());
break;
case DIRECTIVE:
case DIRECTIVE:
printStream.printf("%s ", token.text());
break;
case NUMBER:
if (Math.rint(token.number()) == token.number()) {
printStream.print(token.number().intValue());
} else {
printStream.print(token.number());
}
printStream.print(config.numberToString(token));
break;
}
return token;