2020-12-30 03:06:50 +00:00
|
|
|
package com.bytezone.diskbrowser.applefile;
|
|
|
|
|
2021-03-09 06:23:05 +00:00
|
|
|
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_COLON;
|
|
|
|
import static com.bytezone.diskbrowser.utilities.Utility.ASCII_QUOTE;
|
2021-05-19 08:13:17 +00:00
|
|
|
import static com.bytezone.diskbrowser.utilities.Utility.getShort;
|
2021-02-21 08:03:26 +00:00
|
|
|
|
2020-12-30 03:06:50 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import com.bytezone.diskbrowser.utilities.Utility;
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------//
|
2021-01-08 07:52:47 +00:00
|
|
|
public class SourceLine implements ApplesoftConstants
|
2020-12-30 03:06:50 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-03-09 06:23:05 +00:00
|
|
|
ApplesoftBasicProgram program;
|
2021-02-21 23:24:34 +00:00
|
|
|
int linkField;
|
2020-12-30 03:06:50 +00:00
|
|
|
int lineNumber;
|
|
|
|
int linePtr;
|
|
|
|
int length;
|
|
|
|
byte[] buffer;
|
|
|
|
|
2021-01-05 08:59:36 +00:00
|
|
|
List<SubLine> sublines = new ArrayList<> ();
|
|
|
|
|
2020-12-30 03:06:50 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2021-03-09 06:23:05 +00:00
|
|
|
SourceLine (ApplesoftBasicProgram program, byte[] buffer, int ptr)
|
2020-12-30 03:06:50 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
2021-03-09 06:23:05 +00:00
|
|
|
this.program = program;
|
2020-12-30 03:06:50 +00:00
|
|
|
this.buffer = buffer;
|
2021-01-05 08:59:36 +00:00
|
|
|
|
2020-12-30 03:06:50 +00:00
|
|
|
linePtr = ptr;
|
2021-05-19 08:13:17 +00:00
|
|
|
linkField = getShort (buffer, ptr);
|
|
|
|
lineNumber = getShort (buffer, ptr + 2);
|
2020-12-30 03:06:50 +00:00
|
|
|
|
2021-03-09 06:23:05 +00:00
|
|
|
int startPtr = ptr += 4; // skip link field and lineNumber
|
2021-02-20 09:49:48 +00:00
|
|
|
boolean inString = false; // can toggle
|
|
|
|
boolean inRemark = false; // can only go false -> true
|
2020-12-30 03:06:50 +00:00
|
|
|
byte b;
|
|
|
|
|
|
|
|
while (ptr < buffer.length && (b = buffer[ptr++]) != 0)
|
|
|
|
{
|
2021-02-20 09:49:48 +00:00
|
|
|
if (inRemark) // cannot terminate a REM
|
2020-12-30 03:06:50 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (inString)
|
|
|
|
{
|
2021-03-09 06:23:05 +00:00
|
|
|
if (b == ASCII_QUOTE) // terminate string
|
2020-12-30 03:06:50 +00:00
|
|
|
inString = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (b)
|
|
|
|
{
|
|
|
|
// break IF statements into two sublines (allows for easier line indenting)
|
2021-03-09 06:23:05 +00:00
|
|
|
case TOKEN_IF:
|
2021-01-08 02:03:12 +00:00
|
|
|
while (buffer[ptr] != TOKEN_THEN && buffer[ptr] != TOKEN_GOTO
|
|
|
|
&& buffer[ptr] != 0)
|
2020-12-30 03:06:50 +00:00
|
|
|
ptr++;
|
|
|
|
|
2021-03-09 06:23:05 +00:00
|
|
|
if (buffer[ptr] == TOKEN_THEN) // keep THEN with the IF
|
2020-12-30 03:06:50 +00:00
|
|
|
++ptr;
|
|
|
|
|
2021-03-09 06:23:05 +00:00
|
|
|
startPtr = addSubLine (startPtr, ptr); // create subline from the condition
|
2020-12-30 03:06:50 +00:00
|
|
|
break;
|
|
|
|
|
2021-03-09 06:23:05 +00:00
|
|
|
case ASCII_COLON: // end of subline
|
|
|
|
startPtr = addSubLine (startPtr, ptr);
|
2020-12-30 03:06:50 +00:00
|
|
|
break;
|
|
|
|
|
2021-01-08 02:03:12 +00:00
|
|
|
case TOKEN_REM:
|
2021-03-09 06:23:05 +00:00
|
|
|
if (ptr == startPtr + 1) // at start of line
|
2021-01-15 22:10:19 +00:00
|
|
|
inRemark = true;
|
2021-03-09 06:23:05 +00:00
|
|
|
else // mid-line - should be illegal
|
|
|
|
{
|
|
|
|
System.out.printf ("%s : %5d mid-line REM token%n", program.name, lineNumber);
|
|
|
|
startPtr = addSubLine (startPtr, --ptr); // point back to the REM
|
2020-12-30 03:06:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Utility.ASCII_QUOTE:
|
|
|
|
inString = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 22:59:01 +00:00
|
|
|
length = ptr - linePtr;
|
|
|
|
|
2021-03-09 06:23:05 +00:00
|
|
|
addSubLine (startPtr, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
private int addSubLine (int startPtr, int ptr)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
sublines.add (new SubLine (this, startPtr, ptr - startPtr));
|
|
|
|
return ptr;
|
2020-12-30 03:06:50 +00:00
|
|
|
}
|
|
|
|
}
|