Applesoft symbol table

This commit is contained in:
Denis Molony 2020-12-26 18:32:40 +10:00
parent f0244793a7
commit 51a98398ca
4 changed files with 103 additions and 16 deletions

View File

@ -35,6 +35,7 @@ public class ApplesoftBasicProgram extends BasicProgram
private final int endPtr;
private final Map<Integer, List<Integer>> gotoLines = new TreeMap<> ();
private final Map<Integer, List<Integer>> gosubLines = new TreeMap<> ();
private final Map<String, List<Integer>> symbolLines = new TreeMap<> ();
private final List<Integer> stringsLine = new ArrayList<> ();
private final List<String> stringsText = new ArrayList<> ();
@ -268,21 +269,20 @@ public class ApplesoftBasicProgram extends BasicProgram
}
if (basicPreferences.showXref && !gosubLines.isEmpty ())
{
if (fullText.charAt (fullText.length () - 2) != '\n')
fullText.append ("\n");
fullText.append ("GOSUB:\n");
for (Integer line : gosubLines.keySet ())
fullText.append (String.format (" %5s %s%n", line, gosubLines.get (line)));
}
showLines (fullText, gosubLines, "GOSUB:\n");
if (basicPreferences.showXref && !gotoLines.isEmpty ())
showLines (fullText, gotoLines, "GOTO:\n");
if (basicPreferences.showSymbols && !symbolLines.isEmpty ())
{
if (fullText.charAt (fullText.length () - 2) != '\n')
fullText.append ("\n");
fullText.append ("GOTO:\n");
for (Integer line : gotoLines.keySet ())
fullText.append (String.format (" %5s %s%n", line, gotoLines.get (line)));
fullText.append ("Variables:\n");
for (String symbol : symbolLines.keySet ())
fullText.append (String.format ("%6s %s%n", symbol, symbolLines.get (symbol)));
}
if (basicPreferences.listStrings && stringsLine.size () > 0)
@ -304,6 +304,18 @@ public class ApplesoftBasicProgram extends BasicProgram
return fullText.toString ();
}
// ---------------------------------------------------------------------------------//
private void showLines (StringBuilder fullText, Map<Integer, List<Integer>> lines,
String heading)
// ---------------------------------------------------------------------------------//
{
if (fullText.charAt (fullText.length () - 2) != '\n')
fullText.append ("\n");
fullText.append (heading);
for (Integer line : lines.keySet ())
fullText.append (String.format (" %5s %s%n", line, lines.get (line)));
}
// ---------------------------------------------------------------------------------//
private List<String> splitPrint (String line)
// ---------------------------------------------------------------------------------//
@ -455,7 +467,7 @@ public class ApplesoftBasicProgram extends BasicProgram
if (subline.is (TOKEN_GOSUB))
c1 = "<<";
if (subline.is (TOKEN_GOTO))
else if (subline.is (TOKEN_GOTO))
c1 = " <";
if (gotoLines.containsKey (line.lineNumber))
c2 = "> ";
@ -740,15 +752,65 @@ public class ApplesoftBasicProgram extends BasicProgram
this.startPtr = startPtr;
this.length = length;
byte b = buffer[startPtr];
byte firstByte = buffer[startPtr];
if (isHighBitSet (b))
doToken (b);
else if (isDigit (b))
if (isHighBitSet (firstByte))
doToken (firstByte);
else if (isDigit (firstByte))
doDigit ();
else
doAlpha ();
if (is (TOKEN_REM))
return;
// System.out.println (this);
int ptr = startPtr;
length--;
String var = "";
boolean inQuote = false;
while (length-- > 0)
{
byte b = buffer[ptr++];
if (inQuote && b != ASCII_QUOTE)
continue;
if (isPossibleVariable (b))
var += (char) b;
else
{
checkVar (var, b);
var = "";
if (b == ASCII_QUOTE)
inQuote = !inQuote;
}
}
checkVar (var, (byte) 0);
}
private void checkVar (String var, byte term)
{
if (var.length () == 0)
return;
if (term == ASCII_LEFT_BRACKET)
var += "(";
if (isLetter ((byte) var.charAt (0)))
{
// System.out.printf (" Var: %s%n", var);
List<Integer> lines = symbolLines.get (var);
if (lines == null)
{
lines = new ArrayList<> ();
symbolLines.put (var, lines);
}
lines.add (parent.lineNumber);
}
}
private void doToken (byte b)

View File

@ -7,6 +7,9 @@ public abstract class BasicProgram extends AbstractFile
// -----------------------------------------------------------------------------------//
{
static final byte ASCII_QUOTE = 0x22;
static final byte ASCII_DOLLAR = 0x24;
static final byte ASCII_PERCENT = 0x25;
static final byte ASCII_LEFT_BRACKET = 0x28;
static final byte ASCII_COLON = 0x3A;
static final byte ASCII_SEMI_COLON = 0x3B;
static final byte ASCII_CARET = 0x5E;
@ -46,6 +49,19 @@ public abstract class BasicProgram extends AbstractFile
boolean isDigit (byte value)
// ---------------------------------------------------------------------------------//
{
return value >= 48 && value <= 57;
return value >= 0x30 && value <= 0x39;
}
// ---------------------------------------------------------------------------------//
boolean isLetter (byte value)
// ---------------------------------------------------------------------------------//
{
return value >= 0x41 && value <= 0x5A;
}
boolean isPossibleVariable (byte value)
{
return isDigit (value) || isLetter (value) || value == ASCII_DOLLAR
|| value == ASCII_PERCENT;
}
}

View File

@ -16,6 +16,7 @@ public class BasicPreferences
public boolean deleteExtraRemSpace = false;
public boolean deleteExtraDataSpace = false;
public boolean showXref = false;
public boolean showSymbols = false;
public boolean splitDim = false;
public int wrapPrintAt = 0;
public int wrapRemAt = 60;
@ -37,6 +38,7 @@ public class BasicPreferences
text.append (String.format ("Show caret ............... %s%n", showCaret));
text.append (String.format ("Show THEN ................ %s%n", showThen));
text.append (String.format ("Show Xref ................ %s%n", showXref));
text.append (String.format ("Show symbols ............. %s%n", showSymbols));
text.append (String.format ("List strings ............. %s%n", listStrings));
text.append (String.format ("Blank after RETURN ....... %s%n", blankAfterReturn));
text.append (String.format ("Delete extra REM space ... %s%n", deleteExtraRemSpace));

View File

@ -51,6 +51,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
private static final String PREFS_SHOW_CARET = "showCaret";
private static final String PREFS_SHOW_THEN = "showThen";
private static final String PREFS_SHOW_XREF = "showXref";
private static final String PREFS_SHOW_SYMBOLS = "showSymbols";
private static final String PREFS_LIST_STRINGS = "listStrings";
private static final String PREFS_BLANK_AFTER_RETURN = "blankAfterReturn";
private static final String PREFS_DELETE_EXTRA_REM_SPACE = "deleteExtraRemSpace";
@ -141,6 +142,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
final JMenuItem showCaretItem = new JCheckBoxMenuItem ("Show caret");
final JMenuItem showThenItem = new JCheckBoxMenuItem ("Show THEN after IF");
final JMenuItem showXrefItem = new JCheckBoxMenuItem ("Show Xref");
final JMenuItem showSymbolsItem = new JCheckBoxMenuItem ("Show variables");
final JMenuItem listStringsItem = new JCheckBoxMenuItem ("List strings");
final JMenuItem blankAfterReturn = new JCheckBoxMenuItem ("Blank line after RETURN");
final JMenuItem deleteExtraRemSpace = new JCheckBoxMenuItem ("Delete extra REM space");
@ -243,6 +245,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
applesoftMenu.add (showCaretItem);
applesoftMenu.add (showThenItem);
applesoftMenu.add (showXrefItem);
applesoftMenu.add (showSymbolsItem);
applesoftMenu.add (listStringsItem);
applesoftMenu.add (blankAfterReturn);
applesoftMenu.add (deleteExtraRemSpace);
@ -306,6 +309,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
showCaretItem.addActionListener (basicPreferencesAction);
showThenItem.addActionListener (basicPreferencesAction);
showXrefItem.addActionListener (basicPreferencesAction);
showSymbolsItem.addActionListener (basicPreferencesAction);
listStringsItem.addActionListener (basicPreferencesAction);
blankAfterReturn.addActionListener (basicPreferencesAction);
deleteExtraRemSpace.addActionListener (basicPreferencesAction);
@ -355,6 +359,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
basicPreferences.showCaret = showCaretItem.isSelected ();
basicPreferences.showThen = showThenItem.isSelected ();
basicPreferences.showXref = showXrefItem.isSelected ();
basicPreferences.showSymbols = showSymbolsItem.isSelected ();
basicPreferences.listStrings = listStringsItem.isSelected ();
basicPreferences.blankAfterReturn = blankAfterReturn.isSelected ();
basicPreferences.deleteExtraRemSpace = deleteExtraRemSpace.isSelected ();
@ -514,6 +519,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
prefs.putBoolean (PREFS_SHOW_CARET, showCaretItem.isSelected ());
prefs.putBoolean (PREFS_SHOW_THEN, showThenItem.isSelected ());
prefs.putBoolean (PREFS_SHOW_XREF, showXrefItem.isSelected ());
prefs.putBoolean (PREFS_SHOW_SYMBOLS, showSymbolsItem.isSelected ());
prefs.putBoolean (PREFS_LIST_STRINGS, listStringsItem.isSelected ());
prefs.putBoolean (PREFS_SHOW_HEADER, showHeaderItem.isSelected ());
prefs.putBoolean (PREFS_SHOW_TARGETS, showBasicTargetsItem.isSelected ());
@ -566,6 +572,7 @@ class MenuHandler implements DiskSelectionListener, FileSelectionListener, QuitL
showCaretItem.setSelected (prefs.getBoolean (PREFS_SHOW_CARET, false));
showThenItem.setSelected (prefs.getBoolean (PREFS_SHOW_THEN, true));
showXrefItem.setSelected (prefs.getBoolean (PREFS_SHOW_XREF, false));
showSymbolsItem.setSelected (prefs.getBoolean (PREFS_SHOW_SYMBOLS, false));
listStringsItem.setSelected (prefs.getBoolean (PREFS_LIST_STRINGS, false));
showHeaderItem.setSelected (prefs.getBoolean (PREFS_SHOW_HEADER, true));
showBasicTargetsItem.setSelected (prefs.getBoolean (PREFS_SHOW_TARGETS, false));