Prev: 26116 Up: Map Next: 26437
26217: 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 26217 LD DE,25246 DE=FourLetterBuffer.
26220 LD B,4 Set a counter in B for the 4 letters.
26222 LD A,32 A=32.
Parser_MatchInputWord_Loop1 26224 LD (DE),A Write a space character (32) to the buffer.
26225 INC DE Move to the next letter.
26226 DJNZ Parser_MatchInputWord_Loop1 Decrease the counter by one and loop back to Parser_MatchInputWord_Loop1 until the counter is zero.
26228 XOR A Jump to Handler_UnknownWord when no token slots remain.
26229 OR C
26230 JP Z,Handler_UnknownWord Jump to Handler_UnknownWord when the command has ended.
26233 LD A,(HL) Read the next character of the command.
26234 CP 13 Jump to Handler_UnknownWord when ENTER ends the command.
26236 JP Z,Handler_UnknownWord
Copy the next word, up to 4 characters, stopping at a space, a comma or ENTER.
26239 LD B,4 Keep at most four letters of this word.
26241 LD DE,25246 Point at the word buffer again.
Parser_MatchInputWord_Loop2 26244 LD A,(HL) Jump to Parser_MatchInputWord_Step1 when the character is ENTER.
26245 CP 13
26247 JR Z,Parser_MatchInputWord_Step1
26249 CP 32 Jump to Parser_MatchInputWord_Loop4 when the character is a space or a comma.
26251 JR Z,Parser_MatchInputWord_Loop4
26253 CP 44
26255 JR Z,Parser_MatchInputWord_Loop4
26257 LD (DE),A Copy the character into the word buffer and advance both pointers.
26258 INC HL
26259 INC DE
26260 DJNZ Parser_MatchInputWord_Loop2
A longer word is skipped up to the next delimiter.
Parser_MatchInputWord_Loop3 26262 LD A,(HL) Jump to Parser_MatchInputWord_Step1 on ENTER.
26263 CP 13
26265 JR Z,Parser_MatchInputWord_Step1
26267 CP 32 Jump to Parser_MatchInputWord_Loop4 on a space or a comma.
26269 JR Z,Parser_MatchInputWord_Loop4
26271 CP 44
26273 JR Z,Parser_MatchInputWord_Loop4
26275 INC HL Skip this character and keep looking for a delimiter.
26276 JR Parser_MatchInputWord_Loop3
Parser_MatchInputWord_Loop4 26278 INC HL Skip consecutive spaces.
26279 LD A,(HL)
26280 CP 32
26282 JR Z,Parser_MatchInputWord_Loop4
26284 CP 44 Jump back to Parser_MatchInputWord_Loop4 when that character is a comma (44).
26286 JR Z,Parser_MatchInputWord_Loop4
Ignore the word "THE ".
Parser_MatchInputWord_Step1 26288 EXX Compare the word with "THE " without losing the command pointer.
26289 LD HL,25319
26292 LD DE,25246
26295 LD B,4
Parser_MatchInputWord_Loop5 26297 LD A,(DE) Match the four letters of "THE ".
26298 CP (HL)
26299 JR NZ,Parser_MatchInputWord_Step2
26301 INC DE
26302 INC HL
26303 DJNZ Parser_MatchInputWord_Loop5
Parser_MatchInputWord_Step2 26305 EXX Jump back to Parser_MatchInputWord when the word was THE, so it is not tokenised.
26306 JR Z,Parser_MatchInputWord
26308 DEC C
Look the word up in the vocabulary. A comma separates synonyms of the same token; 255 ends the table.
26309 EXX Switch to the alternate register set for the vocabulary search.
26310 LD HL,(25244) HL=*Pointer_Vocabulary.
26313 LD C,0 Set the token number in C to 0.
Parser_MatchInputWord_Loop6 26315 LD A,(HL) Fetch the next vocabulary byte into A.
26316 CP 255 Jump to Parser_MatchInputWord_Step5 if the word is the 255 terminator.
26318 JR Z,Parser_MatchInputWord_Step5
Parser_MatchInputWord_Loop7 26320 LD DE,25246 DE=FourLetterBuffer.
26323 LD B,4 Set a counter in B for the 4 letters.
Parser_MatchInputWord_Loop8 26325 LD A,(DE) Jump to Parser_MatchInputWord_Step3 if this letter does not match.
26326 CP (HL)
26327 JR NZ,Parser_MatchInputWord_Step3
26329 INC DE Move to the next letter of the four-letter buffer.
26330 INC HL Move to the next vocabulary letter.
26331 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.
26333 EXX Store this vocabulary number as the current word's token.
26334 LD A,9
26336 SUB C
26337 EXX
26338 LD D,0
26340 LD E,A
26341 LD HL,25233
26344 ADD HL,DE
26345 LD (HL),C
26346 EXX
26347 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 26350 LD E,B Turn the remaining letter count into a 16-bit offset in DE.
26351 LD D,0
26353 ADD HL,DE Move HL past the unmatched letters.
26354 LD A,(HL) Fetch the next vocabulary byte into A.
26355 CP 44 Jump to Parser_MatchInputWord_Step4 if this byte is not a comma (44).
26357 JR NZ,Parser_MatchInputWord_Step4
26359 INC HL Move past the comma.
26360 JR Parser_MatchInputWord_Loop7 Jump to Parser_MatchInputWord_Loop7 to try matching the synonym.
Parser_MatchInputWord_Step4 26362 INC C Advance the token number for the next vocabulary entry.
26363 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 26365 EXX Find the unknown word in the command buffer.
26366 LD A,10
26368 SUB C
26369 LD C,A
26370 LD HL,25183
Parser_MatchInputWord_Loop9 26373 DEC C Step back to the previous word of the command.
26374 JR Z,Parser_MatchInputWord_Step6 This is the word the game could not recognise.
Parser_MatchInputWord_Loop10 26376 LD A,(HL) Find the end of the previous word.
26377 CP 32
26379 JR Z,Parser_MatchInputWord_Loop11
26381 CP 44
26383 JR Z,Parser_MatchInputWord_Loop11
26385 INC HL
26386 JR Parser_MatchInputWord_Loop10
Parser_MatchInputWord_Loop11 26388 INC HL Skip the gap after that word.
26389 LD A,(HL)
26390 CP 32
26392 JR Z,Parser_MatchInputWord_Loop11
26394 CP 44 Jump back to Parser_MatchInputWord_Loop11 when that character is a comma (44).
26396 JR Z,Parser_MatchInputWord_Loop11
26398 JR Parser_MatchInputWord_Loop9 Jump back to Parser_MatchInputWord_Loop9 to count back another word.
Parser_MatchInputWord_Step6 26400 PUSH HL Remember where the unknown word starts.
Print "I don't know the word:-".
26401 LD HL,25344 Print "I don't know the word:-".
26404 CALL PrintStringAndANewline
26407 POP HL Restore the start of the unknown word.
26408 LD A,34 Print a quotation mark before the unknown word.
26410 CALL PrintCharacter
Parser_MatchInputWord_Loop12 26413 LD A,(HL) Print the characters of the unknown word.
26414 CP 33
26416 JR C,Parser_MatchInputWord_Step7
26418 INC HL
26419 CALL PrintCharacter
26422 JR Parser_MatchInputWord_Loop12
Parser_MatchInputWord_Step7 26424 LD A,34 Close the quotation.
26426 CALL PrintCharacter
Print ".".
26429 LD HL,25707 Print ".".
26432 CALL PrintStringAndANewline
26435 JR Handler_UnknownWord_Step1 Jump to Handler_UnknownWord_Step1 to ask for another command.
Prev: 26116 Up: Map Next: 26437