Adding $hex directive as a hack to allow hex numbers.

This commit is contained in:
Rob Greene 2018-06-10 22:09:00 -05:00
parent 0a31aee7e9
commit 68709f8ca1
4 changed files with 62 additions and 0 deletions

1
.gitignore vendored
View File

@ -18,6 +18,7 @@ bin/
*.dsk
*.po
*.do
*.bas
# Gradle extras
.gradle/

View File

@ -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`.

View File

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

View File

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