More magic-number elimination, and some information hiding.

This commit is contained in:
Eric S. Raymond 2017-06-07 00:05:03 -04:00
parent 924157fa9f
commit c68b92d9da
3 changed files with 29 additions and 22 deletions

View file

@ -10,7 +10,7 @@ typedef struct lcg_state
unsigned long a, c, m, x; unsigned long a, c, m, x;
} lcg_state; } lcg_state;
typedef long token_t; /* word token - someday this will be a character array */ typedef long token_t; /* word token - someday this will be char[TOKLEN+1] */
typedef long vocab_t; /* index into a vocabulary array */ typedef long vocab_t; /* index into a vocabulary array */
struct game_t { struct game_t {

View file

@ -8,6 +8,8 @@
#define VRBSIZ 35 #define VRBSIZ 35
#define HNTSIZ 20 #define HNTSIZ 20
#define TRVSIZ 885 #define TRVSIZ 885
#define TOKLEN 5
#define HINTLEN 5
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -55,7 +57,7 @@ long ATAB[TABSIZ + 1];
long PLAC[NOBJECTS+1]; long PLAC[NOBJECTS+1];
long FIXD[NOBJECTS+1]; long FIXD[NOBJECTS+1];
long ACTSPK[VRBSIZ + 1]; long ACTSPK[VRBSIZ + 1];
long HINTS[HNTSIZ + 1][5]; long HINTS[HNTSIZ + 1][HINTLEN];
bool is_set(long, long); bool is_set(long, long);
long GETTXT(long, long, long); long GETTXT(long, long, long);
@ -73,7 +75,7 @@ void read_hints(FILE*);
void read_sound_text(FILE*); void read_sound_text(FILE*);
void write_0d(FILE*, FILE*, long, char*); void write_0d(FILE*, FILE*, long, char*);
void write_1d(FILE*, FILE*, long[], long, char*); void write_1d(FILE*, FILE*, long[], long, char*);
void write_hints(FILE*, FILE*, long[][5], long, long, char*); void write_hints(FILE*, FILE*, long[][HINTLEN], long, long, char*);
void write_files(FILE*, FILE*); void write_files(FILE*, FILE*);
bool is_set(long var, long position) bool is_set(long var, long position)
@ -105,7 +107,7 @@ long GETTXT(long SKIP,long ONEWRD, long UPPER) {
} }
TEXT=0; TEXT=0;
for (int I=1; I<=5; I++) { for (int I=1; I<=TOKLEN; I++) {
TEXT=TEXT*64; TEXT=TEXT*64;
if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0)) if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
continue; continue;
@ -527,7 +529,7 @@ void write_1d(FILE* c_file, FILE* header_file, long array[], long dim, char* var
fprintf(header_file, "extern long %s[%ld];\n", varname, dim); fprintf(header_file, "extern long %s[%ld];\n", varname, dim);
} }
void write_hints(FILE* c_file, FILE* header_file, long matrix[][5], long dim1, long dim2, char* varname) void write_hints(FILE* c_file, FILE* header_file, long matrix[][HINTLEN], long dim1, long dim2, char* varname)
{ {
fprintf(c_file, "long %s[][%ld] = {\n", varname, dim2); fprintf(c_file, "long %s[][%ld] = {\n", varname, dim2);
for (int i = 0; i < dim1; ++i) for (int i = 0; i < dim1; ++i)
@ -556,6 +558,8 @@ void write_files(FILE* c_file, FILE* header_file)
fprintf(header_file, "#define VRBSIZ 35\n"); fprintf(header_file, "#define VRBSIZ 35\n");
fprintf(header_file, "#define HNTSIZ 20\n"); fprintf(header_file, "#define HNTSIZ 20\n");
fprintf(header_file, "#define TRVSIZ 885\n"); fprintf(header_file, "#define TRVSIZ 885\n");
fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
fprintf(header_file, "#define HINTLEN %d\n", HINTLEN);
fprintf(header_file, "\n"); fprintf(header_file, "\n");
// include the header in the C file // include the header in the C file

37
misc.c
View file

@ -245,7 +245,7 @@ long GETTXT(bool SKIP,bool ONEWRD, bool UPPER) {
} }
TEXT=0; TEXT=0;
for (int I=1; I<=5; I++) { for (int I=1; I<=TOKLEN; I++) {
TEXT=TEXT*64; TEXT=TEXT*64;
if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0)) if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
continue; continue;
@ -275,12 +275,13 @@ long GETTXT(bool SKIP,bool ONEWRD, bool UPPER) {
token_t MAKEWD(long LETTRS) { token_t MAKEWD(long LETTRS) {
long I, L, WORD; long I, L, WORD;
/* Combine five uppercase letters (represented by pairs of decimal digits /* Combine TOKLEN (currently 5) uppercase letters (represented by
* in lettrs) to form a 30-bit value matching the one that GETTXT would * pairs of decimal digits in lettrs) to form a 30-bit value matching
* return given those characters plus trailing blanks. Caution: * the one that GETTXT would return given those characters plus
* lettrs will overflow 31 bits if 5-letter word starts with V-Z. As a * trailing blanks. Caution: lettrs will overflow 31 bits if
* kludgey workaround, you can increment a letter by 5 by adding 50 to * 5-letter word starts with V-Z. As a kludgey workaround, you can
* the next pair of digits. */ * increment a letter by 5 by adding 50 to the next pair of
* digits. */
WORD=0; WORD=0;
I=1; I=1;
@ -299,15 +300,17 @@ long I, L, WORD;
void fPUTTXT(token_t WORD, long *sTATE, long CASE) { void fPUTTXT(token_t WORD, long *sTATE, long CASE) {
long ALPH1, ALPH2, BYTE, DIV, I, W; long ALPH1, ALPH2, BYTE, DIV, I, W;
/* Unpack the 30-bit value in word to obtain up to 5 integer-encoded chars, /* Unpack the 30-bit value in word to obtain up to TOKLEN (currently
* and store them in inline starting at LNPOSN. If LNLENG>=LNPOSN, shift * 5) integer-encoded chars, and store them in inline starting at
* existing characters to the right to make room. STATE will be zero when * LNPOSN. If LNLENG>=LNPOSN, shift existing characters to the right
* puttxt is called with the first of a sequence of words, but is thereafter * to make room. STATE will be zero when puttxt is called with the
* unchanged by the caller, so PUTTXT can use it to maintain state across * first of a sequence of words, but is thereafter unchanged by the
* calls. LNPOSN and LNLENG are incremented by the number of chars stored. * caller, so PUTTXT can use it to maintain state across calls.
* If CASE=1, all letters are made uppercase; if -1, lowercase; if 0, as is. * LNPOSN and LNLENG are incremented by the number of chars stored.
* any other value for case is the same as 0 but also causes trailing blanks * If CASE=1, all letters are made uppercase; if -1, lowercase; if 0,
* to be included (in anticipation of subsequent additional text). */ * as is. any other value for case is the same as 0 but also causes
* trailing blanks to be included (in anticipation of subsequent
* additional text). */
ALPH1=13*CASE+24; ALPH1=13*CASE+24;
@ -316,7 +319,7 @@ long ALPH1, ALPH2, BYTE, DIV, I, W;
/* ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */ /* ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */
DIV=64L*64L*64L*64L; DIV=64L*64L*64L*64L;
W=WORD; W=WORD;
/* 18 */ for (I=1; I<=5; I++) { /* 18 */ for (I=1; I<=TOKLEN; I++) {
if(W <= 0 && STATE == 0 && labs(CASE) <= 1)return; if(W <= 0 && STATE == 0 && labs(CASE) <= 1)return;
BYTE=W/DIV; BYTE=W/DIV;
if(STATE != 0 || BYTE != 63) goto L12; if(STATE != 0 || BYTE != 63) goto L12;