import static

This commit is contained in:
Denis Molony 2021-01-08 12:03:12 +10:00
parent ce0cabc8e2
commit dbeedff1a2
4 changed files with 94 additions and 77 deletions

View File

@ -1,5 +1,18 @@
package com.bytezone.diskbrowser.applefile;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_DATA;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_DIM;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_FOR;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_GOSUB;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_GOTO;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_IF;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_INPUT;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_NEXT;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_ON;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_PRINT;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_REM;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_RETURN;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -202,7 +215,7 @@ public class ApplesoftBasicProgram extends BasicProgram
// A REM statement might conceal an assembler routine
// - see P.CREATE on Diags2E.DSK
if (subline.is (ApplesoftConstants.TOKEN_REM) && subline.containsToken ())
if (subline.is (TOKEN_REM) && subline.containsToken ())
{
int address = getLoadAddress () + subline.startPtr + 1; // skip the REM token
fullText.append (text + String.format ("REM - Inline assembler @ $%02X (%d)%n",
@ -214,7 +227,7 @@ public class ApplesoftBasicProgram extends BasicProgram
}
// Beagle Bros often have multiline REM statements
if (subline.is (ApplesoftConstants.TOKEN_REM) && subline.containsControlChars ())
if (subline.is (TOKEN_REM) && subline.containsControlChars ())
{
subline.addFormattedRem (text);
fullText.append (text + "\n");
@ -222,7 +235,7 @@ public class ApplesoftBasicProgram extends BasicProgram
}
// Reduce the indent by each NEXT, but only as far as the IF indent allows
if (subline.is (ApplesoftConstants.TOKEN_NEXT))
if (subline.is (TOKEN_NEXT))
{
popLoopVariables (loopVariables, subline);
indent = Math.max (ifIndent, loopVariables.size ());
@ -239,13 +252,11 @@ public class ApplesoftBasicProgram extends BasicProgram
{
// Prepare target indicators for subsequent sublines (ie no line number)
if (basicPreferences.showTargets && !subline.isFirst ())
if (subline.is (ApplesoftConstants.TOKEN_GOSUB)
|| (subline.is (ApplesoftConstants.TOKEN_ON)
&& subline.has (ApplesoftConstants.TOKEN_GOSUB)))
if (subline.is (TOKEN_GOSUB)
|| (subline.is (TOKEN_ON) && subline.has (TOKEN_GOSUB)))
text.append ("<<--");
else if (subline.is (ApplesoftConstants.TOKEN_GOTO)
|| subline.isImpliedGoto () || (subline.is (ApplesoftConstants.TOKEN_ON)
&& subline.has (ApplesoftConstants.TOKEN_GOTO)))
else if (subline.is (TOKEN_GOTO) || subline.isImpliedGoto ()
|| (subline.is (TOKEN_ON) && subline.has (TOKEN_GOTO)))
text.append (" <--");
// Align assign statements if required
@ -260,30 +271,27 @@ public class ApplesoftBasicProgram extends BasicProgram
// Add the current text, then reset it
String lineText = subline.getAlignedText (alignEqualsPos);
if (subline.is (ApplesoftConstants.TOKEN_REM)
&& basicPreferences.deleteExtraRemSpace)
if (subline.is (TOKEN_REM) && basicPreferences.deleteExtraRemSpace)
lineText = lineText.replaceFirst ("REM ", "REM ");
if (subline.is (ApplesoftConstants.TOKEN_DATA)
&& basicPreferences.deleteExtraDataSpace)
if (subline.is (TOKEN_DATA) && basicPreferences.deleteExtraDataSpace)
lineText = lineText.replaceFirst ("DATA ", "DATA ");
// Check for a wrappable REM/DATA/DIM statement
// (see SEA BATTLE on DISK283.DSK)
int inset = Math.max (text.length (), getIndent (fullText)) + 1;
if (subline.is (ApplesoftConstants.TOKEN_REM)
&& lineText.length () > basicPreferences.wrapRemAt)
if (subline.is (TOKEN_REM) && lineText.length () > basicPreferences.wrapRemAt)
{
List<String> lines = splitLine (lineText, basicPreferences.wrapRemAt, ' ');
addSplitLines (lines, text, inset);
}
else if (subline.is (ApplesoftConstants.TOKEN_DATA)
else if (subline.is (TOKEN_DATA)
&& lineText.length () > basicPreferences.wrapDataAt)
{
List<String> lines = splitLine (lineText, basicPreferences.wrapDataAt, ',');
addSplitLines (lines, text, inset);
}
else if (subline.is (ApplesoftConstants.TOKEN_DIM) && basicPreferences.splitDim)
else if (subline.is (TOKEN_DIM) && basicPreferences.splitDim)
{
List<String> lines = splitDim (lineText);
addSplitLines (lines, text, inset);
@ -294,8 +302,7 @@ public class ApplesoftBasicProgram extends BasicProgram
// Check for a wrappable PRINT or INPUT statement
// (see FROM MACHINE LANGUAGE TO BASIC on DOSToolkit2eB.dsk)
if (basicPreferences.wrapPrintAt > 0
&& (subline.is (ApplesoftConstants.TOKEN_PRINT)
|| subline.is (ApplesoftConstants.TOKEN_INPUT))
&& (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);
@ -305,9 +312,9 @@ public class ApplesoftBasicProgram extends BasicProgram
text.setLength (0);
// Calculate indent changes that take effect after the current subline
if (subline.is (ApplesoftConstants.TOKEN_IF))
if (subline.is (TOKEN_IF))
ifIndent = ++indent;
else if (subline.is (ApplesoftConstants.TOKEN_FOR))
else if (subline.is (TOKEN_FOR))
{
String latestLoopVar = loopVariables.size () > 0 ? loopVariables.peek () : "";
if (!subline.forVariable.equals (latestLoopVar)) // don't add repeated loop
@ -316,8 +323,8 @@ public class ApplesoftBasicProgram extends BasicProgram
++indent;
}
}
else if (basicPreferences.blankAfterReturn
&& subline.is (ApplesoftConstants.TOKEN_RETURN) && subline.isFirst ())
else if (basicPreferences.blankAfterReturn && subline.is (TOKEN_RETURN)
&& subline.isFirst ())
insertBlankLine = true;
}
@ -701,13 +708,10 @@ public class ApplesoftBasicProgram extends BasicProgram
SubLine subline = line.sublines.get (0);
String c1 = " ", c2 = " ";
if (subline.is (ApplesoftConstants.TOKEN_GOSUB)
|| (subline.is (ApplesoftConstants.TOKEN_ON)
&& subline.has (ApplesoftConstants.TOKEN_GOSUB)))
if (subline.is (TOKEN_GOSUB) || (subline.is (TOKEN_ON) && subline.has (TOKEN_GOSUB)))
c1 = "<<";
else if (subline.is (ApplesoftConstants.TOKEN_GOTO)
|| (subline.is (ApplesoftConstants.TOKEN_ON)
&& subline.has (ApplesoftConstants.TOKEN_GOTO)))
else if (subline.is (TOKEN_GOTO)
|| (subline.is (TOKEN_ON) && subline.has (TOKEN_GOTO)))
c1 = " <";
if (gotoLines.containsKey (line.lineNumber))
@ -768,7 +772,7 @@ public class ApplesoftBasicProgram extends BasicProgram
}
else if (subline == startSubline)
started = true;
else if (subline.is (ApplesoftConstants.TOKEN_IF))
else if (subline.is (TOKEN_IF))
inIf = true;
}
if (started && inIf)

View File

@ -16,7 +16,7 @@ public interface ApplesoftConstants
"COLOR=", "POP ", "VTAB ", "HIMEM:", // 0xA0
"LOMEM:", "ONERR ", "RESUME", "RECALL ", // 0xA4 - 0xA7
"STORE ", "SPEED=", "LET ", "GOTO ", // 0xA8
"RUN ", "IF ", "RESTORE ", "&", // 0xAC
"RUN ", "IF ", "RESTORE ", "& ", // 0xAC
"GOSUB ", "RETURN ", "REM ", "STOP ", // 0xB0
"ON ", "WAIT ", "LOAD ", "SAVE ", // 0xB4
"DEF", "POKE ", "PRINT ", "CONT", // 0xB8

View File

@ -1,5 +1,9 @@
package com.bytezone.diskbrowser.applefile;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_GOTO;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_REM;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_THEN;
import java.util.ArrayList;
import java.util.List;
@ -55,12 +59,12 @@ public class SourceLine
// break IF statements into two sublines (allows for easier line indenting)
case ApplesoftConstants.TOKEN_IF:
// skip to THEN or GOTO - if not found then it's an error
while (buffer[ptr] != ApplesoftConstants.TOKEN_THEN
&& buffer[ptr] != ApplesoftConstants.TOKEN_GOTO && buffer[ptr] != 0)
while (buffer[ptr] != TOKEN_THEN && buffer[ptr] != TOKEN_GOTO
&& buffer[ptr] != 0)
ptr++;
// keep THEN with the IF
if (buffer[ptr] == ApplesoftConstants.TOKEN_THEN)
if (buffer[ptr] == TOKEN_THEN)
++ptr;
// create subline from the condition (and THEN if it exists)
@ -75,7 +79,7 @@ public class SourceLine
startPtr = ptr;
break;
case ApplesoftConstants.TOKEN_REM:
case TOKEN_REM:
if (ptr != startPtr + 1) // REM appears mid-line (should follow a colon)
{
System.out.println ("mid-line REM token");

View File

@ -1,9 +1,25 @@
package com.bytezone.diskbrowser.applefile;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_CALL;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_DATA;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_DEF;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_EQUALS;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_FN;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_FOR;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_GOSUB;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_GOTO;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_LET;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_MINUS;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_NEXT;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_ON;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_ONERR;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_REM;
import static com.bytezone.diskbrowser.applefile.ApplesoftConstants.TOKEN_THEN;
import java.util.ArrayList;
import java.util.List;
import com.bytezone.diskbrowser.utilities.Utility;
import com.bytezone.diskbrowser.utilities.Utility;;
// -----------------------------------------------------------------------------------//
public class SubLine
@ -13,13 +29,16 @@ public class SubLine
int startPtr;
int length;
String[] nextVariables;
String callTarget;
String forVariable = "";
int equalsPosition; // used for aligning the equals sign
String functionArgument;
String functionName;
boolean isDefine = false;
String callTarget;
byte[] buffer;
private final List<Integer> gotoLines = new ArrayList<> ();
@ -43,9 +62,7 @@ public class SubLine
if (Utility.isHighBitSet (firstByte))
{
doToken (firstByte);
if (is (ApplesoftConstants.TOKEN_REM) || is (ApplesoftConstants.TOKEN_DATA)
|| is (ApplesoftConstants.TOKEN_AMPERSAND)
|| is (ApplesoftConstants.TOKEN_CALL))
if (is (TOKEN_REM) || is (TOKEN_DATA) || is (TOKEN_CALL))
return;
}
else if (Utility.isDigit (firstByte))
@ -72,16 +89,15 @@ public class SubLine
{
byte b = buffer[ptr++];
if (b == ApplesoftConstants.TOKEN_DEF)
if (b == TOKEN_DEF)
{
inDefine = true;
isDefine = true;
continue;
}
if (inDefine) // ignore the name and argument
{
if (b == ApplesoftConstants.TOKEN_EQUALS)
if (b == TOKEN_EQUALS)
inDefine = false;
continue;
@ -96,7 +112,7 @@ public class SubLine
continue;
}
if (b == ApplesoftConstants.TOKEN_FN)
if (b == TOKEN_FN)
{
inFunction = true;
continue;
@ -139,8 +155,7 @@ public class SubLine
if (!Utility.isLetter ((byte) var.charAt (0)))
{
if (is (ApplesoftConstants.TOKEN_GOTO) || is (ApplesoftConstants.TOKEN_GOSUB)
|| is (ApplesoftConstants.TOKEN_ON))
if (is (TOKEN_GOTO) || is (TOKEN_GOSUB) || is (TOKEN_ON))
return;
int varInt = Integer.parseInt (var);
@ -149,7 +164,7 @@ public class SubLine
return;
}
if (isDefine && (var.equals (functionName) || var.equals (functionArgument)))
if (is (TOKEN_DEF) && (var.equals (functionName) || var.equals (functionArgument)))
return;
if (terminator == Utility.ASCII_LEFT_BRACKET)
@ -211,13 +226,13 @@ public class SubLine
{
switch (b)
{
case ApplesoftConstants.TOKEN_FOR:
case TOKEN_FOR:
int p = startPtr + 1;
while (buffer[p] != ApplesoftConstants.TOKEN_EQUALS)
while (buffer[p] != TOKEN_EQUALS)
forVariable += (char) buffer[p++];
break;
case ApplesoftConstants.TOKEN_NEXT:
case TOKEN_NEXT:
if (length == 2) // no variables
nextVariables = new String[0];
else
@ -227,21 +242,21 @@ public class SubLine
}
break;
case ApplesoftConstants.TOKEN_LET:
case TOKEN_LET:
recordEqualsPosition ();
break;
case ApplesoftConstants.TOKEN_GOTO:
case TOKEN_GOTO:
int targetLine = getLineNumber (buffer, startPtr + 1);
addXref (targetLine, gotoLines);
break;
case ApplesoftConstants.TOKEN_GOSUB:
case TOKEN_GOSUB:
targetLine = getLineNumber (buffer, startPtr + 1);
addXref (targetLine, gosubLines);
break;
case ApplesoftConstants.TOKEN_ON:
case TOKEN_ON:
p = startPtr + 1;
int max = startPtr + length - 1;
while (p < max && buffer[p] != ApplesoftConstants.TOKEN_GOTO
@ -250,12 +265,12 @@ public class SubLine
switch (buffer[p++])
{
case ApplesoftConstants.TOKEN_GOSUB:
case TOKEN_GOSUB:
for (int destLine : getLineNumbers (buffer, p))
addXref (destLine, gosubLines);
break;
case ApplesoftConstants.TOKEN_GOTO:
case TOKEN_GOTO:
for (int destLine : getLineNumbers (buffer, p))
addXref (destLine, gotoLines);
break;
@ -265,7 +280,7 @@ public class SubLine
}
break;
case ApplesoftConstants.TOKEN_ONERR:
case TOKEN_ONERR:
if (buffer[startPtr + 1] == ApplesoftConstants.TOKEN_GOTO)
{
targetLine = getLineNumber (buffer, startPtr + 2);
@ -273,18 +288,18 @@ public class SubLine
}
break;
case ApplesoftConstants.TOKEN_CALL:
case TOKEN_CALL:
byte[] lineBuffer = getBuffer ();
if (lineBuffer[0] == ApplesoftConstants.TOKEN_MINUS)
if (lineBuffer[0] == TOKEN_MINUS)
callTarget = "-" + new String (lineBuffer, 1, lineBuffer.length - 1);
else
callTarget = new String (lineBuffer, 0, lineBuffer.length);
break;
case ApplesoftConstants.TOKEN_DEF:
case TOKEN_DEF:
lineBuffer = getBuffer ();
assert lineBuffer[0] == ApplesoftConstants.TOKEN_FN;
assert lineBuffer[0] == TOKEN_FN;
int leftBracket = getPosition (lineBuffer, 1, Utility.ASCII_LEFT_BRACKET);
int rightBracket =
@ -364,13 +379,10 @@ public class SubLine
// ---------------------------------------------------------------------------------//
{
int lineNumber = 0;
while (ptr < buffer.length)
{
int b = (buffer[ptr++] & 0xFF) - 0x30;
if (b < 0 || b > 9)
break;
lineNumber = lineNumber * 10 + b;
}
while (ptr < buffer.length && Utility.isDigit (buffer[ptr]))
lineNumber = lineNumber * 10 + (buffer[ptr++] & 0xFF) - 0x30;
return lineNumber;
}
@ -391,9 +403,9 @@ public class SubLine
{
int p = startPtr + 1;
int max = startPtr + length;
while (buffer[p] != ApplesoftConstants.TOKEN_EQUALS && p < max)
while (buffer[p] != TOKEN_EQUALS && p < max)
p++;
if (buffer[p] == ApplesoftConstants.TOKEN_EQUALS)
if (buffer[p] == TOKEN_EQUALS)
equalsPosition = toString ().indexOf ('='); // use expanded line
}
@ -401,7 +413,7 @@ public class SubLine
boolean isJoinableRem ()
// ---------------------------------------------------------------------------------//
{
return is (ApplesoftConstants.TOKEN_REM) && !isFirst ();
return is (TOKEN_REM) && !isFirst ();
}
// ---------------------------------------------------------------------------------//
@ -504,7 +516,7 @@ public class SubLine
StringBuilder line = toStringBuilder (); // get line
// insert spaces before '=' until it lines up with the other assignment lines
if (!is (ApplesoftConstants.TOKEN_REM))
if (!is (TOKEN_REM))
while (alignEqualsPos-- > equalsPosition)
line.insert (equalsPosition, ' ');
@ -515,9 +527,7 @@ public class SubLine
public byte[] getBuffer ()
// ---------------------------------------------------------------------------------//
{
// System.out.println (HexFormatter.format (buffer, startPtr, length));
int len = length - 1;
// System.out.printf ("%02X%n", buffer[startPtr + len]);
if (buffer[startPtr + len] == Utility.ASCII_COLON || buffer[startPtr + len] == 0)
len--;
byte[] buffer2 = new byte[len];
@ -557,8 +567,7 @@ public class SubLine
int val = b & 0x7F;
if (val < ApplesoftConstants.tokens.length)
{
if (b != ApplesoftConstants.TOKEN_THEN
|| ApplesoftBasicProgram.basicPreferences.showThen)
if (b != TOKEN_THEN || ApplesoftBasicProgram.basicPreferences.showThen)
line.append (ApplesoftConstants.tokens[val]);
}
}