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;
Stack<String> loopVariables = new Stack<> ();
Alignment alignment = new Alignment ();
int alignEqualsPos = 0;
int baseOffset = 7; // 5 digit line number + 2 spaces
for (SourceLine line : sourceLines)
@ -314,7 +314,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
{
// Align assign statements if required
if (basicPreferences.alignAssign)
alignEqualsPos = alignEqualsPosition (subline, alignEqualsPos);
alignEqualsPosition (subline, alignment);
int column = indent * indentSize + baseOffset;
while (text.length () < column)
@ -322,7 +322,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
}
// Add the current text, then reset it
String lineText = subline.getAlignedText (alignEqualsPos);
String lineText = subline.getAlignedText (alignment);
if (subline.is (TOKEN_DATA) && basicPreferences.deleteExtraDataSpace)
lineText = lineText.replaceFirst ("DATA ", "DATA ");
@ -349,6 +349,9 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
else
text.append (lineText);
if (subline == alignment.lastSubLine)
alignment.reset ();
fullText.append (text);
fullText.append (NEWLINE);
text.setLength (0);
@ -375,11 +378,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
fullText.append (NEWLINE);
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),
map.get (symbol));
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,
// 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
return 0; // reset it
if (currentAlignPosition == 0) // examine following sublines
if (subline.equalsPosition == 0)
{
Alignment alignment = new Alignment (subline);
findHighest (subline, alignment);
currentAlignPosition = alignment.equalsPosition;
alignment.reset ();
return;
}
return currentAlignPosition;
if (alignment.equalsPosition == 0)
findHighest (subline, alignment);
}
// 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;
// int highestAssign = startSubline.equalsPosition;
alignment.setFirst (startSubline);
outerLoop: for (int i = sourceLines.indexOf (startSubline.parent); i < sourceLines
.size (); i++)
@ -813,8 +808,6 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (subline.equalsPosition == 0 && !joinableRem (subline))
break outerLoop;
// if (subline.equalsPosition > highestAssign)
// highestAssign = subline.equalsPosition;
if (subline.equalsPosition > 0)
alignment.check (subline);
}
@ -828,10 +821,8 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
break; // don't continue with following SourceLine
}
System.out.printf (" %d %d%n",
alignment.equalsPosition, alignment.targetLength);
// return highestAssign;
// return alignment;
// System.out.printf (" %d %d%n",
// alignment.equalsPosition, alignment.targetLength);
}
// ---------------------------------------------------------------------------------//
@ -1145,27 +1136,40 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
}
// ---------------------------------------------------------------------------------//
private class Alignment
class Alignment
// ---------------------------------------------------------------------------------//
{
int equalsPosition;
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);
}
void check (SubLine subline)
{
System.out.printf ("%-20s %d %d%n", subline, subline.equalsPosition,
subline.endPosition - subline.equalsPosition);
// System.out.printf ("%-20s %d %d%n", subline, subline.equalsPosition,
// subline.endPosition - subline.equalsPosition);
if (equalsPosition < subline.equalsPosition)
equalsPosition = subline.equalsPosition;
int temp = subline.endPosition - subline.equalsPosition;
if (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.List;
import com.bytezone.diskbrowser.applefile.ApplesoftBasicProgram.Alignment;
import com.bytezone.diskbrowser.utilities.HexFormatter;;
// -----------------------------------------------------------------------------------//
@ -37,7 +38,7 @@ public class SubLine implements ApplesoftConstants
String[] nextVariables;
String forVariable = "";
int equalsPosition; // used for aligning the equals sign
int equalsPosition; // used for aligning the equals sign
int endPosition; // not sure yet
String functionArgument;
@ -82,13 +83,12 @@ public class SubLine implements ApplesoftConstants
}
else
{
// ptr = startPtr;
if (isDigit (firstByte)) // split IF xx THEN nnn
{
addXref (getLineNumber (buffer, startPtr), gotoLines);
return;
}
else if (isLetter (firstByte)) // variable assignment
else if (isLetter (firstByte)) // variable name
setEqualsPosition ();
else if (isEndOfLine (firstByte)) // empty subline
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.
// Illegal lines could have a variable name with no equals sign.
// ---------------------------------------------------------------------------------//
private void setEqualsPosition ()
// ---------------------------------------------------------------------------------//
{
// int p = startPtr;
// int max = startPtr + length;
int p = startPtr;
int max = startPtr + length;
// while (++p < max)
// if (buffer[p] == TOKEN_EQUALS)
// {
String expandedLine = toString ();
equalsPosition = expandedLine.indexOf ('=');
endPosition = expandedLine.length ();
if (expandedLine.endsWith (":"))
endPosition--;
// break;
// }
assert equalsPosition > 0;
while (++p < max)
if (buffer[p] == TOKEN_EQUALS)
{
String expandedLine = toString ();
equalsPosition = expandedLine.indexOf ('=');
endPosition = expandedLine.length ();
if (expandedLine.endsWith (":"))
endPosition--;
break;
}
}
// ---------------------------------------------------------------------------------//
@ -614,16 +614,24 @@ public class SubLine implements ApplesoftConstants
}
// ---------------------------------------------------------------------------------//
public String getAlignedText (int alignEqualsPos)
public String getAlignedText (Alignment alignment)
// ---------------------------------------------------------------------------------//
{
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))
while (alignEqualsPos-- > equalsPosition)
line.insert (equalsPosition, ' ');
int alignEqualsPos = alignment.equalsPosition;
int targetLength = endPosition - 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 ();
}