| 1 |
/* ------------------------------------------------------------------------ */ |
| 2 |
/* |
| 3 |
* "Quit" exception. This exception does not represent an error |
| 4 |
* condition, but simply provides a convenient way to terminate the game |
| 5 |
* at any point in the code. All of the library exception handlers will |
| 6 |
* pass this exception up to the main outer loop of the game, which will |
| 7 |
* simply exit the program with no error when it catches this type of |
| 8 |
* exception. |
| 9 |
*/ |
| 10 |
QuitException: Exception |
| 11 |
; |
| 12 |
|
| 13 |
/* |
| 14 |
* "Abort" exception. This exception can be thrown at most points in |
| 15 |
* command processing to completely terminate the current turn. |
| 16 |
*/ |
| 17 |
AbortException: Exception |
| 18 |
; |
| 19 |
|
| 20 |
/* |
| 21 |
* "Exit" exception. This exception can be thrown at most points in |
| 22 |
* command processing to skip additional processing on the current |
| 23 |
* action, and proceed immediately to the next action on the command |
| 24 |
* line. |
| 25 |
*/ |
| 26 |
ExitException: Exception |
| 27 |
; |
| 28 |
|
| 29 |
|
| 30 |
/* ------------------------------------------------------------------------ */ |
| 31 |
/* |
| 32 |
* Quit the game. This can be called at any point; we simply throw |
| 33 |
* QuitException in order to jump out to and then exit the main game |
| 34 |
* loop. |
| 35 |
*/ |
| 36 |
quit() |
| 37 |
{ |
| 38 |
throw QuitException; |
| 39 |
} |
| 40 |
|
| 41 |
/* |
| 42 |
* Abort the current turn. This can be called at most points during |
| 43 |
* command processing; we'll simply end the current turn, skipping all |
| 44 |
* remaining processing on the current turn, skipping all fuses and |
| 45 |
* daemons, and ignoring any following commands on the same command |
| 46 |
* line. |
| 47 |
*/ |
| 48 |
abort() |
| 49 |
{ |
| 50 |
throw AbortException; |
| 51 |
} |
| 52 |
|
| 53 |
/* |
| 54 |
* Exit the current turn. This can be called at most points during |
| 55 |
* command processing to skip any further processing for the current |
| 56 |
* action, and proceed immediately to the next action on the same |
| 57 |
* command line. |
| 58 |
* |
| 59 |
* Note that this routine will not skip additional objects that are part |
| 60 |
* of the same command. For example, if this is called while processing |
| 61 |
* the 'ball' object in the command 'take ball and book,' we will |
| 62 |
* proceed to process the command for the 'book' object. |
| 63 |
*/ |
| 64 |
exit() |
| 65 |
{ |
| 66 |
throw ExitException; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/* ------------------------------------------------------------------------ */ |
| 71 |
/* |
| 72 |
* Command object. We create an instance of this class for each command |
| 73 |
* we execute. |
| 74 |
*/ |
| 75 |
class Command: object |
| 76 |
/* the actor performing the command */ |
| 77 |
actor = nil |
| 78 |
|
| 79 |
/* the action to be performed - this is an instance of Action */ |
| 80 |
action = nil |
| 81 |
|
| 82 |
/* |
| 83 |
* The direct object, if any - for an action that takes no direct |
| 84 |
* object, this will be nil. Note that this is a single object, |
| 85 |
* even if the player's command had a list of direct object - we |
| 86 |
* will create a separate command for each direct object and execute |
| 87 |
* each command separately. |
| 88 |
*/ |
| 89 |
dobj = nil |
| 90 |
|
| 91 |
/* |
| 92 |
* The indirect object, if any - for an action that takes no |
| 93 |
* indirect object, this will be nil. |
| 94 |
*/ |
| 95 |
iobj = nil |
| 96 |
|
| 97 |
/* |
| 98 |
* The standard library only defines verbs with zero, one, or two |
| 99 |
* objects, hence we only define dobj and iobj properties here. |
| 100 |
* However, there is nothing stopping library extensions or games |
| 101 |
* from adding actions taking additional objects - actions with |
| 102 |
* three or more objects should simply add an additional property |
| 103 |
* here for each additional object. We stop at two objects only |
| 104 |
* because that's the most we need for the library actions. |
| 105 |
*/ |
| 106 |
; |
| 107 |
|
| 108 |
/* ------------------------------------------------------------------------ */ |
| 109 |
/* |
| 110 |
* Command line tokenizer. |
| 111 |
*/ |
| 112 |
CommandTokenizer: Tokenizer |
| 113 |
/* |
| 114 |
* The default tokenizer rules are suitable for command lines, so we |
| 115 |
* don't need to override anything here. |
| 116 |
*/ |
| 117 |
; |
| 118 |
|
| 119 |
/* ------------------------------------------------------------------------ */ |
| 120 |
/* |
| 121 |
* Parse and execute a command line. 'cmd' is a string giving the |
| 122 |
* command line to be parsed, and 'prod' is the root grammar production |
| 123 |
* to use in parsing the command. |
| 124 |
*/ |
| 125 |
processCommandLine(cmd, prod) |
| 126 |
{ |
| 127 |
/* catch exceptions that cancel the entire command */ |
| 128 |
try |
| 129 |
{ |
| 130 |
/* catch exceptions that skip to end-of-turn processing */ |
| 131 |
try |
| 132 |
{ |
| 133 |
local toklist; |
| 134 |
|
| 135 |
/* perform text-level pre-parsing */ |
| 136 |
forEachInstance(PreParseObject, { obj: cmd = obj.preParse(cmd) }); |
| 137 |
|
| 138 |
/* tokenize the command */ |
| 139 |
toklist = CommandTokenizer.tokenize(cmd); |
| 140 |
|
| 141 |
/* |
| 142 |
* if the command is empty, use the special empty command |
| 143 |
* processor |
| 144 |
*/ |
| 145 |
if (toklist[1] == []) |
| 146 |
{ |
| 147 |
/* respond to the empty command */ |
| 148 |
libMessages.emptyCommandResponse(); |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* keep going until we've processed all of the tokens on the |
| 153 |
* command line |
| 154 |
*/ |
| 155 |
while (toklist[1] != []) |
| 156 |
{ |
| 157 |
// $$$ match the token list to 'prod' |
| 158 |
|
| 159 |
// $$$ resolve noun phrases. If we have multiple grammar matches, |
| 160 |
// try resolving in the first match first; if that succeeds, we'll |
| 161 |
// consider the first match to be the right match, otherwise we'll |
| 162 |
// try resolving in the second match, and so on until we find a |
| 163 |
// match with resolvable noun phrases or run out of matches. |
| 164 |
// Choose the first successfully resolved match as the winner, |
| 165 |
// and ignore all of the other matches. |
| 166 |
|
| 167 |
// $$$ remove the matching tokens from toklist, so that |
| 168 |
// toklist contains the remainder of the command line after |
| 169 |
// the current command we just matched |
| 170 |
|
| 171 |
/* perform token-level pre-parsing */ |
| 172 |
forEachInstance(PreParseTokensObject, |
| 173 |
{ obj: obj.preParseTokens(matchToklist) }); |
| 174 |
|
| 175 |
/* execute the command */ |
| 176 |
match.execute(); |
| 177 |
} |
| 178 |
} |
| 179 |
catch (ExitException exitExc) |
| 180 |
{ |
| 181 |
/* |
| 182 |
* exit - the command line was cancelled, but we're still to |
| 183 |
* perform end-of-turn processing |
| 184 |
*/ |
| 185 |
} |
| 186 |
} |
| 187 |
catch (QuitException quitExc) |
| 188 |
{ |
| 189 |
/* |
| 190 |
* a game-terminating exception was thrown - simply re-throw the |
| 191 |
* exception to our caller to exit from the game |
| 192 |
*/ |
| 193 |
throw quitExc; |
| 194 |
} |
| 195 |
catch (AbortException abortExc) |
| 196 |
{ |
| 197 |
/* |
| 198 |
* abort - the entire command line was cancelled, including all |
| 199 |
* end-of-turn processing; this means we're done with the |
| 200 |
* command line |
| 201 |
*/ |
| 202 |
} |
| 203 |
catch (Exception exc) |
| 204 |
{ |
| 205 |
/* ignore all other exceptions */ |
| 206 |
// $$$ but maybe display it, since we consider any other exception |
| 207 |
// that makes it to this point to be an error, and hence it should |
| 208 |
// be reported to the user for debugging purposes |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|