xref: /original-bsd/games/battlestar/parse.c (revision 65d10654)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)parse.c	8.2 (Berkeley) 04/28/95";
10 #endif /* not lint */
11 
12 #include "extern.h"
13 
14 wordinit()
15 {
16 	register struct wlist *w;
17 
18 	for (w = wlist; w->string; w++)
19 		install(w);
20 }
21 
22 hash(s)
23 	register char *s;
24 {
25 	register hashval = 0;
26 
27 	while (*s) {
28 		hashval += *s++;
29 		hashval *= HASHMUL;
30 		hashval &= HASHMASK;
31 	}
32 	return hashval;
33 }
34 
35 struct wlist *
36 lookup(s)
37 	char *s;
38 {
39 	register struct wlist *wp;
40 
41 	for (wp = hashtab[hash(s)]; wp != NULL; wp = wp->next)
42 		if (*s == *wp->string && strcmp(s, wp->string) == 0)
43 			return wp;
44 	return NULL;
45 }
46 
47 install(wp)
48 	register struct wlist *wp;
49 {
50 	int hashval;
51 
52 	if (lookup(wp->string) == NULL) {
53 		hashval = hash(wp->string);
54 		wp->next = hashtab[hashval];
55 		hashtab[hashval] = wp;
56 	} else
57 		printf("Multiply defined %s.\n", wp->string);
58 }
59 
60 parse()
61 {
62 	register struct wlist *wp;
63 	register n;
64 
65 	wordnumber = 0;           /* for cypher */
66 	for (n = 0; n <= wordcount; n++) {
67 		if ((wp = lookup(words[n])) == NULL) {
68 			wordvalue[n] = -1;
69 			wordtype[n] = -1;
70 		} else {
71 			wordvalue[n] = wp -> value;
72 			wordtype[n] = wp -> article;
73 		}
74 	}
75 }
76