Prev: 10411 Up: Map Next: 10577
10418: THE 'LOOK-VARS' SUBROUTINE
Used by the routines at SAVE_ETC, CLASS_01, CLASS_04, S_LETTER and DIM.
This subroutine is called whenever a search of the variables area or of the arguments of a DEF FN statement is required. The subroutine is entered with the system variable CH-ADD pointing to the first letter of the name of the variable whose location is being sought. The name will be in the program area or the work space. The subroutine initially builds up a discriminator byte, in the C register, that is based on the first letter of the variable's name. Bits 5 and 6 of this byte indicate the type of the variable that is being handled.
The B register is used as a bit register to hold flags.
Output
C Bits 0-4: Code of the variable's name (if executing)
C Bit 5: Set if the variable is numeric, reset if it's a string
C Bit 6: Set if the variable is simple, reset if it's an array
C Bit 7: Set if checking syntax, reset if executing
HL Address of the last letter of the variable's name (in the variables area, if found)
F Carry flag reset if the variable already exists or if checking syntax
F Zero flag reset if the variable is simple (not an array) and does not exist
LOOK_VARS 10418 SET 6,(IY+1) Presume a numeric variable (set bit 6 of FLAGS).
10422 RST 24 Get the first character into A.
10423 CALL ALPHA Is it alphabetic?
10426 JP NC,REPORT_C Give an error report if it is not so.
10429 PUSH HL Save the pointer to the first letter.
10430 AND 31 Transfer bits 0 to 4 of the letter to the C register; bits 5 and 7 are always reset.
10432 LD C,A
10433 RST 32 Get the second character into A.
10434 PUSH HL Save this pointer also.
10435 CP "(" Is the second character a '('?
10437 JR Z,V_RUN_SYN Separate arrays of numbers.
10439 SET 6,C Now set bit 6.
10441 CP "$" Is the second character a '$'?
10443 JR Z,V_STR_VAR Separate all the strings.
10445 SET 5,C Now set bit 5.
10447 CALL ALPHANUM If the variable's name has only one character then jump forward.
10450 JR NC,V_TEST_FN
Now find the end character of a name that has more than one character.
V_CHAR 10452 CALL ALPHANUM Is the character alphanumeric?
10455 JR NC,V_RUN_SYN Jump out of the loop when the end of the name is found.
10457 RES 6,C Mark the discriminator byte.
10459 RST 32 Get the next character.
10460 JR V_CHAR Go back to test it.
Simple strings and arrays of strings require that bit 6 of FLAGS is reset.
V_STR_VAR 10462 RST 32 Step CH-ADD past the '$'.
10463 RES 6,(IY+1) Reset bit 6 of FLAGS to indicate a string.
If DEFADD-hi is non-zero, indicating that a 'function' (a 'FN') is being evaluated, and if in 'run-time', a search will be made of the arguments in the DEF FN statement.
V_TEST_FN 10467 LD A,(23564) Is DEFADD-hi zero?
10470 AND A
10471 JR Z,V_RUN_SYN If so, jump forward.
10473 CALL SYNTAX_Z In 'run-time'?
10476 JP NZ,STK_F_ARG If so, jump forward to search the DEF FN statement.
This entry point is used by the routine at STK_F_ARG.
Otherwise (or if the variable was not found in the DEF FN statement) a search of variables area will be made, unless syntax is being checked.
V_RUN_SYN 10479 LD B,C Copy the discriminator byte to the B register.
10480 CALL SYNTAX_Z Jump forward if in 'run-time'.
10483 JR NZ,V_RUN
10485 LD A,C Move the discriminator to A.
10486 AND 224 Drop the character code part.
10488 SET 7,A Indicate syntax by setting bit 7.
10490 LD C,A Restore the discriminator.
10491 JR V_SYNTAX Jump forward to continue.
A BASIC line is being executed so make a search of the variables area.
V_RUN 10493 LD HL,(23627) Pick up the VARS pointer.
Now enter a loop to consider the names of the existing variables.
V_EACH 10496 LD A,(HL) The first letter of each existing variable.
10497 AND 127 Match on bits 0 to 6.
10499 JR Z,V_80_BYTE Jump when the '128-byte' is reached.
10501 CP C The actual comparison.
10502 JR NZ,V_NEXT Jump forward if the first characters do not match.
10504 RLA Rotate A leftwards and then double it to test bits 5 and 6.
10505 ADD A,A
10506 JP P,V_FOUND_2 Strings and array variables.
10509 JR C,V_FOUND_2 Simple numeric and FOR-NEXT variables.
Long names are required to be matched fully.
10511 POP DE Take a copy of the pointer to the second character.
10512 PUSH DE
10513 PUSH HL Save the first letter pointer.
V_MATCHES 10514 INC HL Consider the next character.
V_SPACES 10515 LD A,(DE) Fetch each character in turn.
10516 INC DE Point to the next character.
10517 CP " " Is the character a 'space'?
10519 JR Z,V_SPACES Ignore the spaces.
10521 OR 32 Set bit 5 so as to match lower and upper case letters.
10523 CP (HL) Make the comparison.
10524 JR Z,V_MATCHES Back for another character if it does match.
10526 OR 128 Will it match with bit 7 set?
10528 CP (HL) Try it.
10529 JR NZ,V_GET_PTR Jump forward if the 'last characters' do not match.
10531 LD A,(DE) Check that the end of the name has been reached before jumping forward.
10532 CALL ALPHANUM
10535 JR NC,V_FOUND_1
In all cases where the names fail to match the HL register pair has to be made to point to the next variable in the variables area.
V_GET_PTR 10537 POP HL Fetch the pointer.
V_NEXT 10538 PUSH BC Save B and C briefly.
10539 CALL NEXT_ONE DE is made to point to the next variable.
10542 EX DE,HL Switch the two pointers.
10543 POP BC Get B and C back.
10544 JR V_EACH Go around the loop again.
Come here if no entry was found with the correct name.
V_80_BYTE 10546 SET 7,B Signal 'variable not found'.
Come here if checking syntax.
V_SYNTAX 10548 POP DE Drop the pointer to the second character.
10549 RST 24 Fetch the present character.
10550 CP "(" Is it a '('?
10552 JR Z,V_PASS Jump forward if so.
10554 SET 5,B Indicate not dealing with an array and jump forward.
10556 JR V_END
Come here when an entry with the correct name was found.
V_FOUND_1 10558 POP DE Drop the saved variable pointer.
V_FOUND_2 10559 POP DE Drop the second character pointer.
10560 POP DE Drop the first letter pointer.
10561 PUSH HL Save the 'last' letter pointer.
10562 RST 24 Fetch the current character.
If the matching variable name has more than a single letter then the other characters must be passed over.
Note: this appears to have been done already at V_CHAR.
V_PASS 10563 CALL ALPHANUM Is it alphanumeric?
10566 JR NC,V_END Jump when the end of the name has been found.
10568 RST 32 Fetch the next character.
10569 JR V_PASS Go back and test it.
The exit-parameters are now set.
V_END 10571 POP HL HL holds the pointer to the letter of a short name or the 'last' character of a long name.
10572 RL B Rotate the whole register.
10574 BIT 6,B Specify the state of bit 6.
10576 RET Finished.
The exit-parameters for the subroutine can be summarised as follows.
The system variable CH-ADD points to the first location after the name of the variable as it occurs in the BASIC line.
When 'variable not found':
  • The carry flag is set.
  • The zero flag is set only when the search was for an array variable.
  • The HL register pair points to the first letter of the name of the variable as it occurs in the BASIC line.
When 'variable found':
  • The carry flag is reset.
  • The zero flag is set for both simple string variables and all array variables.
  • The HL register pair points to the letter of a 'short' name, or the last character of a 'long' name, of the existing entry that was found in the variables area.
In all cases bits 5 and 6 of the C register indicate the type of variable being handled. Bit 7 is the complement of the SYNTAX/RUN flag. But only when the subroutine is used in 'runtime' will bits 0 to 4 hold the code of the variable's letter.
In syntax time the return is always made with the carry flag reset. The zero flag is set for arrays and reset for all other variables, except that a simple string name incorrectly followed by a '$' sets the zero flag and, in the case of SAVE "name" DATA a$(), passes syntax as well.
Prev: 10411 Up: Map Next: 10577