Express word type with an enum instead of magic numbers.

* Make a cleaner function for getting vocab metadata.
* Get rid of magic numbers.
* Purge get_vocab_id().
* Abolish the *_WORD() macros.
* Add FIXME comment on some ugliness.
This commit is contained in:
Jason S. Ninneman 2017-07-11 17:44:06 -07:00
parent b5badcacc8
commit 50efa22849
4 changed files with 89 additions and 68 deletions

View file

@ -185,13 +185,13 @@ static int bigwords(long id)
* Look up foo in special section of vocab to determine which word we've got. * Look up foo in special section of vocab to determine which word we've got.
* Last word zips the eggs back to the giant room (unless already there). */ * Last word zips the eggs back to the giant room (unless already there). */
{ {
if ((game.foobar == WORD_EMPTY && id == ACTION_WORD(FEE)) || if ((game.foobar == WORD_EMPTY && id == FEE) ||
(game.foobar == ACTION_WORD(FEE) && id == ACTION_WORD(FIE)) || (game.foobar == FEE && id == FIE) ||
(game.foobar == ACTION_WORD(FIE) && id == ACTION_WORD(FOE)) || (game.foobar == FIE && id == FOE) ||
(game.foobar == ACTION_WORD(FOE) && id == ACTION_WORD(FOO)) || (game.foobar == FOE && id == FOO) ||
(game.foobar == ACTION_WORD(FOE) && id == ACTION_WORD(FUM))) { (game.foobar == FOE && id == FUM)) {
game.foobar = id; game.foobar = id;
if ((id != ACTION_WORD(FOO)) && (id != ACTION_WORD(FUM))) { if ((id != FOO) && (id != FUM)) {
rspeak(OK_MAN); rspeak(OK_MAN);
return GO_CLEAROBJ; return GO_CLEAROBJ;
} }
@ -1127,16 +1127,18 @@ static int say(struct command_t *command)
} }
char word1[TOKLEN + 1]; char word1[TOKLEN + 1];
packed_to_token(command->wd1, word1); packed_to_token(command->wd1, word1);
int wd = (int) get_vocab_id(word1); long wd;
if (wd == MOTION_WORD(XYZZY) || enum wordtype type;
wd == MOTION_WORD(PLUGH) || get_vocab_metadata(word1, &wd, &type);
wd == MOTION_WORD(PLOVER) || if (wd == XYZZY ||
wd == ACTION_WORD(FEE) || wd == PLUGH ||
wd == ACTION_WORD(FIE) || wd == PLOVER ||
wd == ACTION_WORD(FOE) || wd == FEE ||
wd == ACTION_WORD(FOO) || wd == FIE ||
wd == ACTION_WORD(FUM) || wd == FOE ||
wd == ACTION_WORD(PART)) { wd == FOO ||
wd == FUM ||
wd == PART) {
/* FIXME: scribbles on the interpreter's command block */ /* FIXME: scribbles on the interpreter's command block */
wordclear(&command->wd2); wordclear(&command->wd2);
return GO_LOOKUP; return GO_LOOKUP;

View file

@ -69,13 +69,6 @@
#define INDEEP(LOC) ((LOC) >= LOC_MISTHALL && !OUTSID(LOC)) #define INDEEP(LOC) ((LOC) >= LOC_MISTHALL && !OUTSID(LOC))
#define BUG(x) bug(x, #x) #define BUG(x) bug(x, #x)
#define MOTION_WORD(n) ((n) + 0)
#define OBJECT_WORD(n) ((n) + 1000)
#define ACTION_WORD(n) ((n) + 2000)
#define SPECIAL_WORD(n) ((n) + 3000)
#define PROMOTE_WORD(n) ((n) + 1000)
#define DEMOTE_WORD(n) ((n) - 1000)
enum bugtype { enum bugtype {
SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST, SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST,
VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3, VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3,
@ -94,6 +87,8 @@ enum termination {endgame, quitgame, scoregame};
enum speechpart {unknown, intransitive, transitive}; enum speechpart {unknown, intransitive, transitive};
enum wordtype {NO_WORD_TYPE, MOTION, OBJECT, ACTION, SPECIAL};
/* Phase codes for action returns. /* Phase codes for action returns.
* These were at one time FORTRAN line numbers. * These were at one time FORTRAN line numbers.
* The values don't matter, but perturb their order at your peril. * The values don't matter, but perturb their order at your peril.
@ -200,6 +195,8 @@ struct command_t {
long id1; long id1;
long id2; long id2;
char raw1[LINESIZE], raw2[LINESIZE]; char raw1[LINESIZE], raw2[LINESIZE];
enum wordtype type1;
enum wordtype type2;
}; };
extern struct game_t game; extern struct game_t game;
@ -225,7 +222,7 @@ extern int get_motion_vocab_id(const char*);
extern int get_object_vocab_id(const char*); extern int get_object_vocab_id(const char*);
extern int get_action_vocab_id(const char*); extern int get_action_vocab_id(const char*);
extern int get_special_vocab_id(const char*); extern int get_special_vocab_id(const char*);
extern long get_vocab_id(const char*); extern void get_vocab_metadata(const char*, long*, enum wordtype*);
extern void juggle(obj_t); extern void juggle(obj_t);
extern void move(obj_t, loc_t); extern void move(obj_t, loc_t);
extern long put(obj_t, long, long); extern long put(obj_t, long, long);

40
main.c
View file

@ -1006,8 +1006,8 @@ static bool get_command_input(struct command_t *command)
packed_to_token(command->wd1, word1); packed_to_token(command->wd1, word1);
packed_to_token(command->wd2, word2); packed_to_token(command->wd2, word2);
command->id1 = get_vocab_id(word1); get_vocab_metadata(word1, &(command->id1), &(command->type1));
command->id2 = get_vocab_id(word2); get_vocab_metadata(word2, &(command->id2), &(command->type2));
return true; return true;
} }
@ -1015,7 +1015,6 @@ static bool get_command_input(struct command_t *command)
static bool do_command() static bool do_command()
/* Get and execute a command */ /* Get and execute a command */
{ {
long kmod, defn;
static struct command_t command; static struct command_t command;
char word1[TOKLEN + 1]; char word1[TOKLEN + 1];
@ -1115,7 +1114,7 @@ Lclosecheck:
lampcheck(); lampcheck();
if (command.id1 == ENTER && (command.id2 == STREAM || if (command.id1 == ENTER && (command.id2 == STREAM ||
command.id2 == PROMOTE_WORD(WATER))) { command.id2 == WATER)) {
if (LIQLOC(game.loc) == WATER) if (LIQLOC(game.loc) == WATER)
rspeak(FEET_WET); rspeak(FEET_WET);
else else
@ -1127,13 +1126,11 @@ Lclosecheck:
command.id1 = command.id2; command.id1 = command.id2;
command.id2 = WORD_EMPTY; command.id2 = WORD_EMPTY;
} else { } else {
/* FIXME: Magic numbers related to vocabulary */ if (!((command.id1 != WATER && command.id1 != OIL) || (command.id2 != PLANT && command.id2 != DOOR))) {
if (!((command.id1 != PROMOTE_WORD(WATER) && command.id1 != PROMOTE_WORD(OIL)) || if (AT(command.id2))
(command.id2 != PROMOTE_WORD(PLANT) && command.id2 != PROMOTE_WORD(DOOR)))) {
if (AT(DEMOTE_WORD(command.id2)))
command.wd2 = token_to_packed("POUR"); command.wd2 = token_to_packed("POUR");
} }
if (command.id1 == PROMOTE_WORD(CAGE) && command.id2 == PROMOTE_WORD(BIRD) && HERE(CAGE) && HERE(BIRD)) if (command.id1 == CAGE && command.id2 == BIRD && HERE(CAGE) && HERE(BIRD))
command.wd1 = token_to_packed("CATCH"); command.wd1 = token_to_packed("CATCH");
} }
Lookup: Lookup:
@ -1146,7 +1143,9 @@ Lookup:
rspeak(GO_UNNEEDED); rspeak(GO_UNNEEDED);
} }
packed_to_token(command.wd1, word1); packed_to_token(command.wd1, word1);
defn = get_vocab_id(word1); long defn;
enum wordtype type;
get_vocab_metadata(word1, &defn, &type);
if (defn == WORD_NOT_FOUND) { if (defn == WORD_NOT_FOUND) {
if (fallback_handler(command)) if (fallback_handler(command))
continue; continue;
@ -1154,22 +1153,21 @@ Lookup:
sspeak(DONT_KNOW, command.raw1); sspeak(DONT_KNOW, command.raw1);
goto Lclearobj; goto Lclearobj;
} }
/* FIXME: magic numbers related to vocabulary */ switch (type) {
kmod = MOD(defn, 1000); case NO_WORD_TYPE: // FIXME: treating NO_WORD_TYPE as a motion word is confusing
switch (defn / 1000) { case MOTION:
case 0: playermove(defn);
playermove(kmod);
return true; return true;
case 1: case OBJECT:
command.part = unknown; command.part = unknown;
command.obj = kmod; command.obj = defn;
break; break;
case 2: case ACTION:
command.part = intransitive; command.part = intransitive;
command.verb = kmod; command.verb = defn;
break; break;
case 3: case SPECIAL:
speak(specials[kmod].message); speak(specials[defn].message);
goto Lclearobj; goto Lclearobj;
default: default:
BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE BUG(VOCABULARY_TYPE_N_OVER_1000_NOT_BETWEEN_0_AND_3); // LCOV_EXCL_LINE

70
misc.c
View file

@ -523,37 +523,61 @@ int get_special_vocab_id(const char* word)
return (WORD_NOT_FOUND); return (WORD_NOT_FOUND);
} }
long get_vocab_id(const char* word) void get_vocab_metadata(const char* word, long* id, enum wordtype* type)
// Search the vocab categories in order for the supplied word.
{ {
/* Check for an empty string */ /* Check for an empty string */
if (strncmp(word, "", sizeof("")) == 0) if (strncmp(word, "", sizeof("")) == 0)
return (WORD_EMPTY); {
*id = WORD_EMPTY;
*type = NO_WORD_TYPE;
return;
}
long ref_num; long ref_num;
/* FIXME: Magic numbers related to vocabulary */ ref_num = get_motion_vocab_id(word);
ref_num = get_motion_vocab_id(word); if (ref_num != WORD_NOT_FOUND)
if (ref_num != WORD_NOT_FOUND) {
return MOTION_WORD(ref_num); *id = ref_num;
*type = MOTION;
return;
}
ref_num = get_object_vocab_id(word); ref_num = get_object_vocab_id(word);
if (ref_num != WORD_NOT_FOUND) if (ref_num != WORD_NOT_FOUND)
return OBJECT_WORD(ref_num); {
*id = ref_num;
*type = OBJECT;
return;
}
ref_num = get_action_vocab_id(word); ref_num = get_action_vocab_id(word);
if (ref_num != WORD_NOT_FOUND) if (ref_num != WORD_NOT_FOUND)
return ACTION_WORD(ref_num); {
*id = ref_num;
*type = ACTION;
return;
}
ref_num = get_special_vocab_id(word); ref_num = get_special_vocab_id(word);
if (ref_num != WORD_NOT_FOUND) if (ref_num != WORD_NOT_FOUND)
return SPECIAL_WORD(ref_num); {
*id = ref_num;
*type = SPECIAL;
return;
}
// Check for the reservoir magic word. // Check for the reservoir magic word.
if (strcasecmp(word, game.zzword) == 0) if (strcasecmp(word, game.zzword) == 0)
return ACTION_WORD(PART); {
*id = PART;
*type = ACTION;
return;
}
return (WORD_NOT_FOUND); *id = WORD_NOT_FOUND;
*type = NO_WORD_TYPE;
return;
} }
void juggle(obj_t object) void juggle(obj_t object)