Prev: 6604 Up: Map Next: 6745
6669: Parser: Match Input Word
Split the command into words and look each one up in the vocabulary.
Clear the four-letter buffer to spaces so a short word cannot match the previous one's tail.
Parser_MatchInputWord 6669 LD DE,$629E DE=FourLetterBuffer.
666C LD B,$04 Set a counter in B for the 04 letters.
666E LD A,$20 A=20.
Parser_MatchInputWord_Loop1 6670 LD (DE),A Write a space character (20) to the buffer.
6671 INC DE Move to the next letter.
6672 DJNZ Parser_MatchInputWord_Loop1 Decrease the counter by one and loop back to Parser_MatchInputWord_Loop1 until the counter is zero.
6674 XOR A Jump to Handler_UnknownWord when no token slots remain.
6675 OR C
6676 JP Z,Handler_UnknownWord Jump to Handler_UnknownWord when the command has ended.
6679 LD A,(HL) Read the next character of the command.
667A CP $0D Jump to Handler_UnknownWord when ENTER ends the command.
667C JP Z,Handler_UnknownWord
Copy the next word, up to 04 characters, stopping at a space, a comma or ENTER.
667F LD B,$04 Keep at most four letters of this word.
6681 LD DE,$629E Point at the word buffer again.
Parser_MatchInputWord_Loop2 6684 LD A,(HL) Jump to Parser_MatchInputWord_Step1 when the character is ENTER.
6685 CP $0D
6687 JR Z,Parser_MatchInputWord_Step1
6689 CP $20 Jump to Parser_MatchInputWord_Loop4 when the character is a space or a comma.
668B JR Z,Parser_MatchInputWord_Loop4
668D CP $2C
668F JR Z,Parser_MatchInputWord_Loop4
6691 LD (DE),A Copy the character into the word buffer and advance both pointers.
6692 INC HL
6693 INC DE
6694 DJNZ Parser_MatchInputWord_Loop2
A longer word is skipped up to the next delimiter.
Parser_MatchInputWord_Loop3 6696 LD A,(HL) Jump to Parser_MatchInputWord_Step1 on ENTER.
6697 CP $0D
6699 JR Z,Parser_MatchInputWord_Step1
669B CP $20 Jump to Parser_MatchInputWord_Loop4 on a space or a comma.
669D JR Z,Parser_MatchInputWord_Loop4
669F CP $2C
66A1 JR Z,Parser_MatchInputWord_Loop4
66A3 INC HL Skip this character and keep looking for a delimiter.
66A4 JR Parser_MatchInputWord_Loop3
Parser_MatchInputWord_Loop4 66A6 INC HL Skip consecutive spaces.
66A7 LD A,(HL)
66A8 CP $20
66AA JR Z,Parser_MatchInputWord_Loop4
66AC CP $2C Jump back to Parser_MatchInputWord_Loop4 when that character is a comma (2C).
66AE JR Z,Parser_MatchInputWord_Loop4
Ignore the word "THE ".
Parser_MatchInputWord_Step1 66B0 EXX Compare the word with "THE " without losing the command pointer.
66B1 LD HL,$62E7
66B4 LD DE,$629E
66B7 LD B,$04
Parser_MatchInputWord_Loop5 66B9 LD A,(DE) Match the four letters of "THE ".
66BA CP (HL)
66BB JR NZ,Parser_MatchInputWord_Step2
66BD INC DE
66BE INC HL
66BF DJNZ Parser_MatchInputWord_Loop5
Parser_MatchInputWord_Step2 66C1 EXX Jump back to Parser_MatchInputWord when the word was THE, so it is not tokenised.
66C2 JR Z,Parser_MatchInputWord
66C4 DEC C
Look the word up in the vocabulary. A comma separates synonyms of the same token; FF ends the table.
66C5 EXX Switch to the alternate register set for the vocabulary search.
66C6 LD HL,($629C) HL=*Pointer_Vocabulary.
66C9 LD C,$00 Set the token number in C to 00.
Parser_MatchInputWord_Loop6 66CB LD A,(HL) Fetch the next vocabulary byte into A.
66CC CP $FF Jump to Parser_MatchInputWord_Step5 if the word is the FF terminator.
66CE JR Z,Parser_MatchInputWord_Step5
Parser_MatchInputWord_Loop7 66D0 LD DE,$629E DE=FourLetterBuffer.
66D3 LD B,$04 Set a counter in B for the 04 letters.
Parser_MatchInputWord_Loop8 66D5 LD A,(DE) Jump to Parser_MatchInputWord_Step3 if this letter does not match.
66D6 CP (HL)
66D7 JR NZ,Parser_MatchInputWord_Step3
66D9 INC DE Move to the next letter of the four-letter buffer.
66DA INC HL Move to the next vocabulary letter.
66DB DJNZ Parser_MatchInputWord_Loop8 Decrease the letter counter by one and loop back to Parser_MatchInputWord_Loop8 until the counter is zero.
The word matched. Store its vocabulary index in the token buffer and parse the next word.
66DD EXX Store this vocabulary number as the current word's token.
66DE LD A,$09
66E0 SUB C
66E1 EXX
66E2 LD D,$00
66E4 LD E,A
66E5 LD HL,$6291
66E8 ADD HL,DE
66E9 LD (HL),C
66EA EXX
66EB JP Parser_MatchInputWord Jump to Parser_MatchInputWord to read the next word.
This vocabulary word did not match. Step past the remaining letters. A comma keeps the same token for a synonym; anything else advances the token number for the next entry.
Parser_MatchInputWord_Step3 66EE LD E,B Turn the remaining letter count into a 16-bit offset in DE.
66EF LD D,$00
66F1 ADD HL,DE Move HL past the unmatched letters.
66F2 LD A,(HL) Fetch the next vocabulary byte into A.
66F3 CP $2C Jump to Parser_MatchInputWord_Step4 if this byte is not a comma (2C).
66F5 JR NZ,Parser_MatchInputWord_Step4
66F7 INC HL Move past the comma.
66F8 JR Parser_MatchInputWord_Loop7 Jump to Parser_MatchInputWord_Loop7 to try matching the synonym.
Parser_MatchInputWord_Step4 66FA INC C Advance the token number for the next vocabulary entry.
66FB JR Parser_MatchInputWord_Loop6 Jump to Parser_MatchInputWord_Loop6.
The word is not in the vocabulary, so quote it and ask again.
Parser_MatchInputWord_Step5 66FD EXX Find the unknown word in the command buffer.
66FE LD A,$0A
6700 SUB C
6701 LD C,A
6702 LD HL,$625F
Parser_MatchInputWord_Loop9 6705 DEC C Step back to the previous word of the command.
6706 JR Z,Parser_MatchInputWord_Step6 This is the word the game could not recognise.
Parser_MatchInputWord_Loop10 6708 LD A,(HL) Find the end of the previous word.
6709 CP $20
670B JR Z,Parser_MatchInputWord_Loop11
670D CP $2C
670F JR Z,Parser_MatchInputWord_Loop11
6711 INC HL
6712 JR Parser_MatchInputWord_Loop10
Parser_MatchInputWord_Loop11 6714 INC HL Skip the gap after that word.
6715 LD A,(HL)
6716 CP $20
6718 JR Z,Parser_MatchInputWord_Loop11
671A CP $2C Jump back to Parser_MatchInputWord_Loop11 when that character is a comma (2C).
671C JR Z,Parser_MatchInputWord_Loop11
671E JR Parser_MatchInputWord_Loop9 Jump back to Parser_MatchInputWord_Loop9 to count back another word.
Parser_MatchInputWord_Step6 6720 PUSH HL Remember where the unknown word starts.
Print "I don't know the word:-".
6721 LD HL,$6300 Print "I don't know the word:-".
6724 CALL PrintStringAndANewline
6727 POP HL Restore the start of the unknown word.
6728 LD A,$22 Print a quotation mark before the unknown word.
672A CALL PrintCharacter
Parser_MatchInputWord_Loop12 672D LD A,(HL) Print the characters of the unknown word.
672E CP $21
6730 JR C,Parser_MatchInputWord_Step7
6732 INC HL
6733 CALL PrintCharacter
6736 JR Parser_MatchInputWord_Loop12
Parser_MatchInputWord_Step7 6738 LD A,$22 Close the quotation.
673A CALL PrintCharacter
Print ".".
673D LD HL,$646B Print ".".
6740 CALL PrintStringAndANewline
6743 JR Handler_UnknownWord_Step1 Jump to Handler_UnknownWord_Step1 to ask for another command.
Prev: 6604 Up: Map Next: 6745