This commit is contained in:
Denis Molony 2021-01-19 05:47:01 +10:00
parent 4791366b1e
commit 135a91c985
4 changed files with 69 additions and 117 deletions

View File

@ -20,6 +20,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
+ "----------------------------------------------"; + "----------------------------------------------";
private static Pattern dimPattern = private static Pattern dimPattern =
Pattern.compile ("[A-Z][A-Z0-9]*[$%]?\\([0-9]+(,[0-9]+)*\\)[,:]?"); Pattern.compile ("[A-Z][A-Z0-9]*[$%]?\\([0-9]+(,[0-9]+)*\\)[,:]?");
private static String NEWLINE = "\n";
private final List<SourceLine> sourceLines = new ArrayList<> (); private final List<SourceLine> sourceLines = new ArrayList<> ();
private final int endPtr; private final int endPtr;
@ -200,7 +201,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
addHeader (text); addHeader (text);
if (showDebugText) if (showDebugText)
return getHexText (text); return getDebugText (text);
if (sourceLines.size () == 0) if (sourceLines.size () == 0)
{ {
@ -216,10 +217,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (basicPreferences.showAllXref) if (basicPreferences.showAllXref)
addXref (text); addXref (text);
while (text.length () > 0 && text.charAt (text.length () - 1) == '\n') return Utility.rtrim (text);
text.deleteCharAt (text.length () - 1);
return text.toString ();
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -244,7 +242,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
switch (b) switch (b)
{ {
case Utility.ASCII_CR: case Utility.ASCII_CR:
text.append ("\n"); text.append (NEWLINE);
break; break;
case Utility.ASCII_BACKSPACE: case Utility.ASCII_BACKSPACE:
@ -261,7 +259,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
System.out.printf ("ptr: %04X, nextLine: %04X%n", ptr, nextLine - loadAddress); System.out.printf ("ptr: %04X, nextLine: %04X%n", ptr, nextLine - loadAddress);
// ptr = nextLine - loadAddress; // ptr = nextLine - loadAddress;
} }
text.append ("\n"); text.append (NEWLINE);
} }
} }
@ -300,7 +298,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
address, address)); address, address));
String padding = " ".substring (0, text.length () + 2); String padding = " ".substring (0, text.length () + 2);
for (String asm : getRemAssembler (subline)) for (String asm : getRemAssembler (subline))
fullText.append (padding + asm + "\n"); fullText.append (padding + asm + NEWLINE);
continue; continue;
} }
@ -309,7 +307,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
&& subline.containsControlChars ()) && subline.containsControlChars ())
{ {
subline.addFormattedRem (text); subline.addFormattedRem (text);
fullText.append (text + "\n"); fullText.append (text + NEWLINE);
continue; continue;
} }
@ -386,7 +384,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
&& countChars (text, Utility.ASCII_CARET) == 0) // no control characters && countChars (text, Utility.ASCII_CARET) == 0) // no control characters
wrapPrint (fullText, text, lineText); wrapPrint (fullText, text, lineText);
else else
fullText.append (text + "\n"); fullText.append (text + NEWLINE);
text.setLength (0); text.setLength (0);
@ -409,7 +407,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (insertBlankLine) if (insertBlankLine)
{ {
fullText.append ("\n"); fullText.append (NEWLINE);
insertBlankLine = false; insertBlankLine = false;
} }
@ -419,15 +417,18 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
alignEqualsPos = 0; alignEqualsPos = 0;
} }
int ptr = endPtr + 2; if (false)
if (ptr < buffer.length - 1) // sometimes there's an extra byte on the end
{ {
int offset = Utility.unsignedShort (buffer, 0); int ptr = endPtr + 2;
int programLoadAddress = offset - getLineLength (0); if (ptr < buffer.length - 1) // sometimes there's an extra byte on the end
fullText.append ("\nExtra data:\n\n"); {
fullText.append (HexFormatter.formatNoHeader (buffer, ptr, buffer.length - ptr, int offset = Utility.unsignedShort (buffer, 0);
programLoadAddress + ptr)); int programLoadAddress = offset - getLineLength (0);
fullText.append ("\n"); fullText.append ("\nExtra data:\n\n");
fullText.append (HexFormatter.formatNoHeader (buffer, ptr, buffer.length - ptr,
programLoadAddress + ptr));
fullText.append (NEWLINE);
}
} }
} }
@ -502,11 +503,11 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
if (fullText.charAt (fullText.length () - 2) != '\n') if (fullText.charAt (fullText.length () - 2) != '\n')
fullText.append ("\n"); fullText.append (NEWLINE);
fullText.append (String.format (format, underline)); fullText.append (String.format (format, underline));
fullText.append (underline); fullText.append (underline);
fullText.append ("\n"); fullText.append (NEWLINE);
fullText.append (String.format (format, heading[0])); fullText.append (String.format (format, heading[0]));
if (heading.length == 1) if (heading.length == 1)
@ -514,10 +515,10 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
else else
fullText.append (heading[1]); fullText.append (heading[1]);
fullText.append ("\n"); fullText.append (NEWLINE);
fullText.append (String.format (format, underline)); fullText.append (String.format (format, underline));
fullText.append (underline); fullText.append (underline);
fullText.append ("\n"); fullText.append (NEWLINE);
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -639,7 +640,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (text.length () > underline.length () - maxDigits + longestVarName) if (text.length () > underline.length () - maxDigits + longestVarName)
{ {
fullText.append (text); fullText.append (text);
fullText.append ("\n"); fullText.append (NEWLINE);
text.setLength (0); text.setLength (0);
text.append (String.format (formatRight, "")); text.append (String.format (formatRight, ""));
} }
@ -678,42 +679,22 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
private void wrapPrint (StringBuilder fullText, StringBuilder text, String lineText) private void wrapPrint (StringBuilder fullText, StringBuilder text, String lineText)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
if (true) // new method List<String> lines = splitPrint (lineText);
if (lines != null)
{ {
List<String> lines = splitPrint (lineText); int offset = text.indexOf ("PRINT");
if (lines != null) if (offset < 0)
offset = text.indexOf ("INPUT");
String fmt = "%-" + offset + "." + offset + "s%s%n";
String padding = text.substring (0, offset);
for (String s : lines)
{ {
int offset = text.indexOf ("PRINT"); fullText.append (String.format (fmt, padding, s));
if (offset < 0) padding = "";
offset = text.indexOf ("INPUT");
String fmt = "%-" + offset + "." + offset + "s%s%n";
String padding = text.substring (0, offset);
for (String s : lines)
{
fullText.append (String.format (fmt, padding, s));
padding = "";
}
} }
else
fullText.append (text + "\n");
} }
// else // old method else
// { fullText.append (text + "\n");
// int first = text.indexOf ("\"") + 1;
// int last = text.indexOf ("\"", first + 1) - 1;
// if ((last - first) > basicPreferences.wrapPrintAt)
// {
// int ptr = first + basicPreferences.wrapPrintAt;
// do
// {
// fullText.append (text.substring (0, ptr)
// + "\n ".substring (0, first + 1));
// text.delete (0, ptr);
// ptr = basicPreferences.wrapPrintAt;
// } while (text.length () > basicPreferences.wrapPrintAt);
// }
// fullText.append (text + "\n");
// }
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -948,7 +929,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private String getHexText (StringBuilder text) private String getDebugText (StringBuilder text)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int offset = Utility.unsignedShort (buffer, 0); int offset = Utility.unsignedShort (buffer, 0);
@ -961,74 +942,39 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
programLoadAddress + sourceLine.linePtr))); programLoadAddress + sourceLine.linePtr)));
for (SubLine subline : sourceLine.sublines) for (SubLine subline : sourceLine.sublines)
{ {
byte b = buffer[subline.startPtr]; String token = getDisplayToken (buffer[subline.startPtr]);
String token = String formattedHex = HexFormatter.formatNoHeader (buffer, subline.startPtr,
Utility.isHighBitSet (b) ? ApplesoftConstants.tokens[b & 0x7F] : "";
String hex = HexFormatter.formatNoHeader (buffer, subline.startPtr,
subline.length, programLoadAddress + subline.startPtr); subline.length, programLoadAddress + subline.startPtr);
String[] chunks = hex.split ("\n");
for (String s : chunks) for (String bytes : formattedHex.split (NEWLINE))
{ {
text.append (String.format (" %-8s %s%n", token, s)); text.append (String.format (" %-8s %s%n", token, bytes));
token = ""; token = "";
} }
} }
text.append ("\n"); text.append (NEWLINE);
} }
if (endPtr < buffer.length) if (endPtr < buffer.length)
{ {
String hex = HexFormatter.formatNoHeader (buffer, endPtr, buffer.length - endPtr, String formattedHex = HexFormatter.formatNoHeader (buffer, endPtr,
programLoadAddress + endPtr); buffer.length - endPtr, programLoadAddress + endPtr);
String[] chunks = hex.split ("\n"); for (String bytes : formattedHex.split (NEWLINE))
for (String s : chunks) text.append (String.format (" %s%n", bytes));
text.append (String.format (" %s%n", s));
} }
while (text.length () > 0 && text.charAt (text.length () - 1) == '\n') return Utility.rtrim (text);
text.deleteCharAt (text.length () - 1);
return text.toString ();
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private String getHexText2 (StringBuilder text) // old version private String getDisplayToken (byte b)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
if (buffer.length < 2) if (Utility.isHighBitSet (b))
return super.getHexDump (); return ApplesoftConstants.tokens[b & 0x7F];
else if (Utility.isDigit (b) || Utility.isLetter (b))
int ptr = 0; return "";
int offset = Utility.unsignedShort (buffer, 0); return "*******";
int programLoadAddress = offset - getLineLength (0);
while (ptr <= endPtr) // stop at the same place as the source listing
{
int length = getLineLength (ptr);
if (length == 0)
{
text.append (
HexFormatter.formatNoHeader (buffer, ptr, 2, programLoadAddress + ptr));
ptr += 2;
break;
}
if (ptr + length < buffer.length)
text.append (
HexFormatter.formatNoHeader (buffer, ptr, length, programLoadAddress + ptr)
+ "\n\n");
ptr += length;
}
if (ptr < buffer.length)
{
int length = buffer.length - ptr;
text.append ("\n\n");
text.append (
HexFormatter.formatNoHeader (buffer, ptr, length, programLoadAddress + ptr));
}
return text.toString ();
} }
// A REM statement might conceal an assembler routine // A REM statement might conceal an assembler routine

View File

@ -94,8 +94,5 @@ public class SourceLine implements ApplesoftConstants
// if no colon was found this is the entire line // if no colon was found this is the entire line
int bytesLeft = ptr - startPtr; int bytesLeft = ptr - startPtr;
sublines.add (new SubLine (this, startPtr, bytesLeft)); sublines.add (new SubLine (this, startPtr, bytesLeft));
// if (lineNumber == 1022)
// System.out.println (HexFormatter.format (buffer, linePtr, length));
} }
} }

View File

@ -69,7 +69,7 @@ public class SubLine implements ApplesoftConstants
recordEqualsPosition (); recordEqualsPosition ();
else if (firstByte == Utility.ASCII_COLON || firstByte == 0) // empty subline else if (firstByte == Utility.ASCII_COLON || firstByte == 0) // empty subline
return; return;
else // probably Beagle Bros 0D... else // probably Beagle Bros 0D or 0A
System.out.printf ("Unexpected bytes at %5d: %s%n", parent.lineNumber, System.out.printf ("Unexpected bytes at %5d: %s%n", parent.lineNumber,
HexFormatter.formatNoHeader (buffer, startPtr, length).substring (5)); HexFormatter.formatNoHeader (buffer, startPtr, length).substring (5));
} }
@ -356,15 +356,15 @@ public class SubLine implements ApplesoftConstants
if (chunk.isEmpty ()) if (chunk.isEmpty ())
continue; continue;
b = (byte) chunk.charAt (0); b = (byte) chunk.charAt (0);
if (Utility.isDigit (b) || b == Utility.ASCII_MINUS || b == Utility.ASCII_DOT) if (Utility.isPossibleNumber (b) || b == Utility.ASCII_MINUS)
{ {
if (!addNumber (chunk)) if (!addNumber (chunk))
stringsText.add (chunk); stringsText.add (chunk);
} }
else if (Utility.isLetter (b) || b == Utility.ASCII_QUOTE)
stringsText.add (chunk);
else else
System.out.printf ("Unknown data: [%s]%n", chunk); stringsText.add (chunk);
// else
// System.out.printf ("Unknown data: [%s]%n", chunk);
} }
break; break;

View File

@ -50,6 +50,15 @@ public class Utility
System.out.println (ste); System.out.println (ste);
} }
// ---------------------------------------------------------------------------------//
public static String rtrim (StringBuilder text)
// ---------------------------------------------------------------------------------//
{
while (text.length () > 0 && text.charAt (text.length () - 1) == '\n')
text.deleteCharAt (text.length () - 1);
return text.toString ();
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
public static int getLong (byte[] buffer, int ptr) public static int getLong (byte[] buffer, int ptr)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//