This commit is contained in:
Denis Molony 2021-03-17 14:31:37 +10:00
parent 62bf355ca3
commit 9fece93d31
3 changed files with 113 additions and 112 deletions

View File

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

View File

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

View File

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