From 9fece93d3160c2a8ed2507ca4ea8f20ed9398bfa Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Wed, 17 Mar 2021 14:31:37 +1000 Subject: [PATCH] tidying --- .../applefile/AppleBasicFormatter.java | 203 ++++++++++-------- .../diskbrowser/applefile/BasicProgram.java | 20 -- .../diskbrowser/applefile/SubLine.java | 2 +- 3 files changed, 113 insertions(+), 112 deletions(-) diff --git a/src/com/bytezone/diskbrowser/applefile/AppleBasicFormatter.java b/src/com/bytezone/diskbrowser/applefile/AppleBasicFormatter.java index 896f5e3..502be77 100644 --- a/src/com/bytezone/diskbrowser/applefile/AppleBasicFormatter.java +++ b/src/com/bytezone/diskbrowser/applefile/AppleBasicFormatter.java @@ -13,8 +13,8 @@ import com.bytezone.diskbrowser.gui.BasicPreferences; public class AppleBasicFormatter extends BasicFormatter // -----------------------------------------------------------------------------------// { - private static final int LEFT_MARGIN = 5; - private static final int RIGHT_MARGIN = 33; + private final LineFormatter flatFormatter = new FlatLine (); + private final LineFormatter wrapFormatter = new WrapLine (); // ---------------------------------------------------------------------------------// public AppleBasicFormatter (ApplesoftBasicProgram program, @@ -34,6 +34,8 @@ public class AppleBasicFormatter extends BasicFormatter int linkField; StringBuilder currentLine = new StringBuilder (); + LineFormatter formatter = + basicPreferences.appleLineWrap ? wrapFormatter : flatFormatter; while ((linkField = unsignedShort (buffer, ptr)) != 0) { @@ -41,10 +43,7 @@ public class AppleBasicFormatter extends BasicFormatter currentLine.append (String.format (" %d ", lineNumber)); ptr += 4; - if (basicPreferences.appleLineWrap) - ptr = wrapLine (currentLine, ptr); - else - ptr = flatLine (currentLine, ptr); + ptr = formatter.formatLine (currentLine, ptr); if (ptr != (linkField - loadAddress)) System.out.printf ("%s: ptr: %04X, nextLine: %04X%n", program.name, @@ -57,100 +56,122 @@ public class AppleBasicFormatter extends BasicFormatter } // ---------------------------------------------------------------------------------// - private int flatLine (StringBuilder currentLine, int ptr) + interface LineFormatter // ---------------------------------------------------------------------------------// { - byte b; - - while ((b = buffer[ptr++]) != 0) - if (isHighBitSet (b)) - { - String token = String.format (" %s ", ApplesoftConstants.tokens[b & 0x7F]); - currentLine.append (token); - } - else - switch (b) - { - case ASCII_CR: - currentLine.append (NEWLINE); - break; - - case ASCII_BACKSPACE: - if (currentLine.length () > 0) - currentLine.deleteCharAt (currentLine.length () - 1); - break; - - case ASCII_LF: - int indent = getIndent (currentLine); - currentLine.append ("\n"); - for (int i = 0; i < indent; i++) - currentLine.append (" "); - break; - - default: - currentLine.append ((char) b); - } - - return ptr; + abstract int formatLine (StringBuilder currentLine, int ptr); } // ---------------------------------------------------------------------------------// - private int wrapLine (StringBuilder currentLine, int ptr) + class FlatLine implements LineFormatter // ---------------------------------------------------------------------------------// { - byte b; - int cursor = currentLine.length (); - - while ((b = buffer[ptr++]) != 0) - if (isHighBitSet (b)) - { - String token = String.format (" %s ", ApplesoftConstants.tokens[b & 0x7F]); - currentLine.append (token); - cursor = incrementCursor (currentLine, cursor, token.length ()); - } - else - switch (b) - { - case ASCII_CR: - currentLine.append (NEWLINE); - cursor = 0; - break; - - case ASCII_BACKSPACE: - if (cursor > 0) - { - currentLine.deleteCharAt (currentLine.length () - 1); - --cursor; - } - break; - - case ASCII_LF: - currentLine.append ("\n"); - for (int i = 0; i < cursor; i++) - currentLine.append (" "); - break; - - default: - currentLine.append ((char) b); - cursor = incrementCursor (currentLine, cursor, 1); - } - - return ptr; - } - - // ---------------------------------------------------------------------------------// - private int incrementCursor (StringBuilder currentLine, int cursor, int size) - // ---------------------------------------------------------------------------------// - { - assert size <= 9; // longest token possible (7 plus 2 spaces) - cursor += size; - - if ((cursor) >= RIGHT_MARGIN) + // -------------------------------------------------------------------------------// + @Override + public int formatLine (StringBuilder currentLine, int ptr) + // -------------------------------------------------------------------------------// { - cursor = cursor >= 40 ? cursor - 40 : LEFT_MARGIN; - currentLine.append ("\n ".substring (0, cursor + 1)); + byte b; + + while ((b = buffer[ptr++]) != 0) + if (isHighBitSet (b)) + { + String token = String.format (" %s ", ApplesoftConstants.tokens[b & 0x7F]); + currentLine.append (token); + } + else + switch (b) + { + case ASCII_CR: + currentLine.append (NEWLINE); + break; + + case ASCII_BACKSPACE: + if (currentLine.length () > 0) + currentLine.deleteCharAt (currentLine.length () - 1); + break; + + case ASCII_LF: + int indent = getIndent (currentLine); + currentLine.append ("\n"); + for (int i = 0; i < indent; i++) + currentLine.append (" "); + break; + + default: + currentLine.append ((char) b); + } + + return ptr; + } + } + + // ---------------------------------------------------------------------------------// + class WrapLine implements LineFormatter + // ---------------------------------------------------------------------------------// + { + private static final int LEFT_MARGIN = 5; + private static final int RIGHT_MARGIN = 33; + + // -------------------------------------------------------------------------------// + @Override + public int formatLine (StringBuilder currentLine, int ptr) + // -------------------------------------------------------------------------------// + { + byte b; + int cursor = currentLine.length (); + + while ((b = buffer[ptr++]) != 0) + if (isHighBitSet (b)) + { + String token = String.format (" %s ", ApplesoftConstants.tokens[b & 0x7F]); + currentLine.append (token); + cursor = incrementCursor (currentLine, cursor, token.length ()); + } + else + switch (b) + { + case ASCII_CR: + currentLine.append (NEWLINE); + cursor = 0; + break; + + case ASCII_BACKSPACE: + if (cursor > 0) + { + currentLine.deleteCharAt (currentLine.length () - 1); + --cursor; + } + break; + + case ASCII_LF: + currentLine.append ("\n"); + for (int i = 0; i < cursor; i++) + currentLine.append (" "); + break; + + default: + currentLine.append ((char) b); + cursor = incrementCursor (currentLine, cursor, 1); + } + + return ptr; } - return cursor; + // -------------------------------------------------------------------------------// + private int incrementCursor (StringBuilder currentLine, int cursor, int size) + // -------------------------------------------------------------------------------// + { + assert size <= 9; // longest token possible (7 plus 2 spaces) + cursor += size; + + if ((cursor) >= RIGHT_MARGIN) + { + cursor = cursor >= 40 ? cursor - 40 : LEFT_MARGIN; + currentLine.append ("\n ".substring (0, cursor + 1)); + } + + return cursor; + } } } diff --git a/src/com/bytezone/diskbrowser/applefile/BasicProgram.java b/src/com/bytezone/diskbrowser/applefile/BasicProgram.java index 0dbeb83..74099ef 100644 --- a/src/com/bytezone/diskbrowser/applefile/BasicProgram.java +++ b/src/com/bytezone/diskbrowser/applefile/BasicProgram.java @@ -1,10 +1,6 @@ package com.bytezone.diskbrowser.applefile; -import static com.bytezone.diskbrowser.utilities.Utility.isDigit; -import static com.bytezone.diskbrowser.utilities.Utility.isLetter; - import com.bytezone.diskbrowser.gui.BasicPreferences; -import com.bytezone.diskbrowser.utilities.Utility; // -----------------------------------------------------------------------------------// public abstract class BasicProgram extends AbstractFile @@ -25,20 +21,4 @@ public abstract class BasicProgram extends AbstractFile { super (name, buffer); } - - // ---------------------------------------------------------------------------------// - boolean isControlCharacter (byte value) - // ---------------------------------------------------------------------------------// - { - int val = value & 0xFF; - return val > 0 && val < 32; - } - - // ---------------------------------------------------------------------------------// - boolean isPossibleVariable (byte value) - // ---------------------------------------------------------------------------------// - { - return isDigit (value) || isLetter (value) || value == Utility.ASCII_DOLLAR - || value == Utility.ASCII_PERCENT; - } } diff --git a/src/com/bytezone/diskbrowser/applefile/SubLine.java b/src/com/bytezone/diskbrowser/applefile/SubLine.java index 16a69ec..a130bf7 100644 --- a/src/com/bytezone/diskbrowser/applefile/SubLine.java +++ b/src/com/bytezone/diskbrowser/applefile/SubLine.java @@ -37,7 +37,7 @@ public class SubLine implements ApplesoftConstants String forVariable = ""; int equalsPosition; // used for aligning the equals sign - int endPosition; // not sure yet + int endPosition; // not sure yet - possibly for aligning REMs String functionArgument; String functionName;