2019-08-09 23:45:49 +00:00
|
|
|
package com.bytezone.diskbrowser.applefile;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2020-12-23 10:34:18 +00:00
|
|
|
import java.util.Map;
|
2019-08-09 23:45:49 +00:00
|
|
|
import java.util.Stack;
|
2020-12-23 10:34:18 +00:00
|
|
|
import java.util.TreeMap;
|
2020-12-24 08:41:35 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2019-08-09 23:45:49 +00:00
|
|
|
|
|
|
|
import com.bytezone.diskbrowser.utilities.HexFormatter;
|
2020-06-26 03:29:46 +00:00
|
|
|
import com.bytezone.diskbrowser.utilities.Utility;
|
2019-08-09 23:45:49 +00:00
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
2021-01-08 07:52:47 +00:00
|
|
|
public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftConstants
|
2020-09-13 00:22:49 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2021-01-12 00:34:29 +00:00
|
|
|
private static final String underline =
|
|
|
|
"----------------------------------------------------"
|
|
|
|
+ "----------------------------------------------";
|
|
|
|
private static Pattern dimPattern =
|
2021-01-13 20:29:36 +00:00
|
|
|
Pattern.compile ("[A-Z][A-Z0-9]*[$%]?\\([0-9]+(,[0-9]+)*\\)[,:]?");
|
2021-01-10 06:18:48 +00:00
|
|
|
|
2019-08-09 23:45:49 +00:00
|
|
|
private final List<SourceLine> sourceLines = new ArrayList<> ();
|
|
|
|
private final int endPtr;
|
2021-01-06 01:36:41 +00:00
|
|
|
private final int longestVarName;
|
2020-12-31 09:50:53 +00:00
|
|
|
|
2021-01-05 03:24:23 +00:00
|
|
|
private final Map<Integer, List<Integer>> gotoLines = new TreeMap<> ();
|
|
|
|
private final Map<Integer, List<Integer>> gosubLines = new TreeMap<> ();
|
2021-01-13 22:47:10 +00:00
|
|
|
private final Map<Integer, List<Integer>> constantsInt = new TreeMap<> ();
|
|
|
|
private final Map<Float, List<Integer>> constantsFloat = new TreeMap<> ();
|
2021-01-05 03:24:23 +00:00
|
|
|
|
|
|
|
private final Map<String, List<Integer>> callLines = new TreeMap<> ();
|
2021-01-03 03:04:24 +00:00
|
|
|
private final Map<String, List<Integer>> symbolLines = new TreeMap<> ();
|
2021-01-04 10:13:31 +00:00
|
|
|
private final Map<String, List<Integer>> functionLines = new TreeMap<> ();
|
|
|
|
private final Map<String, List<Integer>> arrayLines = new TreeMap<> ();
|
2021-01-05 03:24:23 +00:00
|
|
|
|
2021-01-03 03:04:24 +00:00
|
|
|
private final Map<String, List<String>> uniqueSymbols = new TreeMap<> ();
|
2021-01-05 03:24:23 +00:00
|
|
|
private final Map<String, List<String>> uniqueArrays = new TreeMap<> ();
|
2020-12-31 09:50:53 +00:00
|
|
|
|
2020-12-30 03:06:50 +00:00
|
|
|
final List<Integer> stringsLine = new ArrayList<> ();
|
|
|
|
final List<String> stringsText = new ArrayList<> ();
|
2019-08-09 23:45:49 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
private final String formatLeft;
|
|
|
|
private final String formatLineNumber;
|
|
|
|
private final String formatRight;
|
|
|
|
final String formatCall;
|
|
|
|
private final int maxDigits;
|
2021-01-06 07:37:54 +00:00
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
public ApplesoftBasicProgram (String name, byte[] buffer)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
super (name, buffer);
|
|
|
|
|
|
|
|
int ptr = 0;
|
2020-12-31 09:50:53 +00:00
|
|
|
int currentAddress = 0;
|
2019-08-09 23:45:49 +00:00
|
|
|
|
2020-11-21 11:15:08 +00:00
|
|
|
int max = buffer.length - 6; // need at least 6 bytes to make a SourceLine
|
2020-11-20 09:19:51 +00:00
|
|
|
while (ptr <= max)
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2020-11-21 11:15:08 +00:00
|
|
|
int nextAddress = Utility.unsignedShort (buffer, ptr);
|
2020-12-31 09:50:53 +00:00
|
|
|
if (nextAddress <= currentAddress) // usually zero when finished
|
2019-08-09 23:45:49 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-30 03:06:50 +00:00
|
|
|
SourceLine line = new SourceLine (this, buffer, ptr);
|
2019-08-09 23:45:49 +00:00
|
|
|
sourceLines.add (line);
|
|
|
|
ptr += line.length;
|
2020-12-31 09:50:53 +00:00
|
|
|
currentAddress = nextAddress;
|
2021-01-03 03:04:24 +00:00
|
|
|
|
|
|
|
for (SubLine subline : line.sublines)
|
|
|
|
{
|
|
|
|
for (String symbol : subline.getSymbols ())
|
2021-01-05 03:24:23 +00:00
|
|
|
checkVar (symbol, line.lineNumber, symbolLines, uniqueSymbols);
|
2021-01-04 10:13:31 +00:00
|
|
|
for (String symbol : subline.getArrays ())
|
2021-01-05 03:24:23 +00:00
|
|
|
checkVar (symbol, line.lineNumber, arrayLines, uniqueArrays);
|
2021-01-04 10:13:31 +00:00
|
|
|
for (String symbol : subline.getFunctions ())
|
|
|
|
checkFunction (symbol, line.lineNumber);
|
2021-01-03 03:04:24 +00:00
|
|
|
for (int targetLine : subline.getGosubLines ())
|
2021-01-14 07:28:42 +00:00
|
|
|
addNumberInt (line.lineNumber, targetLine, gosubLines);
|
2021-01-03 03:04:24 +00:00
|
|
|
for (int targetLine : subline.getGotoLines ())
|
2021-01-14 07:28:42 +00:00
|
|
|
addNumberInt (line.lineNumber, targetLine, gotoLines);
|
2021-01-13 22:47:10 +00:00
|
|
|
for (int num : subline.getConstantsInt ())
|
|
|
|
addNumberInt (line.lineNumber, num, constantsInt);
|
|
|
|
for (float num : subline.getConstantsFloat ())
|
|
|
|
addNumberFloat (line.lineNumber, num, constantsFloat);
|
2021-01-03 05:06:51 +00:00
|
|
|
if (subline.callTarget != null)
|
2021-01-14 07:28:42 +00:00
|
|
|
addString (line.lineNumber, subline.callTarget, callLines);
|
2021-01-14 11:55:57 +00:00
|
|
|
for (String s : subline.getStringsText ())
|
|
|
|
{
|
|
|
|
stringsText.add (s);
|
|
|
|
stringsLine.add (line.lineNumber);
|
|
|
|
}
|
2021-01-03 03:04:24 +00:00
|
|
|
}
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
endPtr = ptr;
|
2021-01-06 01:36:41 +00:00
|
|
|
|
|
|
|
longestVarName = getLongestName ();
|
2021-01-06 07:37:54 +00:00
|
|
|
formatLeft = longestVarName > 7 ? "%-" + longestVarName + "." + longestVarName + "s "
|
|
|
|
: "%-7.7s ";
|
2021-01-10 06:18:48 +00:00
|
|
|
formatRight = formatLeft.replace ("-", "");
|
2021-01-12 00:34:29 +00:00
|
|
|
formatCall = longestVarName > 7 ? "%-" + longestVarName + "s " : "%-7s ";
|
2021-01-06 09:15:58 +00:00
|
|
|
|
|
|
|
maxDigits = getMaxDigits ();
|
|
|
|
formatLineNumber = "%" + maxDigits + "d ";
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 03:04:24 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-05 03:24:23 +00:00
|
|
|
void checkVar (String var, int lineNumber, Map<String, List<Integer>> map,
|
|
|
|
Map<String, List<String>> unique)
|
2021-01-04 10:13:31 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-05 03:24:23 +00:00
|
|
|
List<Integer> lines = map.get (var);
|
2021-01-04 10:13:31 +00:00
|
|
|
if (lines == null)
|
|
|
|
{
|
|
|
|
lines = new ArrayList<> ();
|
2021-01-05 03:24:23 +00:00
|
|
|
map.put (var, lines);
|
2021-01-04 10:13:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 03:04:24 +00:00
|
|
|
if (lines.size () == 0)
|
|
|
|
lines.add (lineNumber);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int lastLine = lines.get (lines.size () - 1);
|
|
|
|
if (lastLine != lineNumber)
|
|
|
|
lines.add (lineNumber);
|
|
|
|
}
|
2021-01-04 10:13:31 +00:00
|
|
|
|
2021-01-05 03:24:23 +00:00
|
|
|
checkUniqueName (var, unique);
|
2021-01-03 03:04:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 10:13:31 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
void checkFunction (String var, int lineNumber)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
List<Integer> lines = functionLines.get (var);
|
|
|
|
if (lines == null)
|
|
|
|
{
|
|
|
|
lines = new ArrayList<> ();
|
|
|
|
functionLines.put (var, lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lines.size () == 0)
|
|
|
|
lines.add (lineNumber);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int lastLine = lines.get (lines.size () - 1);
|
|
|
|
if (lastLine != lineNumber)
|
|
|
|
lines.add (lineNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 03:04:24 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-14 07:28:42 +00:00
|
|
|
private void addNumberInt (int sourceLine, Integer key, Map<Integer, List<Integer>> map)
|
2021-01-03 03:04:24 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-14 07:28:42 +00:00
|
|
|
List<Integer> lines = map.get (key);
|
2021-01-03 03:04:24 +00:00
|
|
|
if (lines == null)
|
|
|
|
{
|
|
|
|
lines = new ArrayList<> ();
|
2021-01-14 07:28:42 +00:00
|
|
|
map.put (key, lines);
|
2021-01-03 03:04:24 +00:00
|
|
|
}
|
|
|
|
lines.add (sourceLine);
|
|
|
|
}
|
|
|
|
|
2021-01-13 22:47:10 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-14 07:28:42 +00:00
|
|
|
private void addNumberFloat (int sourceLine, Float key, Map<Float, List<Integer>> map)
|
2021-01-13 22:47:10 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-14 07:28:42 +00:00
|
|
|
List<Integer> lines = map.get (key);
|
2021-01-13 22:47:10 +00:00
|
|
|
if (lines == null)
|
|
|
|
{
|
|
|
|
lines = new ArrayList<> ();
|
2021-01-14 07:28:42 +00:00
|
|
|
map.put (key, lines);
|
2021-01-13 22:47:10 +00:00
|
|
|
}
|
|
|
|
lines.add (sourceLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-14 07:28:42 +00:00
|
|
|
private void addString (int sourceLine, String key, Map<String, List<Integer>> map)
|
2021-01-13 22:47:10 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-14 07:28:42 +00:00
|
|
|
List<Integer> lines = map.get (key);
|
2021-01-13 22:47:10 +00:00
|
|
|
if (lines == null)
|
|
|
|
{
|
|
|
|
lines = new ArrayList<> ();
|
2021-01-14 07:28:42 +00:00
|
|
|
map.put (key, lines);
|
2021-01-03 05:06:51 +00:00
|
|
|
}
|
|
|
|
lines.add (sourceLine);
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
@Override
|
|
|
|
public String getText ()
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2020-12-19 05:42:25 +00:00
|
|
|
{
|
2021-01-13 20:29:36 +00:00
|
|
|
StringBuilder text = new StringBuilder ();
|
|
|
|
|
|
|
|
if (basicPreferences.showHeader)
|
|
|
|
addHeader (text);
|
|
|
|
|
2021-01-18 00:06:17 +00:00
|
|
|
if (showDebugText)
|
|
|
|
return getHexText (text);
|
|
|
|
|
2021-01-13 20:29:36 +00:00
|
|
|
if (sourceLines.size () == 0)
|
|
|
|
{
|
|
|
|
text.append ("\n\nThis page intentionally left blank");
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (basicPreferences.formatApplesoft)
|
|
|
|
userFormat (text);
|
|
|
|
else
|
|
|
|
appleFormat (text);
|
2021-01-11 02:59:01 +00:00
|
|
|
|
|
|
|
if (basicPreferences.showAllXref)
|
|
|
|
addXref (text);
|
|
|
|
|
|
|
|
while (text.length () > 0 && text.charAt (text.length () - 1) == '\n')
|
|
|
|
text.deleteCharAt (text.length () - 1);
|
|
|
|
|
|
|
|
return text.toString ();
|
2020-12-19 05:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-13 20:29:36 +00:00
|
|
|
private void appleFormat (StringBuilder text)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
int loadAddress = getLoadAddress ();
|
|
|
|
int ptr = 0;
|
|
|
|
int nextLine;
|
|
|
|
byte b;
|
|
|
|
|
|
|
|
while ((nextLine = Utility.unsignedShort (buffer, ptr)) != 0)
|
|
|
|
{
|
|
|
|
int lineNumber = Utility.unsignedShort (buffer, ptr + 2);
|
|
|
|
text.append (String.format (" %5d ", lineNumber));
|
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
while ((b = buffer[ptr++]) != 0)
|
|
|
|
if (Utility.isHighBitSet (b))
|
|
|
|
text.append (String.format (" %s ", ApplesoftConstants.tokens[b & 0x7F]));
|
|
|
|
else
|
2021-01-15 22:10:19 +00:00
|
|
|
switch (b)
|
|
|
|
{
|
|
|
|
case Utility.ASCII_CR:
|
|
|
|
text.append ("\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Utility.ASCII_BACKSPACE:
|
|
|
|
if (text.length () > 0)
|
|
|
|
text.deleteCharAt (text.length () - 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
text.append ((char) b);
|
|
|
|
}
|
2021-01-13 20:29:36 +00:00
|
|
|
|
2021-01-16 22:59:01 +00:00
|
|
|
if (ptr != (nextLine - loadAddress))
|
|
|
|
{
|
|
|
|
System.out.printf ("ptr: %04X, nextLine: %04X%n", ptr, nextLine - loadAddress);
|
|
|
|
// ptr = nextLine - loadAddress;
|
|
|
|
}
|
2021-01-13 20:29:36 +00:00
|
|
|
text.append ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private void userFormat (StringBuilder fullText)
|
2020-12-19 05:42:25 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2020-12-20 21:50:51 +00:00
|
|
|
int indentSize = 2;
|
2020-12-21 05:33:03 +00:00
|
|
|
boolean insertBlankLine = false;
|
2020-12-20 21:50:51 +00:00
|
|
|
|
2020-12-19 05:42:25 +00:00
|
|
|
Stack<String> loopVariables = new Stack<> ();
|
2021-01-03 03:04:24 +00:00
|
|
|
|
|
|
|
int alignEqualsPos = 0;
|
2019-08-09 23:45:49 +00:00
|
|
|
StringBuilder text;
|
|
|
|
int baseOffset = basicPreferences.showTargets ? 12 : 8;
|
|
|
|
|
|
|
|
for (SourceLine line : sourceLines)
|
|
|
|
{
|
|
|
|
text = new StringBuilder (getBase (line) + " ");
|
|
|
|
|
|
|
|
int indent = 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)
|
|
|
|
{
|
|
|
|
// Allow empty statements (caused by a single colon)
|
|
|
|
if (subline.isEmpty ())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// A REM statement might conceal an assembler routine
|
|
|
|
// - see P.CREATE on Diags2E.DSK
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_REM) && subline.containsToken ())
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2021-01-03 05:06:51 +00:00
|
|
|
int address = getLoadAddress () + subline.startPtr + 1; // skip the REM token
|
2019-08-09 23:45:49 +00:00
|
|
|
fullText.append (text + String.format ("REM - Inline assembler @ $%02X (%d)%n",
|
|
|
|
address, address));
|
|
|
|
String padding = " ".substring (0, text.length () + 2);
|
2021-01-03 05:06:51 +00:00
|
|
|
for (String asm : getRemAssembler (subline))
|
2019-08-09 23:45:49 +00:00
|
|
|
fullText.append (padding + asm + "\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-22 10:23:35 +00:00
|
|
|
// Beagle Bros often have multiline REM statements
|
2021-01-16 22:59:01 +00:00
|
|
|
if (subline.is (TOKEN_REM) && basicPreferences.formatRem
|
|
|
|
&& subline.containsControlChars ())
|
2020-11-22 10:23:35 +00:00
|
|
|
{
|
|
|
|
subline.addFormattedRem (text);
|
|
|
|
fullText.append (text + "\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-09 23:45:49 +00:00
|
|
|
// Reduce the indent by each NEXT, but only as far as the IF indent allows
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_NEXT))
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
popLoopVariables (loopVariables, subline);
|
|
|
|
indent = Math.max (ifIndent, loopVariables.size ());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we joining REM lines with the previous subline?
|
|
|
|
if (!basicPreferences.splitRem && subline.isJoinableRem ())
|
|
|
|
{
|
|
|
|
// Join this REM statement to the previous line, so no indenting
|
|
|
|
fullText.deleteCharAt (fullText.length () - 1); // remove newline
|
|
|
|
fullText.append (" ");
|
|
|
|
}
|
|
|
|
else // ... otherwise do all the indenting and showing of targets etc.
|
|
|
|
{
|
|
|
|
// Prepare target indicators for subsequent sublines (ie no line number)
|
|
|
|
if (basicPreferences.showTargets && !subline.isFirst ())
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_GOSUB)
|
|
|
|
|| (subline.is (TOKEN_ON) && subline.has (TOKEN_GOSUB)))
|
2019-08-09 23:45:49 +00:00
|
|
|
text.append ("<<--");
|
2021-01-08 02:03:12 +00:00
|
|
|
else if (subline.is (TOKEN_GOTO) || subline.isImpliedGoto ()
|
|
|
|
|| (subline.is (TOKEN_ON) && subline.has (TOKEN_GOTO)))
|
2019-08-09 23:45:49 +00:00
|
|
|
text.append (" <--");
|
|
|
|
|
|
|
|
// Align assign statements if required
|
|
|
|
if (basicPreferences.alignAssign)
|
2021-01-03 03:04:24 +00:00
|
|
|
alignEqualsPos = alignEqualsPosition (subline, alignEqualsPos);
|
2019-08-09 23:45:49 +00:00
|
|
|
|
2020-12-20 21:50:51 +00:00
|
|
|
int column = indent * indentSize + baseOffset;
|
2019-08-09 23:45:49 +00:00
|
|
|
while (text.length () < column)
|
|
|
|
text.append (" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the current text, then reset it
|
2021-01-03 03:04:24 +00:00
|
|
|
String lineText = subline.getAlignedText (alignEqualsPos);
|
2019-08-09 23:45:49 +00:00
|
|
|
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_REM) && basicPreferences.deleteExtraRemSpace)
|
2020-12-21 05:33:03 +00:00
|
|
|
lineText = lineText.replaceFirst ("REM ", "REM ");
|
|
|
|
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_DATA) && basicPreferences.deleteExtraDataSpace)
|
2020-12-21 06:10:07 +00:00
|
|
|
lineText = lineText.replaceFirst ("DATA ", "DATA ");
|
|
|
|
|
2021-01-03 03:04:24 +00:00
|
|
|
// Check for a wrappable REM/DATA/DIM statement
|
2019-08-09 23:45:49 +00:00
|
|
|
// (see SEA BATTLE on DISK283.DSK)
|
2020-12-25 04:30:26 +00:00
|
|
|
int inset = Math.max (text.length (), getIndent (fullText)) + 1;
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_REM) && lineText.length () > basicPreferences.wrapRemAt)
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2020-11-22 01:04:27 +00:00
|
|
|
List<String> lines = splitLine (lineText, basicPreferences.wrapRemAt, ' ');
|
2020-12-25 04:30:26 +00:00
|
|
|
addSplitLines (lines, text, inset);
|
2020-11-22 01:04:27 +00:00
|
|
|
}
|
2021-01-08 02:03:12 +00:00
|
|
|
else if (subline.is (TOKEN_DATA)
|
2020-11-22 01:04:27 +00:00
|
|
|
&& lineText.length () > basicPreferences.wrapDataAt)
|
|
|
|
{
|
|
|
|
List<String> lines = splitLine (lineText, basicPreferences.wrapDataAt, ',');
|
2020-12-25 04:30:26 +00:00
|
|
|
addSplitLines (lines, text, inset);
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
2021-01-08 02:03:12 +00:00
|
|
|
else if (subline.is (TOKEN_DIM) && basicPreferences.splitDim)
|
2020-12-24 08:41:35 +00:00
|
|
|
{
|
|
|
|
List<String> lines = splitDim (lineText);
|
2020-12-25 04:30:26 +00:00
|
|
|
addSplitLines (lines, text, inset);
|
2020-12-24 08:41:35 +00:00
|
|
|
}
|
2019-08-09 23:45:49 +00:00
|
|
|
else
|
|
|
|
text.append (lineText);
|
|
|
|
|
2021-01-01 00:49:42 +00:00
|
|
|
// Check for a wrappable PRINT or INPUT statement
|
2019-08-09 23:45:49 +00:00
|
|
|
// (see FROM MACHINE LANGUAGE TO BASIC on DOSToolkit2eB.dsk)
|
2021-01-14 07:28:42 +00:00
|
|
|
if (basicPreferences.wrapPrintAt > 0 // not currently used
|
2021-01-08 02:03:12 +00:00
|
|
|
&& (subline.is (TOKEN_PRINT) || subline.is (TOKEN_INPUT))
|
2021-01-14 07:28:42 +00:00
|
|
|
&& countChars (text, Utility.ASCII_QUOTE) == 2 // just start and end quotes
|
|
|
|
&& countChars (text, Utility.ASCII_CARET) == 0) // no control characters
|
2021-01-01 00:49:42 +00:00
|
|
|
wrapPrint (fullText, text, lineText);
|
2019-08-09 23:45:49 +00:00
|
|
|
else
|
|
|
|
fullText.append (text + "\n");
|
|
|
|
|
|
|
|
text.setLength (0);
|
|
|
|
|
|
|
|
// Calculate indent changes that take effect after the current subline
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_IF))
|
2019-08-09 23:45:49 +00:00
|
|
|
ifIndent = ++indent;
|
2021-01-08 02:03:12 +00:00
|
|
|
else if (subline.is (TOKEN_FOR))
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2021-01-03 03:04:24 +00:00
|
|
|
String latestLoopVar = loopVariables.size () > 0 ? loopVariables.peek () : "";
|
|
|
|
if (!subline.forVariable.equals (latestLoopVar)) // don't add repeated loop
|
|
|
|
{
|
|
|
|
loopVariables.push (subline.forVariable);
|
|
|
|
++indent;
|
|
|
|
}
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
2021-01-08 02:03:12 +00:00
|
|
|
else if (basicPreferences.blankAfterReturn && subline.is (TOKEN_RETURN)
|
|
|
|
&& subline.isFirst ())
|
2020-12-21 05:33:03 +00:00
|
|
|
insertBlankLine = true;
|
2020-12-20 21:50:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 05:33:03 +00:00
|
|
|
if (insertBlankLine)
|
2020-12-20 21:50:51 +00:00
|
|
|
{
|
|
|
|
fullText.append ("\n");
|
2020-12-21 05:33:03 +00:00
|
|
|
insertBlankLine = false;
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
// Reset alignment value if we just left an IF
|
|
|
|
// - the indentation will be different now
|
2019-08-09 23:45:49 +00:00
|
|
|
if (ifIndent > 0)
|
2021-01-03 03:04:24 +00:00
|
|
|
alignEqualsPos = 0;
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 02:47:21 +00:00
|
|
|
int ptr = endPtr + 2;
|
2019-10-13 05:14:47 +00:00
|
|
|
if (ptr < buffer.length - 1) // sometimes there's an extra byte on the end
|
2019-10-12 02:47:21 +00:00
|
|
|
{
|
2020-06-26 03:29:46 +00:00
|
|
|
int offset = Utility.unsignedShort (buffer, 0);
|
2019-10-12 02:47:21 +00:00
|
|
|
int programLoadAddress = offset - getLineLength (0);
|
|
|
|
fullText.append ("\nExtra data:\n\n");
|
|
|
|
fullText.append (HexFormatter.formatNoHeader (buffer, ptr, buffer.length - ptr,
|
|
|
|
programLoadAddress + ptr));
|
2020-12-24 02:03:00 +00:00
|
|
|
fullText.append ("\n");
|
2019-10-12 02:47:21 +00:00
|
|
|
}
|
2021-01-11 02:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private void addXref (StringBuilder fullText)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-14 07:28:42 +00:00
|
|
|
if (basicPreferences.showSymbols)
|
|
|
|
{
|
|
|
|
if (!symbolLines.isEmpty ())
|
|
|
|
showSymbolsLeft (fullText, symbolLines, "Var");
|
2020-12-26 08:32:40 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (!arrayLines.isEmpty ())
|
|
|
|
showSymbolsLeft (fullText, arrayLines, "Array");
|
|
|
|
}
|
2020-12-30 03:06:50 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (basicPreferences.showDuplicateSymbols)
|
|
|
|
{
|
|
|
|
if (!uniqueSymbols.isEmpty ())
|
|
|
|
showDuplicates (fullText, uniqueSymbols, "Var");
|
2021-01-05 08:59:36 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (!uniqueArrays.isEmpty ())
|
|
|
|
showDuplicates (fullText, uniqueArrays, "Array");
|
|
|
|
}
|
2021-01-05 08:59:36 +00:00
|
|
|
|
2021-01-04 10:13:31 +00:00
|
|
|
if (basicPreferences.showFunctions && !functionLines.isEmpty ())
|
2021-01-06 07:37:54 +00:00
|
|
|
showSymbolsLeft (fullText, functionLines, "Fnction");
|
2020-12-29 04:52:32 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (basicPreferences.showConstants)
|
|
|
|
{
|
|
|
|
if (!constantsInt.isEmpty ())
|
|
|
|
showSymbolsRightInt (fullText, constantsInt, "Integer");
|
2021-01-13 22:47:10 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (!constantsFloat.isEmpty ())
|
|
|
|
showSymbolsRightFloat (fullText, constantsFloat, "Float");
|
2021-01-05 03:24:23 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (stringsLine.size () > 0)
|
|
|
|
{
|
|
|
|
heading (fullText, formatRight, "Line", "String");
|
|
|
|
for (int i = 0; i < stringsLine.size (); i++)
|
|
|
|
fullText.append (String.format (formatRight + "%s%n", stringsLine.get (i),
|
|
|
|
stringsText.get (i)));
|
|
|
|
}
|
2020-12-24 02:03:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (basicPreferences.showXref)
|
|
|
|
{
|
|
|
|
if (!gosubLines.isEmpty ())
|
|
|
|
showSymbolsRight (fullText, gosubLines, "GOSUB");
|
2021-01-04 10:13:31 +00:00
|
|
|
|
2021-01-14 07:28:42 +00:00
|
|
|
if (!gotoLines.isEmpty ())
|
|
|
|
showSymbolsRight (fullText, gotoLines, "GOTO");
|
|
|
|
}
|
2021-01-04 10:13:31 +00:00
|
|
|
|
|
|
|
if (basicPreferences.showCalls && !callLines.isEmpty ())
|
2021-01-15 22:10:19 +00:00
|
|
|
showSymbolsLeftRight (fullText, callLines, " CALL");
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 08:59:36 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private int getMaxDigits ()
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-10 06:18:48 +00:00
|
|
|
if (sourceLines.size () == 0)
|
|
|
|
return 4; // anything non-zero
|
|
|
|
|
2021-01-05 08:59:36 +00:00
|
|
|
SourceLine lastLine = sourceLines.get (sourceLines.size () - 1);
|
|
|
|
return (lastLine.lineNumber + "").length ();
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:49:42 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-06 01:36:41 +00:00
|
|
|
private void heading (StringBuilder fullText, String format, String... heading)
|
2021-01-01 00:49:42 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
if (fullText.charAt (fullText.length () - 2) != '\n')
|
|
|
|
fullText.append ("\n");
|
|
|
|
|
2021-01-06 09:15:58 +00:00
|
|
|
fullText.append (String.format (format, underline));
|
|
|
|
fullText.append (underline);
|
|
|
|
fullText.append ("\n");
|
|
|
|
|
2021-01-06 01:36:41 +00:00
|
|
|
fullText.append (String.format (format, heading[0]));
|
|
|
|
if (heading.length == 1)
|
|
|
|
fullText.append ("Line numbers");
|
2021-01-01 00:49:42 +00:00
|
|
|
else
|
2021-01-06 01:36:41 +00:00
|
|
|
fullText.append (heading[1]);
|
2021-01-06 07:37:54 +00:00
|
|
|
|
2021-01-06 01:36:41 +00:00
|
|
|
fullText.append ("\n");
|
|
|
|
fullText.append (String.format (format, underline));
|
|
|
|
fullText.append (underline);
|
|
|
|
fullText.append ("\n");
|
2021-01-03 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 03:06:50 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-05 08:59:36 +00:00
|
|
|
private void showDuplicates (StringBuilder fullText, Map<String, List<String>> map,
|
|
|
|
String heading)
|
2021-01-04 10:13:31 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-05 08:59:36 +00:00
|
|
|
boolean headingShown = false;
|
|
|
|
for (String key : map.keySet ())
|
|
|
|
{
|
|
|
|
List<String> usage = map.get (key);
|
|
|
|
if (usage.size () > 1)
|
|
|
|
{
|
|
|
|
if (!headingShown)
|
|
|
|
{
|
|
|
|
headingShown = true;
|
2021-01-06 07:37:54 +00:00
|
|
|
heading (fullText, formatLeft, heading, "Duplicate Names");
|
2021-01-05 08:59:36 +00:00
|
|
|
}
|
2021-01-06 09:15:58 +00:00
|
|
|
|
2021-01-05 08:59:36 +00:00
|
|
|
String line = usage.toString ();
|
|
|
|
line = line.substring (1, line.length () - 1);
|
|
|
|
fullText.append (String.format ("%-6s %s%n", key, line));
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 10:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-06 07:37:54 +00:00
|
|
|
private void showSymbolsLeft (StringBuilder fullText, Map<String, List<Integer>> map,
|
2021-01-04 10:13:31 +00:00
|
|
|
String heading)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-06 07:37:54 +00:00
|
|
|
heading (fullText, formatLeft, heading);
|
2021-01-04 10:13:31 +00:00
|
|
|
|
2021-01-06 07:37:54 +00:00
|
|
|
for (String symbol : map.keySet ()) // left-justify strings
|
2021-01-12 00:34:29 +00:00
|
|
|
{
|
|
|
|
if (symbol.length () <= 7)
|
|
|
|
appendLineNumbers (fullText, String.format (formatLeft, symbol),
|
|
|
|
map.get (symbol));
|
|
|
|
else
|
|
|
|
appendLineNumbers (fullText, symbol + " ", map.get (symbol));
|
|
|
|
}
|
2021-01-04 10:13:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:10:19 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private void showSymbolsLeftRight (StringBuilder fullText,
|
|
|
|
Map<String, List<Integer>> map, String heading)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
heading (fullText, formatLeft, heading);
|
|
|
|
|
|
|
|
for (String symbol : map.keySet ()) // left-justify strings
|
|
|
|
{
|
|
|
|
if (isNumeric (symbol))
|
|
|
|
appendLineNumbers (fullText, String.format (formatRight, symbol),
|
|
|
|
map.get (symbol));
|
|
|
|
else if (symbol.length () <= 7)
|
|
|
|
appendLineNumbers (fullText, String.format (formatLeft, symbol),
|
|
|
|
map.get (symbol));
|
|
|
|
else
|
|
|
|
appendLineNumbers (fullText, symbol + " ", map.get (symbol));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private boolean isNumeric (String value)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
byte[] bytes = value.getBytes ();
|
|
|
|
int start = value.charAt (0) == Utility.ASCII_MINUS ? 1 : 0;
|
|
|
|
for (int i = start; i < bytes.length; i++)
|
|
|
|
if (!Utility.isPossibleNumber (bytes[i]))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:59:36 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-06 07:37:54 +00:00
|
|
|
private void showSymbolsRight (StringBuilder fullText, Map<Integer, List<Integer>> map,
|
|
|
|
String heading)
|
2021-01-05 08:59:36 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-06 07:37:54 +00:00
|
|
|
heading (fullText, formatRight, heading);
|
2021-01-05 08:59:36 +00:00
|
|
|
|
2021-01-06 07:37:54 +00:00
|
|
|
for (Integer symbol : map.keySet ()) // right-justify integers
|
2021-01-07 02:56:20 +00:00
|
|
|
appendLineNumbers (fullText, String.format (formatRight, symbol), map.get (symbol));
|
|
|
|
}
|
|
|
|
|
2021-01-13 22:47:10 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private void showSymbolsRightInt (StringBuilder fullText,
|
|
|
|
Map<Integer, List<Integer>> map, String heading)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
heading (fullText, formatRight, heading);
|
|
|
|
|
|
|
|
for (int symbol : map.keySet ()) // right-justify integers
|
|
|
|
appendLineNumbers (fullText, String.format (formatRight, symbol), map.get (symbol));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private void showSymbolsRightFloat (StringBuilder fullText,
|
|
|
|
Map<Float, List<Integer>> map, String heading)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
heading (fullText, formatRight, heading);
|
|
|
|
|
|
|
|
for (float symbol : map.keySet ()) // right-justify integers
|
|
|
|
appendLineNumbers (fullText, String.format (formatRight, symbol), map.get (symbol));
|
|
|
|
}
|
|
|
|
|
2021-01-07 02:56:20 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private void appendLineNumbers (StringBuilder fullText, String symbol,
|
|
|
|
List<Integer> lineNumbers)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
StringBuilder text = new StringBuilder ();
|
|
|
|
text.append (symbol);
|
|
|
|
|
|
|
|
for (int lineNo : lineNumbers)
|
2021-01-06 01:36:41 +00:00
|
|
|
{
|
2021-01-07 02:56:20 +00:00
|
|
|
if (text.length () > underline.length () - maxDigits + longestVarName)
|
2021-01-06 01:36:41 +00:00
|
|
|
{
|
2021-01-07 02:56:20 +00:00
|
|
|
fullText.append (text);
|
|
|
|
fullText.append ("\n");
|
|
|
|
text.setLength (0);
|
|
|
|
text.append (String.format (formatRight, ""));
|
2021-01-06 01:36:41 +00:00
|
|
|
}
|
2021-01-07 02:56:20 +00:00
|
|
|
text.append (String.format (formatLineNumber, lineNo));
|
2021-01-06 01:36:41 +00:00
|
|
|
}
|
2021-01-07 02:56:20 +00:00
|
|
|
|
|
|
|
if (text.length () > longestVarName + 3)
|
|
|
|
fullText.append (text + "\n");
|
2021-01-05 08:59:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 10:13:31 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-06 01:36:41 +00:00
|
|
|
private int getLongestName ()
|
2020-12-30 03:06:50 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
int longestName = 0;
|
2021-01-04 10:13:31 +00:00
|
|
|
|
2021-01-06 01:36:41 +00:00
|
|
|
longestName = getLongestName (symbolLines, longestName);
|
|
|
|
longestName = getLongestName (arrayLines, longestName);
|
|
|
|
longestName = getLongestName (functionLines, longestName);
|
|
|
|
|
|
|
|
return longestName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private int getLongestName (Map<String, List<Integer>> map, int longestName)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-04 10:13:31 +00:00
|
|
|
for (String symbol : map.keySet ())
|
2020-12-30 03:06:50 +00:00
|
|
|
if (symbol.length () > longestName)
|
|
|
|
longestName = symbol.length ();
|
2021-01-04 10:13:31 +00:00
|
|
|
|
2020-12-30 03:06:50 +00:00
|
|
|
return longestName;
|
|
|
|
}
|
|
|
|
|
2020-12-26 08:32:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-01 00:49:42 +00:00
|
|
|
private void wrapPrint (StringBuilder fullText, StringBuilder text, String lineText)
|
2020-12-26 08:32:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-01-01 00:49:42 +00:00
|
|
|
if (true) // new method
|
|
|
|
{
|
|
|
|
List<String> lines = splitPrint (lineText);
|
|
|
|
if (lines != null)
|
|
|
|
{
|
|
|
|
int offset = text.indexOf ("PRINT");
|
|
|
|
if (offset < 0)
|
|
|
|
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");
|
|
|
|
}
|
2021-01-14 07:28:42 +00:00
|
|
|
// else // old method
|
|
|
|
// {
|
|
|
|
// 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");
|
|
|
|
// }
|
2020-12-26 08:32:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private List<String> splitPrint (String line)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
int first = line.indexOf ("\"") + 1;
|
|
|
|
int last = line.indexOf ("\"", first + 1) - 1;
|
|
|
|
|
|
|
|
if (first != 7 || (last - first) <= basicPreferences.wrapPrintAt)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
int charsLeft = last - first + 1;
|
|
|
|
|
|
|
|
List<String> lines = new ArrayList<> ();
|
|
|
|
String padding = line.substring (0, 7);
|
|
|
|
line = line.substring (7);
|
|
|
|
String sub;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (line.length () >= basicPreferences.wrapPrintAt)
|
|
|
|
{
|
|
|
|
sub = line.substring (0, basicPreferences.wrapPrintAt);
|
|
|
|
line = line.substring (basicPreferences.wrapPrintAt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sub = line;
|
|
|
|
line = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
String subline = padding + sub;
|
|
|
|
charsLeft -= basicPreferences.wrapPrintAt;
|
|
|
|
|
|
|
|
if (charsLeft > 0)
|
|
|
|
lines.add (subline);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lines.add (subline + line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
padding = " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2020-11-22 01:04:27 +00:00
|
|
|
private List<String> splitLine (String line, int wrapLength, char breakChar)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2021-01-13 20:29:36 +00:00
|
|
|
int spaceAt = 0;
|
|
|
|
while (spaceAt < line.length () && line.charAt (spaceAt) != ' ')
|
|
|
|
++spaceAt;
|
|
|
|
String indent = spaceAt < 8 ? " ".substring (0, spaceAt + 1) : " ";
|
2020-11-22 01:04:27 +00:00
|
|
|
|
2020-12-24 08:41:35 +00:00
|
|
|
List<String> lines = new ArrayList<> ();
|
2021-01-13 20:29:36 +00:00
|
|
|
|
2020-11-22 01:04:27 +00:00
|
|
|
while (line.length () > wrapLength)
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2021-01-13 20:29:36 +00:00
|
|
|
int breakAt = wrapLength - 1;
|
|
|
|
while (breakAt > spaceAt && line.charAt (breakAt) != breakChar)
|
2021-01-12 00:34:29 +00:00
|
|
|
--breakAt;
|
|
|
|
|
2021-01-13 20:29:36 +00:00
|
|
|
if (breakAt <= spaceAt)
|
2019-08-09 23:45:49 +00:00
|
|
|
break;
|
2021-01-12 00:34:29 +00:00
|
|
|
|
|
|
|
lines.add (line.substring (0, breakAt + 1)); // keep breakChar at end
|
|
|
|
line = indent + line.substring (breakAt + 1);
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
2020-12-24 08:41:35 +00:00
|
|
|
|
2021-01-13 20:29:36 +00:00
|
|
|
while (line.length () > wrapLength) // no breakChars found
|
|
|
|
{
|
|
|
|
lines.add (line.substring (0, wrapLength));
|
|
|
|
line = indent + line.substring (wrapLength);
|
|
|
|
}
|
2021-01-12 00:34:29 +00:00
|
|
|
|
2020-12-24 08:41:35 +00:00
|
|
|
lines.add (line);
|
2020-12-31 09:50:53 +00:00
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2020-12-24 08:41:35 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private List<String> splitDim (String line)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
List<String> lines = new ArrayList<> ();
|
|
|
|
|
2021-01-12 00:34:29 +00:00
|
|
|
Matcher m = dimPattern.matcher (line);
|
2020-12-24 08:41:35 +00:00
|
|
|
|
|
|
|
while (m.find ())
|
|
|
|
lines.add (" " + m.group ());
|
|
|
|
|
|
|
|
if (lines.size () > 0)
|
|
|
|
lines.set (0, "DIM " + lines.get (0).trim ());
|
|
|
|
|
|
|
|
return lines;
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 01:04:27 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2020-12-25 04:30:26 +00:00
|
|
|
private void addSplitLines (List<String> lines, StringBuilder text, int indent)
|
2020-11-22 01:04:27 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
boolean first = true;
|
|
|
|
|
|
|
|
for (String line : lines)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
text.append (line);
|
|
|
|
}
|
|
|
|
else
|
2020-12-25 04:30:26 +00:00
|
|
|
text.append (
|
|
|
|
"\n ".substring (0, indent) + line);
|
2020-11-22 01:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 04:30:26 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private int getIndent (StringBuilder fullText)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
int ptr = fullText.length () - 1;
|
|
|
|
int indent = 0;
|
|
|
|
while (ptr >= 0 && fullText.charAt (ptr) != '\n')
|
|
|
|
{
|
|
|
|
--ptr;
|
|
|
|
++indent;
|
|
|
|
}
|
|
|
|
return indent;
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private int countChars (StringBuilder text, byte ch)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
int total = 0;
|
|
|
|
for (int i = 0; i < text.length (); i++)
|
|
|
|
if (text.charAt (i) == ch)
|
|
|
|
total++;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private String getBase (SourceLine line)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2020-12-23 10:34:18 +00:00
|
|
|
boolean isTarget = gotoLines.containsKey (line.lineNumber)
|
|
|
|
|| gosubLines.containsKey (line.lineNumber);
|
2020-11-11 09:13:07 +00:00
|
|
|
|
2019-08-09 23:45:49 +00:00
|
|
|
if (!basicPreferences.showTargets)
|
2020-11-11 09:13:07 +00:00
|
|
|
{
|
|
|
|
if (!isTarget && basicPreferences.onlyShowTargetLineNumbers)
|
|
|
|
return " ";
|
2019-08-09 23:45:49 +00:00
|
|
|
return String.format (" %5d", line.lineNumber);
|
2020-11-11 09:13:07 +00:00
|
|
|
}
|
2019-08-09 23:45:49 +00:00
|
|
|
|
|
|
|
String lineNumberText = String.format ("%5d", line.lineNumber);
|
|
|
|
SubLine subline = line.sublines.get (0);
|
|
|
|
String c1 = " ", c2 = " ";
|
2020-11-11 09:13:07 +00:00
|
|
|
|
2021-01-08 02:03:12 +00:00
|
|
|
if (subline.is (TOKEN_GOSUB) || (subline.is (TOKEN_ON) && subline.has (TOKEN_GOSUB)))
|
2019-08-09 23:45:49 +00:00
|
|
|
c1 = "<<";
|
2021-01-08 02:03:12 +00:00
|
|
|
else if (subline.is (TOKEN_GOTO)
|
|
|
|
|| (subline.is (TOKEN_ON) && subline.has (TOKEN_GOTO)))
|
2019-08-09 23:45:49 +00:00
|
|
|
c1 = " <";
|
2020-12-29 00:45:43 +00:00
|
|
|
|
2020-12-23 10:34:18 +00:00
|
|
|
if (gotoLines.containsKey (line.lineNumber))
|
2019-08-09 23:45:49 +00:00
|
|
|
c2 = "> ";
|
2020-12-23 10:34:18 +00:00
|
|
|
if (gosubLines.containsKey (line.lineNumber))
|
2019-08-09 23:45:49 +00:00
|
|
|
c2 = ">>";
|
|
|
|
if (c1.equals (" ") && !c2.equals (" "))
|
|
|
|
c1 = "--";
|
|
|
|
if (!c1.equals (" ") && c2.equals (" "))
|
|
|
|
c2 = "--";
|
2020-11-11 09:13:07 +00:00
|
|
|
|
|
|
|
if (!isTarget && basicPreferences.onlyShowTargetLineNumbers)
|
2019-08-09 23:45:49 +00:00
|
|
|
lineNumberText = "";
|
2020-11-11 09:13:07 +00:00
|
|
|
|
2019-08-09 23:45:49 +00:00
|
|
|
return String.format ("%s%s %s", c1, c2, lineNumberText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private int alignEqualsPosition (SubLine subline, int currentAlignPosition)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2021-01-03 03:04:24 +00:00
|
|
|
if (subline.equalsPosition > 0) // does the line have an equals sign?
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
if (currentAlignPosition == 0)
|
2020-11-20 09:19:51 +00:00
|
|
|
currentAlignPosition = findHighest (subline); // examine following sublines
|
2019-08-09 23:45:49 +00:00
|
|
|
return currentAlignPosition;
|
|
|
|
}
|
|
|
|
return 0; // reset it
|
|
|
|
}
|
|
|
|
|
|
|
|
// The IF processing is so that any assignment that is being aligned doesn't continue
|
|
|
|
// to the next full line (because the indentation has changed).
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private int findHighest (SubLine startSubline)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
boolean started = false;
|
2021-01-03 03:04:24 +00:00
|
|
|
int highestAssign = startSubline.equalsPosition;
|
2020-12-29 00:45:43 +00:00
|
|
|
|
2019-08-09 23:45:49 +00:00
|
|
|
fast: for (SourceLine line : sourceLines)
|
|
|
|
{
|
|
|
|
boolean inIf = false;
|
|
|
|
for (SubLine subline : line.sublines)
|
|
|
|
{
|
|
|
|
if (started)
|
|
|
|
{
|
|
|
|
// Stop when we come to a line without an equals sign (except for non-split REMs).
|
|
|
|
// Lines that start with a REM always break.
|
2021-01-03 03:04:24 +00:00
|
|
|
if (subline.equalsPosition == 0
|
2019-08-09 23:45:49 +00:00
|
|
|
// && (splitRem || !subline.is (TOKEN_REM) || subline.isFirst ()))
|
|
|
|
&& (basicPreferences.splitRem || !subline.isJoinableRem ()))
|
|
|
|
break fast; // of champions
|
|
|
|
|
2021-01-03 03:04:24 +00:00
|
|
|
if (subline.equalsPosition > highestAssign)
|
|
|
|
highestAssign = subline.equalsPosition;
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
else if (subline == startSubline)
|
|
|
|
started = true;
|
2021-01-08 02:03:12 +00:00
|
|
|
else if (subline.is (TOKEN_IF))
|
2019-08-09 23:45:49 +00:00
|
|
|
inIf = true;
|
|
|
|
}
|
|
|
|
if (started && inIf)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return highestAssign;
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-18 00:06:17 +00:00
|
|
|
private String getHexText (StringBuilder text)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
int offset = Utility.unsignedShort (buffer, 0);
|
|
|
|
int programLoadAddress = offset - getLineLength (0);
|
|
|
|
|
|
|
|
for (SourceLine sourceLine : sourceLines)
|
|
|
|
{
|
|
|
|
text.append (String.format ("%5d %s%n", sourceLine.lineNumber,
|
|
|
|
HexFormatter.formatNoHeader (buffer, sourceLine.linePtr, 4,
|
|
|
|
programLoadAddress + sourceLine.linePtr)));
|
|
|
|
for (SubLine subline : sourceLine.sublines)
|
|
|
|
{
|
|
|
|
byte b = buffer[subline.startPtr];
|
|
|
|
String token =
|
|
|
|
Utility.isHighBitSet (b) ? ApplesoftConstants.tokens[b & 0x7F] : "";
|
|
|
|
String hex = HexFormatter.formatNoHeader (buffer, subline.startPtr,
|
|
|
|
subline.length, programLoadAddress + subline.startPtr);
|
|
|
|
String[] chunks = hex.split ("\n");
|
|
|
|
for (String s : chunks)
|
|
|
|
{
|
|
|
|
text.append (String.format (" %-8s %s%n", token, s));
|
|
|
|
token = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text.append ("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endPtr < buffer.length)
|
|
|
|
{
|
|
|
|
String hex = HexFormatter.formatNoHeader (buffer, endPtr, buffer.length - endPtr,
|
|
|
|
programLoadAddress + endPtr);
|
|
|
|
String[] chunks = hex.split ("\n");
|
|
|
|
for (String s : chunks)
|
|
|
|
text.append (String.format (" %s%n", s));
|
|
|
|
}
|
|
|
|
|
|
|
|
while (text.length () > 0 && text.charAt (text.length () - 1) == '\n')
|
|
|
|
text.deleteCharAt (text.length () - 1);
|
|
|
|
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private String getHexText2 (StringBuilder text) // old version
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
if (buffer.length < 2)
|
|
|
|
return super.getHexDump ();
|
|
|
|
|
|
|
|
int ptr = 0;
|
2020-06-26 03:29:46 +00:00
|
|
|
int offset = Utility.unsignedShort (buffer, 0);
|
2019-08-09 23:45:49 +00:00
|
|
|
int programLoadAddress = offset - getLineLength (0);
|
|
|
|
|
|
|
|
while (ptr <= endPtr) // stop at the same place as the source listing
|
|
|
|
{
|
|
|
|
int length = getLineLength (ptr);
|
|
|
|
if (length == 0)
|
|
|
|
{
|
2021-01-18 00:06:17 +00:00
|
|
|
text.append (
|
2019-08-09 23:45:49 +00:00
|
|
|
HexFormatter.formatNoHeader (buffer, ptr, 2, programLoadAddress + ptr));
|
|
|
|
ptr += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr + length < buffer.length)
|
2021-01-18 00:06:17 +00:00
|
|
|
text.append (
|
2019-08-09 23:45:49 +00:00
|
|
|
HexFormatter.formatNoHeader (buffer, ptr, length, programLoadAddress + ptr)
|
|
|
|
+ "\n\n");
|
|
|
|
ptr += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr < buffer.length)
|
|
|
|
{
|
|
|
|
int length = buffer.length - ptr;
|
2021-01-18 00:06:17 +00:00
|
|
|
text.append ("\n\n");
|
|
|
|
text.append (
|
2019-08-09 23:45:49 +00:00
|
|
|
HexFormatter.formatNoHeader (buffer, ptr, length, programLoadAddress + ptr));
|
|
|
|
}
|
|
|
|
|
2021-01-18 00:06:17 +00:00
|
|
|
return text.toString ();
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 05:06:51 +00:00
|
|
|
// A REM statement might conceal an assembler routine
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private String[] getRemAssembler (SubLine subline)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
AssemblerProgram program = new AssemblerProgram ("REM assembler",
|
|
|
|
subline.getBuffer (), getLoadAddress () + subline.startPtr + 1);
|
|
|
|
|
|
|
|
return program.getAssembler ().split ("\n");
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private void addHeader (StringBuilder pgm)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
pgm.append ("Name : " + name + "\n");
|
|
|
|
pgm.append (String.format ("Length : $%04X (%<,d)%n", buffer.length));
|
|
|
|
pgm.append (String.format ("Load at : $%04X (%<,d)%n%n", getLoadAddress ()));
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2020-12-30 03:06:50 +00:00
|
|
|
int getLoadAddress ()
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
int programLoadAddress = 0;
|
|
|
|
if (buffer.length > 1)
|
|
|
|
{
|
2020-11-20 09:19:51 +00:00
|
|
|
int offset = Utility.unsignedShort (buffer, 0);
|
2019-08-09 23:45:49 +00:00
|
|
|
programLoadAddress = offset - getLineLength (0);
|
|
|
|
}
|
|
|
|
return programLoadAddress;
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private int getLineLength (int ptr)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2020-06-26 03:29:46 +00:00
|
|
|
int offset = Utility.unsignedShort (buffer, ptr);
|
2019-08-09 23:45:49 +00:00
|
|
|
if (offset == 0)
|
|
|
|
return 0;
|
2021-01-18 00:06:17 +00:00
|
|
|
|
2019-08-09 23:45:49 +00:00
|
|
|
ptr += 4; // skip offset and line number
|
|
|
|
int length = 5;
|
|
|
|
|
|
|
|
while (ptr < buffer.length && buffer[ptr++] != 0)
|
|
|
|
length++;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private void popLoopVariables (Stack<String> loopVariables, SubLine subline)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2020-12-29 00:45:43 +00:00
|
|
|
if (subline.nextVariables.length == 0) // naked NEXT
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
|
|
|
if (loopVariables.size () > 0)
|
|
|
|
loopVariables.pop ();
|
|
|
|
}
|
|
|
|
else
|
2020-12-29 00:45:43 +00:00
|
|
|
for (String variable : subline.nextVariables) // e.g. NEXT X,Y,Z
|
2019-08-09 23:45:49 +00:00
|
|
|
while (loopVariables.size () > 0)
|
|
|
|
if (sameVariable (variable, loopVariables.pop ()))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
private boolean sameVariable (String v1, String v2)
|
2020-09-13 00:22:49 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2019-08-09 23:45:49 +00:00
|
|
|
{
|
2021-01-03 03:04:24 +00:00
|
|
|
return getUniqueName (v1).equals (getUniqueName (v2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-01-05 03:24:23 +00:00
|
|
|
private void checkUniqueName (String symbol, Map<String, List<String>> map)
|
2021-01-03 03:04:24 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
String uniqueName = getUniqueName (symbol);
|
|
|
|
|
2021-01-05 03:24:23 +00:00
|
|
|
List<String> usage = map.get (uniqueName);
|
2021-01-03 03:04:24 +00:00
|
|
|
if (usage == null)
|
|
|
|
{
|
|
|
|
usage = new ArrayList<> ();
|
2021-01-05 03:24:23 +00:00
|
|
|
map.put (uniqueName, usage);
|
2021-01-03 03:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!usage.contains (symbol))
|
|
|
|
usage.add (symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private String getUniqueName (String symbol)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
int ptr = symbol.length () - 1;
|
2021-01-03 05:06:51 +00:00
|
|
|
|
2021-01-03 03:04:24 +00:00
|
|
|
if (symbol.charAt (ptr) == Utility.ASCII_DOLLAR // string
|
|
|
|
|| symbol.charAt (ptr) == Utility.ASCII_PERCENT) // integer
|
|
|
|
ptr--;
|
|
|
|
|
2021-01-03 05:06:51 +00:00
|
|
|
return (ptr <= 1) ? symbol : symbol.substring (0, 2) + symbol.substring (ptr + 1);
|
2019-08-09 23:45:49 +00:00
|
|
|
}
|
|
|
|
}
|