handle negative literals

This commit is contained in:
Denis Molony 2021-01-14 06:29:36 +10:00
parent 5a6c3841f9
commit 488b1cfeb2
2 changed files with 63 additions and 58 deletions

View File

@ -19,7 +19,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,]*\\)[,:]?"); Pattern.compile ("[A-Z][A-Z0-9]*[$%]?\\([0-9]+(,[0-9]+)*\\)[,:]?");
private final List<SourceLine> sourceLines = new ArrayList<> (); private final List<SourceLine> sourceLines = new ArrayList<> ();
private final int endPtr; private final int endPtr;
@ -176,8 +176,21 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (showDebugText) if (showDebugText)
return getHexText (); return getHexText ();
StringBuilder text = StringBuilder text = new StringBuilder ();
basicPreferences.formatApplesoft ? getFormatted () : originalFormat ();
if (basicPreferences.showHeader)
addHeader (text);
if (sourceLines.size () == 0)
{
text.append ("\n\nThis page intentionally left blank");
return text.toString ();
}
if (basicPreferences.formatApplesoft)
userFormat (text);
else
appleFormat (text);
if (basicPreferences.showAllXref) if (basicPreferences.showAllXref)
addXref (text); addXref (text);
@ -189,17 +202,41 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private StringBuilder getFormatted () 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
text.append ((char) b);
assert ptr == nextLine - loadAddress;
// ptr = nextLine - loadAddress;
text.append ("\n");
}
}
// ---------------------------------------------------------------------------------//
private void userFormat (StringBuilder fullText)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int indentSize = 2; int indentSize = 2;
boolean insertBlankLine = false; boolean insertBlankLine = false;
StringBuilder fullText = new StringBuilder ();
Stack<String> loopVariables = new Stack<> (); Stack<String> loopVariables = new Stack<> ();
if (basicPreferences.showHeader)
addHeader (fullText);
int alignEqualsPos = 0; int alignEqualsPos = 0;
StringBuilder text; StringBuilder text;
int baseOffset = basicPreferences.showTargets ? 12 : 8; int baseOffset = basicPreferences.showTargets ? 12 : 8;
@ -353,8 +390,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
programLoadAddress + ptr)); programLoadAddress + ptr));
fullText.append ("\n"); fullText.append ("\n");
} }
return fullText;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -622,27 +657,27 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
private List<String> splitLine (String line, int wrapLength, char breakChar) private List<String> splitLine (String line, int wrapLength, char breakChar)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int firstSpace = 0; int spaceAt = 0;
while (firstSpace < line.length () && line.charAt (firstSpace) != ' ') while (spaceAt < line.length () && line.charAt (spaceAt) != ' ')
++firstSpace; ++spaceAt;
String indent = " ".substring (0, firstSpace + 1); String indent = spaceAt < 8 ? " ".substring (0, spaceAt + 1) : " ";
List<String> lines = new ArrayList<> (); List<String> lines = new ArrayList<> ();
while (line.length () > wrapLength) while (line.length () > wrapLength)
{ {
int breakAt = wrapLength; int breakAt = wrapLength - 1;
while (breakAt > firstSpace && line.charAt (breakAt) != breakChar) while (breakAt > spaceAt && line.charAt (breakAt) != breakChar)
--breakAt; --breakAt;
if (breakAt <= firstSpace) if (breakAt <= spaceAt)
break; break;
lines.add (line.substring (0, breakAt + 1)); // keep breakChar at end lines.add (line.substring (0, breakAt + 1)); // keep breakChar at end
line = indent + line.substring (breakAt + 1); line = indent + line.substring (breakAt + 1);
} }
if (lines.size () == 0) // no breaks while (line.length () > wrapLength) // no breakChars found
while (line.length () > wrapLength)
{ {
lines.add (line.substring (0, wrapLength)); lines.add (line.substring (0, wrapLength));
line = indent + line.substring (wrapLength); line = indent + line.substring (wrapLength);
@ -949,38 +984,4 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
return (ptr <= 1) ? symbol : symbol.substring (0, 2) + symbol.substring (ptr + 1); return (ptr <= 1) ? symbol : symbol.substring (0, 2) + symbol.substring (ptr + 1);
} }
// ---------------------------------------------------------------------------------//
private StringBuilder originalFormat ()
// ---------------------------------------------------------------------------------//
{
StringBuilder text = new StringBuilder ();
if (basicPreferences.showHeader)
addHeader (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
text.append ((char) b);
assert ptr == nextLine - loadAddress;
// ptr = nextLine - loadAddress;
text.append ("\n");
}
return text;
}
} }

View File

@ -46,7 +46,7 @@ public class SubLine implements ApplesoftConstants
if (Utility.isHighBitSet (firstByte)) if (Utility.isHighBitSet (firstByte))
{ {
doToken (firstByte); doToken (firstByte);
if (is (TOKEN_REM) || is (TOKEN_DATA) || is (TOKEN_CALL)) if (is (TOKEN_REM) || is (TOKEN_DATA))
return; return;
} }
else if (Utility.isDigit (firstByte)) else if (Utility.isDigit (firstByte))
@ -63,7 +63,6 @@ public class SubLine implements ApplesoftConstants
boolean inQuote = false; boolean inQuote = false;
boolean inFunction = false; boolean inFunction = false;
boolean inDefine = false; boolean inDefine = false;
// int functionParens = 0;
int max = startPtr + length - 1; int max = startPtr + length - 1;
if (buffer[max] == 0) if (buffer[max] == 0)
@ -100,7 +99,11 @@ public class SubLine implements ApplesoftConstants
if (Utility.isPossibleVariable (b)) // A-Z 0-9 $ % if (Utility.isPossibleVariable (b)) // A-Z 0-9 $ %
{ {
if (var.isEmpty () && buffer[ptr - 2] == TOKEN_MINUS && Utility.isDigit (b))
var = "-";
var += (char) b; var += (char) b;
if ((b == Utility.ASCII_DOLLAR || b == Utility.ASCII_PERCENT) // var name end if ((b == Utility.ASCII_DOLLAR || b == Utility.ASCII_PERCENT) // var name end
&& buffer[ptr] != Utility.ASCII_LEFT_BRACKET) // not an array && buffer[ptr] != Utility.ASCII_LEFT_BRACKET) // not an array
{ {
@ -117,6 +120,7 @@ public class SubLine implements ApplesoftConstants
} }
else else
checkVar (var, b); checkVar (var, b);
var = ""; var = "";
if (b == Utility.ASCII_QUOTE) if (b == Utility.ASCII_QUOTE)