This commit is contained in:
Denis Molony 2021-02-25 15:59:41 +10:00
parent e3eb32d493
commit a20f026cf2
3 changed files with 84 additions and 77 deletions

View File

@ -49,13 +49,13 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
private final Map<String, List<String>> uniqueSymbols = new TreeMap<> (); private final Map<String, List<String>> uniqueSymbols = new TreeMap<> ();
private final Map<String, List<String>> uniqueArrays = new TreeMap<> (); private final Map<String, List<String>> uniqueArrays = new TreeMap<> ();
final List<Integer> stringsLine = new ArrayList<> (); private final List<Integer> stringsLine = new ArrayList<> ();
final List<String> stringsText = new ArrayList<> (); private final List<String> stringsText = new ArrayList<> ();
private final String formatLeft; private final String formatLeft;
private final String formatLineNumber; private final String formatLineNumber;
private final String formatRight; private final String formatRight;
final String formatCall;
private final int maxDigits; private final int maxDigits;
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -74,15 +74,15 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
ptr += line.length; // assumes lines are contiguous ptr += line.length; // assumes lines are contiguous
} }
endPtr = ptr; endPtr = ptr; // record where the end-of-program marker is
longestVarName = getLongestName (); longestVarName = getLongestName ();
maxDigits = getMaxDigits ();
// build format strings based on existing line numbers and variable names
formatLeft = longestVarName > 7 ? "%-" + longestVarName + "." + longestVarName + "s " formatLeft = longestVarName > 7 ? "%-" + longestVarName + "." + longestVarName + "s "
: "%-7.7s "; : "%-7.7s ";
formatRight = formatLeft.replace ("-", ""); formatRight = formatLeft.replace ("-", "");
formatCall = longestVarName > 7 ? "%-" + longestVarName + "s " : "%-7s ";
maxDigits = getMaxDigits ();
formatLineNumber = "%" + maxDigits + "d "; formatLineNumber = "%" + maxDigits + "d ";
} }
@ -242,11 +242,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if ((cursor) >= RIGHT_MARGIN) if ((cursor) >= RIGHT_MARGIN)
{ {
if (cursor >= 40) // already wrapped cursor = cursor >= 40 ? cursor - 40 : LEFT_MARGIN;
cursor -= 40;
else
cursor = LEFT_MARGIN;
currentLine.append ("\n ".substring (0, cursor + 1)); currentLine.append ("\n ".substring (0, cursor + 1));
} }
@ -353,16 +349,8 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
else else
text.append (lineText); text.append (lineText);
// Check for a wrappable PRINT or INPUT statement fullText.append (text);
// (see FROM MACHINE LANGUAGE TO BASIC on DOSToolkit2eB.dsk) fullText.append (NEWLINE);
if (basicPreferences.wrapPrintAt > 0 // not currently used
&& (subline.is (TOKEN_PRINT) || subline.is (TOKEN_INPUT))
&& countChars (text, Utility.ASCII_QUOTE) == 2 // just start and end quotes
&& countChars (text, Utility.ASCII_CARET) == 0) // no control characters
wrapPrint (fullText, text, lineText);
else
fullText.append (text + NEWLINE);
text.setLength (0); text.setLength (0);
// Calculate indent changes that take effect after the current subline // Calculate indent changes that take effect after the current subline
@ -461,6 +449,17 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
return (lastLine.lineNumber + "").length (); return (lastLine.lineNumber + "").length ();
} }
// ---------------------------------------------------------------------------------//
private int getLongestName ()
// ---------------------------------------------------------------------------------//
{
int longestName = getLongestName (symbolLines, 0);
longestName = getLongestName (arrayLines, longestName);
longestName = getLongestName (functionLines, longestName);
return longestName;
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private void heading (StringBuilder fullText, String format, String... heading) private void heading (StringBuilder fullText, String format, String... heading)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -614,19 +613,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
fullText.append (text + "\n"); fullText.append (text + "\n");
} }
// ---------------------------------------------------------------------------------//
private int getLongestName ()
// ---------------------------------------------------------------------------------//
{
int longestName = 0;
longestName = getLongestName (symbolLines, longestName);
longestName = getLongestName (arrayLines, longestName);
longestName = getLongestName (functionLines, longestName);
return longestName;
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private int getLongestName (Map<String, List<Integer>> map, int longestName) private int getLongestName (Map<String, List<Integer>> map, int longestName)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -660,6 +646,17 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
fullText.append (text + "\n"); fullText.append (text + "\n");
} }
// ---------------------------------------------------------------------------------//
private int countChars (StringBuilder text, byte ch)
// ---------------------------------------------------------------------------------//
{
int total = 0;
for (int i = 0; i < text.length (); i++)
if (text.charAt (i) == ch)
total++;
return total;
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private List<String> splitPrint (String line) private List<String> splitPrint (String line)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -775,30 +772,19 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
} }
// ---------------------------------------------------------------------------------//
private int countChars (StringBuilder text, byte ch)
// ---------------------------------------------------------------------------------//
{
int total = 0;
for (int i = 0; i < text.length (); i++)
if (text.charAt (i) == ch)
total++;
return total;
}
// Decide whether the current subline needs to be aligned on its equals sign. If so, // Decide whether the current subline needs to be aligned on its equals sign. If so,
// and the column hasn't been calculated, read ahead to find the highest position. // and the column hasn't been calculated, read ahead to find the highest position.
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private int alignEqualsPosition (SubLine subline, int currentAlignPosition) private int alignEqualsPosition (SubLine subline, int currentAlignPosition)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
if (subline.equalsPosition > 0) // does the line have an equals sign? if (subline.equalsPosition == 0) // if the line has no equals sign
{ return 0; // reset it
if (currentAlignPosition == 0)
currentAlignPosition = findHighest (subline); // examine following sublines if (currentAlignPosition == 0)
return currentAlignPosition; currentAlignPosition = findHighest (subline); // examine following sublines
}
return 0; // reset it return currentAlignPosition;
} }
// The IF processing is so that any assignment that is being aligned doesn't continue // The IF processing is so that any assignment that is being aligned doesn't continue
@ -810,19 +796,18 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
boolean started = false; boolean started = false;
int highestAssign = startSubline.equalsPosition; int highestAssign = startSubline.equalsPosition;
fast: for (SourceLine line : sourceLines) outerLoop: for (int i = sourceLines.indexOf (startSubline.parent); i < sourceLines
.size (); i++)
{ {
boolean inIf = false; boolean precededByIf = false;
for (SubLine subline : line.sublines) for (SubLine subline : sourceLines.get (i).sublines)
{ {
if (started) if (started)
{ {
// Stop when we come to a line without an equals sign (except for non-split REMs). // Stop when we come to a subline without an equals sign (joinable REMs
// Lines that start with a REM always break. // can be ignored)
if (subline.equalsPosition == 0 if (subline.equalsPosition == 0 && !joinableRem (subline))
// && (splitRem || !subline.is (TOKEN_REM) || subline.isFirst ())) break outerLoop;
&& (basicPreferences.splitRem || !subline.isJoinableRem ()))
break fast; // of champions
if (subline.equalsPosition > highestAssign) if (subline.equalsPosition > highestAssign)
highestAssign = subline.equalsPosition; highestAssign = subline.equalsPosition;
@ -830,14 +815,23 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
else if (subline == startSubline) else if (subline == startSubline)
started = true; started = true;
else if (subline.is (TOKEN_IF)) else if (subline.is (TOKEN_IF))
inIf = true; precededByIf = true;
} }
if (started && inIf)
break; if (started && precededByIf) // sublines of IF have now finished
break; // don't continue with following SourceLine
} }
return highestAssign; return highestAssign;
} }
// ---------------------------------------------------------------------------------//
private boolean joinableRem (SubLine subline)
// ---------------------------------------------------------------------------------//
{
return subline.isJoinableRem () && !basicPreferences.splitRem;
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private String getDebugText (StringBuilder text) private String getDebugText (StringBuilder text)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -888,7 +882,10 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
buffer.length - ptr, programLoadAddress + ptr); buffer.length - ptr, programLoadAddress + ptr);
for (String bytes : formattedHex.split (NEWLINE)) for (String bytes : formattedHex.split (NEWLINE))
text.append (String.format (" %s%n", bytes)); text.append (String.format (" %s%n", bytes));
}
if (length > 1)
{
// show the extra bytes as a disassembly // show the extra bytes as a disassembly
byte[] extraBuffer = new byte[length]; byte[] extraBuffer = new byte[length];
System.arraycopy (buffer, ptr, extraBuffer, 0, extraBuffer.length); System.arraycopy (buffer, ptr, extraBuffer, 0, extraBuffer.length);
@ -908,8 +905,10 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
{ {
if (isHighBitSet (b)) if (isHighBitSet (b))
return ApplesoftConstants.tokens[b & 0x7F]; return ApplesoftConstants.tokens[b & 0x7F];
else if (isDigit (b) || isLetter (b))
if (isDigit (b) || isLetter (b))
return ""; return "";
return "*******"; return "*******";
} }
@ -934,7 +933,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
int getLoadAddress () private int getLoadAddress ()
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
return (buffer.length > 1) ? unsignedShort (buffer, 0) - getLineLength (0) : 0; return (buffer.length > 1) ? unsignedShort (buffer, 0) - getLineLength (0) : 0;
@ -1043,7 +1042,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
void checkVar (String var, int lineNumber, Map<String, List<Integer>> map, private void checkVar (String var, int lineNumber, Map<String, List<Integer>> map,
Map<String, List<String>> unique) Map<String, List<String>> unique)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
@ -1067,7 +1066,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
void checkFunction (int sourceLine, String var) private void checkFunction (int sourceLine, String var)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
List<Integer> lines = functionLines.get (var); List<Integer> lines = functionLines.get (var);

View File

@ -457,21 +457,22 @@ public class SubLine implements ApplesoftConstants
private void setEqualsPosition () private void setEqualsPosition ()
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int p = startPtr + 1; int p = startPtr;
int max = startPtr + length; int max = startPtr + length;
while (buffer[p] != TOKEN_EQUALS && p < max) while (++p < max)
p++; if (buffer[p] == TOKEN_EQUALS)
{
if (buffer[p] == TOKEN_EQUALS) equalsPosition = toString ().indexOf ('='); // use expanded line
equalsPosition = toString ().indexOf ('='); // use expanded line break;
}
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
boolean isJoinableRem () boolean isJoinableRem ()
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
return is (TOKEN_REM) && !isFirst (); return is (TOKEN_REM) && isNotFirst ();
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -481,6 +482,13 @@ public class SubLine implements ApplesoftConstants
return (parent.linePtr + 4) == startPtr; return (parent.linePtr + 4) == startPtr;
} }
// ---------------------------------------------------------------------------------//
boolean isNotFirst ()
// ---------------------------------------------------------------------------------//
{
return !isFirst ();
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
boolean is (byte token) boolean is (byte token)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//

View File

@ -154,7 +154,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
final JMenuItem appleLineWrapItem = final JMenuItem appleLineWrapItem =
new JCheckBoxMenuItem ("Applesoft 40 column line wrap"); new JCheckBoxMenuItem ("Applesoft 40 column line wrap");
final JMenuItem splitRemarkItem = new JCheckBoxMenuItem ("Split remarks"); final JMenuItem splitRemarkItem = new JCheckBoxMenuItem ("Split REM");
final JMenuItem splitDimItem = new JCheckBoxMenuItem ("Split DIM"); final JMenuItem splitDimItem = new JCheckBoxMenuItem ("Split DIM");
final JMenuItem alignAssignItem = new JCheckBoxMenuItem ("Align consecutive assign"); final JMenuItem alignAssignItem = new JCheckBoxMenuItem ("Align consecutive assign");
// final JMenuItem showBasicTargetsItem = new JCheckBoxMenuItem ("Show targets"); // final JMenuItem showBasicTargetsItem = new JCheckBoxMenuItem ("Show targets");