Gets a line number from a string pointer. The string pointer is passed in in HL, and the integer result is returned in DE. Leading spaces are skipped over, and it returns on finding the first non-digit. The largest possible line number is 65529 - it syntax errors out if the value of the first four digits is more then 6552.
One interesting feature of this function is that it returns with Z set if it found a valid number (or the string was empty), or NZ if the string didn't lead with a number.
|Decrement string ptr (so we're pointing at preceding character) and initialise result to 0.|
|Get next character and exit if it's not alphanumeric.|
|04A4||F5||PUSH PSW||Preserve flags|
|Syntax Error out if line number is already > 6552. This is really erroring out of the line number is >65529, since the next digit has not been counted in yet.|
|04A5||219819||LXI H,1998||Decimal 6552|
|Multiply result by 10.|
|Add this digit's value to the result and jump back.|
Gosub sets up a flow struct on the stack and then falls into Goto. The flow struct is KWID_GOSUB, preceded by the line number of the gosub statement, in turn preceded by prog ptr to just after the gosub statement.
|Check we've got at least 12 bytes of memory spare.|
|Preserve return address in BC.|
|Push prog ptr.|
|Push the current line number on the stack so we can RETURN to it later.|
|Push keyword ID of 'GOSUB' onto stack. Note we only push the single byte.|
|Push return address preserved in BC, and fall into GOTO.|
Sets program execution to continue from the line number argument.
|Get line number argument in DE and return NZ indicating syntax error if the argument was a non-number .|
|Look up the address of the program line with the provided number, put that into HL, and return if the program line was found (ie it exists).|
|Carry flag was not set by FindProgramLine so Undefined Subroutine (US) error.|
Returns program execution to the statement following the last GOSUB. Information about where to return to is kept on the stack in a flow struct (see notes).
|No arguments allowed.|
|Get pointer to flow struct on stack|
|If the first byte of the flow struct is not KWID_GOSUB (as placed there by the Gosub handler) then the gosub can't have happened so exit to a Return without Gosub (RG) error.|
|Get line number of Gosub statement from flow struct into CURRENT_LINE.|
|Set return address to ExecNext and pop the prog ptr to just after the Gosub statement into HL.|
Safe to fall into FindNextStatement, since we're already at the end of the line!...
Finds the end of the statement or the end of the program line.
BUG: There is an interesting bug in this block, although it's harmless as by luck it's impossible to see it. The byte at 04F7 is 0x10, an illegal instruction, which is in turn followed by a NOP. This illegal instruction is almost certainly supposed to be 0x0E, so as to become the two-byte instruction MOV C,00. If this were the case it would make perfect sense as the loop reads ahead until it finds a null byte terminating the line or whatever the C register is loaded with.
04F7 is jumped to in two places - it is the REM handler, and also when an IF statement's condition evals to false and the rest of the line needs to be skipped. Luckily in both these cases, C just happens to be loaded with a byte that cannot occur in the program so the null byte marking the end of the line is found as expected.
|Load C with the statement seperator character, a colon ':', and also LXI over the illegal instruction (which is a bug - see above).|