diff --git a/.gitignore b/.gitignore index 6a31f19..16b76f6 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ bin/ *.dsk *.po *.do +*.bas # Gradle extras .gradle/ diff --git a/api/README.md b/api/README.md index 72650f2..afb627c 100644 --- a/api/README.md +++ b/api/README.md @@ -96,6 +96,22 @@ LDY #0 JMP $FE2C ``` +### `$hex` + +If embedding hexidecimal addresses into an application makes sense, the `$hex` directive allows that to be done in a rudimentary manner. + +Sample: + +``` +10 call $hex "fc58" +``` + +Yields: + +``` +10 call -936 +``` + ## Optimizations Optimizations are mechanisms to rewrite the `Program`, typically making the program smaller. `Optimization` itself is an enum which has a `create` method to setup the `Visitor`. diff --git a/api/src/main/java/io/github/applecommander/bastokenizer/api/Directives.java b/api/src/main/java/io/github/applecommander/bastokenizer/api/Directives.java index 2525e57..ff577ee 100644 --- a/api/src/main/java/io/github/applecommander/bastokenizer/api/Directives.java +++ b/api/src/main/java/io/github/applecommander/bastokenizer/api/Directives.java @@ -6,6 +6,7 @@ import java.util.Map; import java.util.TreeMap; import io.github.applecommander.bastokenizer.api.directives.EmbeddedBinaryDirective; +import io.github.applecommander.bastokenizer.api.directives.HexDirective; public class Directives { private Directives() { /* Prevent construction. */ } @@ -16,6 +17,7 @@ public class Directives { { put("$embed", EmbeddedBinaryDirective.class); + put("$hex", HexDirective.class); } }; diff --git a/api/src/main/java/io/github/applecommander/bastokenizer/api/directives/HexDirective.java b/api/src/main/java/io/github/applecommander/bastokenizer/api/directives/HexDirective.java new file mode 100644 index 0000000..e42a497 --- /dev/null +++ b/api/src/main/java/io/github/applecommander/bastokenizer/api/directives/HexDirective.java @@ -0,0 +1,43 @@ +package io.github.applecommander.bastokenizer.api.directives; + +import java.io.IOException; +import java.io.OutputStream; + +import io.github.applecommander.bastokenizer.api.Configuration; +import io.github.applecommander.bastokenizer.api.Directive; +import io.github.applecommander.bastokenizer.api.model.ApplesoftKeyword; +import io.github.applecommander.bastokenizer.api.model.Line; + +/** + * A simple directive to introduce hexidecimal capabilities. StreamTokenizer does not + * appear to support syntax, so using a directive to introduce the capability. + */ +public class HexDirective extends Directive { + public HexDirective(Configuration config, OutputStream outputStream) { + super(config, outputStream); + } + + @Override + public void writeBytes(int startAddress, Line line) throws IOException { + if (parameters.size() != 1) { + throw new RuntimeException("$hex directive requires one parameter"); + } + String string = requiresString(); + int value = Integer.parseInt(string, 16); + + if (value < 0 || value > 65535) { + throw new RuntimeException("$hex address out of range"); + } + + String value1 = Integer.toString(value); + String value2 = Integer.toString(value - 65536); + + String shortestValue = value1.length() < value2.length() ? value1 : value2; + + if (shortestValue.startsWith("-")) { + outputStream.write(ApplesoftKeyword.sub.code); + shortestValue = shortestValue.substring(1); + } + outputStream.write(shortestValue.getBytes()); + } +}