DEV website instance!The main loop – player inputBack to the main loop... It is time to obtain and parse theplayer's command. repeat set status, no.amatch # Suppress approximate matching of command words input # Get and parse player's command ifeq status, 2 and iflt arg2, 0 # Can't find 2nd command word in the vocabulary or iflt arg1, 0 # Can't find 1st command word in the vocabulary quip "Pardon?" # Command parse failure -- abort the main loop fin The INPUT directive considers the next simple command (a complex one may have been previously given). If the previous command (simple or complex) has been fully processed, it will first prompt the player for a new one. It will parse the verb/noun command thus obtained and will set three automatic variables: STATUS, ARG1 and ARG2. The STATUS variable gets set to the number of words in the command being parsed, which can be one or two. It cannot be zero, because a null command is simply ignored. Parsing the command includes matching its words against game's vocabulary. If all is well, ARG1 becomes a pointer to the first word of the command and ARG2 becomes a pointer to the second one – or, if only one word is supplied, ARG2 is set to zero. If either of the supplied words cannot be matched, the corresponding ARG variable is set to a negative value indicating the kind of failure involved. In this simple implementation we won't bother with details and simply report parsing failure of some kind. The conditional directive IFEQ checks for equality of its two arguments, while IFLT checks whether the value if the first argument is smaller than that of the second one. Such conditional statements can be joined by means of AND and OR directives into compound ones. The joint effect is conditional execution of any subsequent code up to the matching FIN directive. One unusual aspect of A-code should be noted here. Compound IF statements are parsed on the "as-you-go" basis. Or to put it otherwise, AND and OR have the same precedence and compound IFs are processed in the order in which they are given. Thus the above triple IF will report a failure either if a two word command is supplied and the second word cannot be parsed or the first word cannot be parsed (regardless of the number of words supplied).
|