new Alignment class

This commit is contained in:
Denis Molony 2021-02-26 12:52:32 +10:00
parent d71075a16c
commit d4d568ac78
2 changed files with 62 additions and 50 deletions

View File

@ -257,8 +257,8 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
boolean insertBlankLine = false; boolean insertBlankLine = false;
Stack<String> loopVariables = new Stack<> (); Stack<String> loopVariables = new Stack<> ();
Alignment alignment = new Alignment ();
int alignEqualsPos = 0;
int baseOffset = 7; // 5 digit line number + 2 spaces int baseOffset = 7; // 5 digit line number + 2 spaces
for (SourceLine line : sourceLines) for (SourceLine line : sourceLines)
@ -314,7 +314,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
{ {
// Align assign statements if required // Align assign statements if required
if (basicPreferences.alignAssign) if (basicPreferences.alignAssign)
alignEqualsPos = alignEqualsPosition (subline, alignEqualsPos); alignEqualsPosition (subline, alignment);
int column = indent * indentSize + baseOffset; int column = indent * indentSize + baseOffset;
while (text.length () < column) while (text.length () < column)
@ -322,7 +322,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// Add the current text, then reset it // Add the current text, then reset it
String lineText = subline.getAlignedText (alignEqualsPos); String lineText = subline.getAlignedText (alignment);
if (subline.is (TOKEN_DATA) && basicPreferences.deleteExtraDataSpace) if (subline.is (TOKEN_DATA) && basicPreferences.deleteExtraDataSpace)
lineText = lineText.replaceFirst ("DATA ", "DATA "); lineText = lineText.replaceFirst ("DATA ", "DATA ");
@ -349,6 +349,9 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
else else
text.append (lineText); text.append (lineText);
if (subline == alignment.lastSubLine)
alignment.reset ();
fullText.append (text); fullText.append (text);
fullText.append (NEWLINE); fullText.append (NEWLINE);
text.setLength (0); text.setLength (0);
@ -375,11 +378,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
fullText.append (NEWLINE); fullText.append (NEWLINE);
insertBlankLine = false; insertBlankLine = false;
} }
// Reset alignment value if we just left an IF
// - the indentation will be different now
if (ifIndent > 0)
alignEqualsPos = 0;
} }
} }
@ -520,7 +518,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
appendLineNumbers (fullText, String.format (formatLeft, symbol), appendLineNumbers (fullText, String.format (formatLeft, symbol),
map.get (symbol)); map.get (symbol));
else else
appendLineNumbers (fullText, symbol + " ", map.get (symbol)); appendLineNumbers (fullText, symbol + " ", map.get (symbol));
} }
} }
@ -775,20 +773,17 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
// 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 void alignEqualsPosition (SubLine subline, Alignment alignment)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
if (subline.equalsPosition == 0) // if the line has no equals sign if (subline.equalsPosition == 0)
return 0; // reset it
if (currentAlignPosition == 0) // examine following sublines
{ {
Alignment alignment = new Alignment (subline); alignment.reset ();
findHighest (subline, alignment); return;
currentAlignPosition = alignment.equalsPosition;
} }
return currentAlignPosition; if (alignment.equalsPosition == 0)
findHighest (subline, alignment);
} }
// 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
@ -798,7 +793,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
boolean started = false; boolean started = false;
// int highestAssign = startSubline.equalsPosition; alignment.setFirst (startSubline);
outerLoop: for (int i = sourceLines.indexOf (startSubline.parent); i < sourceLines outerLoop: for (int i = sourceLines.indexOf (startSubline.parent); i < sourceLines
.size (); i++) .size (); i++)
@ -813,8 +808,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (subline.equalsPosition == 0 && !joinableRem (subline)) if (subline.equalsPosition == 0 && !joinableRem (subline))
break outerLoop; break outerLoop;
// if (subline.equalsPosition > highestAssign)
// highestAssign = subline.equalsPosition;
if (subline.equalsPosition > 0) if (subline.equalsPosition > 0)
alignment.check (subline); alignment.check (subline);
} }
@ -828,10 +821,8 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
break; // don't continue with following SourceLine break; // don't continue with following SourceLine
} }
System.out.printf (" %d %d%n", // System.out.printf (" %d %d%n",
alignment.equalsPosition, alignment.targetLength); // alignment.equalsPosition, alignment.targetLength);
// return highestAssign;
// return alignment;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -1145,27 +1136,40 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private class Alignment class Alignment
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int equalsPosition; int equalsPosition;
int targetLength; int targetLength;
SubLine firstSubLine;
SubLine lastSubLine;
Alignment (SubLine subline) void reset ()
{ {
firstSubLine = null;
equalsPosition = 0;
targetLength = 0;
}
void setFirst (SubLine subline)
{
reset ();
firstSubLine = subline;
check (subline); check (subline);
} }
void check (SubLine subline) void check (SubLine subline)
{ {
System.out.printf ("%-20s %d %d%n", subline, subline.equalsPosition, // System.out.printf ("%-20s %d %d%n", subline, subline.equalsPosition,
subline.endPosition - subline.equalsPosition); // subline.endPosition - subline.equalsPosition);
if (equalsPosition < subline.equalsPosition) if (equalsPosition < subline.equalsPosition)
equalsPosition = subline.equalsPosition; equalsPosition = subline.equalsPosition;
int temp = subline.endPosition - subline.equalsPosition; int temp = subline.endPosition - subline.equalsPosition;
if (targetLength < temp) if (targetLength < temp)
targetLength = temp; targetLength = temp;
lastSubLine = subline;
} }
} }
} }

View File

@ -22,6 +22,7 @@ import static com.bytezone.diskbrowser.utilities.Utility.isPossibleVariable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.bytezone.diskbrowser.applefile.ApplesoftBasicProgram.Alignment;
import com.bytezone.diskbrowser.utilities.HexFormatter;; import com.bytezone.diskbrowser.utilities.HexFormatter;;
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
@ -37,7 +38,7 @@ public class SubLine implements ApplesoftConstants
String[] nextVariables; String[] nextVariables;
String forVariable = ""; String forVariable = "";
int equalsPosition; // used for aligning the equals sign int equalsPosition; // used for aligning the equals sign
int endPosition; // not sure yet int endPosition; // not sure yet
String functionArgument; String functionArgument;
@ -82,13 +83,12 @@ public class SubLine implements ApplesoftConstants
} }
else else
{ {
// ptr = startPtr;
if (isDigit (firstByte)) // split IF xx THEN nnn if (isDigit (firstByte)) // split IF xx THEN nnn
{ {
addXref (getLineNumber (buffer, startPtr), gotoLines); addXref (getLineNumber (buffer, startPtr), gotoLines);
return; return;
} }
else if (isLetter (firstByte)) // variable assignment else if (isLetter (firstByte)) // variable name
setEqualsPosition (); setEqualsPosition ();
else if (isEndOfLine (firstByte)) // empty subline else if (isEndOfLine (firstByte)) // empty subline
return; return;
@ -455,24 +455,24 @@ public class SubLine implements ApplesoftConstants
} }
// Record the position of the equals sign so it can be aligned with adjacent lines. // Record the position of the equals sign so it can be aligned with adjacent lines.
// Illegal lines could have a variable name with no equals sign.
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private void setEqualsPosition () private void setEqualsPosition ()
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
// int p = startPtr; int p = startPtr;
// int max = startPtr + length; int max = startPtr + length;
// while (++p < max) while (++p < max)
// if (buffer[p] == TOKEN_EQUALS) if (buffer[p] == TOKEN_EQUALS)
// { {
String expandedLine = toString (); String expandedLine = toString ();
equalsPosition = expandedLine.indexOf ('='); equalsPosition = expandedLine.indexOf ('=');
endPosition = expandedLine.length (); endPosition = expandedLine.length ();
if (expandedLine.endsWith (":")) if (expandedLine.endsWith (":"))
endPosition--; endPosition--;
// break; break;
// } }
assert equalsPosition > 0;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -614,16 +614,24 @@ public class SubLine implements ApplesoftConstants
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
public String getAlignedText (int alignEqualsPos) public String getAlignedText (Alignment alignment)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
StringBuilder line = toStringBuilder (); // get line StringBuilder line = toStringBuilder (); // get line
// insert spaces before '=' until it lines up with the other assignment lines if (alignment.equalsPosition == 0 || is (TOKEN_REM))
return line.toString ();
if (!is (TOKEN_REM)) int alignEqualsPos = alignment.equalsPosition;
while (alignEqualsPos-- > equalsPosition) int targetLength = endPosition - equalsPosition;
line.insert (equalsPosition, ' ');
// insert spaces before '=' until it lines up with the other assignment lines
while (alignEqualsPos-- > equalsPosition)
line.insert (equalsPosition, ' ');
if (line.charAt (line.length () - 1) == ':')
while (targetLength++ <= alignment.targetLength)
line.append (" ");
return line.toString (); return line.toString ();
} }