This commit is contained in:
Denis Molony 2021-03-04 13:57:49 +10:00
parent a290327a3e
commit df955b4885
5 changed files with 40 additions and 46 deletions

View File

@ -1,11 +1,13 @@
package com.bytezone.diskbrowser.applefile;
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_BACKSPACE;
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_CR;
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_LF;
import static com.bytezone.diskbrowser.utilities.Utility.getIndent;
import static com.bytezone.diskbrowser.utilities.Utility.isHighBitSet;
import static com.bytezone.diskbrowser.utilities.Utility.unsignedShort;
import com.bytezone.diskbrowser.gui.BasicPreferences;
import com.bytezone.diskbrowser.utilities.Utility;
// -----------------------------------------------------------------------------------//
public class AppleBasicFormatter extends BasicFormatter
@ -73,16 +75,16 @@ public class AppleBasicFormatter extends BasicFormatter
else
switch (b)
{
case Utility.ASCII_CR:
case ASCII_CR:
currentLine.append (NEWLINE);
break;
case Utility.ASCII_BACKSPACE:
case ASCII_BACKSPACE:
if (currentLine.length () > 0)
currentLine.deleteCharAt (currentLine.length () - 1);
break;
case Utility.ASCII_LF:
case ASCII_LF:
int indent = getIndent (currentLine);
currentLine.append ("\n");
for (int i = 0; i < indent; i++)
@ -113,12 +115,12 @@ public class AppleBasicFormatter extends BasicFormatter
else
switch (b)
{
case Utility.ASCII_CR:
case ASCII_CR:
currentLine.append (NEWLINE);
cursor = 0;
break;
case Utility.ASCII_BACKSPACE:
case ASCII_BACKSPACE:
if (cursor > 0)
{
currentLine.deleteCharAt (currentLine.length () - 1);
@ -126,7 +128,7 @@ public class AppleBasicFormatter extends BasicFormatter
}
break;
case Utility.ASCII_LF:
case ASCII_LF:
currentLine.append ("\n");
for (int i = 0; i < cursor; i++)
currentLine.append (" ");

View File

@ -10,13 +10,13 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
// -----------------------------------------------------------------------------------//
{
private final List<SourceLine> sourceLines = new ArrayList<> ();
private final int endPtr;
private int ptr; // end-of-program marker
private UserBasicFormatter userBasicFormatter;
private AppleBasicFormatter appleBasicFormatter;
private DebugBasicFormatter debugBasicFormatter;
private XrefFormatter xrefFormatter;
private HeaderFormatter headerFormatter;
private final UserBasicFormatter userBasicFormatter;
private final AppleBasicFormatter appleBasicFormatter;
private final DebugBasicFormatter debugBasicFormatter;
private final XrefFormatter xrefFormatter;
private final HeaderFormatter headerFormatter;
// ---------------------------------------------------------------------------------//
public ApplesoftBasicProgram (String name, byte[] buffer)
@ -24,8 +24,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
{
super (name, buffer);
int ptr = 0;
while (buffer[ptr + 1] != 0) // msb of link field
{
SourceLine line = new SourceLine (this, buffer, ptr);
@ -33,8 +31,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
ptr += line.length; // assumes lines are contiguous
}
endPtr = ptr; // record where the end-of-program marker is
userBasicFormatter = new UserBasicFormatter (this, basicPreferences);
appleBasicFormatter = new AppleBasicFormatter (this, basicPreferences);
debugBasicFormatter = new DebugBasicFormatter (this, basicPreferences);
@ -93,6 +89,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
int getEndPtr ()
// ---------------------------------------------------------------------------------//
{
return endPtr;
return ptr;
}
}

View File

@ -35,24 +35,22 @@ public abstract class BasicFormatter implements ApplesoftConstants
int getLoadAddress ()
// ---------------------------------------------------------------------------------//
{
return (buffer.length > 1) ? unsignedShort (buffer, 0) - getLineLength (0) : 0;
return (buffer.length > 3) ? unsignedShort (buffer, 0) - getFirstLineLength () : 0;
}
// ---------------------------------------------------------------------------------//
private int getLineLength (int ptr)
private int getFirstLineLength ()
// ---------------------------------------------------------------------------------//
{
int linkField = unsignedShort (buffer, ptr);
int linkField = unsignedShort (buffer, 0);
if (linkField == 0)
return 2;
ptr += 4; // skip link field and line number
int length = 5;
int ptr = 4; // skip link field and line number
while (ptr < buffer.length && buffer[ptr++] != 0)
length++;
;
assert length == ptr;
return length;
return ptr;
}
}

View File

@ -1,5 +1,6 @@
package com.bytezone.diskbrowser.applefile;
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_COLON;
import static com.bytezone.diskbrowser.utilities.Utility.isDigit;
import static com.bytezone.diskbrowser.utilities.Utility.isHighBitSet;
import static com.bytezone.diskbrowser.utilities.Utility.isLetter;
@ -96,7 +97,7 @@ public class DebugBasicFormatter extends BasicFormatter
if (isHighBitSet (b))
return ApplesoftConstants.tokens[b & 0x7F];
if (isDigit (b) || isLetter (b))
if (isDigit (b) || isLetter (b) || b == ASCII_COLON || b == 0)
return "";
return "*******";

View File

@ -1,5 +1,7 @@
package com.bytezone.diskbrowser.applefile;
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_DOLLAR;
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_PERCENT;
import static com.bytezone.diskbrowser.utilities.Utility.getIndent;
import java.util.ArrayList;
@ -9,7 +11,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.bytezone.diskbrowser.gui.BasicPreferences;
import com.bytezone.diskbrowser.utilities.Utility;
// -----------------------------------------------------------------------------------//
public class UserBasicFormatter extends BasicFormatter
@ -17,6 +18,9 @@ public class UserBasicFormatter extends BasicFormatter
{
private static final Pattern dimPattern =
Pattern.compile ("[A-Z][A-Z0-9]*[$%]?\\([0-9]+(,[0-9]+)*\\)[,:]?");
private static final int INDENT_SIZE = 2;
private static final String EIGHT_SPACES = " ";
private static final String FOUR_SPACES = " ";
// ---------------------------------------------------------------------------------//
public UserBasicFormatter (ApplesoftBasicProgram program,
@ -31,19 +35,17 @@ public class UserBasicFormatter extends BasicFormatter
public void format (StringBuilder fullText)
// ---------------------------------------------------------------------------------//
{
int indentSize = 2;
boolean insertBlankLine = false;
int baseOffset = 7; // 5 digit line number + 2 spaces
Stack<String> loopVariables = new Stack<> ();
Alignment alignment = new Alignment ();
int baseOffset = 7; // 5 digit line number + 2 spaces
for (SourceLine line : sourceLines)
{
StringBuilder text = new StringBuilder (String.format ("%5d", (line.lineNumber)));
int indent = loopVariables.size (); // each full line starts at the loop indent
int indentLevel = loopVariables.size (); // each full line starts at the loop indent
int ifIndent = 0; // IF statement(s) limit back indentation by NEXT
for (SubLine subline : line.sublines)
@ -78,7 +80,7 @@ public class UserBasicFormatter extends BasicFormatter
if (subline.is (TOKEN_NEXT))
{
popLoopVariables (loopVariables, subline);
indent = Math.max (ifIndent, loopVariables.size ());
indentLevel = Math.max (ifIndent, loopVariables.size ());
}
// Are we joining REM lines with the previous subline?
@ -94,7 +96,7 @@ public class UserBasicFormatter extends BasicFormatter
if (basicPreferences.alignAssign)
alignEqualsPosition (subline, alignment);
int column = indent * indentSize + baseOffset;
int column = indentLevel * INDENT_SIZE + baseOffset;
while (text.length () < column)
text.append (" ");
}
@ -136,14 +138,14 @@ public class UserBasicFormatter extends BasicFormatter
// Calculate indent changes that take effect after the current subline
if (subline.is (TOKEN_IF))
ifIndent = ++indent;
ifIndent = ++indentLevel;
else if (subline.is (TOKEN_FOR))
{
String latestLoopVar = loopVariables.size () > 0 ? loopVariables.peek () : "";
if (!subline.forVariable.equals (latestLoopVar)) // don't add repeated loop
{
loopVariables.push (subline.forVariable);
++indent;
++indentLevel;
}
}
else if (basicPreferences.blankAfterReturn && subline.is (TOKEN_RETURN)
@ -166,7 +168,7 @@ public class UserBasicFormatter extends BasicFormatter
int spaceAt = 0;
while (spaceAt < line.length () && line.charAt (spaceAt) != ' ')
++spaceAt;
String indent = spaceAt < 8 ? " ".substring (0, spaceAt + 1) : " ";
String indent = spaceAt < 8 ? EIGHT_SPACES.substring (0, spaceAt + 1) : EIGHT_SPACES;
List<String> lines = new ArrayList<> ();
@ -202,7 +204,7 @@ public class UserBasicFormatter extends BasicFormatter
Matcher m = dimPattern.matcher (line);
while (m.find ())
lines.add (" " + m.group ());
lines.add (FOUR_SPACES + m.group ());
if (lines.size () > 0)
lines.set (0, "DIM " + lines.get (0).trim ());
@ -217,7 +219,6 @@ public class UserBasicFormatter extends BasicFormatter
boolean first = true;
for (String line : lines)
{
if (first)
{
first = false;
@ -226,7 +227,6 @@ public class UserBasicFormatter extends BasicFormatter
else
text.append (
"\n ".substring (0, indent) + line);
}
}
// Decide whether the current subline needs to be aligned on its equals sign. If so,
@ -279,9 +279,6 @@ public class UserBasicFormatter extends BasicFormatter
if (started && precededByIf) // sublines of IF have now finished
break; // don't continue with following SourceLine
}
// System.out.printf (" %d %d%n",
// alignment.equalsPosition, alignment.targetLength);
}
// ---------------------------------------------------------------------------------//
@ -320,8 +317,8 @@ public class UserBasicFormatter extends BasicFormatter
{
int ptr = symbol.length () - 1;
if (symbol.charAt (ptr) == Utility.ASCII_DOLLAR // string
|| symbol.charAt (ptr) == Utility.ASCII_PERCENT) // integer
if (symbol.charAt (ptr) == ASCII_DOLLAR // string
|| symbol.charAt (ptr) == ASCII_PERCENT) // integer
ptr--;
return (ptr <= 1) ? symbol : symbol.substring (0, 2) + symbol.substring (ptr + 1);
@ -335,6 +332,6 @@ public class UserBasicFormatter extends BasicFormatter
AssemblerProgram program = new AssemblerProgram ("REM assembler",
subline.getBuffer (), getLoadAddress () + subline.startPtr + 1);
return program.getAssembler ().split ("\n");
return program.getAssembler ().split (NEWLINE);
}
}