mirror of
https://github.com/AppleCommander/bastools.git
synced 2026-01-23 03:16:04 +00:00
Adding shorten-numbers optimization. (0.6 is output as just .6 since leading 0 is not needed.)
This commit is contained in:
@@ -19,12 +19,7 @@ package org.applecommander.bastools.api;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.applecommander.bastools.api.optimizations.ExtractConstantValues;
|
||||
import org.applecommander.bastools.api.optimizations.ShortenVariableNames;
|
||||
import org.applecommander.bastools.api.optimizations.MergeLines;
|
||||
import org.applecommander.bastools.api.optimizations.RemoveEmptyStatements;
|
||||
import org.applecommander.bastools.api.optimizations.RemoveRemStatements;
|
||||
import org.applecommander.bastools.api.optimizations.Renumber;
|
||||
import org.applecommander.bastools.api.optimizations.*;
|
||||
|
||||
/**
|
||||
* All optimization capabilities are definined here in the "best" manner of execution.
|
||||
@@ -36,7 +31,8 @@ public enum Optimization {
|
||||
SHORTEN_VARIABLE_NAMES(ShortenVariableNames::new),
|
||||
EXTRACT_CONSTANT_VALUES(ExtractConstantValues::new),
|
||||
MERGE_LINES(MergeLines::new),
|
||||
RENUMBER(Renumber::new);
|
||||
RENUMBER(Renumber::new),
|
||||
SHORTEN_NUMBERS(ShortenNumbers::new);
|
||||
|
||||
private final Function<Configuration,Visitor> factory;
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.applecommander.bastools.api.optimizations;
|
||||
|
||||
import org.applecommander.bastools.api.Configuration;
|
||||
import org.applecommander.bastools.api.model.Token;
|
||||
|
||||
public class ShortenNumbers extends BaseVisitor {
|
||||
public ShortenNumbers(Configuration config) {
|
||||
if (!config.preserveNumbers) {
|
||||
System.err.println("Warning: number preservation should be enabled for shorten numbers optimization.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token visit(Token token) {
|
||||
if (token.type() == Token.Type.NUMBER) {
|
||||
String text = Double.toString(token.number());
|
||||
if (Math.rint(token.number()) == token.number()) {
|
||||
text = Integer.toString(token.number().intValue());
|
||||
} else {
|
||||
while (text.startsWith("0") && text.length() > 1) {
|
||||
text = text.substring(1);
|
||||
}
|
||||
}
|
||||
return Token.number(token.line(), token.number(), text);
|
||||
}
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@
|
||||
Usage: bt [-chOVx] [--addresses] [--applesingle] [--debug] [--list] [--pretty]
|
||||
[--stdout] [--tokens] [--variables] [--wrapper] [-a=<address>]
|
||||
[--max-line-length=<maxLineLength>] [-o=<outputFile>]
|
||||
[-f=<optimizations>[,<optimizations>...]]... [--modern | --classic |
|
||||
--preserve] <sourceFile>
|
||||
[-f=<optimizations>[,<optimizations>...]]... [--preserve | --modern |
|
||||
--classic] <sourceFile>
|
||||
|
||||
Transforms an AppleSoft program from text back to its tokenized state.
|
||||
<sourceFile> AppleSoft BASIC program to process.
|
||||
@@ -29,6 +29,7 @@ Options:
|
||||
values first.
|
||||
* merge-lines - Merge lines.
|
||||
* renumber - Renumber program.
|
||||
* shorten-numbers - Shorten numbers.
|
||||
-h, --help Show this help message and exit.
|
||||
--list List structure as bastools understands it.
|
||||
--max-line-length=<maxLineLength>
|
||||
|
||||
@@ -101,7 +101,8 @@ public class Main implements Callable<Integer> {
|
||||
"* @|green shorten-variable-names|@ - Ensure all variables are 1 or 2 characters long.",
|
||||
"* @|green extract-constant-values|@ - Assign all constant values first.",
|
||||
"* @|green merge-lines|@ - Merge lines.",
|
||||
"* @|green renumber|@ - Renumber program."
|
||||
"* @|green renumber|@ - Renumber program.",
|
||||
"* @|green shorten-numbers|@ - Shorten numbers."
|
||||
})
|
||||
private List<Optimization> optimizations = new ArrayList<>();
|
||||
|
||||
@@ -193,6 +194,10 @@ public class Main implements Callable<Integer> {
|
||||
optimizations.clear();
|
||||
optimizations.addAll(Arrays.asList(Optimization.values()));
|
||||
}
|
||||
// Special handling for the "shorten numbers" optimization to be applied
|
||||
if (optimizations.contains(Optimization.SHORTEN_NUMBERS)) {
|
||||
tokenizer.preserveNumbers = true;
|
||||
}
|
||||
boolean hasTextOutput = hexFormat || copyFormat || prettyPrint || listPrint || showTokens || showVariableReport
|
||||
|| debugFlag || showLineAddresses;
|
||||
if (stdoutFlag && hasTextOutput) {
|
||||
|
||||
@@ -112,3 +112,18 @@ tests:
|
||||
80 PRINT "B=";B
|
||||
90 PRINT "C=";C
|
||||
95 PRINT "D=";D
|
||||
- name: check number optimization
|
||||
steps:
|
||||
- command: bt -f=shorten-numbers --list $numberPreservation
|
||||
criteria:
|
||||
whitespace: trim
|
||||
stdout: |
|
||||
10 PRINT "MATHING"
|
||||
30 A = .4
|
||||
40 B = .6
|
||||
50 C = - .25
|
||||
60 D = - .7
|
||||
70 PRINT "A=";A
|
||||
80 PRINT "B=";B
|
||||
90 PRINT "C=";C
|
||||
95 PRINT "D=";D
|
||||
|
||||
Reference in New Issue
Block a user