Mostly confine assumptions about what token_t is to misc.c

The token_t things like WD* are presently longs and will someday be
char[6].  By introducing some trivial functions - wordeq(),
wordempty(), and wordclear() - we mostly hide the difference.

All runtime knowledge about packing now lives only in misc.c and the
list of magic WORD_* defines in advent.h.  Outside this, literals are
now accessed through #define names that could expand to either longs
or strings.

Still to be done: WD* values are sometiimes compated to zero in
ways implying they can be negative. Must figure out wat thus means.
This commit is contained in:
Eric S. Raymond 2017-06-19 17:21:45 -04:00
parent 87961483a2
commit a678b68b39
5 changed files with 28 additions and 6 deletions

View file

@ -1054,7 +1054,8 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
switch (part) { switch (part) {
case intransitive: case intransitive:
if (WD2 > 0 && verb != SAY) return (2800); if (WD2 > 0 && verb != SAY)
return GO_WORD2;
if (verb == SAY)obj = WD2; if (verb == SAY)obj = WD2;
if (obj == 0 || obj == INTRANSITIVE) { if (obj == 0 || obj == INTRANSITIVE) {
/* Analyse an intransitive verb (ie, no object given yet). */ /* Analyse an intransitive verb (ie, no object given yet). */

View file

@ -91,6 +91,9 @@ extern void* xmalloc(size_t size);
extern char* xstrdup(const char*); extern char* xstrdup(const char*);
extern void packed_to_token(long, char token[]); extern void packed_to_token(long, char token[]);
extern void speak(const char*); extern void speak(const char*);
extern bool wordeq(token_t, token_t);
extern bool wordempty(token_t);
extern void wordclear(token_t *);
extern void PSPEAK(vocab_t, int); extern void PSPEAK(vocab_t, int);
extern void RSPEAK(vocab_t); extern void RSPEAK(vocab_t);
extern void SETPRM(long, long, long); extern void SETPRM(long, long, long);

2
init.c
View file

@ -131,7 +131,7 @@
* apply to players whose scores are higher than the previous N but not * apply to players whose scores are higher than the previous N but not
* higher than this N. Note that these scores probably change with every * higher than this N. Note that these scores probably change with every
* modification (and particularly expansion) of the program. * modification (and particularly expansion) of the program.
* SECTION 11: Hints. Each line contains a hint number (add 10 to get cond * Section 11: Hints. Each line contains a hint number (add 10 to get cond
* bit; see section 9), the number of turns he must be at the right loc(s) * bit; see section 9), the number of turns he must be at the right loc(s)
* before triggering the hint, the points deducted for taking the hint, * before triggering the hint, the points deducted for taking the hint,
* the message number (section 6) of the question, and the message number * the message number (section 6) of the question, and the message number

9
main.c
View file

@ -1057,8 +1057,9 @@ L2607:
if (V1 == ENTER && WD2 > 0) { if (V1 == ENTER && WD2 > 0) {
WD1 = WD2; WD1 = WD2;
WD1X = WD2X; WD1X = WD2X;
WD2 = 0; wordclear(&WD2);
} else { } else {
/* FIXME: Magic numbers */
if (!((V1 != 1000 + WATER && V1 != 1000 + OIL) || if (!((V1 != 1000 + WATER && V1 != 1000 + OIL) ||
(V2 != 1000 + PLANT && V2 != 1000 + DOOR))) { (V2 != 1000 + PLANT && V2 != 1000 + DOOR))) {
if (AT(V2 - 1000)) if (AT(V2 - 1000))
@ -1068,12 +1069,12 @@ L2607:
WD1 = MAKEWD(WORD_CATCH); WD1 = MAKEWD(WORD_CATCH);
} }
L2620: L2620:
if (WD1 == MAKEWD(WORD_WEST)) { if (wordeq(WD1, MAKEWD(WORD_WEST))) {
++game.iwest; ++game.iwest;
if (game.iwest == 10) if (game.iwest == 10)
RSPEAK(W_IS_WEST); RSPEAK(W_IS_WEST);
} }
if (WD1 == MAKEWD(WORD_GO) && WD2 != 0) { if (wordeq(WD1, MAKEWD(WORD_GO)) && !wordempty(WD2)) {
if (++igo == 10) if (++igo == 10)
RSPEAK(GO_UNNEEDED); RSPEAK(GO_UNNEEDED);
} }
@ -1130,7 +1131,7 @@ Laction:
/* Get second word for analysis. */ /* Get second word for analysis. */
WD1 = WD2; WD1 = WD2;
WD1X = WD2X; WD1X = WD2X;
WD2 = 0; wordclear(&WD2);
goto L2620; goto L2620;
case GO_UNKNOWN: case GO_UNKNOWN:
/* Random intransitive verbs come here. Clear obj just in case /* Random intransitive verbs come here. Clear obj just in case

17
misc.c
View file

@ -50,6 +50,23 @@ void packed_to_token(long packed, char token[6])
} }
} }
/* Hide the fact that wods are corrently packed longs */
bool wordeq(token_t a, token_t b)
{
return a == b;
}
bool wordempty(token_t a)
{
return a == 0;
}
void wordclear(token_t *v)
{
*v = 0;
}
/* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */ /* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
void speak(const char* msg) void speak(const char* msg)