mirror of
https://github.com/AppleCommander/AppleCommander.git
synced 2026-11-25 21:27:36 +00:00
Generalizing a BASICConverter interface.
This commit is contained in:
1 parent
dc646a2309
commit
f7770da36f
5 files changed
+103
-67
No files matched your search
@@ -35,8 +35,8 @@ import javafx.scene.layout.StackPane;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.scene.text.Text;
|
||||
import javafx.stage.Stage;
|
||||
import org.applecommander.converter.ApplesoftConverter;
|
||||
import org.applecommander.converter.ApplesoftVariables;
|
||||
import org.applecommander.converter.BASICConverter;
|
||||
import org.applecommander.converter.ConverterRepository;
|
||||
import org.applecommander.filestore.ContentType;
|
||||
import org.applecommander.filestore.ContentType.Subtype;
|
||||
@@ -257,9 +257,9 @@ public class FileContentWindow {
|
||||
textView.getChildren().add(new Text(sw.toString()));
|
||||
}
|
||||
|
||||
private void showBASIC(ApplesoftConverter converter) {
|
||||
private void showBASIC(BASICConverter converter) {
|
||||
List<Text> components = new ArrayList<>();
|
||||
converter.generate(new ApplesoftConverter.EventHandler() {
|
||||
converter.generate(new BASICConverter.EventHandler() {
|
||||
@Override
|
||||
public void startOfLine(String lineNumber) {
|
||||
components.add(new Text(lineNumber));
|
||||
@@ -301,6 +301,10 @@ public class FileContentWindow {
|
||||
components.add(new Text(syntax));
|
||||
}
|
||||
});
|
||||
|
||||
Font font = Font.font("monospaced", fontSize);
|
||||
components.forEach(t -> t.setFont(font));
|
||||
|
||||
textView.getChildren().clear();
|
||||
textView.getChildren().addAll(components);
|
||||
}
|
||||
|
||||
@@ -23,15 +23,11 @@ import org.applecommander.filestore.FileEntry;
|
||||
import org.applecommander.transfer.ProdosAttributes;
|
||||
import org.applecommander.util.DataBuffer;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/// Detokenize the given Applesoft file. Generates a series of events
|
||||
/// that can be used to generate a variety of outputs. The intent is
|
||||
/// to allow a GUI to (for instance) allow line numbers to be clicked
|
||||
/// on for navigation and to allow a CLI tool to dump the file as text.
|
||||
public class ApplesoftConverter {
|
||||
public class ApplesoftConverter implements BASICConverter {
|
||||
private static final Token[] TOKENS;
|
||||
static {
|
||||
Token[] tokens = new Token[256];
|
||||
@@ -54,7 +50,7 @@ public class ApplesoftConverter {
|
||||
this.program = program;
|
||||
}
|
||||
|
||||
public void generate(EventHandler handler) {
|
||||
public void generate(BASICConverter.EventHandler handler) {
|
||||
program.position(0);
|
||||
int nextAddress = 0;
|
||||
State state = State.START;
|
||||
@@ -117,49 +113,9 @@ public class ApplesoftConverter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Test if this character is an ASCII character between A and Z. Java's tests are for Unicode.
|
||||
public boolean isCharacter(int ch) {
|
||||
return ch >= 'A' && ch <= 'Z';
|
||||
}
|
||||
|
||||
/// Test if this character is a digit. Just in case the digits have extra codes for Unicode.
|
||||
public boolean isDigit(int ch) {
|
||||
return ch >= '0' && ch <= '9';
|
||||
}
|
||||
|
||||
private String scanUntil(DataBuffer program, String start, int... endChars) {
|
||||
Set<Integer> endValues = new HashSet<>();
|
||||
for (int endChar : endChars) endValues.add(endChar);
|
||||
StringBuilder sb = new StringBuilder(start);
|
||||
int value = program.readUnsignedByte();
|
||||
while (!endValues.contains(value)) {
|
||||
sb.append((char)value);
|
||||
value = program.readUnsignedByte();
|
||||
}
|
||||
if (value != 0) {
|
||||
sb.append((char)value);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
private String scanWhile(DataBuffer program, int firstChar, Predicate<Integer> includeFn) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (firstChar != -1) {
|
||||
sb.append((char)firstChar);
|
||||
}
|
||||
int value = program.readUnsignedByte();
|
||||
while (includeFn.test(value)) {
|
||||
sb.append((char)value);
|
||||
value = program.readUnsignedByte();
|
||||
}
|
||||
// Rewind to the character we don't want
|
||||
program.position(program.position()-1);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private enum State {
|
||||
START,
|
||||
CODE,
|
||||
TARGET
|
||||
CODE
|
||||
}
|
||||
|
||||
public enum Token {
|
||||
@@ -293,17 +249,4 @@ public class ApplesoftConverter {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
public interface EventHandler {
|
||||
void startOfLine(String lineNumber);
|
||||
void rawBytes(byte[] data);
|
||||
void token(String text);
|
||||
void comment(String comment);
|
||||
void endOfLine();
|
||||
void string(String string);
|
||||
void targetLine(String lineNumber);
|
||||
void identifier(String identifier);
|
||||
void number(String number);
|
||||
void syntax(String syntax);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* AppleCommander - An Apple ][ image utility.
|
||||
* Copyright (C) 2026 by Robert Greene and others
|
||||
* robgreene at users.sourceforge.net
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
package org.applecommander.converter;
|
||||
|
||||
import org.applecommander.util.DataBuffer;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/// This is a generalized BASIC converter interface.
|
||||
public interface BASICConverter {
|
||||
void generate(EventHandler handler);
|
||||
|
||||
/// Test if this character is an ASCII character between A and Z. Java's tests are for Unicode.
|
||||
default boolean isCharacter(int ch) {
|
||||
return ch >= 'A' && ch <= 'Z';
|
||||
}
|
||||
|
||||
/// Test if this character is a digit. Just in case the digits have extra codes for Unicode.
|
||||
default boolean isDigit(int ch) {
|
||||
return ch >= '0' && ch <= '9';
|
||||
}
|
||||
|
||||
/// Scan the program buffer until the end characters are encountered. Includes end character
|
||||
/// in the output unless it's a `$00`. Useful for reading an entire string or to find the end of line.
|
||||
default String scanUntil(DataBuffer program, String start, int... endChars) {
|
||||
Set<Integer> endValues = new HashSet<>();
|
||||
for (int endChar : endChars) endValues.add(endChar);
|
||||
StringBuilder sb = new StringBuilder(start);
|
||||
int value = program.readUnsignedByte();
|
||||
while (!endValues.contains(value)) {
|
||||
sb.append((char)value);
|
||||
value = program.readUnsignedByte();
|
||||
}
|
||||
if (value != 0) {
|
||||
sb.append((char)value);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/// Scan the program buffer while the test is true. Includes the first character in the
|
||||
/// buffer unless it's a `-1`. Rewinds the buffer by a character when the character
|
||||
/// test indicates it's read too far. Useful for reading a number in its entirety.
|
||||
default String scanWhile(DataBuffer program, int firstChar, Predicate<Integer> includeFn) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (firstChar != -1) {
|
||||
sb.append((char)firstChar);
|
||||
}
|
||||
int value = program.readUnsignedByte();
|
||||
while (includeFn.test(value)) {
|
||||
sb.append((char)value);
|
||||
value = program.readUnsignedByte();
|
||||
}
|
||||
// Rewind to the character we don't want
|
||||
program.position(program.position()-1);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
interface EventHandler {
|
||||
void startOfLine(String lineNumber);
|
||||
void rawBytes(byte[] data);
|
||||
void token(String text);
|
||||
void comment(String comment);
|
||||
void endOfLine();
|
||||
void string(String string);
|
||||
void targetLine(String lineNumber);
|
||||
void identifier(String identifier);
|
||||
void number(String number);
|
||||
void syntax(String syntax);
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class ConverterRepository {
|
||||
}
|
||||
|
||||
/// Get the BASIC file converter.
|
||||
public static Optional<ApplesoftConverter> getBASIC(FileEntry fileEntry) {
|
||||
public static Optional<BASICConverter> getBASIC(FileEntry fileEntry) {
|
||||
return switch(fileEntry.getContentType()) {
|
||||
case APPLESOFT -> Optional.of(new ApplesoftConverter(fileEntry));
|
||||
default -> Optional.empty();
|
||||
|
||||
@@ -44,7 +44,7 @@ public class ApplesoftConverterTest {
|
||||
30 PRINT CHR$ (4);"STORE STRINGVARS"
|
||||
""";
|
||||
ApplesoftConverter converter = new ApplesoftConverter(DataBuffer.wrap(program), 0x801);
|
||||
ApplesoftConverter.EventHandler handler = new BasicText();
|
||||
BASICConverter.EventHandler handler = new BasicText();
|
||||
converter.generate(handler);
|
||||
assertEquals(expectedText, handler.toString());
|
||||
}
|
||||
@@ -75,12 +75,12 @@ public class ApplesoftConverterTest {
|
||||
100 PRINT CHR$ (4);"STORE ARRAYVARS"
|
||||
""";
|
||||
ApplesoftConverter converter = new ApplesoftConverter(DataBuffer.wrap(program), 0x801);
|
||||
ApplesoftConverter.EventHandler handler = new BasicText();
|
||||
BASICConverter.EventHandler handler = new BasicText();
|
||||
converter.generate(handler);
|
||||
assertEquals(expectedText, handler.toString());
|
||||
}
|
||||
|
||||
public static class BasicText implements ApplesoftConverter.EventHandler {
|
||||
public static class BasicText implements BASICConverter.EventHandler {
|
||||
private final StringBuilder program = new StringBuilder();
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user